-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.py
243 lines (191 loc) · 6.13 KB
/
compiler.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
from sly import Lexer, Parser
from symbol_table import SymbolTable, Array, Variable
from code_generator import CodeGenerator
import sys
class ImpLexer(Lexer):
tokens = {DECLARE, BEGIN, END, PID, NUM, IF, THEN, ELSE, ENDIF, WHILE, DO, ENDWHILE, REPEAT, UNTIL, FOR, FROM, TO,
DOWNTO, ENDFOR, READ, WRITE, EQ, NEQ, GT, LT, GEQ, LEQ, GETS}
literals = {'+', '-', '*', '/', '%', ',', ':', ';', '(', ')'}
ignore = ' \t'
@_(r'\[[^\]]*\]')
def ignore_comment(self, t):
self.lineno += t.value.count('\n')
@_(r'\n+')
def ignore_newline(self, t):
self.lineno += len(t.value)
DECLARE = r"DECLARE"
BEGIN = r"BEGIN"
ENDWHILE = r"ENDWHILE"
ENDFOR = r"ENDFOR"
ENDIF = r"ENDIF"
END = r"END"
WHILE = r"WHILE"
FOR = r"FOR"
IF = r"IF"
THEN = r"THEN"
ELSE = r"ELSE"
DOWNTO = r"DOWNTO"
DO = r"DO"
TO = r"TO"
FROM = r"FROM"
REPEAT = r"REPEAT"
UNTIL = r"UNTIL"
READ = r"READ"
WRITE = r"WRITE"
GETS = r":="
NEQ = r"!="
GEQ = r">="
LEQ = r"<="
EQ = r"="
GT = r">"
LT = r"<"
PID = r"[_a-z]+"
@_(r'\d+')
def NUM(self, t):
t.value = int(t.value)
return t
def error(self, t):
raise Exception(f"Illegal character '{t.value[0]}'")
class ImpParser(Parser):
tokens = ImpLexer.tokens
symbols = SymbolTable()
code = None
# We need a set of consts that will be written inside a loop/if. These need to be generated and stored pre-entry
# because the entry might not happen at all, for instance for [if 1 > 2 then write 1; endif write 1;] the code
# for storing 1 in memory would get generated inside the if and never be executed, causing the second write to
# print something undefined.
consts = set()
@_('DECLARE declarations BEGIN commands END', 'BEGIN commands END')
def program(self, p):
self.code = CodeGenerator(p.commands, self.symbols)
return self.code
@_('declarations "," PID', 'PID')
def declarations(self, p):
self.symbols.add_variable(p[-1])
@_('declarations "," PID "(" NUM ":" NUM ")" ')
def declarations(self, p):
self.symbols.add_array(p[2], p[4], p[6])
@_('PID "(" NUM ":" NUM ")"')
def declarations(self, p):
self.symbols.add_array(p[0], p[2], p[4])
@_('commands command')
def commands(self, p):
return p[0] + [p[1]]
@_('command')
def commands(self, p):
return [p[0]]
@_('identifier GETS expression ";"')
def command(self, p):
return "assign", p[0], p[2]
@_('IF condition THEN commands ELSE commands ENDIF')
def command(self, p):
resp = "ifelse", p[1], p[3], p[5], self.consts.copy()
self.consts.clear()
return resp
@_('IF condition THEN commands ENDIF')
def command(self, p):
resp = "if", p[1], p[3], self.consts.copy()
self.consts.clear()
return resp
@_('WHILE condition DO commands ENDWHILE')
def command(self, p):
resp = "while", p[1], p[3], self.consts.copy()
self.consts.clear()
return resp
@_('REPEAT commands UNTIL condition ";"')
def command(self, p):
return "until", p[3], p[1]
@_('FOR PID FROM value TO value DO commands ENDFOR')
def command(self, p):
resp = "forup", p[1], p[3], p[5], p[7], self.consts.copy()
self.consts.clear()
return resp
@_('FOR PID FROM value DOWNTO value DO commands ENDFOR')
def command(self, p):
resp = "fordown", p[1], p[3], p[5], p[7], self.consts.copy()
self.consts.clear()
return resp
@_('READ identifier ";"')
def command(self, p):
return "read", p[1]
@_('WRITE value ";"')
def command(self, p):
if p[1][0] == "const":
self.consts.add(int(p[1][1]))
return "write", p[1]
@_('value')
def expression(self, p):
return p[0]
@_('value "+" value')
def expression(self, p):
return "add", p[0], p[2]
@_('value "-" value')
def expression(self, p):
return "sub", p[0], p[2]
@_('value "*" value')
def expression(self, p):
return "mul", p[0], p[2]
@_('value "/" value')
def expression(self, p):
return "div", p[0], p[2]
@_('value "%" value')
def expression(self, p):
return "mod", p[0], p[2]
@_('value EQ value')
def condition(self, p):
return "eq", p[0], p[2]
@_('value NEQ value')
def condition(self, p):
return "ne", p[0], p[2]
@_('value LT value')
def condition(self, p):
return "lt", p[0], p[2]
@_('value GT value')
def condition(self, p):
return "gt", p[0], p[2]
@_('value LEQ value')
def condition(self, p):
return "le", p[0], p[2]
@_('value GEQ value')
def condition(self, p):
return "ge", p[0], p[2]
@_('NUM')
def value(self, p):
return "const", p[0]
@_('identifier')
def value(self, p):
return "load", p[0]
@_('PID')
def identifier(self, p):
if p[0] in self.symbols:
return p[0]
else:
return "undeclared", p[0]
@_('PID "(" PID ")"')
def identifier(self, p):
if p[0] in self.symbols and type(self.symbols[p[0]]) == Array:
if p[2] in self.symbols and type(self.symbols[p[2]]) == Variable:
return "array", p[0], ("load", p[2])
else:
return "array", p[0], ("load", ("undeclared", p[2]))
else:
raise Exception(f"Undeclared array {p[0]}")
@_('PID "(" NUM ")"')
def identifier(self, p):
if p[0] in self.symbols and type(self.symbols[p[0]]) == Array:
return "array", p[0], p[2]
else:
raise Exception(f"Undeclared array {p[0]}")
def error(self, token):
raise Exception(f"Syntax error: '{token.value}' in line {token.lineno}")
sys.tracebacklimit = 0
lex = ImpLexer()
pars = ImpParser()
with open(sys.argv[1]) as in_f:
text = in_f.read()
pars.parse(lex.tokenize(text))
code_gen = pars.code
code_gen.gen_code()
with open(sys.argv[2], 'w') as out_f:
for line in code_gen.code:
print(line, file=out_f)