With the following rule:
expr:
'(' expr ')' #exprExpr
| expr ( AND expr )+ #exprAnd
| expr ( OR expr )+ #exprOr
| atom #exprAtom
| ID #exprId
;
atom:
'[' ID RELOP INT ']'
;
I would like to allow statements like this:
[a<3] and [b<4]
[a<3] or [b<4]
[a<3] or ([b<4]and [c<5])
but forbid statements like this:
[a<3] or [b<4] and [c<5]
This basic idea seems to work with this grammar. But there is one aspect/side effect, which I do not understand:
While parsing code with 3 atoms ( like atom1 and atom2 and atom3) the method exprAnd ist called twice (not once, as I would expect it to).
So code like this:
public String visitExprAnd(myParser.ExprAndContext ctx) {
String res = "";
int type=-1;
int nAtoms = ctx.atom().size();
for (int i=0;i<nAtoms;i++) {
String s = visit(ctx.expr(i));
}
return s;
}
does not work for all and-expressions at once.
So somehow I would have expected the exprAnd and exprOr rules to be more greedy.
How could one achieve this?