-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPARSER.py
360 lines (315 loc) · 9.35 KB
/
PARSER.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env python
# coding: utf-8
# In[23]:
from SCANNER import getToken
from graphviz import Digraph
g = Digraph('G', filename='hello')
g.format = 'png'
g.attr('node',rankdir='LR',shape='box')
currentNode = None
lastNode = None
lastNode_val = None
index = 0
Temp = None
class Error(Exception):
def __init__(self, message):
super().__init__()
self.message = message
def getNext ():
global token
global tokenType
fullRecord = getToken()
if fullRecord == 'finish':
g.view()
else :
token = fullRecord.split(',')[0]
tokenType = fullRecord.split(',')[1]
def programe ():
'''
we call it only at beginning of programme
takes no input and no output
'''
getNext()
stmt_sequence()
def stmt_sequence ():
global token
global tokenType
st1 = statement()
while token == ';':
match(';','value')
statement()
return st1
def statement():
global token
global tokenType
if token == 'if':
if_stmt()
elif token == 'repeat':
repeat_stmt()
elif token == 'read':
return read_stmt()
elif token == 'write':
return write_stmt()
else:
return assign_stmt()
def if_stmt():
global token
global tokenType
global currentNode
global lastNode
global lastNode_val
match('if','value')
if currentNode == None and lastNode == None:
currentNode = str('if')
g.node(currentNode)
else:
lastNode = currentNode
currentNode = str('if')
g.node(currentNode)
if lastNode_val == 'if' or lastNode_val == 'repeat':
g.edge(lastNode,currentNode)
else:
g.edge(lastNode,currentNode, constraint='false')
lastNode_val = 'if'
g.edge(currentNode,exp())
match('then','value')
g.edge(currentNode,stmt_sequence())
if token == 'else':
match('else','value')
g.edge(currentNode,stmt_sequence())
match('end','value')
def repeat_stmt():
global token
global tokenType
global currentNode
global lastNode
global index
global Temp
global lastNode_val
g.attr('node',shape='box')
if currentNode == None and lastNode == None:
currentNode = str('n' + str(index))
g.node(str('n' + str(index)),'repeat')
index = index + 1
else:
lastNode = currentNode
currentNode = str('n' + str(index))
g.node(str('n' + str(index)),'repeat')
index = index + 1
if lastNode_val == 'if' or lastNode_val == 'repeat':
pass
else:
g.edge(lastNode,currentNode, constraint='false')
lastNode_val = 'repeat'
match('repeat','value')
g.attr('node',rankdir='LR')
g.edge(currentNode,stmt_sequence())
match('until','value')
currentNode = Temp
g.edge(currentNode,exp())
def assign_stmt():
global token
global tokenType
global currentNode
global lastNode
global index
global Temp
global lastNode_val
tempId = token
match('identifier','type')
g.attr('node',shape='box')
if currentNode == None and lastNode == None:
currentNode = str('n' + str(index))
g.node(str('n' + str(index)),str('assign \n' + '(' + tempId + ')'))
index = index + 1
else:
lastNode = currentNode
currentNode = str('n' + str(index))
g.node(str('n' + str(index)),str('assign \n' + '(' + tempId +')'))
index = index + 1
if lastNode_val == 'if' or lastNode_val == 'repeat':
Temp = lastNode
lastNode_val = None
else:
g.edge(lastNode,currentNode, constraint='false')
match(':=','value')
g.edge(currentNode,exp())
return currentNode
def read_stmt():
global token
global tokenType
global currentNode
global lastNode
global index
global Temp
global lastNode_val
match('read','value')
if currentNode == None and lastNode == None:
currentNode = str('n' + str(index))
g.node(str('n' + str(index)),str('read \n' + '(' + token + ')'))
index = index + 1
else:
lastNode = currentNode
currentNode = str('n' + str(index))
g.node(str('n' + str(index)),str('read \n' + '(' + token +')'))
index = index + 1
if lastNode_val == 'if' or lastNode_val == 'repeat':
Temp = lastNode
lastNode_val = None
else:
g.edge(lastNode,currentNode, constraint='false')
match('identifier','type')
return currentNode
def write_stmt():
global token
global tokenType
global currentNode
global lastNode
global index
global Temp
global lastNode_val
g.attr('node',rankdir='LR',shape='box')
if currentNode == None and lastNode == None:
currentNode = str('n' + str(index))
g.node(str('n' + str(index)),'write')
index = index + 1
else:
lastNode = currentNode
currentNode = str('n' + str(index))
g.node(str('n' + str(index)),'write')
index = index + 1
if lastNode_val == 'if' or lastNode_val == 'repeat':
Temp = lastNode
lastNode_val = None
else:
g.edge(lastNode,currentNode, constraint='false')
match('write','value')
g.attr('node',shape='circle')
g.edge(currentNode,exp())
return currentNode
def exp():
global token
global tokenType
tokenTempCh1 = simple_exp()
if token == '<' or token == '=':
tokenTempP = comparison_op()
g.attr('node',rankdir='TB',shape='circle')
g.edge(tokenTempP,tokenTempCh1)
g.edge(tokenTempP,simple_exp())
return tokenTempP
return tokenTempCh1
def comparison_op():
global token
global tokenType
global index
if token == '<':
tokenTemp = str('n' + str(index))
g.attr('node',shape='circle')
g.node(str('n' + str(index)),str('op \n' + '(' + token + ')'))
index = index + 1
match('<','value')
return tokenTemp
elif token == '=':
tokenTemp = str('n' + str(index))
g.attr('node',shape='circle')
g.node(str('n' + str(index)),str('op \n' + '(' + token + ')'))
index = index + 1
match('=','value')
return tokenTemp
def simple_exp():
global token
global tokenType
tokenTemp = term()
while token == '+' or token == '-':
tokenTemp1 = addop()
g.edge(tokenTemp1,tokenTemp)
g.edge(tokenTemp1,term())
return tokenTemp1
return tokenTemp
def addop():
global token
global tokenType
global index
if token == '+':
tokenTemp = str('n' + str(index))
g.attr('node',shape='circle')
g.node(str('n' + str(index)),str('op \n' + '(' + token + ')'))
index = index + 1
match('+','value')
return tokenTemp
elif token == '-':
tokenTemp = str('n' + str(index))
g.attr('node',shape='circle')
g.node(str('n' + str(index)),str('op \n' + '(' + token + ')'))
index = index + 1
match('-','value')
return tokenTemp
def term():
global token
global tokenType
tokenTemp = factor()
while token == '*' or token == '/':
tokenTemp1 = mulop()
g.edge(tokenTemp1,tokenTemp)
g.edge(tokenTemp1,factor())
return tokenTemp1
return tokenTemp
def mulop():
global token
global tokenType
global index
if token == '*':
tokenTemp = str('n' + str(index))
g.attr('node',shape='circle')
g.node(str('n' + str(index)),str('op \n' + '(' + token + ')'))
index = index + 1
match('*','value')
return tokenTemp
elif token == '/':
tokenTemp = str('n' + str(index))
g.attr('node',shape='circle')
g.node(str('n' + str(index)),str('op \n' + '(' + token + ')'))
index = index + 1
match('/','value')
return tokenTemp
def factor():
global token
global tokenType
global index
if token == '(':
match('(','value')
exp()
match(')','value')
elif tokenType == 'Number':
tokenTemp = str('n' + str(index))
g.attr('node',shape='circle')
g.node(str('n' + str(index)),str('const \n' + '(' + token + ')'))
index = index + 1
match('Number','type')
return tokenTemp
elif tokenType == 'identifier':
tokenTemp = str('n' + str(index))
g.attr('node',shape='circle')
g.node(str('n' + str(index)),str('id \n' + '(' + token + ')'))
index = index + 1
match('identifier','type')
return tokenTemp
def match(matched,by):
'''
matched string input to check if it's equals to or equivalent to token
by to check by value of token or it's type
'''
global token
global tokenType
if by == 'value':
if token == matched:
getNext()
else:
raise Error('error : missmatching value of token check it again it excepted to be ' + matched)
elif by == 'type':
if tokenType == matched:
getNext()
else:
raise Error('error : missmatching type of token check it again it excepted to be ' + matched)
# In[ ]:
#programe ()