-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
289 lines (218 loc) · 8.81 KB
/
Copy pathparser.py
File metadata and controls
289 lines (218 loc) · 8.81 KB
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
""" Parser for jftt compiler, here on the other hand fancy stuff start to happen. """
import sys
import ply.yacc as yacc
from symbolTable import SymbolTable
symbol_table = SymbolTable()
class Command:
def __init__(self, command_type, index=None):
self.type = command_type
self.index = index
self.commands = []
def create_program(commands: Command):
program = Command("COM_PROGRAM")
program.commands.append(commands)
return program
def set_variable_in_declaration(name):
""" Function for declaring variables inside DECLARE field. It's special cause it throws exception when same variable is
being declared twice. """
if symbol_table.get_symbol_by_name(name) is not None:
print(f"Double {name} variable declaration", file=sys.stderr)
sys.exit() # in this case we programs stops
else:
symbol_table.add(name, "VARIABLE")
def set_variable(name):
""" Function for adding everything approached by parser to symbol table.
checking if all variables used in program where declared happens in
symbol_table.check_declaration_sack() """
if symbol_table.get_symbol_by_name(name) is not None:
return
else:
symbol_table.add(name, "VARIABLE")
def set_const(name):
symbol_table.add(name, "CONST", int(name), None, None, True)
def set_array(name, begin, end):
if begin > end:
print(f"Wrong array {name} declaration.", file=sys.stderr)
sys.exit()
symbol_table.add(name, "ARRAY", None, begin, end)
def add_command(parent: Command, child: Command):
parent.commands.append(child)
return parent
def create_parent_command(command_type, *children):
parent = Command(command_type)
for i, child in enumerate(children):
parent.commands.append(child)
return parent
def create_value_command(command_type, name):
if type(name) == int:
name = str(name)
index = symbol_table.get(name)
if index is None:
print(f"Variable {name} was not declared.", file=sys.stderr)
sys.exit()
else:
value_command = Command(command_type, index)
return value_command
def create_iterator_command(command_type, name):
index = symbol_table.get(name)
if index is not None and symbol_table.dict[index][1] == "VARIABLE":
return
if index is not None and symbol_table.dict[index][1] != "ITERATOR":
print(f"Iterator {name} was declared as VAR or ARR before.", file=sys.stderr)
sys.exit()
elif index is not None and symbol_table.dict[index][1] == "ITERATOR":
new_index = symbol_table.get(name)
iterator_command = Command(command_type, new_index)
return iterator_command
elif index is None:
symbol_table.add(name, "ITERATOR")
new_index = symbol_table.get(name)
iterator_command = Command(command_type, new_index)
return iterator_command
def create_empty_command(command_type):
return Command(command_type)
""" CLEAN PARSER """
from lex import tokens
precedence = (
('left', 'PLUS', 'MINUS'),
('left', 'TIMES', 'DIVIDE'),
)
start = 'program'
def p_program(p):
"""program : DECLARE declarations BEGIN commands END
| BEGIN commands END"""
if len(p) == 6:
p[0] = create_program(p[4])
elif len(p) == 4:
p[0] = create_program(p[3])
def p_declarations(p):
"""declarations : declarations COMMA PIDENTIFIER
| declarations COMMA PIDENTIFIER LPAREN NUM COLON NUM RPAREN
| PIDENTIFIER
| PIDENTIFIER LPAREN NUM COLON NUM RPAREN"""
if len(p) == 4:
set_variable_in_declaration(p[3])
symbol_table.add_to_declaration_sack(p[3])
elif len(p) == 9:
set_array(p[3], p[5], p[7])
elif len(p) == 2:
set_variable_in_declaration(p[1])
symbol_table.add_to_declaration_sack(p[1])
elif len(p) == 7:
set_array(p[1], p[3], p[5])
def p_commands(p):
"""commands :
| commands command """
if len(p) == 3:
p[0] = add_command(p[1], p[2])
elif len(p) == 1:
p[0] = create_empty_command("COM_COMMANDS")
def p_command(p):
"""command : identifier ASSGNOP expression SEMICOLON
| IF condition THEN commands ELSE commands ENDIF
| IF condition THEN commands ENDIF
| WHILE condition DO commands ENDWHILE
| REPEAT commands UNTIL condition SEMICOLON
| FOR PIDENTIFIER FROM value TO value DO commands ENDFOR
| FOR PIDENTIFIER FROM value DOWNTO value DO commands ENDFOR
| READ identifier SEMICOLON
| WRITE value SEMICOLON"""
if p[2] == ":=":
p[0] = create_parent_command("COM_ASSGNOP", p[1], p[3])
elif p[1] == "IF" and p[5] == "ELSE":
p[0] = create_parent_command("COM_IFELSE", p[2], p[4], p[6])
elif p[1] == "IF" and not p[5] == "ELSE":
p[0] = create_parent_command("COM_IF", p[2], p[4])
elif p[1] == "WHILE":
p[0] = create_parent_command("COM_WHILE", p[2], p[4])
elif p[1] == "REPEAT":
p[0] = create_parent_command("COM_REPEAT", p[2], p[4])
elif p[1] == "FOR" and p[5] == "TO":
set_variable(p[2])
set_variable(p[2] + "_FAKE_iter")
symbol_table.add_to_declaration_sack(p[2])
p[0] = create_parent_command("COM_FOR", create_value_command("COM_PID", p[2]),
create_value_command("COM_PID", p[2] + "_FAKE_iter"), p[4], p[6],
p[8])
elif p[1] == "FOR" and p[5] == "DOWNTO":
set_variable(p[2])
set_variable(p[2] + "_FAKE_iter")
symbol_table.add_to_declaration_sack(p[2])
p[0] = create_parent_command("COM_FORDOWN", create_value_command("COM_PID", p[2]),
create_value_command("COM_PID", p[2] + "_FAKE_iter"), p[4], p[6], p[8])
elif p[1] == "READ":
p[0] = create_parent_command("COM_READ", p[2])
elif p[1] == "WRITE":
p[0] = create_parent_command("COM_WRITE", p[2])
def p_expression(p):
"""expression : value
| value PLUS value
| value MINUS value
| value TIMES value
| value DIVIDE value
| value MODULO value"""
if len(p) == 2:
p[0] = p[1]
elif p[2] == "+":
p[0] = create_parent_command("COM_ADD", p[1], p[3])
elif p[2] == "-":
p[0] = create_parent_command("COM_SUB", p[1], p[3])
elif p[2] == "*":
p[0] = create_parent_command("COM_MUL", p[1], p[3])
elif p[2] == "/":
p[0] = create_parent_command("COM_DIV", p[1], p[3])
elif p[2] == "%":
p[0] = create_parent_command("COM_MOD", p[1], p[3])
def p_condition(p):
"""condition : value EQUAL value
| value NOTEQUAL value
| value LESSER value
| value BIGGER value
| value LESSEREQUAL value
| value BIGGEREQUAL value"""
if p[2] == "=":
p[0] = create_parent_command("COM_EQ", p[1], p[3])
elif p[2] == "!=":
p[0] = create_parent_command("COM_NEQ", p[1], p[3])
elif p[2] == "<":
p[0] = create_parent_command("COM_LT", p[1], p[3])
elif p[2] == ">":
p[0] = create_parent_command("COM_GT", p[1], p[3])
elif p[2] == "<=":
p[0] = create_parent_command("COM_LEQ", p[1], p[3])
elif p[2] == ">=":
p[0] = create_parent_command("COM_GEQ", p[1], p[3])
def p_value_num(p):
"""value : NUM"""
if symbol_table.get(str(p[1])) is None:
set_const(str(p[1]))
p[0] = create_value_command("COM_NUM", p[1])
def p_value_identifier(p):
"""value : identifier"""
p[0] = p[1]
def p_identifier_pidentifier(p):
"""identifier : PIDENTIFIER
| PIDENTIFIER LPAREN PIDENTIFIER RPAREN"""
if len(p) == 2:
set_variable(p[1])
p[0] = create_value_command("COM_PID", p[1])
elif len(p) == 5:
set_variable(p[3])
if symbol_table.get_symbol_by_name(p[1])[1] != "ARRAY":
print(f"{p[1]} is not array.", file=sys.stderr)
sys.exit()
p[0] = create_parent_command("COM_ARR", create_value_command("COM_PID", p[1]),
create_value_command("COM_PID", p[3]))
def p_identifier_num(p):
"""identifier : PIDENTIFIER LPAREN NUM RPAREN"""
if symbol_table.get_symbol_by_name(p[1])[1] != "ARRAY":
print(f"{p[1]} is not array.", file=sys.stderr)
sys.exit()
if symbol_table.get(str(p[3])) is None:
set_const(str(p[3]))
p[0] = create_parent_command("COM_ARR", create_value_command("COM_PID", p[1]),
create_value_command("COM_NUM", p[3]))
def p_error(t):
print("Syntax error at '%s'" % t.value)
sys.exit()
parser = yacc.yacc()