[英]ANTLR4 parser generation: issue with simple grammar for conditional expressions
I am trying to generate a lexer and parser for a simple language. 我正在尝试为一种简单的语言生成一个词法分析器和解析器。 At the moment, the language can only parse a conditional expression. 目前,该语言只能解析条件表达式。 A conditional expression is much like one in C, and because I am only learning the rudiments here, all the gotchas we have in a language like CI am not going to be concerned about implementing. 条件表达式很像C语言中的条件表达式,并且由于我只是在这里学习基础知识,因此我们使用诸如CI之类的语言获得的所有陷阱都不会在意实现。
I have the following grammar: 我有以下语法:
grammar Simple ;
cond_expr : left_paren operand operator operand right_paren ;
operand : function_expr | ID | literal | cond_expr ;
function_expr : ID left_paren paramList right_paren ;
paramList : param (',' param)* ;
param : ID | function_expr ;
literal : string | number ;
string : '"' sentence '"' ;
sentence : (WORD | WORD (' ' WORD)*)* ;
number : INTEGER | FLOAT ;
left_paren : LEFT_PAREN ;
right_paren : RIGHT_PAREN ;
operator : OPERATOR ;
INTEGER : [0-9]+ ;
FLOAT : INTEGER '.' INTEGER | '.' INTEGER ;
LEFT_PAREN : '(' ;
RIGHT_PAREN : ')' ;
ID : [A-Za-z]+[A-Za-z0-9_]* ;
WORD : [A-Za-z]+ ;
OPERATOR : ('==' | '>=' | '<=' | '!=' | '&&' | '||' | '~') ;
WS : (' '|'\r'|'\n'|'\t') -> channel(HIDDEN);
And when I run the parser on it, I am not getting the results I expect. 当我在其上运行解析器时,我没有得到预期的结果。 Here's an example of an incorrect output: 这是不正确的输出的示例:
~/sandbox $ grun Simple cond_expr -tree
(a (c, d (e, f)) != b)
line 1:2 mismatched input ' ' expecting OPERATOR
line 1:5 mismatched input ',' expecting OPERATOR
line 1:8 mismatched input ' ' expecting ')'
(cond_expr (left_paren () (operand a) (operator ) (operand (cond_expr (left_paren () (operand c) (operator , ) (operand d) (right_paren ( e , f))) (right_paren )))
What is the mistake in my grammar? 我的语法有什么错误? Any help is appreciated. 任何帮助表示赞赏。
Appears that the grammar is a direct implementation of an EBNF representation of the desired DSL. 似乎语法是所需DSL的EBNF表示的直接实现。 A bit more work is required to get it to work well in ANTLR. 要使其在ANTLR中正常工作,还需要做更多的工作。 As is: 照原样:
1) there is a mutual left recursion problem with the rules function_expr
, paramList
, and param
; 1)规则function_expr
, paramList
和param
存在相互左递归问题;
2) sentence
can match nothing; 2) sentence
不能匹配;
3) WS
is hidden in the lexer, so sentence
could never match, anyway ;); 3) WS
隐藏在词法分析器中,因此sentence
无论如何都不会匹配;);
4) the ID
rule shadows WORD
, so WORD
tokens will never be emitted. 4) ID
规则遮盖了WORD
,因此将永远不会发出WORD
令牌。
(You should have received a Tool warning on 1 and 2; never ignore, since warnings indicate that run-time behavior can be affected.) (你应该已经收到警告工具对1和2;从不忽略,因为警告表明,运行时的行为可能会受到影响。)
The basic form for an expression rule is to list, in a single rule, all of the alternate forms of the expression. 表达式规则的基本形式是在一条规则中列出表达式的所有替代形式。
expr : LPAREN expr RPAREN
| expr operator expr
| function
| string
| number
| ID
;
function : ID LPAREN ( ID | function ) (',' ( ID | function ))* RPAREN ;
string : STRING ;
number : INTEGER | FLOAT ;
operator : OPERATOR ;
STRING : '"' .*? '"' ;
(untested) (未试)
So (a (c, d (e, f)) != b)
should correctly evaluate to 因此(a (c, d (e, f)) != b)
应该正确计算为
expr (function ( ID, function ( ID, ID ) ) operator ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.