aboutsummaryrefslogtreecommitdiff
path: root/sgfyacc.py
blob: e48e6b952a93cbe97ed9543386c71a49b6039541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python

# --------------------------------------
# sgyacc.py
""" Parser for SGF """
# --------------------------------------

import ply.yacc as yacc

from imago.sgfParser.sgflex import tokens
from imago.sgfParser.astNode import ASTNode, Property

def p_tree(p):
    '''tree : LPAREN node RPAREN
            | LPAREN tree RPAREN'''
    p[0] = p[2]

def p_node_sequence(p):
    '''node : node node'''
    p[1].addToSequence(p[2])
    p[0] = p[1]

def p_node_tree(p):
    '''node : node tree'''
    p[1].children.append(p[2])
    p[0] = p[1]

def p_node(p):
    'node : SCOLON'
    p[0] = ASTNode()

def p_node_prop(p):
    'node : node property'
    p[1].props[p[2].name] = p[2].value
    p[0] = p[1]

def p_property(p):
    'property : PROPID PROPVALUE'
    p[0] = Property(p[1], p[2])

def p_property_value(p):
    'property : property PROPVALUE'
    p[1].addValue(p[2])
    p[0] = p[1]

def p_error(_):
    """Error rule for syntax errors"""
    print("Syntax error in input!")

def main():

    # Build the parser
    parser = yacc.yacc()

    s = ""
    while True:
        try:
            s = input('calc > ')
        except EOFError:
            break
        if not s:
            continue
        result = parser.parse(s)
        print(result.toString())

if __name__ == '__main__':
    main()