1

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();
Mark Fila
  • 25
  • 4
  • I would guess the antl jars aren't on your classpath. – Lee Meador Mar 06 '13 at 21:01
  • I haven't used this version of Antlr but according to the new api reference none of those classes are part of antlr 4. http://antlr4.org/api/Java/index.html what you reference is code that should work for Antlr 3. – Daniel Williams Mar 06 '13 at 21:01
  • Maybe your tool is designed for a different version of Antlr. The docs should tell. – Lee Meador Mar 06 '13 at 21:02

1 Answers1

1

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

Community
  • 1
  • 1
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288