我正在尝试构建语法,但我陷入了这个错误并且看不到我哪里出错了。
如果我输入短语:
if(hello = "test", 1, 0)
进入 antlr4 测试平台,我得到 *"mismatched input '=' Expecting COMPARE_OP"*。我究竟做错了什么?
这是我的完整语法:
prog: (expr|stats);
val_expr: (rename|rex);
cond_expr: (match|xif|compare_op);
expr: (val_expr|cond_expr|var|math_expr|eval);
rename : 'rename' var 'as' LITERAL; // match keyword hello followed by an
count : 'count' ('as' LITERAL)* ('by' var (',' var)* )*; //
match : 'match' '(' cond_expr ',' expr ')'; // match(x, y)
xif : 'if' '(' cond_expr ',' expr ',' expr ')'; // if(x, y, z)
var: (VARNAME|LITERAL|BLANK);
stats: 'stats' count;
eval: 'eval' VARNAME '=' expr;
rex: 'rex' ('field' '=' var)* LITERAL;
compare_op: math_term COMPARE_OP math_term;
math_expr: math_term (MATH_OP math_term)*;
math_term: (var|DIGITS);
VARNAME : [a-zA-Z_]([a-zA-Z0-9_])*;
DIGITS: [0-9]+;
WS : [ \t\r\n]+ -> skip ;
BLANK: '""';
LITERAL : '"' ( '\\"' | . )*? '"' ;
COMPARE_OP: ([=><]|'>='|'<=');
MATH_OP: ('+'|'/'|'%'|'*'|'-');