Context free grammar for AN

545 Views Asked by At

I need to write Context free grammar for describing moves in a game of chess using the Algebric Notation. Can anyone help me get started. f.ex. how do I write this for this move: Bb5 Bd7.

2

There are 2 best solutions below

0
On

For your example, $Bb7$ means that the bishop is on square $b7$. From this square, the bishop has nine different possible squares to move to. We could write this in our CFG as $$Bb7 \rightarrow Bc8 \; | \; Ba6 \; | \; Ba8 \; | \; Bc6 \; | \; Bd5 \; | \; Be4 \; | \; Bf3 \; | \; Bg2 \; | \; Bh1$$ Then we would have to do the same for $Bc8, Ba6$, etc. to give all possible moves for the bishop.

0
On

An answer and discussion is contained in this SO article and answer I provided there:

https://stackoverflow.com/questions/4330606/bnf-input-going-to-wrong-nonterminal