Wrong rule matched

别说谁变了你拦得住时间么 提交于 2019-12-24 18:52:25

问题


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;

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?


回答1:


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]

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:'-';.

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]

Also, intLiteral may conflict with expr and should be included as one of the possible expressions.

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] ;

File input.txt :

-2
- 2
1 - 2
3 + 4
5
abc + def

Execution :

$ 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


来源:https://stackoverflow.com/questions/46960604/wrong-rule-matched

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!