[antlr-interest] translation of $x references in v3.0

Terence Parr parrt at cs.usfca.edu
Sun May 1 11:01:09 PDT 2005


Howdy,

I'm neck deep in the action translation at the moment; mostly 
refactoring and adding unit tests after speaking with John Mitchell 
yesterday at length.  After he smacked me around a little, I am 
convinced that I need to make some changes.  One is that we will use $x 
to refer to the stuff you're used to like parameters, return values, 
token labels, and rule labels; @x.y will refer to the new dynamic 
attribute stuff that you've been hearing about.  John and I agreed that 
a separate symbol is justified as the dynamic attributes are so 
different than the regular attributes.

Anyway, on to simple $x references.  One of the things that drives me 
nuts in 2.x is that sometimes you can forget the #x or $x and have it 
still get through ANTLR to compile and run...but #x, for example, is 
different than x!  Ack!  This kind of bug has trapped me for hours 
sometimes.

My proposal is that all labels and simple attributes like parameters 
are generated with a prefix or something so that you cannot 
accidentally reference them in an action.  This helps me track which 
return values you access, for example, which helps me generate better 
code.  If nobody references a rule return value, I won't generate code 
for it. :)

So if you had:

a[String s, int x] returns [float y]
   : id=ID f=field {s, x, y, id, f.z;}
   ;
field returns [int z]
   : ...
   ;

you'd get compilation errors on s, x, y, id, and f.  You need to do

a[String s, int x] returns [float y]
   : id=ID f=field {$s, $x, $y, $id, $f.z;}
   ;

Note that multiple return values require that I build a struct or 
simple class to hold the values so s and x do not even exist as simple 
variable references.

Anybody care to comment?

Ter
I've posted this to one of the blogs:
http://www.antlr.org/blog/antlr3/rewrite.tml
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com



More information about the antlr-interest mailing list