[antlr-interest] FEN grammar
bkiers at gmail.com
Sun Aug 28 14:36:50 PDT 2011
After skimming through the Wiki page you mentioned, I realize what you mean
by "overlap". Then no, making fragments will not help you since you will
only know at parse-time if a digit should be a part of a `number` or a
`rank` or a `move`. Fragment rules are only for other fragment rules: the
parser has no notion of them.
I quickly hacked a FEN grammar together. I'm sure I goofed up some things,
but perhaps it's a starting point:
: placement Space
: place+ (FSlash place+)+
| K_uc? Q_uc? K_lc? Q_lc? // Must macth at least one though! You can do
that by using a semantic predicate.
: file Rank
rook : R_lc | R_uc;
knight : N_lc | N_uc;
bishop : B_lc | B_uc;
queen : Q_lc | Q_uc;
king : K_lc | K_uc;
pawn : P_lc | P_uc;
FSlash : '/';
Space : ' ';
Zero : '0';
Rank : '1'..'8';
Nine : '9';
Hyphen : '-';
W_lc : 'w';
R_lc : 'r';
N_lc : 'n';
A_lc : 'a';
B_lc : 'b';
C_H_lc : 'c'..'h';
Q_lc : 'q';
K_lc : 'k';
P_lc : 'p';
R_uc : 'R';
N_uc : 'N';
B_uc : 'B';
Q_uc : 'Q';
K_uc : 'K';
P_uc : 'P';
On Sun, Aug 28, 2011 at 10:26 PM, Jonne Zutt <jonne.zutt.ml at gmail.com>wrote:
> Hi all,
> I made my first attempts to use antlr today.
> Although I read some tutorials, example programs and a page about
> common pitfalls, I stepped
> into several pitfalls myself as well, I guess.
> Is there anybody who wants to shed some light on the below grammar to
> parse chess FEN strings
> (see http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation).
> I am debugging with the string:
> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
> without the quotes (this is the initial position for chess).
> I have several problems:
> - I was using more tokens, but several are overlapping (e.g., for the
> enPassant rule I used to have FILE RANK
> where RANK was a lexer token '1'..'8', but that overlaps with the
> NUMBER token and also with pieces).
> I'm not sure how to deal with tokens that have overlap? Should they
> always be changed into fragments?
> I wanted to make tokens for each piece as well. Such as KNIGHT : 'n'
> | 'N'; But the bishop turns out to be
> quite overloaded as well (with BLACK and FILE).
> - For some reason, 0 seems to match my NUMBER, but 1 does not match.
> This is what the debugger shows
> me. If I switch 0 1 into 1 0, the halfmoveClock is not matching.
> - If I press ctrl-Y in the AntlrWorks plugin, I loose all my data!!
> arghh. In IntelliJ that is my shortcut to delete
> a line.
> Below is my grammer. Any help / comments would be nice :)
> grammar Fen;
> : fen EOF;
> : piecePlacement WS activeColor WS castling WS enPassant WS
> halfmoveClock WS fullmoveNumber;
> : pieces SEP pieces SEP pieces SEP pieces SEP pieces SEP
> pieces SEP
> pieces SEP pieces;
> : ('p'|'P' | 'n'|'N' | 'b'|'B' | 'r'|'R' | 'q'|'Q' | 'k'|'K' |
> : 'w' | 'b';
> : NONE
> | ('K' | 'Q' | 'k' | 'q')+;
> : NONE
> | FILE '1'..'8';
> : NUMBER;
> : NUMBER;
> // LEXER
> WS : (' ' | '\t')+;
> SEP : '/';
> NONE : '-';
> FILE : 'a'..'h';
> NUMBER : '0' | ('1'..'9' ('0'..'9')*);
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
More information about the antlr-interest