匹配错误规则 [英] Wrong rule matched
问题描述
我在词法分析器中有以下内容
I have the following in the lexer
INTEGER : DIGIT+;
NOT: '!';
MINUS:'-';
PLUS:'+';
fragment DIGIT: '0'..'9';
我在解析器中有以下内容
I have the following in the parser
expr:
intLiteral
| UnaryOp expr;
intLiteral: (PLUS|MINUS)? INTEGER;
UnaryOp: NOT|MINUS;
当我使用 grun 用 -2 测试它时,我发现它与 UnaryOp expr 匹配,而不仅仅是 intLiteral.换句话说,减号被检测为 UnaryOp.为什么会发生这种情况,有没有办法解决?
When I use grun to test it with -2, I get it being matched to UnaryOp expr instead of just intLiteral. In other words, the minus sign is being detected as a UnaryOp. Why would this be occuring and is there a way to fix it?
推荐答案
做法是对词法规则使用全部大写,对解析规则使用全部小写.如果你混合它们,它会导致像你这样的错误:
The practice is to use all CAPITALS for lexer rules, and all lower case for parser rules. If you mix them, it causes errors like the one you have :
$ grun Question expr -tokens -diagnostics input.txt
[@0,0:0='-',<UnaryOp>,1:0]
[@1,1:1='2',<INTEGER>,1:1]
[@2,2:1='<EOF>',<EOF>,1:2]
因为它以大写字母开头,UnaryOp
是一个词法分析器规则,而不是您可能相信的解析器规则,并且 -
符号已匹配为 UnaryOp
标记,因为这个规则是在 MINUS:'-';
之前定义的.
Because it starts with a capital letter, UnaryOp
is a lexer rule, not a parser rule as you may believe, and the -
sign has been matched as a UnaryOp
token, because this rule is defined before MINUS:'-';
.
如果 MINUS
规则出现在 UnaryOp
之前,-
符号将作为 MINUS 匹配:
If the MINUS
rule comes before UnaryOp
, the -
sign will be matched as a MINUS :
$ grun Question question -tokens -diagnostics input1.txt
[@0,0:0='-',<'-'>,1:0]
[@1,1:1='2',<INTEGER>,1:1]
[@2,2:1='<EOF>',<EOF>,1:2]
此外,intLiteral
可能与 expr
冲突,应作为可能的表达式之一包含在内.
Also, intLiteral
may conflict with expr
and should be included as one of the possible expressions.
以下语法遵循我的风格(文件 Question.g4):
The following grammar follows my style (file Question.g4) :
grammar Question;
question
@init {System.out.println("Question last update 2108");}
: line+ EOF
;
line
: expr NL
{System.out.println("Expression found : " + $expr.text); }
;
expr
: ( PLUS | MINUS ) expr # exprUnaryOp
| expr PLUS expr # exprAddition
| expr MINUS expr # exprSutraction
| atom # exprAtom
;
atom
: INTEGER
| ID
;
ID : LETTER ( LETTER | DIGIT | '_' )* ;
INTEGER : DIGIT+ ;
NOT : '!' ;
MINUS : '-' ;
PLUS : '+' ;
NL : [\r\n] ;
WS : [ \t] -> channel(HIDDEN) ; // -> skip ;
fragment LETTER : [a-zA-Z] ;
fragment DIGIT : [0-9] ;
文件 input.txt
:
-2
- 2
1 - 2
3 + 4
5
abc + def
执行:
$ grun Question question -tokens -diagnostics input.txt
[@0,0:0='-',<'-'>,1:0]
[@1,1:1='2',<INTEGER>,1:1]
[@2,2:2='\n',<NL>,1:2]
[@3,3:3='-',<'-'>,2:0]
[@4,4:4=' ',<WS>,channel=1,2:1]
[@5,5:5='2',<INTEGER>,2:2]
...
[@25,27:29='def',<ID>,6:6]
[@26,30:30='\n',<NL>,6:9]
[@27,31:30='<EOF>',<EOF>,7:0]
Question last update 2108
Expression found : -2
Expression found : - 2
Expression found : 1 - 2
Expression found : 3 + 4
Expression found : 5
Expression found : abc + def
这篇关于匹配错误规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!