[antlr-interest] Rewriting in non tree grammar

Indhu Bharathi indhu.b at s7software.com
Fri Feb 27 00:43:12 PST 2009


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 <indhu.b at s7software.com>
To: Terence Parr <parrt at cs.usfca.edu>
Cc: antlr-interest <antlr-interest at antlr.org>
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:parrt at cs.usfca.edu] 
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



More information about the antlr-interest mailing list