-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser_class.py
More file actions
executable file
·207 lines (172 loc) · 4.88 KB
/
Parser_class.py
File metadata and controls
executable file
·207 lines (172 loc) · 4.88 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
from in_out import *
import pygraphviz as pgv
Graph=pgv.AGraph()
index = 0
tokens = read_lines('testCase.txt')
def get_token():
global index
if(index < len(tokens)):
temp = index
index = index +1
return tokens[temp]
token = tokens[index]
index = index +1
count = 0
def ConnectHorizontal(firstNode,secondNode):
Graph.subgraph(nbunch=[firstNode,secondNode],rank= 'same')
Graph.add_edge(firstNode,secondNode)
def match(expected_token):
global token
token = get_token()
def factor():
global token
global Graph
global count
if (token == '('):
match('(')
ret = exp()
match(')')
else:
temp = token
match(token)
Graph.add_node(count,label = temp)
ret = Graph.get_node(count)
count += 1
return ret
def term():
temp = factor()
global token
global count
while (token == '*' or token == '/') :
#name = name + '1'
Graph.add_node(count,label = token)
parentnode = Graph.get_node(count)
count += 1
match(token)
leftchild = temp
rightchild =factor()
Graph.add_edge(parentnode,leftchild)
Graph.add_edge(parentnode,rightchild) #rightchild
temp = parentnode
return temp
def exp():
temp = term()
global token
global count
while (token == '+' or token == '-' ) :
Graph.add_node(count,label = token)
parentnode = Graph.get_node(count)
count += 1
match(token)
leftchild = temp
rightchild =term()
Graph.add_edge(parentnode,leftchild)
Graph.add_edge(parentnode,rightchild) #rightchild
temp = parentnode
if (token == '<' or token == '>'or token == '='):
Graph.add_node(count,label = token)
parentnode = Graph.get_node(count)
count += 1
match(token)
leftchild = temp
rightchild = exp()
Graph.add_edge(parentnode,leftchild)
Graph.add_edge(parentnode,rightchild)
return parentnode
else:
return temp
def Read():
global count
match('read')
if(isIdentfier(token)):
Graph.add_node(count,label = 'read \n'+token,shape='rectangle')
temp = Graph.get_node(count)
count +=1
match(token)
return temp
def Write():
global count
match('write')
temp1 = exp()
Graph.add_node(count, label ='write',shape='rectangle')
Graph.add_edge(count , temp1)
temp = Graph.get_node(count)
count+=1
match(token)
return temp
def ifStatement():
global count
global token
match('if')
Graph.add_node(count,label = 'if',shape='rectangle')
parentnode= Graph.get_node(count)
count +=1
leftchild=exp()
Graph.subgraph(nbunch=[leftchild],rank= 'same')
match('then')
rightchild=stmtSeq()
Graph.subgraph(nbunch=[leftchild,rightchild],rank= 'same')
Graph.add_edge(leftchild,rightchild,color='white')
Graph.add_edge(parentnode,leftchild)
Graph.add_edge(parentnode,rightchild)
if (token=='else'):
match('else')
elsechild = stmtSeq()
Graph.add_edge(parentnode,elsechild)
match('end')
return parentnode
def repeat():
global count
global token
match('repeat')
Graph.add_node(count,label = 'repeat',shape='rectangle')
parentnode= Graph.get_node(count)
count +=1
leftchild = stmtSeq()
match('until')
rightchild = exp()
Graph.subgraph(nbunch=[rightchild],rank = 'same')
Graph.add_edge(parentnode,leftchild)
Graph.add_edge(parentnode,rightchild)
return parentnode
def assign():
temp = exp()
global token
global count
while (token == ':=') :
Graph.add_node(count,label = token)
parentnode = Graph.get_node(count)
count += 1
match(token)
leftchild = temp
rightchild =exp()
Graph.subgraph(nbunch=[leftchild,rightchild],rank = 'same')
Graph.add_edge(leftchild,rightchild , color = 'white')
Graph.add_edge(parentnode,leftchild)
Graph.add_edge(parentnode,rightchild) #rightchild
temp = parentnode
return temp
def stmt():
if token == 'read':
temp = Read()
elif token == 'write':
temp = Write()
elif token == 'if':
temp = ifStatement()
elif token =='repeat':
temp = repeat()
else:
temp = assign()
return temp
def stmtSeq():
temp = stmt()
temp1 = temp
global count
while (token == ';'):
match(token)
temp2 = stmt()
ConnectHorizontal(temp1,temp2)
temp1 = temp2
return temp
stmtSeq()
Graph.draw('SyntaxTree.png',prog='dot')