[antlr-interest] Newbie: rule equivalent of a token reference?

Tarun Khanna tarunkhanna at gmail.com
Tue Jul 19 07:55:14 PDT 2005

You'll have to return the string from the rule constant.

So your two rules would look something like the following. In the variable 
declaration rule the x would then contain whatever string constant returns.

You should look at the manual that describes this stuff. One of the getting 
started tuts has it. Hoewever they talk abt returning integers.

{String x;}
x=constant type ( COMMA constant type )* 
constant returns [String x=""}
i:INDENTIFIER {x=i.getText();}
type : .....

On 7/19/05, JConner at ssp-uk.com <JConner at ssp-uk.com> wrote:
> Dear all,
> Sorry if this isn't the right place to ask this, and I know that I might 
> be
> trying to run before I can walk, but...
> I'm trying to play around and create a simple translator. Getting the
> "text" of a token seems straight forwards - you label the token reference,
> and call xzy.getText() in an action. However, what do I do if I wrap the
> token reference up in another parser rule? Eg, in:
> variable_declaration : constant type ( COMMA constant type )* ;
> constant : INDENTIFIER ;
> type : .....
> Given that COMMA and IDENTIFIER are lexer rules, how would I get the 
> "text"
> from the rule constant into an action in the rule variable_declaration? Am
> I missing something obvious, or am I trying to go about this in the wrong
> way?
> John

Tarun Khanna
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20050719/61fc82f8/attachment.html

More information about the antlr-interest mailing list