-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.py
51 lines (29 loc) · 877 Bytes
/
parser.py
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
#!/usr/bin/python
import scanner
import ply.yacc as yacc
tokens = scanner.tokens
precedence = (
# to fill ...
("left", '+', '-'),
("left", '*', '/')
# to fill ...
)
def p_error(p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno, scanner.find_tok_column(p),
p.type, p.value))
else:
print("Unexpected end of input")
def p_program(p):
"""program : instructions_opt"""
def p_instructions_opt_1(p):
"""instructions_opt : instructions """
def p_instructions_opt_2(p):
"""instructions_opt : """
def p_instructions_1(p):
"""instructions : instructions instruction """
def p_instructions_2(p):
"""instructions : instruction """
# to finish the grammar
# ....
parser = yacc.yacc()