Skip to content

This repository aims to generate an analyzer to read chess files in PGN format. It supports one or more moves, ending with a score, such as "1. e4 e5 ... 0-1", but also long formats: "1. d2d4 Ng8f6", the classic castling "OO oo" but also "e1g1 e8g8", supports parsing of headers and comments. PGN is the de-facto standard for chess games, especial…

License

Notifications You must be signed in to change notification settings

emezac/peg_parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This repository aims to generate an analyzer to read chess files in PGN format. It supports one or more moves, ending with a score, such as "1. e4 e5 ... 0-1", but also long formats: "1. d2d4 Ng8f6", the classic castling "OO oo" but also "e1g1 e8g8" , supports parsing of headers and comments.

PGN is the de-facto standard for chess games, especially when it comes to interoperability. Unfortunately, PGN is somewhat misdesigned. Apparently, it’s not just me who thinks so. PGN is designed to make it easy for humans to read PGN files, and edit or write them manually with a text editor. At a cost, namely that it’s difficult to parse it with computers.

The odd thing here is that PGN files are rarely created manually, almost everyone uses a chess program to enter or edit moves, and then save the game afterwards. That’s the basic mis-design.

The main difficulty in parsing PGN files – aside from a lot of ambiguities – is SAN notation, i.e. the source-field of a move is missing. For a human it’s easy to spot the source-field, but for a computer it means that the program has to know the chess-rules to figure out which move is executed .

This parser was generated by using canopy a PEG (Parsing Expression Grammars) parser compiler.

Why did I decide to use a PEG parser?

Because it has several advantages:

  1. I can generate the parser for Python, Ruby, Java and JS
  2. I have to developed just 50% more of the parser
  3. Surely my knowledge of PEG Parsers will should important for my next homework: a pattern language for chess.

Status:

Python Parser : 100% Ruby Parser : 95% Java Parser : 50% Js Parser : 50%

Usage example (Ruby):

test.rb

$LOAD_PATH << '.'

require 'parser' require_relative 'pgn'

include PGN

options = { actions: Actions.new, types: nil } file_data = File.open("kasparov.pgn", :encoding => 'ISO-8859-1').read game = PGN::parse(file_data, options ) #game = PGN::parse("1. c2c4 { Comments by/Kommentare von: GM Gerald Hertneck } 1... c7c6 2. d2d4 d7d5", options ) puts game.move(1) puts game.move(1).black.san puts game.movetext puts game.tag_pairs puts game.score

Execution:

➜ ruby git:(master) ✗ ruby test.rb

  1. c4 c6

  2. c4 c6

  3. d4 d5

  4. Nf3 Nf6

  5. Qc2 dxc4

  6. Qxc4 Bf5

  7. Nc3 Nbd7

  8. g3 e6

  9. Bg2 Be7

  10. O-O O-O

  11. e3 Ne4

  12. Qe2 Qb6

  13. Rd1 Rad8

  14. Ne1 Ndf6

  15. Nxe4 Nxe4

  16. f3 Nd6

  17. a4 Qb3

  18. e4 Bg6

  19. Rd3 Qb4

  20. b3 Nc8

  21. Nc2 Qb6

  22. Bf4 c5

  23. Be3 cxd4

  24. Nxd4 Bc5

  25. Rad1 e5

  26. Nc2 Rxd3

  27. Qxd3 Ne7

  28. b4 Bxe3

  29. Qxe3 Rd8

  30. Rxd8 Qxd8

  31. Bf1 b6

  32. Qc3 f6

  33. Bc4 Bf7

  34. Ne3 Qd4

  35. Bxf7 Kxf7

  36. Qb3 Kf8

  37. Kg2 Qd2

  38. Kh3 Qe2

  39. Ng2 h5

  40. Qe3 Qc4

  41. Qd2 Qe6

  42. g4 hxg4

  43. fxg4 Qc4

  44. Qe1 Qb3

  45. Ne3 Qd3

  46. Kg3 Qxe4

  47. Qd2 Qf4

  48. Kg2 Qd4

  49. Qxd4 exd4

  50. Nc4 Nc6

  51. b5 Ne5

  52. Nd6 d3

  53. Kf2 Nxg4

  54. Ke1 Nxh2

  55. Kd2 Nf3

  56. Kxd3 Ke7

  57. Nf5 Kf7

  58. Ke4 Nd2

  59. Kd5 g5

  60. Nd6 Kg6

  61. Kd4 Nb3 Event London PCA/Intel-GP Site London ENG Date 1994.08.31 EventDate ? Round 1 Result 0-1 White Garry Kasparov Black Chess Genius (Computer) ECO D11 WhiteElo ? BlackElo ? PlyCount 120 0-1

Any help is wellcome!

Canopy

Canopy is a parser compiler targeting Java, JavaScript, Python and Ruby. It takes a file describing a parsing expression grammar and compiles it into a parser module in the target language. The generated parsers have no runtime dependency on Canopy itself.

For usage documentation see canopy.jcoglan.com.

https://blog.jcoglan.com/2015/07/19/canopy-produces-portable-peg-parsers/ https://github.com/jcoglan/canopy

About

This repository aims to generate an analyzer to read chess files in PGN format. It supports one or more moves, ending with a score, such as "1. e4 e5 ... 0-1", but also long formats: "1. d2d4 Ng8f6", the classic castling "OO oo" but also "e1g1 e8g8", supports parsing of headers and comments. PGN is the de-facto standard for chess games, especial…

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published