[antlr-interest] Repeatedly parsing strings without constructing new parser instances?

Benjamin Han benjamin.han at gmail.com
Sun Apr 20 11:50:54 PDT 2008


Hi,

I just started using ANTLR (target language is JAVA) and I can't seem
to figure out how to parse different strings without constructing a
new parser instance every time.

The scenario is I have many different formulae to parse, at separate
times. I'm following this code:

private MyParser createParser(String formula) throws IOException {
    	CharStream stream = new ANTLRStringStream(formula);
    	MyLexer lexer = new MyLexer(stream);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        MyParser parser = new MyParser(tokens);
        return parser;
    }

But I don't want to create a new instance of MyParser each time I want
to parse a new formula. Can I just create a lexer/parser without
giving any input first, and then give the input to lexer later to
parse?

How is it usually done?

Thanks!


Ben


More information about the antlr-interest mailing list