[antlr-interest] array list action attributes

Aaron Leiby aleiby.antlr at gmail.com
Sat Jan 8 12:48:19 PST 2011


Sorry, that was a bad example.  I would either hoist the 'parameters' rule
up into 'functionLiteral' to keep track of the count locally or more likely
just have the 'parameters' rule return the count.

functionLiteral
: 'function' name=NameLiteral? args=parameters body=block
-> ^( FUNC $name? $args.count $args $body )
;

The real point is that the output AST is composed of tree nodes, so you
can't just mix an integer in with that without wrapping it up in a node
first, and I haven't figured out how to do that (if it's even something
supported by ANTLR).

On Sat, Jan 8, 2011 at 12:37 PM, Aaron Leiby <aleiby.antlr at gmail.com> wrote:

> I would prefer to handle the actions as suggested above, but in most cases
> I need to know how many elements exist first.  So what I've been doing is
> collecting them, getting the count, then processing them.
>
> This is all in a tree walker grammar.  Is there an easy way to write out
> the count in the lexer/parser grammar rewrite rules so it's available to the
> tree walker without having to gather them first as I've been doing?
>
> For example, I'd like to be able to do something like this (in the
> lexer/parser grammar):
>
> functionLiteral
> : 'function' name=NameLiteral? args=parameters body=block
>  -> ^( FUNC $name? $args->count $args $body )
> ;
>
> But the $args->count obviously won't work since the output tree is
> homogenous.  I imagine there might be a way to create a dummy node with the
> value as a payload, but I haven't tracked down the syntax for that yet.
>
> Perhaps I'm still going about this the wrong way?
>
> > the vector access functions are all documented in the API and there
> > a copious examples if you read through the runtime source code.
>
> Yep.  I was more hoping that there was higher level syntax in the ANTLR
> language itself that could be used to autogenerate the API calls when
> working with those vectors (making the grammar a bit more portable in the
> process).  I was aware of the single element support (e.g. $arg.text), and
> hoped I had simply missed the multi-value support.  Failing that, I was
> familiar with the SCOPE macros that the C target provides for hiding some of
> the data structure API details, and imagined similar macros had been created
> for working with the += output that I had maybe missed.  It sounds like
> neither of those are the case, so working directly with the API calls is the
> correct/only path currently.
>
> Thanks!
>
> On Mon, Jan 3, 2011 at 10:27 AM, Jim Idle <jimi at temporal-wave.com> wrote:
>
>> 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
>>
>> 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