PGN chess file reader in Xojo

Yes, I’ve seen lots of them, and I can create reasonable RegEx expressions. The issue is more reading ones created by other people. They seem completely uninterpretable. Thus the joke about it being a “write-only” language.

That’s why I like to use white space liberally and add comments to the long ones, as I did above. Otherwise (sometimes even with that), it looks like a wall of gibberish.

Indeed :slight_smile:

I can’t help with a Xojo parser, but I have written a PGN parser in Perl.

The tricky thing about a PGN parser is that it has to understand the rules of chess to be able to disambiguate moves.

A move like “Nf6” (move the knight to the f6 square) might be ambiguous because it doesn’t say which knight to move - only where to move it. PGN has provision for resolving amniguities like this by specifying the file, rank (or both) of the piece to move. Example: “Ndf6” (move the knight on the d-file to f6). But if the move is not ambiguous under the rules, then PGN usually does not specify the peice to move explicitly. The parser has to work out which knight can legally move to f6.

And it’s not as simple as knowing how the pieces move. Suppose there are two knights which are both “a knight’s move” away from the f6 square, but moving one of them would leave your king in check. In that case the PGN will usually not specify which knight to move, because it’s clear from the rules that one of them can’t move. So the parser has to understand when the king is being attacked to know which knight can legally move and thus disambiguate “Nf6”.

PGN is regarded as a very bad format for this reason and there have been various proposals to replace it, but it seems we’re stuck with it for a while yet.

3 Likes

Indeed, it turned out to be much more complex than expected and I am really close to give up…
Maybe I should try to create a plugin using one of the many open source c++ codes around, but I suspect it will be a pain anyway.