[antlr-interest] Rewriting in non tree grammar

Indhu Bharathi indhu.b at s7software.com
Fri Feb 27 23:22:13 PST 2009


Got it. What I missed is the '$' symbol while referencing

I wrote:

r : t=a b a
  {
    ... t.start.getTokenIndex() ...
  }

It must have been 

r : t=a b a
  {
    ... $t.start.getTokenIndex() ...
  }

Thanks :-)


----- Original Message -----
From: Terence Parr <parrt at cs.usfca.edu>
To: Indhu Bharathi <indhu.b at s7software.com>
Cc: antlr-interest <antlr-interest at antlr.org>
Sent: Saturday, February 28, 2009 11:44:21 AM GMT+0530 Asia/Calcutta
Subject: Re: [antlr-interest] Rewriting in non tree grammar

label the rule refs, dude :)
Ter
On Feb 27, 2009, at 8:42 PM, Indhu Bharathi wrote:

>> $a.start.getTokenIndex() is better than relying on index probably.
>
> Great! But how to do the same when I have a production like this:
>
> a : b c b
>  ;
>
> Suppose I need to insert some text before the first 'b' as well as the
> second 'b', I cannot write
>
> {
> 	Input.insertBefore( $b.start.getTokenIndex(), "some text");
> 	Input.insertBefore( $b.start.getTokenIndex(), "some other text");
> }
>
> If 'b' is a lexer rule ('B'). then I can do
>
> a : t1=B c t2=B
> {
> 	Input.insertBefore( t1, "some text");
> 	Input.insertBefore( t2, "some other text");
> }
> ;
>
> I tried doing the same thing for parser rule 'b' but it doesn't  
> work. Any
> idea how to get this working?
>
>
> - Indhu
>
>
> -----Original Message-----
> From: Terence Parr [mailto:parrt at cs.usfca.edu]
> Sent: Friday, February 27, 2009 9:53 PM
> To: indhu.b at s7software.com
> Cc: antlr-interest
> Subject: Re: [antlr-interest] Rewriting in non tree grammar
>
>
> On Feb 27, 2009, at 12:43 AM, Indhu Bharathi wrote:
>
>> Figured it out :-) Here is a sample grammar that does what is
>> required:
>>
>> grammar Test;
>>
>> @members {
>> 	TokenRewriteStream input = (TokenRewriteStream) getTokenStream();
>> }
>>
>> r
>> @init {
>> 	int pos;
>> }
>> 	:	a { pos = input.index(); } b
>> 		{
>> 			input.insertBefore(pos, "inserted");
>> 		}
>> 	;
>
> $a.start.getTokenIndex() is better than relying on index probably.
> Ter
>




More information about the antlr-interest mailing list