From 63f177ad62c86e5c45aa9bab0e59fae2268e2429 Mon Sep 17 00:00:00 2001 From: Andrés Ambrois Date: Wed, 04 Nov 2009 00:19:00 +0000 Subject: sgf.g: A small YAPPS (http://theory.stanford.edu/~amitp/yapps/) grammar for the Smart Game Format sgfparser.py: Parser generated by YAPPS from sgf.g example.sgf: An example SGF file with variations for testing. --- (limited to 'sgf.g') diff --git a/sgf.g b/sgf.g new file mode 100644 index 0000000..3629f56 --- /dev/null +++ b/sgf.g @@ -0,0 +1,40 @@ +parser SGF: + ignore: '\\s+' + + + ### Go-specific tokens: http://www.red-bean.com/sgf/go.html ### + token Point: '[a-zA-Z]{2}' + token Move: '[a-zA-Z]{2}' + token Stone: '[a-zA-Z]{2}' + + ### SGF Tokens as specified in http://www.red-bean.com/sgf/sgf4.html ### + + token PropID: '([a-z]*[A-Z]){1,2}[a-z]*' + token Number: '[+-]?[0-9]+' + token Real: '[+-]?[0-9]+(\\.[0-9]+)?' + token Color: '(B|W)' + token Text: '[^\\]]*' # In YAPPS, the longest matches take precedence, + # if they're both the same length, then the + # first one listed in the grammar is used. + + + rule GameTree: "\\(" {{ res = [] }} + (Node {{ res.append(Node) }})+ + (GameTree {{ res.append(GameTree) }})* + "\\)" {{ return res }} + + rule Node: ";" {{ res = [] }} + (Property {{ res.append(Property) }} + )+ {{ return dict(res) }} + + rule Property: PropID {{ res = (PropID, []) }} + ("\\[" ValueType "\\]" {{ res[1].append(ValueType) }} + )+ {{ return res }} + + rule ValueType: Number {{ return int(Number) }} + | Real {{ return float(Real) }} + | Color {{ return Color }} + | Move {{ return (Move[0].islower() and ord(Move[0])-96 or ord(Move[0])-64, Move[1].islower() and ord(Move[1])-96 or ord(Move[1])-64) }} + | Text {{ return Text }} + | '' {{ return None }} + -- cgit v0.9.1