Input mismatch error with java antlr grammar | بلاگ

Input mismatch error with java antlr grammar

تعرفه تبلیغات در سایت

آخرین مطالب

امکانات وب

Vote count: 0

I'm trying to use the antlr grammar for Java available on Antlr Grammars. I was using antlr 4.7.1 but I got the error saying that the antlr version used was different from runtime version which was 4.5.3 so I switched to 4.5.3. But then I started getting this error when I run my program:

line 1:0 mismatched input 'src' expecting {, 'abstract', 'class', 'enum', 'final', 'import', 'interface', 'package', 'private', 'protected', 'public', 'static', 'strictfp', ';', '@'}

I've checked and re-checked my code and can't understand what's wrong.

My java code is as follows:

TestListener.java

public class TestListener{
    public static void main(String args[]) throws Exception {
          String initialFile = "src/test/resources/ComplexTest.java";
          CharStream in = new ANTLRInputStream(initialFile);
          if (in == null){
               System.err.println("Unable to find test file.");
            }
          Java8Lexer lex = new Java8Lexer(in);
          CommonTokenStream tokens = new CommonTokenStream(lex); 
          Java8Parser parser = new Java8Parser(tokens);
          ParseTree tree = null;
          tree = parser.compilationUnit();
          ParseTreeWalker walker = new ParseTreeWalker();
          MyListener listener = new MyListener();
          walker.walk(listener, tree);
        }
    }

MyListener.java

    public class MyListener extends Java8BaseListener {
    @Override public void enterClassDeclaration(Java8Parser.ClassDeclarationContext ctx) { 
            String className = ctx.getClass().getName();
            System.out.println("Entered class " + className);
    }
    }

Any help on how to solve this issue will be appreciated.

asked 1 min ago
Zack Gattinger

...
نویسنده : استخدام کار بازدید : 0 تاريخ : پنجشنبه 22 شهريور 1397 ساعت: 10:21