[antlr-interest] Variable definition annoyances

Terence Parr parrt at cs.usfca.edu
Sat Mar 26 10:32:45 PST 2005


On Mar 25, 2005, at 7:30 PM, Scott Stanchfield wrote:

>> Yep, that is really annoying.  I racked my brain over it for
>> a long time.  It turns out you don't need to define them
>> anymore.  You can do
>> this:
>>
>> start : r=rule { @r.i ... @r.s ... } ;
>
> I pretty-much like, but can't we infer a variable for the rule? So it 
> would
> just be:
>
>   start
>     : rule { @rule.i ... } ;

That is possible, yes.  I've considered it but you run into trouble 
with multiple refs...The $1 $2 thing died with yacc (i hoped) ;)  
Perhaps then you have to label.  I'll reconsider.

> You could easily do this by genning
>
>   ResultType __rule = rule();
>   __rule.i
>
> Or some such thing.
>
> For multiple refs. They could be numbered:
>
>   start
>     : rule otherRule rule { @rule[1].i ... @rule[2] ... }
>     ;

....

> Though while we're at it, I'd really like to define a standard syntax 
> that
> can be for antlr replacement in action code with an obvious start and 
> end,
> which is why I suggested a paired delimiter like :: on either end. 
> We'd just
> need to pick something not super likely to appear in the action code...

Why do you need the replacement thing?  So you could alter the code 
generator more easily?  I'm trying to avoid translation but I need to 
for @i.

>> rule returns [int i, String s] : ... {@i=3; @s="foo";} ... ;
>>
>> Cool eh?  Does this satisfy you?  It integrates very nicely
>> with the dynamically scoped attributes and trees etc...
>
> Actually, why would we need to use the @ in that syntax? In ANTLR 2.0 
> with
> the same syntax we just use i and s...

Because these suckers can be dynamically scoped and don't translate to 
simply i and s. :)

>  Wait a sec... Multiple return
> values? Are you pushing them on a stack or something?

Currently I'm using the method stack, but I'm going to change that to 
my own stack.  I push an aggregate with the return values as fields.

Ter





More information about the antlr-interest mailing list