ANTLR Grammar for expressions

后端 未结 3 1580
一生所求
一生所求 2020-12-21 09:16

I\'m trying to implement a expression handling grammar (that deals with nested parenthesis and stuff). I have the following so far, but they can\'t deal with some cases (suc

3条回答
  •  悲&欢浪女
    2020-12-21 09:50

    Make use of the ^ placed after the token/rule name is very useful for defining expressions.

        expression :    e1 (OR^ e1)* ;
        e1  :   e2 (AND^ e2)*;
        e2  :   e3 (PIPE^ e3)*;
        e3  :   e4 (ANDSYMB^ e4)*;
        e4  :   e5 ((EQUAL^|NOTEQUAL^) e5)*;
        e5  :   e6 ((LESS^|GREATER^) e6)*;
        e6  :   e7 ((PLUS^|MINUS^) e7)* ;
        e7  :   e8 ((STAR^|SLASH^) e8)* ;
        e8  :   e9 (NEW^ ID LPAREN RPAREN)*;
        e9  :   (NOT^)? e10;
        e10 :   e11 | call_def;
        e11 :   constant 
            | '(' expression ')' -> expression;
    

提交回复
热议问题