[antlr-interest] ast rewrites in left-recursive rules

Terence Parr parrt at cs.usfca.edu
Wed Feb 23 16:37:28 PST 2011


So I have it working with rewrite rules now:

e : e '.' ID 			-> ^('.' e ID)
  | e '.' 'this' 		-> ^('.' e 'this')
  | '-' e 			-> ^('-' e)
  | e '*' b=e 			-> ^('*' e $b)
  | e (op='+'|op='-') b=e	-> ^($op e $b)
  | INT 			-> INT
  | ID 				-> ID
  ;

But take a look at the multiplication rule: it needs a label on the second e. plain e is ambiguous. I decided that plain e references the left recursive version; since it will disappear during the transformation, putting a label on that one won't work. we have to put a label on the second reference as you see above. this is not optimal. can anyone think of a better way to differentiate between the left and right e references in a single alternative? [Note that e refers to the entire tree created so far.]

Ter


More information about the antlr-interest mailing list