“FOLLOW_set_in_"...在生成的解析器中未定义 [英] "FOLLOW_set_in_"... is undefined in generated parser
问题描述
我已经为模糊的类似 Java 的 DSL 编写了语法.虽然它仍然存在一些问题(它不能像我希望的那样识别所有输入),但我最担心的是生成的 C 代码不可编译.
I have written a grammar for vaguely Java-like DSL. While there are still some issues with it (it doesn't recognize all the inputs as I would want it to), what concerns me most is that the generated C code is not compilable.
我使用 AntlrWorks 1.5 和 Antlr 3.5(Antlr 4 显然不支持 C 目标).
I use AntlrWorks 1.5 with Antlr 3.5 (Antlr 4 apparently does not support C target).
问题在于表达式规则.我有规则 prio14Expression 到 prio0Expression 处理运算符优先级.问题的优先级为 2,它评估前缀和后缀运算符:
The problem is with expression rules. I have rules prio14Expression to prio0Expression which handle operator precedence. To problem is at priority 2, which evaluates prefix and postfix operators:
...
prio3Expression: prio2Expression (('*' | '/' | '%') prio2Expression)*;
prio2Expression: ('++' | '--' | '!' | '+' | '-')* prio1Expression ('++' | '--')*;
prio1Expression:
prio0Expression (
('.' prio0Expression) |
('(' (expression (',' expression)*)? ')') |
('[' expression (',' expression)* ']')
)*;
prio0Expression:
/*('(') => */('(' expression ')') |
IDENTIFIER |
//collectionLiteral |
coordinateLiteral |
'true' |
'false' |
NUMBER |
STRING
;
...
Expression 是 prio14Expression 的标签.你可以看到完整的语法 这里.
Expression is a label for prio14Expression. You can see the full grammar here.
代码生成本身是成功的(没有任何错误或严重警告).它生成以下代码:
The code generation itself is successful (without any errors or serious warnings). It generates following code:
CONSTRUCTEX();
EXCEPTION->type = ANTLR3_MISMATCHED_SET_EXCEPTION;
EXCEPTION->name = (void *)ANTLR3_MISMATCHED_SET_NAME;
EXCEPTION->expectingSet = &FOLLOW_set_in_prio2Expression962;
RECOVERFROMMISMATCHEDSET(&FOLLOW_set_in_prio2Expression962);
goto ruleprio2ExpressionEx;
构建时不会出现错误Error 5 error C2065: 'FOLLOW_set_in_prio2Expression962': undeclared identifier
".
Which does not build with error "Error 5 error C2065: 'FOLLOW_set_in_prio2Expression962' : undeclared identifier
".
我在语法上做错了吗?没有其他规则会导致此错误,如果我稍微重新制定相关规则,生成的代码是有效的(但语法不符合我的要求).我该怎么做才能解决这个问题?
Did I do something wrong in the grammar? No other rules cause this error and if I somewhat reformulate the rule concerned, the generated code is valid (but then the grammar doesn't do what I want it to). What can I do to fix this issue?
感谢您的帮助.
推荐答案
我遇到了同样的问题.
我认为如果解析器规则具有像这样的简单 OR-ed 标记的一部分,就会发生这种情况:
I think it happens if parser rule has a part of simple OR-ed token like this:
problem_case: problematic_rule;
problematic_rule: 'A' | 'B' ;
如果是词法分析器规则,则不会发生这种情况.
This doesn't happen if it is lexer rule.
workaround1: As_lexer_rule;
As_lexer_rule: 'A' | 'B' ;
或者,如果它是复杂的规则(不是简单的 OR ed token).
Or, if it is complicated rule (not simple OR-ed token).
workaround2: make_it_complicated_needlessly;
make_it_complicated_needlessly: 'A' | 'B' | {false}? NeverUsedRule;
NeverUsedRule: /* don't care*/ ;
(我使用语义谓词{false}?"进行此修改.我相信它不会改变目标语言的语法.)
( I used semantic predicate "{false}?" for this modification. I believe it doesn't change the grammar of target language.)
这篇关于“FOLLOW_set_in_"...在生成的解析器中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!