-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthax_analyze.py
337 lines (272 loc) · 10.2 KB
/
synthax_analyze.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Project : Compilateur (Python) #
# #
# File : synthax_analyze.py #
# #
# Description : Synthax analyze file and functions. #
# #
# Contributors : Corentin TROADEC & Anthony Vuillemin #
# #
# Date : September 2018 #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# - - - - - - - - - - - - - - - - - #
# IMPORT #
# - - - - - - - - - - - - - - - - - #
# PROJECT MODULES
from conf import *
from node import *
from token import *
from utils import *
# - - - - - - - - - - - - - - - - - #
# FUNCTIONS #
# - - - - - - - - - - - - - - - - - #
# Launch synthax analyze
def synthax_analyse() :
global index_tab
# Main node (racine)
P = Node("prog")
# Parcour all token tab
#while index_tab < len(tab_token):
while tab_token[index_tab].token != "toke_eop" :
#N = get_statment()
N = get_function()
P.add_child(N)
return P
# Return a tree compose by cons/id & unaire operator
def atom() :
#GET GLOBAL VAR
global index_tab
#Search an atom but end of the list, so a paramter missing
if index_tab >= len(tab_token) : # and tab_token[-1].token != "toke_parantClose" :
error_compilation(tab_token[index_tab-1],"Operator Missing the second parameter.")
current_toke = tab_token[index_tab]
#UNAIRE OPERATOR
if current_toke.token in unaire_operator :
index_tab = index_tab + 1
current_node = atom()
return NodeUnaire(current_toke,current_node)
#CONST
elif current_toke.token == "toke_const" :
index_tab = index_tab + 1
return NodeToken(current_toke)
#IDENT OR FUNCTION REF
elif current_toke.token == "toke_id" :
index_tab = index_tab + 1
# Function reference
if tab_token[index_tab] != None and tab_token[index_tab].token == "toke_parantOpen" :
N = Node("funct_ref",current_toke.val)
accept("toke_parantOpen")
# Parameters
while tab_token[index_tab].token != "toke_parantClose" :
N.add_child(expr())
# eval next expression. If next = ")", end param declaration
if tab_token[index_tab].token == "toke_parantClose" :
break;
# else need a virgule
accept("toke_virgule")
# End of function call
accept("toke_parantClose")
return N
# Var reference
else :
return NodeVarRef(current_toke)
#PARANTHESE
elif current_toke.token == "toke_parantOpen" :
index_tab = index_tab + 1
N = expr()
#End expression
if index_tab >= len(tab_token) :
error_compilation(tab_token[len(tab_token)-1],"Parenthesis missing.")
#Missing parenthesis
elif tab_token[index_tab].token != "toke_parantClose" :
error_compilation(tab_token[index_tab],"Parenthesis missing.")
index_tab = index_tab + 1
return N
#IMPLICIT ELSE --> Incompatible token
error_compilation(current_toke,"Incoherent Char : Double operator deteted.")
# Launch the synthax analyze with a high priority level
def expr() :
return expr_launch(1000);
# Build a tree composed with the tokens tab
def expr_launch(priority) :
global index_tab
#Recup first atom
A = atom()
#End of token list ?
if index_tab < len(tab_token) :
current_toke = tab_token[index_tab]
#Toke const or id follow by a same other
if tab_token[index_tab].token == "toke_const" or tab_token[index_tab].token == "toke_id" :
error_compilation(current_toke,"Constant or identifiant repetition without operator.")
#Toke is an open paranthesis.
if current_toke.token == "toke_parantOpen" :
error_compilation(current_toke,"Illegal expression.")
while current_toke.token in binaire_operator and index_tab < len(tab_token) : # -1
#If priority superior
if binaire_operator[current_toke.token]["priority"] >= priority :
break
index_tab = index_tab + 1
#Simplify reading
thisPriority = binaire_operator[current_toke.token]["priority"]
thisAssociativity = binaire_operator[current_toke.token]["associativity"]
#Build the second child (recursif)
secondChild = expr_launch(thisPriority + thisAssociativity)
#No second child
if(secondChild == None) :
error_compilation(current_toke,"Operator Missing the second parameter.")
break
N = BasicNode(current_toke,A,secondChild)
A = N
#End toke list ?
if index_tab < len(tab_token) : #-1
current_toke = tab_token[index_tab]
return A
# Find a statment model and build it
def get_statment():
global index_tab
# Declaration (ex : var x;)
if tab_token[index_tab].token == "toke_var":
accept("toke_var")
cpy_toke_id = accept("toke_id")
accept("toke_semicolon")
node_dcl = Node("node_dcl",cpy_toke_id.val)
return node_dcl
# Return
if tab_token[index_tab].token == "toke_return" :
accept("toke_return")
node_return = Node("return")
E = expr()
accept("toke_semicolon")
node_return.add_child(E)
return node_return;
# Begining a block section
elif tab_token[index_tab].token == "toke_braceOpen" :
N = Node("node_block")
accept("toke_braceOpen")
while tab_token[index_tab].token != "toke_braceClose" :
N.add_child(get_statment())
accept("toke_braceClose")
return N
# If (and else)
elif tab_token[index_tab].token == "toke_if" :
accept("toke_if")
accept("toke_parantOpen")
T = expr()
accept("toke_parantClose")
B = get_statment()
N = Node("node_cond")
N.add_child(T)
N.add_child(B)
if index_tab < len(tab_token) and tab_token[index_tab].token == "toke_else" :
#index_tab = index +1
accept("toke_else")
N.add_child(get_statment())
return N
# While
elif tab_token[index_tab].token == "toke_while" :
N = Node("node_loop")
accept("toke_while")
accept("toke_parantOpen")
T = expr()
accept("toke_parantClose")
B = get_statment()
# Add an empty node step
B.add_child(Node("step"))
N_cond = Node("node_cond")
N_cond.add_child(T)
N_cond.add_child(B)
N_cond.add_child(Node("break"))
N.add_child(N_cond)
return N
# For
elif tab_token[index_tab].token == "toke_for" :
# Main node
main_node = Node("node_seq")
# Gramar
accept("toke_for")
accept("toke_parantOpen")
init = expr()
accept("toke_semicolon")
test = expr()
accept("toke_semicolon")
step = expr()
# Add the step in a specific step node
node_step = Node("step")
node_step.add_child(step)
accept("toke_parantClose")
B = get_statment()
# body & step
seq_node = Node("seq")
seq_node.add_child(B)
seq_node.add_child(node_step)
# Test / sequence & break
cond_node = Node("node_cond")
cond_node.add_child(test)
cond_node.add_child(seq_node)
cond_node.add_child(Node("break"))
# Loop (encaps cond node)
loop_node = Node("node_loop")
loop_node.add_child(cond_node)
# Add in main_node
main_node.add_child(init)
main_node.add_child(loop_node)
return main_node
# Print
elif tab_token[index_tab].token == "toke_print" :
N = Node("node_print")
accept("toke_print")
N.add_child(expr())
accept("toke_semicolon")
return N
# BREAK ET CONTINUE
elif tab_token[index_tab].token == "toke_break" :
N = Node("break")
accept("toke_break")
accept("toke_semicolon")
return N
elif tab_token[index_tab].token == "toke_continue" :
N = Node("continue")
accept("toke_continue")
accept("toke_semicolon")
return N
# Default expression + ";"
else :
A = expr()
accept("toke_semicolon")
D = Node("node_drop")
D.add_child(A)
return D
# Return a function node
def get_function() :
global index_tab
# Function name
if tab_token[index_tab].token != "toke_id" :
error_compilation( tab_token[index_tab],"Invalid name function.")
# Build node
N = Node("funct",tab_token[index_tab].val)
accept("toke_id") #index_tab = index_tab + 1
accept("toke_parantOpen")
# Parameters
while tab_token[index_tab].token != "toke_parantClose" :
# Copy toke_id
cpy_toke_id = tab_token[index_tab]
# Next & create var declaration
accept("toke_id")
N.add_child(Node("node_dcl",cpy_toke_id.val))
# evalue next expression. Virgule if not toke_parantClose
if tab_token[index_tab].token != "toke_parantClose" :
accept("toke_virgule")
accept("toke_parantClose")
# Build function node & return
N.add_child(get_statment())
return N
def accept(token_waiting) :
global index_tab
if index_tab >= len(tab_token):
error_compilation(tab_token[-1],"Missing semicolon.")
current_toke = tab_token[index_tab]
if token_waiting == current_toke.token :
index_tab = index_tab + 1
return current_toke
else :
error_compilation(current_toke,"Token not excepted. '"+token_waiting+"' waiting.")