[antlr-interest] Re: Getting at the token text in a parser

lgcraymer lgc at mail1.jpl.nasa.gov
Sun Feb 29 18:09:13 PST 2004


$getText works in the lexer, not in the parser--in the lexer, there is
text associated with a rule, but not in the parser..  What you want to
do is label the AST (or Token) nodes that you want to extract text
from--say
v:value { #v.getText(); }

(assuming Java, not C++).

Look at the examples directory in the distribution--your problem is
most likely that you need to mentally do a paradigm shift, and the
examples should give you a clue.  

--Loring


--- In antlr-interest at yahoogroups.com, "offline314159" <offline at s...>
wrote:
> Here's what i have  - copied verbatim, because i'm not sure where the
> error is.  I'm working w/ 2.7.2 in Eclipse (using the antlreclipse
> plugin)
> 
> === begin grammar segment ---
> baseitem
> :
> (
> 	{ System.out.println("Entering address"); }
> 	ADR
> 	(
> 		params
> 	)?
> 	addressparts
> 	|
> 	{ System.out.println("Entering org"); }
> 	ORG
> 	(
> 		params
> 	)?
> 	orgparts
> 	|
> 	{ System.out.println("Entering N"); }
> 	N
> 	(
> 		params
> 	)?
> 	nameparts
> 	|
> 	{ System.out.println("Entering Agent"); }
> 	AGENT
> 	(
> 		params
> 	)?
> 	value
> 	|
> 	{ System.out.println("Entering " + $getText); }
> 	name
> 	(
> 		params
> 	)?
> 	value
> )
> ENDL
> ;
> === end grammar segment ===
> 
> It's the $getText above that is giving me troubles - i can't seem to
> use it anywhere - i get the following runtime error if i try:
> 
> == error ==
> java.lang.Error: Unresolved compilation problems: 
> 	text cannot be resolved
> 	_begin cannot be resolved
> 	text cannot be resolved
> 	_begin cannot be resolved
> == end error ==
> 
> I get that error anywhere and any time i try to use $getText
> 
> I would just be using the debugging parser, but i can't get that to
> work either - if i try to turn on analytic debugging in the parser
> options, antlr borks on me with a fairly unhelpful silence.
> 
> I'm trying hard to get a grasp of this tool, because i can see that
> it's very powerful, but i'm getting frustrated at the seeming
> impossibility of doing basic things - and the lack of good
> troubleshooting and/or common hitches.  The fact that the jguru forum
> for it can't be searched unless you're a paying member isn't helping.
> 
> Hopefully someone here will be kind enough to walk me through what i'm
> doing wrong.  I'll add in anything else that is necessary to diagnose
> the problem.
> 
> Thanks again
> Cheers
> --
> Chris



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list