为国际象棋PGN文件生成语法时出错

时间:2013-12-24 10:21:31

标签: antlr4 chess

我制作了这个ANTLR4语法,以便在我的Java程序中解析PGN,但我无法解决其中的歧义:

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 
error(50): Pgn.g4:6:6: syntax error: 'TAG_TYPE "TAG_VALUE"' came as a complete surprise to me while matching alternative

我认为错误来自于“[”,“]”和“”不能在Grammar和Lexer中自由使用的事实。

欢迎提供帮助或建议。

1 个答案:

答案 0 :(得分:1)

查看PGN的规范,http://www.thechessdrum.net/PGN_Reference.txt,我看到PGN格式的正式定义:

18: Formal syntax

<PGN-database> ::= <PGN-game> <PGN-database>
                   <empty>

<PGN-game> ::= <tag-section> <movetext-section>

<tag-section> ::= <tag-pair> <tag-section>
                  <empty>

<tag-pair> ::= [ <tag-name> <tag-value> ]

<tag-name> ::= <identifier>

<tag-value> ::= <string>

<movetext-section> ::= <element-sequence> <game-termination>

<element-sequence> ::= <element> <element-sequence>
                       <recursive-variation> <element-sequence>
                       <empty>

<element> ::= <move-number-indication>
              <SAN-move>
              <numeric-annotation-glyph>

<recursive-variation> ::= ( <element-sequence> )

<game-termination> ::= 1-0
                       0-1
                       1/2-1/2
                       *
<empty> ::=

我强烈建议你让你的ANTLR语法尽可能地类似。我在Github上用ANTLR 4做了一个小项目,你可以试试:https://github.com/bkiers/PGN-parser

语法(没有评论):

parse
 : pgn_database EOF
 ;

pgn_database
 : pgn_game*
 ;

pgn_game
 : tag_section movetext_section
 ;

tag_section
 : tag_pair*
 ;

tag_pair
 : LEFT_BRACKET tag_name tag_value RIGHT_BRACKET
 ;

tag_name
 : SYMBOL
 ;

tag_value
 : STRING
 ;

movetext_section
 : element_sequence game_termination
 ;

element_sequence
 : (element | recursive_variation)*
 ;

element
 : move_number_indication
 | san_move
 | NUMERIC_ANNOTATION_GLYPH
 ;

move_number_indication
 : INTEGER PERIOD?
 ;

san_move
 : SYMBOL
 ;

recursive_variation
 : LEFT_PARENTHESIS element_sequence RIGHT_PARENTHESIS
 ;

game_termination
 : WHITE_WINS
 | BLACK_WINS
 | DRAWN_GAME
 | ASTERISK
 ;

WHITE_WINS
 : '1-0'
 ;

BLACK_WINS
 : '0-1'
 ;

DRAWN_GAME
 : '1/2-1/2'
 ;

REST_OF_LINE_COMMENT
 : ';' ~[\r\n]* -> skip
 ;

BRACE_COMMENT
 : '{' ~'}'* '}' -> skip
 ;

ESCAPE
 : {getCharPositionInLine() == 0}? '%' ~[\r\n]* -> skip
 ;

SPACES
 : [ \t\r\n]+ -> skip
 ;

STRING
 : '"' ('\\\\' | '\\"' | ~[\\"])* '"'
 ;

INTEGER
 : [0-9]+
 ;

PERIOD
 : '.'
 ;

ASTERISK
 : '*'
 ;

LEFT_BRACKET
 : '['
 ;

RIGHT_BRACKET
 : ']'
 ;

LEFT_PARENTHESIS
 : '('
 ;

RIGHT_PARENTHESIS
 : ')'
 ;

LEFT_ANGLE_BRACKET
 : '<'
 ;

RIGHT_ANGLE_BRACKET
 : '>'
 ;

NUMERIC_ANNOTATION_GLYPH
 : '$' [0-9]+
 ;

SYMBOL
 : [a-zA-Z0-9] [a-zA-Z0-9_+#=:-]*
 ;

SUFFIX_ANNOTATION
 : [?!] [?!]?
 ;

UNEXPECTED_CHAR
 : .
 ;

对于带有评论的版本,请参阅:https://github.com/bkiers/PGN-parser/blob/master/src/main/antlr4/nl/bigo/pp/PGN.g4