-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpl0yacc.py
230 lines (194 loc) · 4.76 KB
/
pl0yacc.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import ply.yacc as yacc
import ply.lex as lex
from pl0lex import *
procs = {}
ast = []
def p_program(p):
'program : block period_token'
# | | |
# p[0] p[1] p[2]
global ast
p[0] = ['program',p[1]]
ast = p[0]
print(p[0])
def p_block(p):
'block : con_dec var_dec proc_dec stmt'
p[0] = ["block",p[1],p[2],p[3],p[4]]
def p_con_dec(p):
'''con_dec : const_token ident_token eql_token number_token con_dec_cdr semicolom_token
| empty
'''
p[0] = ['const']
if len(p) != 2:
p[0].append([p[2],p[4]])
p[0] += p[5]
def p_con_dec_cdr(p):
'''con_dec_cdr : comma_token ident_token eql_token number_token con_dec_cdr
| empty
'''
p[0] = []
if len(p) != 2:
p[0].append([p[2],p[4]])
p[0] += p[5]
def p_var_dec(p):
'''var_dec : var_token ident_token var_dec_cdr semicolom_token
| empty
'''
p[0] = ['var']
if len(p) != 2:
p[0].append(p[2])
p[0] += p[3]
def p_var_dec_cdr(p):
'''var_dec_cdr : comma_token ident_token var_dec_cdr
| empty
'''
p[0] = []
if len(p) != 2:
p[0].append(p[2])
p[0] += p[3]
def p_proc_dec(p):
'''proc_dec : proc_token ident_token semicolom_token block semicolom_token proc_dec_cdr
| empty
'''
p[0] = ['proc']
if len(p) != 2:
p[0].append(p[2])
procs[p[2]] = p[4]
p[0] += p[6]
def p_proc_dec_cdr(p):
'''proc_dec_cdr : proc_token ident_token semicolom_token block semicolom_token proc_dec_cdr
| empty
'''
p[0] = []
if len(p) != 2:
p[0].append(p[2])
procs[p[2]] = p[4]
p[0] += p[6]
def p_stmt(p):
'''stmt : assign_stmt
| call_stmt
| begin_stmt
| if_stmt
| while_stmt
| read_stmt
| write_stmt
| empty
'''
p[0] = p[1]
def p_assign_stmt(p):
'assign_stmt : ident_token become_token expr'
p[0] = [':=',p[1],p[3]]
def p_call_stmt(p):
'call_stmt : call_token ident_token'
p[0] = ['call',p[2]]
def p_begin_stmt(p):
'begin_stmt : begin_token stmt begin_stmt_cdr end_token'
p[0] = ['stmt',p[2],p[3]]
def p_begin_stmt_cdr(p):
'''begin_stmt_cdr : semicolom_token stmt begin_stmt_cdr
| empty
'''
if len(p) != 2:
p[0] = ['stmt',p[2],p[3]]
def p_if_stmt(p):
'''if_stmt : if_token cond then_token stmt
| if_token cond then_token stmt else_token stmt
'''
if len(p) == 5:
p[0] = ['if',p[2],p[4]]
else:
p[0] = ['if-else',p[2],p[4],p[6]]
def p_while_stmt(p):
'while_stmt : while_token cond do_token stmt'
p[0] = ['while',p[2],p[4]]
def p_read_stmt(p):
'read_stmt : read_token ident_token'
p[0] = ['read',p[2]]
def p_write_stmt(p):
'write_stmt : write_token expr'
p[0] = ['write',p[2]]
def p_cond(p):
'''cond : odd_token expr
| expr cmp expr
'''
if len(p) == 3:
p[0] = ['odd',p[2]]
else:
p[0] = [p[2],p[1],p[3]]
def p_cmp(p):
'''cmp : eql_token
| les_token
| leq_token
| grt_token
| geq_token
| neq_token
'''
p[0] = p[1]
def p_expr_flag(p):
'''expr : flag no_flag_expr
| no_flag_expr
'''
if len(p) == 2:
p[0] = p[1]
elif p[1] == '+':
p[0] = p[2]
else:
p[0] = ['-',p[2]]
def p_expr_no_flag(p):
'''no_flag_expr : term plus_token term
no_flag_expr : term minus_token term
no_flag_expr : term
'''
if len(p) == 2:
p[0] = p[1]
elif p[2] == '+':
p[0] = ['+',p[1],p[3]]
else:
p[0] = ['-',p[1],p[3]]
def p_flag(p):
'''flag : plus_token
| minus_token
'''
p[0] = p[1]
def p_term_factor(p):
'term : factor'
p[0] = p[1]
def p_term_mul(p):
'''term : factor mul_token term
term : factor divide_token term
'''
if(p[2] == '*'):
p[0] = ['*',p[1],p[3]]
else:
p[0] = ['/',p[1],p[3]]
def p_factor_ident(p):
'factor : ident_token'
p[0] = ['ident',p[1]]
def p_factor_number(p):
'factor : number_token'
p[0] = ['number',p[1]]
def p_factor_expr(p):
'factor : lparent_token expr rparent_token'
p[0] = p[2]
def p_empty(p):
'empty : '
pass
def p_error(p):
print("Error in line {0}".format(p))
# Test it out
def print_tokens(lexer):
for token in lexer:
print token
def pl0_lexer(data):
lexer = lex.lex()
lexer.input(data)
print_tokens(lexer)
def pl0_parse(filepath):
with open(filepath) as file:
data = file.read()
pl0_lexer(data)
y = yacc.yacc()
y.parse(data)
print(procs)
return ast
#pl0_parse()