1
votes

I'm trying to parse a logical expression using Antlr version 4.0 and generate the tree to evaluate the expression.

I ran the antlr tool and generated the parser and lexer, but when i place the generated files in the project I get the following errors: "The constructor LogicLexer(ANTLRStringStream) is undefined" and "The constructor CommonTokenStream(LogicLexer) is undefined".

the code is below:

LogicLexer lexer = new LogicLexer(new ANTLRStringStream(expression));
LogicParser parser = new LogicParser(new CommonTokenStream(lexer));
CommonTree tree = (CommonTree)parser.parse().getTree();
1
I would guess the antl jars aren't on your classpath. - Lee Meador
I haven't used this version of Antlr but according to the new api reference none of those classes are part of antlr 4. antlr4.org/api/Java/index.html what you reference is code that should work for Antlr 3. - Daniel Williams
Maybe your tool is designed for a different version of Antlr. The docs should tell. - Lee Meador

1 Answers

1
votes

It looks like you're using a v3 grammar with the v4 tool to generate the lexer and parser classes. ANTLR 4 does not support the tree-rewrite operators, as v3 did.

The API also changed (ANTLRStringStream is no longer there), so it should look like this:

LogicLexer lexer = new LogicLexer(new ANTLRInputStream(expression));
LogicParser parser = new LogicParser(new CommonTokenStream(lexer));
ParseTree tree = parser.parse();

For a complete demo how to walk the generated parse tree, see: ANTLR 4 tree inject/rewrite operator