0

I'm new to ANTLR, having my first experiments in version 4. Since I use Eclipse Mars, I decided to install the Eclipse ANTLR 4 plugin. After all done, I created the following grammar (MetaCoder.g4 file):

grammar MetaCoder;

init: '{' value (',' value)* '}' ;

value: init
     | INT
     ;

INT: [0-9]+ ;
WS: [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

The plugin generated to following java code (I removed most the comments to make it shorter):

// Generated from MetaCoder.g4 by ANTLR 4.4

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.TerminalNode;

public class MetaCoderBaseListener implements MetaCoderListener {
    @Override public void enterInit(@NotNull MetaCoderParser.InitContext ctx) { }
    @Override public void exitInit(@NotNull MetaCoderParser.InitContext ctx) { }
    @Override public void enterValue(@NotNull MetaCoderParser.ValueContext ctx) { }
    @Override public void exitValue(@NotNull MetaCoderParser.ValueContext ctx) { }

    @Override public void enterEveryRule(@NotNull ParserRuleContext ctx) { }
    @Override public void exitEveryRule(@NotNull ParserRuleContext ctx) { }
    @Override public void visitTerminal(@NotNull TerminalNode node) { }
    @Override public void visitErrorNode(@NotNull ErrorNode node) { }
}

For all the overriden methods Eclipse shows the following error:

The method xxx of type MetaCoderBaseListener must override a superclass method

The message seems to be correct, since this class does not have an ancestor.

What went wrong and how to fix it?

AlexSC
  • 1,823
  • 3
  • 28
  • 54

2 Answers2

0

Just after posting the question, StackOverflow sugested some related questions and this one has the answer:

Trouble Setting Up ANTLR 4 IDE on Eclipse Luna (4.4)

The problem, in my case, was that my project was not converted to Facets.

Once I did that the errors disappeared.

Community
  • 1
  • 1
AlexSC
  • 1,823
  • 3
  • 28
  • 54
0

It sounds like ANTLR did not create the MetaCoderListener class correctly, defining these methods. When you generate the parser with ANTLR, try to generate it with -listener as an argument. This will generate the proper listener, which you class will then implement (The same goes for visitors. Use the -visitor argument to generate visitors).

Chraebe
  • 429
  • 3
  • 12