[antlr-interest] Variable definition annoyances

Scott Stanchfield scott at javadude.com
Fri Mar 25 04:42:59 PST 2005


For 3.0, can we do something about variable definitions?

I get pretty annoyed at things like:

foo
  { String a=null, b=null, c=null, d=null; }
  : a=fee
    b=fie
    c=fo
    d=fum
    { doSomething(a,b,c,d); }
  ;

Can we not infer the variable definitions based on rule usage?

foo
  : a=fee
    b=fie
    c=fo
    d=fum
    { doSomething(a,b,c,d); }
  ;

or nicer still, something like

foo
  : fee fie fo fum
    { doSomething(::fee::, ::fie::, ::fo::, ::fum::); } 
  ;

(picking some syntax that's not likely to occur in any languge... Not sure
if ::x:: is sufficient, but it's a stab) and have the rule-ref assignments
be generated?

For multiple refs, we could go something like:

foo
  : stuff expression stuff fum
    { doSomething(::stuff[1]::, ::expression::, ::stuff[2]::, ::fum::); } 
  ;

which would be very clear, and really nice for tokens:

foo
  : ID EQUALS ID PLUS ID
    { addAndAssign(::ID[1]::, ::ID[2]::, ::ID[3]::); }


Ooooh... Think about all the cool stuff we could do with this:

  ::ID.text:: or ::ID[n].text::
  ::ID.type:: or ::ID[n].type::
     
translated to appropriate "get" call for the token text, type, whatever.

Something like

  ::foo.tree::

could represent the resulting tree, ::foo. at x:: could represent an attribute
(perhaps for XML processing), and this could be extended by later ANTLR
plugins or versions, like

  ::foo.matched-text::

or

  ::foo.start-line::
  ::foo.end-line::
  ::foo.first-set::
  ::foo.follow-set::

and other meta madness.

This would allow easy syntactic sugar to be added in action code, and make
the grammar much cleaner.

Thoughts?
-- Scott






More information about the antlr-interest mailing list