规则匹配错误 [英] Wrong rule matched

查看:105
本文介绍了规则匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在词法分析器中有以下内容

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?

推荐答案

实践是将所有CAPITALS用于词法分析器规则,并将所有小写字母用于语法分析器规则.如果将它们混合使用,则会引起类似您所遇到的错误的错误:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆