Question

With antlr4 I can label rule alternatives like this:

e : e '*' e # Mult
  | e '+' e # Add
  | INT # Int
  ;

From what I understand, in the rule above, Mult has higher precedence over Add because Mult comes before Add in the list of alternatives.

So for instance, if I wrote:

e : e '*'       e # Mult
  | e ('+'|'-') e # Add
  | INT # Int
  ;

The + in 1 + 2 and - in 4 - 2 have the same precedence. However, now the alternative is not in the top level. Is there a way I can label the rules e '+' e # Add and e '-' e #Sub separately while still having both alternatives have same precedence level?

Was it helpful?

Solution

I'm afraid not. You can label the op though with op=('+'|'-') and then get the ctx.op() value during a tree walk and ask for its token type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top