I made my own chess PGN grammar for ANTLR4, but I get a java stacktrace error :
Pgn.g4 grammar :
grammar Pgn;
file: game (NEWLINE+ game)*;
game: (tag+ NEWLINE+)? notation;
tag: '['TAG_TYPE "TAG_VALUE"']';
notation: move+ END_RESULT?;
move: MOVE_NUMBER\. MOVE_DESC MOVE_DESC #CompleteMove
| MOVE_NUMBER\. MOVE_DESC #OnlyWhiteMove
| MOVE_NUMBER\.\.\. MOVE_DESC #OnlyBlackMove
;
END_RESULT: '1-0'
| '0-1'
| '1/2-1/2'
;
TAG_TYPE: LETTER+;
TAG_VALUE: .*;
MOVE_NUMBER: DIGIT+;
MOVE_DESC: .*;
NEWLINE: \r? \n;
SPACES: [ \t]+ -> skip;
fragment LETTER: [a-zA-Z];
fragment DIGIT: [0-9];
And this is the error output :
$ antlr4 Pgn.g4
org\antlr\v4\parse\GrammarTreeVisitor.g: node from line 24:11 required (...)+ loop did not match anything at input 'r'
org\antlr\v4\parse\GrammarTreeVisitor.g: node from line 24:15 mismatched tree node: n expecting <UP>
org\antlr\v4\parse\GrammarTreeVisitor.g: node from line 24:11 required (...)+ loop did not match anything at input 'r'
org\antlr\v4\parse\GrammarTreeVisitor.g: node from line 24:15 mismatched tree node: n expecting <UP>
org\antlr\v4\parse\GrammarTreeVisitor.g: node from line 24:11 required (...)+ loop did not match anything at input 'r'
org\antlr\v4\parse\GrammarTreeVisitor.g: node from line 24:15 mismatched tree node: n expecting <UP>
org\antlr\v4\parse\GrammarTreeVisitor.g: node from line 24:11 required (...)+ loop did not match anything at input 'r'
org\antlr\v4\parse\GrammarTreeVisitor.g: node from line 24:15 mismatched tree node: n expecting <UP>
error(20): internal error: Rule LETTER undefined
error(20): internal error: element list has first|last == null
Exception in thread "main" java.lang.NullPointerException
at org.antlr.v4.automata.ParserATNFactory.elemList(ParserATNFactory.java:445)
at org.antlr.v4.automata.ParserATNFactory.alt(ParserATNFactory.java:414)
at org.antlr.v4.parse.ATNBuilder.alternative(ATNBuilder.java:567)
at org.antlr.v4.parse.ATNBuilder.block(ATNBuilder.java:400)
at org.antlr.v4.parse.ATNBuilder.subrule(ATNBuilder.java:1185)
at org.antlr.v4.parse.ATNBuilder.element(ATNBuilder.java:887)
at org.antlr.v4.parse.ATNBuilder.alternative(ATNBuilder.java:550)
at org.antlr.v4.parse.ATNBuilder.ruleBlock(ATNBuilder.java:289)
at org.antlr.v4.automata.ParserATNFactory._createATN(ParserATNFactory.java:148)
at org.antlr.v4.automata.LexerATNFactory.createATN(LexerATNFactory.java:94)
at org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:407)
at org.antlr.v4.Tool.process(Tool.java:376)
at org.antlr.v4.Tool.processGrammarsOnCommandLine(Tool.java:343)
at org.antlr.v4.Tool.main(Tool.java:190)
So, what's the problem with my grammar ?
(Notice that this post can be a step further the one I exposed in this post, where I had a syntax error).
A couple of things are going wrong:
tag: '['TAG_TYPE "TAG_VALUE"']';
Why are there double quotes around TAG_VALUE
? Remove the quotes if you want to match the token TAG_VALUE
: tag: '['TAG_TYPE TAG_VALUE ']';
MOVE_NUMBER\. MOVE_DESC MOVE_DESC
If you want to match the literal .
(DOT) you need to put quotes around it (and not escape it!): MOVE_NUMBER '.' MOVE_DESC MOVE_DESC
NEWLINE: \r? \n;
Again, if you want to match the literals, put quotes around it: NEWLINE: '\r'? '\n';
TAG_VALUE: .*; ... MOVE_DESC: .*;
Those rule will gobble up your entire input stream. You need another way to match a tag value and move description than you do now. Do not use .*
.
Here’s an existing PGN grammar: https://github.com/antlr/grammars-v4/tree/master/pgn