为国际象棋 PGN 文件生成语法时出错 [英] Error when generating a grammar for chess PGN files

查看:33
本文介绍了为国际象棋 PGN 文件生成语法时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写这个 ANTLR4 语法是为了在我的 Java 程序中解析 PGN,但我无法解决其中的歧义:

I made this ANTLR4 grammar in order to parse a PGN inside my Java programm, but I can't manage to solve the ambiguity in it :

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

我认为错误来自于["、]"和""不能自由使用的事实,无论是在语法还是词法分析器中.

I think the error come from the fact that " [ ", " ] " and ' " ' can't be used freely, neither in Grammar nor Lexer.

欢迎提供帮助或建议.

推荐答案

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

Looking at the specs for PGN, http://www.thechessdrum.net/PGN_Reference.txt, I see there's a formal definition of the PGN format there:

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 解析器

I highly recommend you to let your ANTLR grammar resemble that as much as possible. I made a small project with ANTLR 4 on Github which you can try out: 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

For a version with comments, see: https://github.com/bkiers/PGN-parser/blob/master/src/main/antlr4/nl/bigo/pp/PGN.g4

这篇关于为国际象棋 PGN 文件生成语法时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆