[antlr-interest] how can i get a return string from the parser

Hendrik Maryns qwizv9b02 at sneakemail.com
Mon Nov 24 05:34:03 PST 2008


Su Zhang schreef:
> Hi all,
>  
> My program needs a string from Parser, but there is not any function
> provided in the public class parser, how can I get the string outside? I
> mean I want to get the string generated from the rules of
> antlr(functions in parser), and I have written  "returns[String s]" to
> indicate that I need the value, but how can i get the String? does
> anybody familiar with this kind of problem?

Use the specific parser that ANTLR generated from your grammar instead
of the generic Parser class.  It will have a public method for each rule
with the return type you specified or with some internally specified one
if you didn’t give one.

E.g. this is how I use my parser:

// Use ANTLR to parse the query string
final CharStream antlrInput = new ANTLRStringStream(queryString);
final Lexer lexer = new fsqLexer(antlrInput);
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final fsqParser parser = new fsqParser(tokens);
final Formula queryFormula = parser.formula();

HTH, H.
-- 
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 257 bytes
Desc: OpenPGP digital signature
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20081124/2796231c/attachment.bin 


More information about the antlr-interest mailing list