ANTLR,可选“”在 JavaScript 中
我只是在玩 ANTLR,并决定尝试用它解析 JavaScript。但我在处理可选的“;”时遇到了困难其中,其中语句结束由换行符标记。可以用一些简单的方式来完成吗?
只是一个简单的语法示例,不起作用
grammar optional_newline;
def : statements ;
statements : statement (statement)* ;
statement : expression (';' | '\n') ;
expression : ID | INT | 'var' ID '=' INT ;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
INT : '0'..'9'+ ;
WS : ( ' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;} ;
,我希望能够解析它(可以由 JavaScript 解析器解析)
var i =
10
10;
PS:我不想将 WS
放入解析器规则中,我如果 lexer 摆脱这些,会更高兴。
I'm just playing with ANTLR and decided to try parsing JavaScript with it. But I hit the wall in dealing with optional ';' in it, where statement end is marked by newline instead. Can it be done in some straightforward way?
Just a simple grammar example that doesn't work
grammar optional_newline;
def : statements ;
statements : statement (statement)* ;
statement : expression (';' | '\n') ;
expression : ID | INT | 'var' ID '=' INT ;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
INT : '0'..'9'+ ;
WS : ( ' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;} ;
and I want to be able to parse this (which can be parsed by JavaScript parsers)
var i =
10
10;
PS: I don't want to put WS
in parser rules, I would be much happier if lexer just get rid of those.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是否适用于 javascript 中可能的所有情况,但它可以正确解析您的示例:
替代文本 http://img249.imageshack.us/img249/7131/parsetree.jpg
i'm not sure if this will work in all cases possible in javascript, but it correctly parses your example:
alt text http://img249.imageshack.us/img249/7131/parsetree.jpg