Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

ANTL4 Grammar compile Nullpointer Exception

In gereral I am trying to create a Java based application, where I can compile a dictionary of terms which supports simple regular expressions. The dictionary will then be used to create a simple entity tagger, where a recognised term is marked in a text. Thought that ANTLR might provide all I need. Trying to create a Java application which does not depend on the compiled grammar and lexer files, since the grammar has to be updated in runtime every few minutes.

Here my simple "Hello World" application:

LexerGrammar lg = new LexerGrammar(
                "lexer grammar L;\n" +
                "A : ('a'|'A');\n" +
                "B : ('b'|'B');\n" +
                "C : ('c'|'C');\n" +
                "D : ('d'|'D');\n" +
                "FILL_TOKEN : (.);\n");

Grammar g = new Grammar(
                "parser grammar T;\n" +
                "t_abc : A FILL_TOKEN? B FILL_TOKEN? C;\n" +
                "t_abcd : A FILL_TOKEN? B FILL_TOKEN? C FILL_TOKEN? D;\n" +

                "rule0   : t_abcd|t_abc;\n" +

                "ws  : '.' -> skip ;\n",
                lg);

LexerInterpreter lexEngine =
                lg.createLexerInterpreter(new ANTLRInputStream("Test A BCD"));
CommonTokenStream tokens = new CommonTokenStream(lexEngine);
ParserInterpreter parser = g.createParserInterpreter(tokens);
Rule rule = g.rules.get("rule0");
ParseTree t = parser.parse(rule.index);

System.out.println(t.getText());

When I try to compile the application, I am getting the following error

Exception in thread "main" java.lang.NullPointerException
    at org.antlr.v4.runtime.atn.ATNSerializer.serialize(ATNSerializer.java:73)
    at org.antlr.v4.runtime.atn.ATNSerializer.getSerialized(ATNSerializer.java:601)
    at org.antlr.v4.runtime.atn.ATNSerializer.getSerializedAsChars(ATNSerializer.java:605)
    at org.antlr.v4.tool.Grammar.createParserInterpreter(Grammar.java:1337)
    at main.OnTheFly.main(OnTheFly.java:98)

When I comment out the "ws : '.' -> skip ;\n", part of the grammar, the program runs, but it complains that Test is not known.

What I am doing wrong or does the default grammar not support the skip parameter? Using Antlr 4.7.2 and Java 1.8.0 (131)

Answer*

Cancel