0

I'm implementing a simple program walker grammar and I get this common error in multiple lines. I think it is caused by same reason, but I'm new to antlr so I couldn't figure it out.

For example, in this following code snippet:

program
  : (declaration)*
    (statement)*
    EOF!
  ;

I got error:

No viable alternative at input '!'

after EOF, and I got a similar error with:

declaration
  : INT VARNUM '=' expression ';'
  -> ^(DECL VARNUM expression)
  ;

I got the error:

No viable alternative at input '->'

After reading other questions, I know that matching one token with multiple definitions can cause this problem. But I haven't test it with any input yet, I got this error in intelliJ. How can I fix my problem?

RandomEli
  • 1,527
  • 5
  • 30
  • 53

1 Answers1

1

This is ANTLR v3 syntax, you're trying to compile it with ANTLR v4, which won't work.

Either downgrade to ANTLR v3, or use v4 syntax. The difference comes from the fact that v4 doesn't support automatic AST generation, and you're trying to use AST construction operators, which were removed.

The first snippet only requires you to remove the !. Parentheses aren't necessary.

program
  : declaration*
    statement*
    EOF
  ;

As for the second one, remove everything after the ->:

declaration
  : INT VARNUM '=' expression ';'
  ;

If you need to build an AST with v4, see my answer here.

Community
  • 1
  • 1
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158