[antlr-interest] ab? b?a

Gavin Lambert antlr at mirality.co.nz
Fri Jun 27 19:01:48 PDT 2008


At 22:22 27/06/2008, fat bold cyclop wrote:
 >Secondly, I want the parser to distinguish between:
 >"ab a", "a ba" and "ab ba"

You can resolve these in the lexer (by making them distinct 
tokens):

AB: 'ab';
BA: 'ba';
A: 'a';
B: 'b';

(Make sure you list the more specific ones first.)

As long as these are "direct" rules (no loops or optional clauses; 
alts are ok though) then ANTLR should sort out the ambiguity for 
you.  And this way you're guaranteed that if an AB token appears 
then it was an 'a' followed by a 'b' with no whitespace in 
between.

 >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.))

That's a little trickier.  But you should be able to do it with a 
bit of predicate magic:

fragment POSTPROMOTE: '+';

PREPROMOTE
   : ( '+' (' '|'\t'|'\r'|'\n'|EOF) ) => '+' { $type = 
POSTPROMOTE; }
   | '+'
   ;

This should generate a POSTPROMOTE token where a + appears 
followed by whitespace (or end of file) and a PREPROMOTE token in 
all other cases.



More information about the antlr-interest mailing list