-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntaxparser.py
315 lines (274 loc) · 9.18 KB
/
syntaxparser.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from lexer import lex, TYPE
from lextable import LexTable
from compileerror import CompileError
from typing import *
class Quadruple:
def __init__(self, op, arg1, arg2, result):
self.op = op
self.arg1 = arg1
self.arg2 = arg2
self.result = result
def __str__(self):
arg1 = str(self.arg1) if self.arg1 is not None else 'None'
arg2 = str(self.arg2) if self.arg2 is not None else 'None'
return '(' + self.op + ', ' + arg1 + ', ' + arg2 + ', ' + str(self.result) + ')'
def __getitem__(self, item):
if item == 0:
return self.op
elif item == 1:
return self.arg1
elif item == 2:
return self.arg2
elif item == 3:
return self.result
class Label:
def __init__(self, _id: int):
self.id = _id
class Parser:
def __init__(self, lex_table: LexTable):
self.table = lex_table
self.e_label: int = 0 # expression label
self.r_label: int = 0 # relational expression label
self.t_label: int = 0 # temp label
self.quadruple: List[Union[Quadruple, Label]] = []
def new_temp(self) -> str:
"""
apply for a new temp variable
:return:
"""
self.t_label += 1
return 'T' + str(self.t_label - 1)
def new_label(self) -> None:
"""
append a new label to the list
:return:
"""
self.quadruple.append(Label(self.t_label))
self.t_label += 1
def variable(self):
"""
V -> ID
:return:
"""
if self.table.match(TYPE['ID']):
text = self.table.get()
self.table.advance()
return text
else:
raise CompileError('Expect an identifier, but not found.', self.table.position())
def integer(self):
"""
<INT>: number
:return:
"""
if self.table.match(TYPE['INT']):
text = self.table.get()
self.table.advance()
return text
else:
raise CompileError("Expect an number, but not found.", self.table.position())
def relational_operator(self) -> str:
"""
OP -> < | <= | == | > | >= | <>
:return:
"""
if self.table.match(TYPE['>']) or \
self.table.match(TYPE['>=']) or \
self.table.match(TYPE['<']) or \
self.table.match(TYPE['<=']) or \
self.table.match(TYPE['=']) or \
self.table.match(TYPE['<>']):
text = self.table.get()
self.table.advance()
return text.content
else:
raise CompileError('Expect an relational identifier, but not found.', self.table.position())
def relational_statement(self):
"""
RS -> E OP E
:return:
"""
arg1 = self.expression()
op = self.relational_operator()
arg2 = self.expression()
result = self.new_temp()
self.quadruple.append(Quadruple(op, arg1, arg2, result))
return result
def factor(self):
"""
F -> V | INT | ( E )
:return:
"""
if self.table.match(TYPE['(']):
self.table.advance()
text = self.expression()
if self.table.match(TYPE[')']):
return text
else:
raise CompileError('Missing parenthesis.', self.table.position())
elif self.table.match(TYPE['INT']):
text = self.table.get()
self.table.advance()
return text
else:
text = self.variable()
return text
# def term1(self):
# """
# T1 -> * F T1 | / F T1 | EPSILON
# :return:
# """
# if self.table.match(TYPE['*']) or self.table.match(TYPE['/']):
# self.table.advance()
# self.factor()
# self.term1()
def term(self):
"""
T -> F T1
T -> F { [*/] F }
:return:
"""
arg1 = self.factor()
# self.term1()
while self.table.match(TYPE['*']) or self.table.match(TYPE['/']):
op = self.table.get()
self.table.advance()
arg2 = self.factor()
result = self.new_temp()
self.quadruple.append(Quadruple(op.content, arg1, arg2, result))
arg1 = result
return arg1
def expression(self):
"""
E -> T E1
:return:
"""
arg1 = self.term()
# self.expression1()
while self.table.match(TYPE['+']) or self.table.match(TYPE['-']):
text = self.table.get()
self.table.advance()
arg2 = self.term()
result = self.new_temp()
self.quadruple.append(Quadruple(text.content, arg1, arg2, result))
arg1 = result
return arg1
# def expression1(self):
# """
# E1 -> + T E1 | - T E1 | EPSILON
# :return:
# """
# if self.table.match(TYPE['+']) or self.table.match(TYPE['-']):
# self.table.advance()
# self.term()
# self.expression1()
def multiple_statement(self):
"""
MS -> begin SL end
:return:
"""
if self.table.match(TYPE['begin']):
self.table.advance()
self.statement_list()
if self.table.match(TYPE['end']):
self.table.advance()
else:
raise CompileError('Expect "end" but not found.', self.table.position())
else:
raise CompileError('Expect "begin" but not found.', self.table.position())
def while_statement(self):
"""
WHILE -> while RS do S
:return:
"""
if self.table.match(TYPE['while']):
self.table.advance()
arg1 = self.relational_statement()
if self.table.match(TYPE['do']):
tc = len(self.quadruple) # true chain
self.quadruple.append(Quadruple('jnz', arg1, None, 0))
fc = len(self.quadruple) # false chain
self.quadruple.append(Quadruple('jmp', None, None, 0))
self.table.advance()
self.quadruple[tc].result = len(self.quadruple) # backpatch true chain
self.statement()
self.quadruple[fc].result = len(self.quadruple) # behind statement is false chain, backpatch it
else:
raise CompileError('Expect "do" but not found.', self.table.position())
else:
raise CompileError('Expect "while" but not found.', self.table.position())
def conditional_statement(self):
"""
CS -> if RS then S else S
:return:
"""
if self.table.match(TYPE['if']):
self.table.advance()
arg1 = self.relational_statement()
tc = len(self.quadruple) # true chain
self.quadruple.append(Quadruple('jnz', arg1, None, 0))
fc = len(self.quadruple) # false chain
self.quadruple.append(Quadruple('jmp', None, None, 0))
if self.table.match(TYPE['then']):
self.table.advance()
# self.new_label()
self.quadruple[tc].result = len(self.quadruple) # backpatch true chain
self.statement()
else:
raise CompileError('Expect "then" but not found.', self.table.position())
if self.table.match(TYPE['else']):
self.table.advance()
self.quadruple[fc].result = len(self.quadruple) # backpatch false chain
self.statement()
else:
raise CompileError('Expect "if" but not found.', self.table.position())
def assignment_statement(self):
"""
<AS>: <V>:=<E>
:return:
"""
arg1 = self.variable()
if self.table.match(TYPE[':=']):
self.table.advance()
arg2 = self.expression()
result = self.new_temp()
self.quadruple.append(Quadruple(':=', arg1, arg2, result))
def statement(self):
"""
S -> AS | CS | WHILE | MS
:return:
"""
if self.table.look_forward(TYPE['ID']):
self.assignment_statement()
elif self.table.look_forward(TYPE['if']):
self.conditional_statement()
elif self.table.look_forward(TYPE['while']):
self.while_statement()
elif self.table.look_forward(TYPE['begin']):
self.multiple_statement()
def statement_list(self):
"""
SL -> S SL1
:return:
"""
self.statement()
self.statement_list1()
def statement_list1(self):
"""
SL1 -> ; SL | EPSILON
:return:
"""
if self.table.match(TYPE[';']):
self.table.advance()
self.statement_list()
def parse(self):
self.statement_list()
if __name__ == '__main__':
from lexer import lex
from source import Source
source = Source('test.pas')
table = lex(source)
parser = Parser(table)
parser.parse()
for i in range(len(parser.quadruple)):
print('{0}:\t{1}'.format(i, str(parser.quadruple[i])))