如何以非递归方式编写 Antlr 规则?

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

我有以下表达式需要解析

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

到目前为止我有以下语法

expr
    : orExpr
    ;

orExpr
    : OR '(' andExpr (',' andExpr)+ ')'
    | andExpr
    ;

andExpr
    : AND '(' equalityExpr (',' equalityExpr)+ ')'
    | equalityExpr
    ;

equalityExpr
    : comparison ((EQUALS | NOT_EQUALS) comparison)*
    ;

comparison
    : notExpr ((GREATER_THAN_EQUALS | LESS_THAN_EQUALS | GREATER_THAN | LESS_THAN ) notExpr)?
    ;

notExpr
    : NOT '(' expr ')'
    | primary
    ;

primary
    : '(' expr ')'
    | true
    | false
    ;

我无法用上面的语法解析 and(and(true, false), false)?我哪里错了?

请假设 AND OR NOT 之间有优先级(虽然我知道它可能看起来没有必要)

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 ?

既然如此,完全没有必要把事情复杂化。您所要做的就是:

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
 ;