Parsing Chess PGN files with ANTLR4 (in Java) : error on my own grammar.

115 views
Skip to first unread message

laurent bernabe

unread,
Dec 24, 2013, 7:55:56 AM12/24/13
to antlr-di...@googlegroups.com
Hello everyone,

I wrote a simple grammar in order to parse PGN files (file is attached to this message).
But I got the following error :
_________________________________________________
error(50): /home/my_path/Pgn.g4:6:6: syntax error: 'TAG_TYPE "TAG_VALUE"' came as a complete surprise to me while matching alternative
_______________________________________________

I guess it is because I am using [, ] and " in a grammar definition : so how can I solve the problem (I've tried to backslash thoses, but I got an exception when generating the grammar with antlr4 command).

Thanks in advance for helps and suggestions.

Regards
Pgn.g4

laurent bernabe

unread,
Dec 24, 2013, 8:45:11 AM12/24/13
to antlr-di...@googlegroups.com
I've tried to solve the problem, putting [ in string litterals :

-------------------------------------------------------------------------------------------
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];
_________________________________________________

But this time a get a stacktrace error :

____________________________________________________

$ 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)_______________________________________________________

Regards

laurent bernabe

unread,
Dec 24, 2013, 9:31:49 AM12/24/13
to antlr-di...@googlegroups.com
Got an answer an stackoveflow :)

Reply all
Reply to author
Forward
0 new messages