[antlr-interest] array list action attributes

Jim Idle jimi at temporal-wave.com
Mon Jan 3 10:27:46 PST 2011


The += syntax is really only used for tree rewriting, but the vector
access functions are all documented in the API and there a copious
examples if you read through the runtime source code.

Unless you don't care too much about memory (memory for the STRINGs is not
released until you free the parser), then I would just get the pointers
from the token directly and copy whatever text you want from there.

However, I think that your confusion here is that you are gathering a list
then trying to process it afterwards, where I think you will find it more
useful to do this (and note that you use + as otherwise if there are no
IDs then it is just a TYPE alt):

: TYPE // No IDs
| ^(TYPE

      (
        i=ID { some code that does $i.whatever }
      )+
  )
  { action code to finish up }
;


Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Aaron Leiby
> Sent: Sunday, January 02, 2011 7:30 PM
> To: antlr-interest
> Subject: [antlr-interest] array list action attributes
>
> 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
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list