-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateSymbolTableLatex.py
323 lines (219 loc) · 10.4 KB
/
GenerateSymbolTableLatex.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
from tabulate import tabulate
def name( obj ):
return type(obj).__name__
class SymbolTree():
def __init__(self):
self.node_dict = {}
self.root = None
# Intrinsic Procedures in Fortran
intrinsic_proc = { 'dot_product' : 'Function' , 'matmul' : 'Function' , 'mod' : 'Function' , 'modulo' : 'Function' , 'int' : 'Function' , 'real' : 'Function' , 'max' : 'Function' , 'min' : 'Function' , 'product' : 'Function' , 'random_number' : 'Subroutine' , 'random_seed' : 'Subroutine' , 'shape' : 'Function' , 'size' : 'Function' , 'sum' : 'Function' , 'not' : 'Function' , 'abs' : 'Function' , 'allocated' : 'Function' , 'any' : 'Function' , 'ceiling' : 'Function', 'dble' : 'Function' , 'epsilon' : 'Function' , 'exp' : 'Function' , 'exponent' : 'Function' , 'floor' : 'Function' , 'kind' : 'Function' , 'log' : 'Function' , 'log10' : 'Function' , 'sqrt' : 'Function' , 'transpose' : 'Function' , 'norm2' : 'Function' }
self.intrinsic_proc = SymbolTable()
# Adding intrinsic procedures in symbol table
for proc in intrinsic_proc:
self.intrinsic_proc.AddSymbol((proc,intrinsic_proc[proc]))
# Adds new scope to the Symbol tree
def AddNode(self,node):
self.node_dict[node.scope] = node
def GST_util( self , head , parent ):
ntype = type(head).__name__
if( ntype == 'Execution_Program' ) :
program_units = head.program_units
table = SymbolTable()
children = {}
for program_unit in program_units:
title = program_unit.title
# Adding program unit to symbol table
table.AddSymbol(program_unit)
children[title] = self.GST_util(program_unit,'global')
curr_node = Node('global',table,parent,children)
self.AddNode(curr_node)
return curr_node
elif( ntype == 'Main' or ntype == 'Function' or ntype == 'Subroutine' or ntype == 'Module' ) :
specs = head.specs
table = SymbolTable()
children = {}
internal_subprograms = head.internal_subprograms
for declaration in specs.dec_stmts:
# Adding variable declaration to symbol table of scope of program unit
table.AddSymbol(declaration)
for subprogram in internal_subprograms:
title = subprogram.title
table.AddSymbol(subprogram)
children[title] = self.GST_util(subprogram,head.title)
curr_node = Node(head.title,table,parent,children)
self.AddNode(curr_node)
return curr_node
def GenerateSymbolTable( self , head , parent ):
self.root = self.GST_util(head,parent)
def Lkp_util(self,curr_node,Id):
# Searches id in the current scope
# If found then returns the reference else searches in the parent scope
table = curr_node.table
lkp = table.Lookup(Id)
if( lkp is not None ): return lkp
if( curr_node.parent is None ):
# Id not found even in root scope therefore search in intrinsic procedures
lkp = self.intrinsic_proc.Lookup(Id)
if ( lkp is not None ): return lkp
return None
return self.Lkp_util(self.node_dict[curr_node.parent],Id)
def Lookup(self,scope,Id):
curr_node = None
if( scope in self.node_dict ):
curr_node = self.node_dict[scope]
else:
return None
return self.Lkp_util(curr_node,Id)
def PrintSymbolTable( self, stfile ,root = None ):
# In order printing of symbol tree
if ( root is None ):
root = self.root
stfile.write('\\begin{center}\n')
stfile.write('\\begin{longtable}{|p{3.5cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}|p{1cm}|p{2cm}|p{4cm}| }\n')
stfile.write('\hline\n')
if(root.scope.find("_") != -1):
idx = root.scope.index("_")
root.scope = root.scope[:idx] + "\\" + root.scope[idx:]
if ( root.scope == 'global' ):
stfile.write('\multicolumn{7}{|c|}{\\textbf{Scope : } \\textbf{\\textit{' + root.scope + '}}} \\\ \n')
else :
if(self.node_dict[root.parent].table.Lookup(root.scope) == None):
stfile.write('\multicolumn{7}{|c|}{\\textbf{Scope} - \\textbf{\\textit{' + root.scope + '}}} \\\ \n')
else:
stfile.write('\multicolumn{7}{|c|}{\\textbf{Scope : \qquad} \\textbf{\\textit{' + root.scope +\
' - }' + self.node_dict[root.parent].table.Lookup(root.scope)['type'] + '}}\\\ \n')
stfile.write('\hline\n')
root.table.PrintTable(stfile)
children = root.children
stfile.write('\end{longtable}\n')
stfile.write('\end{center}\n')
stfile.write('\n \\vspace{1cm}\n\n')
for title,child in children.items() :
self.PrintSymbolTable(stfile,child)
class SymbolTable():
def __init__(self):
self.table = {}
# Columns in symbol table
self.headers = [ 'Id' , 'type' , 'precision' , 'Dtype' , 'ndim' , 'Attributes' , 'ref' ]
def CreateRow(self):
# create empty row
row = {}
for col in self.headers:
row[col] = None
return row
def AddSymbol(self,node):
# Adding symbols to symbol table
ntype = type(node).__name__
table = self.table
if ( ntype == 'tuple' ):
Id = node[0]
node_type = node[1]
table[Id] = self.CreateRow()
table[Id]['Id'] = Id
table[Id]['type'] = node_type
table[Id]['ref'] = node
elif ( ntype == 'Main' ):
title = node.title
table[title] = self.CreateRow()
table[title]['Id'] = title
table[title]['type'] = 'Main'
table[title]['ref'] = node
elif ( ntype == 'Function' ):
title = node.title
result = node.result
table[title] = self.CreateRow()
table[title]['Id'] = title
table[title]['type'] = 'Function'
table[title]['Attributes'] = 'result(' + result + ')'
table[title]['ref'] = node
elif ( ntype == 'Subroutine' ):
title = node.title
table[title] = self.CreateRow()
table[title]['Id'] = title
table[title]['type'] = 'Subroutine'
table[title]['ref'] = node
elif ( ntype == 'Module' ):
title = node.title
table[title] = self.CreateRow()
table[title]['Id'] = title
table[title]['type'] = 'Module'
table[title]['ref'] = node
elif ( ntype == 'Declaration' ):
Dtype = repr(node.Type)
Attributes = node.Attributes
Attribute_lis = [ repr(attr) for attr in Attributes ]
Var_lis = node.Var_lis
for var in Var_lis:
Id = var.name
vtype = type(var).__name__
table[Id] = self.CreateRow()
table[Id]['Id'] = Id
table[Id]['Dtype'] = Dtype
table[Id]['type' ] = vtype
if ( node.Type.size ):
table[Id]['precision'] = node.Type.size
elif ( node.Type.name == 'real' or node.Type.name == 'integer' ):
# default precision if precision not provided
table[Id]['precision'] = '4'
if ( vtype == 'Array' ):
table[Id]['ndim'] = len(var.dim_lis)
table[Id]['Attributes'] = Attribute_lis
table[Id]['ref'] = node
elif ( ntype == 'Interface module' ):
title = node.title
table[title] = self.CreateRow()
table[title]['Id'] = title
table[title]['type'] = 'Interface'
table[title]['ref'] = node
elif ( ntype == 'Interface_argument' ):
title = node.title
if ( title is not None ):
table[title] = self.CreateRow()
table[title]['Id'] = title
table[title]['type'] = 'Interface'
table[title]['ref'] = node
for proc in node.proc_lis :
self.AddSymbol(proc)
def Lookup(self,Id):
if(Id in self.table):
return self.table[Id]
else:
return None
def check(string, sub_str):
if (string.find(sub_str) == -1):
print("NO")
else:
print("YES")
def PrintTable(self,stfile):
table = self.table
table_lis = []
for key in table:
table_lis.append(list(table[key].values()))
for i in range(len(self.headers)-1):
stfile.write('\\textbf{'+self.headers[i]+'} & ')
stfile.write('\\textbf{'+self.headers[-1]+'} \\\\\hline\n\n')
for list_lis in table_lis:
for elem in list_lis[:-1]:
if elem:
# stfile.write('%s & '% elem)
if (isinstance(elem, str)):
if(elem.find("_") == -1):
stfile.write('%s & '% elem)
else:
idx = elem.index("_")
elem = elem[:idx] + "\\" + elem[idx:]
stfile.write('%s & '% elem)
else:
stfile.write('%s & '% elem)
else:
stfile.write(' & ')
stfile.write('%s \\\\\hline\n\n'% list_lis[-1])
# print(table_lis)
# print('\n\n\n')
# Types - Main , Subroutine , Function , Scalar , Array
class Node():
def __init__(self,scope,table,parent,children):
self.scope = scope
self.table = table
self.parent = parent
self.children = children