为什么 ANTLR 不解析整个输入?
我对 ANTLR 还很陌生,所以这可能是一个简单的问题。
我定义了一种简单的语法,它应该包含带有数字和标识符的算术表达式(以字母开头并以一个或多个字母或数字继续的字符串。)
语法如下所示:
grammar while;
@lexer::header {
package ConFreeG;
}
@header {
package ConFreeG;
import ConFreeG.IR.*;
}
@parser::members {
}
arith:
term
| '(' arith ( '-' | '+' | '*' ) arith ')'
;
term returns [AExpr a]:
NUM
{
int n = Integer.parseInt($NUM.text);
a = new Num(n);
}
| IDENT
{
a = new Var($IDENT.text);
}
;
fragment LOWER : ('a'..'z');
fragment UPPER : ('A'..'Z');
fragment NONNULL : ('1'..'9');
fragment NUMBER : ('0' | NONNULL);
IDENT : ( LOWER | UPPER ) ( LOWER | UPPER | NUMBER )*;
NUM : '0' | NONNULL NUMBER*;
fragment NEWLINE:'\r'? '\n';
WHITESPACE : ( ' ' | '\t' | NEWLINE )+ { $channel=HIDDEN; };
我正在将 ANTLR v3 与 ANTLR IDE 一起使用Eclipse 插件。当我使用解释器解析表达式 (8 + a45)
时,仅生成解析树的一部分:
为什么第二项 (a45) 没有被解析?如果两项都是数字,也会发生同样的情况。
I am quite new to ANTLR, so this is likely a simple question.
I have defined a simple grammar which is supposed to include arithmetic expressions with numbers and identifiers (strings that start with a letter and continue with one or more letters or numbers.)
The grammar looks as follows:
grammar while;
@lexer::header {
package ConFreeG;
}
@header {
package ConFreeG;
import ConFreeG.IR.*;
}
@parser::members {
}
arith:
term
| '(' arith ( '-' | '+' | '*' ) arith ')'
;
term returns [AExpr a]:
NUM
{
int n = Integer.parseInt($NUM.text);
a = new Num(n);
}
| IDENT
{
a = new Var($IDENT.text);
}
;
fragment LOWER : ('a'..'z');
fragment UPPER : ('A'..'Z');
fragment NONNULL : ('1'..'9');
fragment NUMBER : ('0' | NONNULL);
IDENT : ( LOWER | UPPER ) ( LOWER | UPPER | NUMBER )*;
NUM : '0' | NONNULL NUMBER*;
fragment NEWLINE:'\r'? '\n';
WHITESPACE : ( ' ' | '\t' | NEWLINE )+ { $channel=HIDDEN; };
I am using ANTLR v3 with the ANTLR IDE Eclipse plugin. When I parse the expression (8 + a45)
using the interpreter, only part of the parse tree is generated:
Why does the second term (a45) not get parsed? The same happens if both terms are numbers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要创建一个包含 EOF(文件结尾)标记的解析器规则,以便解析器将被迫遍历整个标记流。
将此规则添加到您的语法中:
并让解释器从该规则而不是 arith 规则开始:
You'll want to create a parser rule that has an
EOF
(end of file) token in it so that the parser will be forced to go through the entire token stream.Add this rule to your grammar:
and let the interpreter start at that rule instead of the
arith
rule: