-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_parser.py
188 lines (149 loc) · 2.8 KB
/
_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# coding=utf-8
from compiler.ast import *
"""
Grammar for P0 implemented by this parser:
module ::= stmts
stmts ::= stmt stmts
| stmt cmt stmts
| cmt stmts
| ε
stmt ::= expr
| print expr
| name "=" expr
expr ::= int
| "(" expr ")"
| name
| "-" expr
| expr "+" expr
| "input" "(" ")"
cmt ::= "#" .*
"""
#
# Lexer
#
tokens = (
"PRINT",
"NAME",
"ASSIGN",
"PLUS",
"NEG",
"INT",
"LPAREN",
"RPAREN",
"COMMENT",
"INPUT"
)
# Unordered tokens
t_ASSIGN = "="
t_PLUS = r"\+"
t_NEG = r"\-"
t_INT = r"\d+"
t_LPAREN = r"\("
t_RPAREN = r"\)"
def t_COMMENT(t):
r"\#[^\n]*"
return t
def t_INPUT(t):
"input"
# Ordered tokens (see https://stackoverflow.com/questions/2910338/python-yacc-lexer-token-priority):
def t_PRINT(t):
"print"
return t
def t_NAME(t):
r"[a-zA-Z_][a-zA-Z_\d]*"
return t
# Non-token
t_ignore = " ;\t"
def t_newline(t):
r"\n|\r\n"
t.lexer.lineno += 1
def t_error(t):
print "Illegal character '%s'" % t.value[0]
t.lexer.skip(1)
import libs.ply.lex as lex
lex.lex()
#
# Parser
#
precedence = (
("left", "PLUS"),
('right', 'NEG'),
('left', 'LPAREN'),
)
def p_module(stack):
"""
module : stmts
"""
stack[0] = Module(None, stack[1])
def p_stmts_some(stack):
"""
stmts : stmt stmts
"""
stack[0] = Stmt([stack[1]] + stack[2].nodes)
def p_stmts_some_comment(stack):
"""
stmts : stmt COMMENT stmts
"""
stack[0] = Stmt([stack[1]] + stack[3].nodes)
def p_stmts_none(stack):
"""
stmts :
"""
stack[0] = Stmt([])
def p_stmts_none_comment(stack):
"""
stmts : COMMENT stmts
"""
stack[0] = stack[2]
# Stmt:
def p_stmt_print(stack):
"""
stmt : PRINT expr
"""
stack[0] = Printnl([stack[2]], None)
def p_stmt_expr(stack):
"""
stmt : expr
"""
stack[0] = Discard(stack[1])
def p_stmt_assign(stack):
"""
stmt : NAME ASSIGN expr
"""
stack[0] = Assign([AssName(stack[1], "OP_ASSIGN")], stack[3])
# Expr:
def p_expr_int(stack):
"""
expr : INT
"""
stack[0] = Const(int(stack[1]))
def p_expr_paren(stack):
"""
expr : LPAREN expr RPAREN
"""
stack[0] = stack[2]
def p_expr_name(stack):
"""
expr : NAME
"""
stack[0] = Name(stack[1])
def p_expr_neg(stack):
"""
expr : NEG expr
"""
stack[0] = UnarySub(stack[2])
def p_expr_plus(stack):
"""
expr : expr PLUS expr
"""
stack[0] = Add((stack[1], stack[3]))
def p_expr_call(stack):
"""
expr : INPUT LPAREN RPAREN
"""
stack[0] = CallFunc(stack[1], [])
def p_error(p):
print("Syntax error at '%s'" % p.value)
import libs.ply.yacc as yacc
yacc.yacc(start="module", debug=False, write_tables=False)
parse = yacc.parse