[antlr-interest] Rewriting in non tree grammar

Terence Parr parrt at cs.usfca.edu
Fri Feb 27 22:14:21 PST 2009


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