[antlr-interest] array list action attributes

Aaron Leiby aleiby.antlr at gmail.com
Sun Jan 2 19:29:51 PST 2011


ANTLR3 allows labeling attributes for referencing in actions.

Example:

decl: type id=ID ';' { print "var" + $id.text; }

With the C language target, the $id.text gets converted nicely into:

(id->getText(id))

However, if you have more than one attribute:

decl: ^( TYPE ids+=ID* )

...$ids becomes a pANTLR3_VECTOR, and it appears those helpful attributes no
longer work?

I was hoping something like $ids[i].text would get automatically converted.
 Instead, I had to dig into the implementation a bit and hand-expand it to:

pANTLR_BASE_TREE id = (pANTLR_BASE_TREE)$ids->get($ids, i);
const char* name = (const char*)id->getText(id)->chars;

So, I guess a couple questions:

1) Does the java language option suffer the same fate?  (i.e. ANTLR3 simply
does not provide syntax for working with attributes on multi-value labels?)
2) Does the C API provide some nice macros I may have missed for making this
less gross?  (e.g. its set of SCOPE accessors)

Thanks,
Aaron


More information about the antlr-interest mailing list