[antlr-interest] ab? b?a

fat bold cyclop fat.bold.cyclop at gmail.com
Fri Jun 27 03:22:29 PDT 2008


I am working on grammar for Shogi (Japanese chess) game record. I am
not very experienced but I do my best ;-)

First of all, there are many points in the game record where
whitespace (WS) could occur. Because I wanted keep the grammar
human readable I chose to ignore WSs.

Secondly, I want the parser to distinguish between:
"ab a", "a ba" and "ab ba"

((In "real life" the game record will hava information about piece
rank (b). In Shogi a piece can be promoted
which is indicated by prefixing it's name with plus sign (a). So a
pawn's symbol is "P" and promoted pawn is "+P".
An unpromoted piece (under certain circumstances) after it's move can
pe promoted. Which is,
unfortunately for me, also indicated by '+'. A move in which a pawn
gets promoted is postfixed with '+'.
The game record can contain entry: "P4c+ +P1a". This means: first pawn
moves to 4c square and gets promoted.
The second pawn, a promoted pawn goes to 4c.))

So I have the following part in my grammar (simplified):
ab? b?a

Ignoring WS introduces nondeterminism. The parser can't distinguish
"ab a" from "a ba"
I know I could switch off ignoring WS but it forces me to put WS in
many places of my grammar,
making less readable.

Can I use any predicate to hint the parser the right choice?
For example for my ab? b?a: "if you find b and the next symbol is a
then you found ba ", or
"if you find b and the previous token was WS then it cannot belong to ab".

Or maybe there is a way of grouping/rearranging the grammar?

Any help is welcomed.

Best regards,
fbc



grammar problem;
tokens {DOT = '.'; PAWN_SYMBOL = 'P'; PROMOTION_SIGN = '+';}

// PARSER RULES ---------------------------------
move 	 : DOT moveData moveData;	
moveData : pieceRank PROMOTION_SIGN?;
pieceRank  : PROMOTION_SIGN? PAWN_SYMBOL;

// LEXER RULES ---------------------------------
WHITESPACE  : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+  { $channel = HIDDEN; } ;


More information about the antlr-interest mailing list