2

I just started with antlr, And I am using 4.2. Easy guessing says it would be like antlr3 in basics. so I followed the accepted answer of this question. (But instead of Exp, I replaced Java, which means I want to parse Java) Everything is fine, Until I want to compile the ANTLRDemo.java example.

When I compile that, I get 4 errors:

    ANTLRStringStream in = new ANTLRStringStream("some random text");
    JavaLexer lexer = new JavaLexer(in);
  • first error: constructor JavaLexer in class JavaLexer cannot be applied to given types; JavaLexer lexer = new JavaLexer(in); required: CharStream found: ANTLRStringStream reason: actual argument ANTLRStringStream cannot be converted to CharStream by method invocation conversion (I know what this error is ;-)

    CommonTokenStream tokens = new CommonTokenStream( lexer);
    JavaParser parser = new JavaParser(tokens);
    System.out.println(parser.eval());
    

to make it short, let's say every line has its own similar error. For example, "parser" does not have an "eval()" method.

What am I missing? I guess antlr4 does not run like 3. Any Ideas? Please consider my beginner status.

Community
  • 1
  • 1
Makan
  • 2,508
  • 4
  • 24
  • 39

1 Answers1

4

In ANTLR 4, use ANTLRInputStream instead of the old ANTLRStringStream from ANTLR 3.

The eval() method exists when you have a parser rule in the grammar named eval. One such method is created for each rule in the grammar. If you do not intend to start parsing at rule eval, then you should replace that call with the name of the start rule for your particular grammar.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • thanks eval() problem solved. But the 'lexer' constructor still expects CharStream, and I don't have one. and the Same goes on: 'tokens' constructor expects TokenSource, and JavaParser constructor takes a TokenStream as argument. – Makan Aug 20 '14 at 04:46
  • I made a mistake of importing org.antlr.runtime.*, and my IDE used it. so It is solved :) thanks again. – Makan Aug 20 '14 at 07:27