[antlr-interest] a crazy problem cost me almost two days ---how can i get the return value from antlr?
Jim Idle
jimi at temporal-wave.com
Mon Nov 24 08:01:50 PST 2008
On Mon, 2008-11-24 at 05:59 -0800, Su Zhang wrote:
> thanks,i see that, yet there is not any value in this String, i
> checked my grammar part and did not find any errors or problem? would
> you take a look at my grammar and see whether there is a problem in
> the project?
> the grammar is here:
>
> num returns [String t]
> :n = Num {$t = $n.getText();}|b64=Base64Block{$t=
> $b64.getText();}|ident=Id{$t=$ident.getText();}
This has already been pointed out once, but don't use getText(). You
want to use:
$n.text $b64.text $ident.text
> ;
> base1 returns[String input]
input is already in use as an object name within ANTLR, you need to use
a different name.
Then if your grammar is called myGrammar, you invoke it as:
myGrammar parser = new myGrammar(tokens); // Set up your parser
// Next, your num rule will either return String (as you only have one return element) or the type num_return:
//
// Either...
//
num_return psrReturn = parser.num();
System.out.println("Return was " + psrReturn.t);
// Or if it just returns string:
//
String psrReturn = parser.num();
System.out.println("Return was " + psrReturn);
If you are not getting back a string value, then there is an error in
your grammar actions.
I suggest that you buy a copy of the book or at least scan all the
available example grammars and contributed grammars.
Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081124/82283e5a/attachment.html
More information about the antlr-interest
mailing list