Docs / PGN and FEN
PGN - supported syntax and limits
The same parser (identical ANTLR grammar and pre-processing on the server and in the browser) is used to upload games, import repertoires, build custom puzzle suites and import the resources of a course. A file can contain several games.
Structure of a game
- Tags:
[Name "value"]. A value cannot contain a double quote"(this is a syntax error). Tags are optional. - Moves, with or without move numbers (
1.,1...). Line breaks, tabs and Windows line endings are accepted. - Result:
1-0,0-1,1/2-1/2or*, written without spaces (1 - 0is an error). The result stored for the game is the one in the[Result]tag, or*if there is none: the termination token at the end of the move text does not replace it.
Accepted move notations
- English SAN:
Nf3,exd5,Qxe7+, disambiguation (Nbd2,R1e2), castlingO-OandO-O-O. Castling written with zeros (0-0,0-0-0) is read as castling. - Promotion with or without
=:e8=Q,e8Q,exd8Q. - UCI:
e2e4,e7e8q. Long algebraic for pawn captures with the origin square (c5xd4). - Suffixes glued to the move:
+,#,!,?,!!,??,!?,?!. The!and?suffixes are ignored: they do not become NAGs.
Tolerated input
Before parsing, the text is normalized (line numbers in error messages stay exact):
- a BOM at the start of the file is removed;
- escape lines starting with
%are ignored; - end-of-line comments
; ...are converted to{ ... }comments. A;inside a tag value or inside a{ }comment stays text, and a%in the middle of a line is not an escape.
Rejected notations (syntax error with a line number)
- Localized piece letters (for example the French
Cf3,Dd1,Fc4,Tae1) and lowercase piece letters (nf3). Use K, Q, R, B, N. - The
e.p.suffix, a result with spaces (1 - 0), and a!or?separated from the move by a space. - Text after the result (
1-0 2. Nf3) silently creates a second game. - An empty file or text that is not PGN.
Annotations
- NAG
$n: one per move (with two NAGs, the last one wins). - Comments
{ ... }before or after a move: parentheses and quotes are plain text inside.[%eval ...],[%clk ...],[%cal ...]and[%csl ...]stay raw text in the comment. - LilyChess comment commands, ignored by other chess programs:
[%title "…"]and[%freq n](repertoires): title and frequency of a repertoire line. On a repertoire import they are read, removed from the comment text and stored; the repertoire export writes them back. See Repertoire line names and order.[%lang <code> "…"](course diagrams): translated text of a move comment in a multilingual course. See Course diagrams and Multilingual courses.
- Pitfall: a comment that contains a move number followed by a move that is legal in the position (
{Stronger than 1. d4}) creates a variation and removes that fragment from the text. Write “Stronger than d4” without a move number. - Variations
( ... ), nested as deeply as needed, placed after the move they replace. A variation placed before the first move is silently dropped.
Special tags
[FEN "..."]: starting position;[SetUp]is not read and not needed. An unreadable FEN (fewer than 4 fields, unknown characters) gives anINVALID_FENerror with the line number and that game is skipped (other games in the file are still read). A readable but impossible FEN (no king, nine ranks) gives an “illegal move” error on the first move. Validate the FEN before writing it (FEN formats).[Variant "..."]:Standard(or empty) andChess960(aliasesfischerandom,960,frc, case-insensitive; “Chess 960” with a space is not an alias). Other variants (Atomic, Crazyhouse…) are read without error by the parser but refused when uploading.[Event]is used as the title (?or empty means no title). Other tags kept:White,Black,Date,Round,Site,WhiteElo,BlackElo,TimeControl,Termination,ECO,Opening.
Chess960
With [Variant "Chess960"] and the starting [FEN], castling is handled (O-O, O-O-O, and the zero forms) and the legality of moves is still checked. A PGN exported from a Chess960 game with castling is written correctly and can be read back.
Errors
- Types:
SYNTAX,ILLEGAL_MOVEandINVALID_FEN, with the line number, for exampleILLEGAL_MOVE ligne 12: ...(the message text may be in French). - Parsing continues after an error, which can cause cascading errors: after an illegal move the following moves are replayed without it and are often rejected too. A syntax error can also create a “phantom” game (the rest of the text is read as a new game). Fix the first error, then run again.
- Inside a variation, an illegal move abandons the variation.
- Only the repertoire import reports these errors to the user; game upload and puzzle suites ignore them, so a malformed game can be skipped without a message.
How to produce a reliable PGN
- English SAN, an explicit
[Result]tag, and[FEN]only if the game does not start from the initial position (and validated). - Prefer
{ }comments to;, no quote inside a tag value, no text after the result. - Comments in
{ }without a move number followed by a legal move. - Replay the moves in an analysis board rather than assuming they are legal.
See also how PGN is used in LilyChess.