我为ANTLR4制作了自己的国际象棋PGN语法,但是我得到了一个java stacktrace错误:
Pgn.g4语法:
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];
这是错误输出:
$ 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)
那么,我的语法有什么问题?
(请注意,这篇文章可能比我在this post中公开的那篇文章更进一步,我的语法错误。)
答案 0 :(得分:1)
有些事情出错:
tag: '['TAG_TYPE "TAG_VALUE"']';
为什么TAG_VALUE
周围有双引号?如果您想匹配令牌TAG_VALUE
:tag: '['TAG_TYPE TAG_VALUE ']';
MOVE_NUMBER\. MOVE_DESC MOVE_DESC
如果你想匹配文字.
(DOT),你需要在它周围加上引号(而不是逃避它!):MOVE_NUMBER '.' MOVE_DESC MOVE_DESC
NEWLINE: \r? \n;
同样,如果你想匹配文字,请在其周围加上引号:NEWLINE: '\r'? '\n';
TAG_VALUE: .*; ... MOVE_DESC: .*;
这些规则会吞噬整个输入流。您需要另一种方法来匹配标记值并移动描述,而不是现在。请勿使用.*
。
这是现有的PGN语法:https://github.com/antlr/grammars-v4/tree/master/pgn