[antlr-interest] 2nd and Subsequent References in Action to Non-Terminals Generate Incorrect Code

Randall R Schulz rschulz at sonic.net
Wed Nov 22 09:32:05 PST 2006


Hi,

It appears that within any given parser rule only the first 
alternative's action can successfully refer to (non-assigned) 
non-terminals.

E.g., this parser rule:

functionTerm
returns [ Term ft ]
  : plainTerm     { $ft = $plainTerm.pt; }
  | definedTerm     { $ft = $definedTerm.dt; }
  ;

generates correct code for the reference in the action to $plainTerm.pt 
but simply emits $definedTerm.dt for the reference in the second 
alternative.


However, by assigning the non-terminals to local labels the correct code 
is generated:

functionTerm
returns [ Term ft ]
  : pt = plainTerm    { $ft = $pt.pt; }
  | dt = definedTerm  { $ft = $dt.dt; }
  ;

This formulation generates correct parser Java code.


Randall Schulz


More information about the antlr-interest mailing list