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       :       'a'
        ;
        
b       :       'b'
        ;


Let me know if there is a better or more elegant way to do this. But I'm fine 
with this :-)


Thanks, Indhu


----- Original Message -----
From: Indhu Bharathi <[email protected]>
To: Terence Parr <[email protected]>
Cc: antlr-interest <[email protected]>
Sent: Friday, February 27, 2009 9:41:20 AM GMT+0530 Asia/Calcutta
Subject: Re: [antlr-interest] Rewriting in non tree grammar

Hi,

I'm sorry I don't understand. When I write

a : t=B C ;

t will be of type token and I can use it in action. But when I write,

a : t=b c ;

t is not of type token. Infact no code is generated for 't'. So how do I do
I refer to it in action?
        
Also I don't find any example out there for this requirement. Can anybody
explain a little more clear. Sorry if there is some simple way to do it and
I'm not getting it.

- Indhu


-----Original Message-----
From: Terence Parr [mailto:[email protected]] 
Sent: Friday, February 27, 2009 2:53 AM
To: Indhu Bharathi
Cc: 'antlr-interest'
Subject: Re: [antlr-interest] Rewriting in non tree grammar


On Feb 26, 2009, at 12:55 PM, Indhu Bharathi wrote:

> Hi,
>
> Suppose I want to do something like this
>
> methodDecl       :               scope t=returnType ident LRAPEN  
> RPAREN
>                                 {
>                                                 insertBefore(t,  
> "something");
>                                                 or
>                                                 replace(t,  
> "something");
>                                                 or
>                                                 insertAfter(t,  
> "something");
>                                 }
>
>
> How can I do this in ANTLR? Note that 'returnType' is non-terminal.  
> Also, I don't want to use tree grammar or string templates. Is this  
> possible with TokenRewriteStream. Is there some documented example  
> in the wiki?
>

Yes, use that class.  easy.  Should be some examples somewhere.

Ter


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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to