How to write Antlr rules in a non-recursive way?

前端 未结 1 531
谎友^
谎友^ 2021-01-23 15:41

I have the following the expressions I need to parse

and(true, false)
or(true, false, true)
not(or(true, false, true))
and(and(true, false), false)
or(or(true, f         


        
相关标签:
1条回答
  • 2021-01-23 16:31

    But for now, I need to be able to parse true=and(5=5, 4=4, 3>2) or vice-versa and(5=5, 4=4, 3>2)=true ?

    In that case, there is absolutely no need to complicate things. All you have to do is this:

    grammar Test;
    
    parse
     : expr EOF
     ;
    
    expr
     : '(' expr ')'
     | OR '(' expr (',' expr)+ ')'
     | AND '(' expr (',' expr)+ ')'
     | NOT '(' expr ')'
     | expr (LT | LTE | GT | GTE) expr
     | expr (EQ | NEQ) expr
     | TRUE
     | FALSE
     | INT
     ;
    
    LT    : '<';
    LTE   : '<=';
    GT    : '>';
    GTE   : '>=';
    NEQ   : '!=';
    EQ    : '=';
    NOT   : 'not';
    TRUE  : 'true';
    FALSE : 'false';
    AND   : 'and';
    OR    : 'or';
    
    INT
     : [0-9]+
     ;
    
    SPACES
     : [ \t\r\n] -> skip
     ;
    
    0 讨论(0)
提交回复
热议问题