Request: PGN reader implementation

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

kinderchocolate
Posts: 454
Joined: Mon Nov 01, 2010 6:55 am
Full name: Ted Wong

Request: PGN reader implementation

Post by kinderchocolate »

Anybody knows whether there is an implementation of PGN game reader on the Internet preferably in C++ that I can use or reference? I am looking for code that reads a game from a pgn file.
UncombedCoconut
Posts: 319
Joined: Fri Dec 18, 2009 11:40 am
Location: Naperville, IL

Re: Request: PGN reader implementation

Post by UncombedCoconut »

pgn-extract comes to mind. There are many larger open-source programs which read PGN for one reason or another: for instance, Xboard, cutechess-cli, Crafty.

Google also suggests pgnlib-cpp. I don't know anything about it.
jdart
Posts: 4367
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Request: PGN reader implementation

Post by jdart »

I have one:

http://www.arasanchess.org

but it doesn't handle variations (only comments).
Rémi Coulom
Posts: 438
Joined: Mon Apr 24, 2006 8:06 pm

Re: Request: PGN reader implementation

Post by Rémi Coulom »

Hi,

I have just updated the source code of my GPL chess library. You can find it on my web page at http://remi.coulom.free.fr/. I only tested it on my Mac, but it should work with any linux or windows compiler too. cd to chesslib/compgcc and make. You may have to install ccache in order to compile. Documentation is minimal, but I'll augment it if anybody asks questions.

The PGN code was written and tested very carefully, and should strongly conform with the PGN specs. It supports variations, comments, NAGs, everything. It can parse this:

Code: Select all

[Event "This is an event with a \""]
[Site "This site has \\\\ xxx"]
[Date "1996.08.15"][Round "-"][White "White"][Black "Black"]
[Result "1/2-1/2"][SetUp "1"];I love comments
[FEN "1nbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/1NBQKBNR w KQkq - "]e4(d4;comment
d5!!2.c4$50(Nf3?))e5 Nf3{Comment !}Nc6 Nc3 Nf6 Bc4 Bc5 O-O O-O
% This is a line with an escape character !
@@@æææ {unexpected characters are skipped}
<> &#123;These symbols are reserved&#125;
1/2-1/2
Rémi
User avatar
hgm
Posts: 27811
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Request: PGN reader implementation

Post by hgm »

A question for the experts. (I was just rewriting the WinBoard move parser to get rid of flex dependence and the limitations it offers.)

Can the value string of a PGN tag contain newlines, like

Code: Select all

&#91;Event "This is an event with a very long name,
           so we have written it on two lines"&#93;
The old WinBoard parser did reconize that, but it has very bad consequences if a quote is missing. It seems preferable the quote should be balanced on the same line, or at least before the closing ']'.
Rémi Coulom
Posts: 438
Joined: Mon Apr 24, 2006 8:06 pm

Re: Request: PGN reader implementation

Post by Rémi Coulom »

hgm wrote:A question for the experts. (I was just rewriting the WinBoard move parser to get rid of flex dependence and the limitations it offers.)

Can the value string of a PGN tag contain newlines, like

Code: Select all

&#91;Event "This is an event with a very long name,
           so we have written it on two lines"&#93;
The old WinBoard parser did reconize that, but it has very bad consequences if a quote is missing. It seems preferable the quote should be balanced on the same line, or at least before the closing ']'.
the specs are there: http://www.chessclub.com/help/PGN-spec

In this document, you can read:
Non-printing characters like newline and tab are not permitted inside of strings.
Rémi
User avatar
hgm
Posts: 27811
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Request: PGN reader implementation

Post by hgm »

OK, thanks. One thing was not immediately clear to me, though:

Do these rules for tokens also apply within a {}-delimited comment, or is the comment a token by itself? E.g. Can I have unbalanced quotes within {}?

Code: Select all

&#123; My opponent was 6'4" tall &#125; 21... Rg8+ &#123; but his nose grew 2" &#125;
O-O &#123; My opponent said&#58; "&#123;...&#125; Kf2 would be a good move &#123;...&#125;" &#125;
Would the moves Rg8+ and Kf2 be part of the game?

I gather this is legal:

Code: Select all

&#91;Event "Tournament for &#91;elderly&#93; people"&#93;
Rémi Coulom
Posts: 438
Joined: Mon Apr 24, 2006 8:06 pm

Re: Request: PGN reader implementation

Post by Rémi Coulom »

hgm wrote:OK, thanks. One thing was not immediately clear to me, though:

Dothese rues for tokens also apply within a {}-delimited comment,or is the comment a token by itself? E.g. Can I have unbalanced quotes within {}?

Code: Select all

&#123; My opponent was 6'4" tall &#125; 21... Rg8+ &#123; but his nose grew 2" &#125;
O-O &#123; My opponent said&#58; "&#123;...&#125; Kf2 would be a good move &#123;...&#125;" &#125;
Would the moves Rg8+ and Kf2 be part of the game?
The second kind starts with a left brace character and continues to the next right brace character.
My understanding is that there is no token inside a comment. But your PGN is not correct because comments don't nest:
Brace comments do not nest; a left brace character appearing in a brace comment loses its special meaning and is ignored. A semicolon appearing inside of a brace comment loses its special meaning and is ignored. Braces appearing inside of a semicolon comments lose their special meaning and are ignored.
Rémi
User avatar
hgm
Posts: 27811
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Request: PGN reader implementation

Post by hgm »

Right, the second line would not be correct if there are no tokens inside a comment. But the question was not if the comments nested, but if the quotes could protect the closing brace. I guess your interpretation is the correct one; the clue is that the text you quoted talk about { and }_characters_, not _tokens_. In the latter case it could be argued that the } in "...}..." is not a token, but a part of the string token. But it obvously is always a character.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Request: PGN reader implementation

Post by Don »

kinderchocolate wrote:Anybody knows whether there is an implementation of PGN game reader on the Internet preferably in C++ that I can use or reference? I am looking for code that reads a game from a pgn file.
The Java tester I will be soon releasing does have legal move generation logic and could easily be fixed up to parse PGN and even test it for legality.

When you say, "parse" what is it that you want to produce? EPD or FEN records or something else?

Don