ANTLR:由于可从 alts 1,2 访问递归规则调用,因此规则令牌具有非 LL(*) 决策
grammar AdifyMapReducePredicate;
PREDICATE
: PREDICATE_BRANCH
| EXPRESSION
;
PREDICATE_BRANCH
: '(' PREDICATE (('&&' PREDICATE)+ | ('||' PREDICATE)+) ')'
;
EXPRESSION
: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
尝试在 ANTLRWorks 1.4 中解释这一点并收到以下错误:
[12:18:21] error(211): <notsaved>:1:8: [fatal] rule Tokens has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2. Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
[12:18:21] Interpreting...
当我解释时,我试图解释 PREDICATE 并且我的测试用例是 (A||B)
我缺少什么?
grammar AdifyMapReducePredicate;
PREDICATE
: PREDICATE_BRANCH
| EXPRESSION
;
PREDICATE_BRANCH
: '(' PREDICATE (('&&' PREDICATE)+ | ('||' PREDICATE)+) ')'
;
EXPRESSION
: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
Trying to interpret this in ANTLRWorks 1.4 and getting the following error:
[12:18:21] error(211): <notsaved>:1:8: [fatal] rule Tokens has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2. Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
[12:18:21] Interpreting...
When I interepret, I'm trying to interpret a PREDICATE and my test case is (A||B)
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 ANTLR 的约定,解析器规则名称以小写字母开头,而词法分析器规则以大写字母开头。因此,正如您所写的那样,语法具有三个词法分析器规则,用于定义标记。这可能不是您想要的。
错误消息的原因显然是这些标记之间的歧义:您的输入模式与 PREDICATE 和 PREDICATE_BRANCH 的定义匹配。
只需使用以小写字母开头的名称,而不是 PREDICATE 和 PREDICATE_BRANCH。您可能还需要为目标符号添加一条不直接参与递归的额外规则。
顺便说一句,这个语法是递归的,但不是左递归的,并且当使用解析器规则时,它肯定是LL(1)。
By ANTLR's conventions, parser rule names start with a lower case letter, while lexer rules start with capital letters. So the grammar, as you wrote it, has three lexer rules, defining tokens. This may not be what you want.
The reason for the error message apparently is the ambiguity between these tokens: your input pattern matches the definitions of both PREDICATE and PREDICATE_BRANCH.
Just use names starting in lower case letters instead of PREDICATE and PREDICATE_BRANCH. You may also have to add an extra rule for the target symbol, that is not directly involved in the recursion.
By the way, this grammar is recursive, but not left-recursive, and when using parser rules, it definitely is LL(1).
您没有解析器规则(解析器规则以小写字母开头),尽管我不确定在解释 ANTLRWorks 中的某些测试用例时最后一部分是否必要。
无论如何,尝试这样的事情:
使用以下测试类:
在生成词法分析器和之后;解析器 (a),编译所有
.java
文件 (b) 并运行测试类 (c),产生以下输出:a
b
c (*nix/MacOS)
c (Windows)
HTH
You don't have a parser rule (parser rules start with a lower case letter), although I'm not sure that last part is necessary when interpreting some test cases in ANTLRWorks.
Anyway, try something like this:
With the following test class:
which after generating a lexer & parser (a), compiling all
.java
files (b) and running the test class (c), produces the following output:a
b
c (*nix/MacOS)
c (Windows)
HTH