-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexicalAnalyzer.py
209 lines (162 loc) · 7.85 KB
/
lexicalAnalyzer.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
import re
import time
import parser
import variables
print "Name: Navin Soni, Email: nsoni@clemson.edu"
print "Name: Vishwanath Chennuru, Email: vchennu@clemson.edu"
print "Timestamp :",time.asctime(time.localtime(time.time()))
identifier = {'start':'1', 'prog':'2', 'body':'3', 'declpart':'4', 'decllist':'5', 'declstat':'6', 'type':'7', 'procpart':'8', 'proclist':'9', 'proc':'10', 'prochead':'11', 'procname':'12', 'fparmlist':'13', 'calltype':'14', 'execpart':'15', 'exechead':'16', 'statlist':'17', 'stat':'18', 'inputstat':'19', 'outputstat':'20', 'callstat':'21', 'callname':'22', 'aparmlist':'23', 'ifstat':'24', 'ifthen':'25', 'ifhead':'26', 'whilestat':'27', 'whileexpr':'28', 'whilehd':'29', 'astat':'30', 'bexpr':'31', 'andexpr':'32', 'notexpr':'33', 'relexpr':'34', 'aexpr':'35', 'term':'36', 'primary':'37', 'constant':'38','var':'41','integer':'44','string':'59','real':'83'}
keyword = {'END':'39', 'PROGRAM':'40', 'DECLARE':'42', 'REAL':'47', 'INTEGER':'46', 'PROCEDURE':'49', 'VALUE':'52', 'REFERENCE':'53', 'MAIN':'54', 'INPUT':'58', 'OUTPUT':'60', 'CALL':'61', 'ELSE':'62', 'IF':'63', 'THEN':'66', 'DO':'67', 'WHILE':'68'}
single_ascii = {';':'43',':':'57',',':'50','[':'55',']':'56','(':'64',')':'65','<':'73','>':'75','!':'72','+':'79','-':'80','*':'81','/':'82','{':'51','}':'48','|':'70','&':'71'}
multi_ascii = {'==':'77', '!=':'78', '<=':'74', '>=':'76', '<-':'69', '::':'45'}
code = ['59','83','44',single_ascii,multi_ascii,'41',keyword,'85']
def flags(each_line, flag):
if re.search('\+',each_line) :
flag[int(each_line.split('+')[1])] = True
else :
flag[int(each_line.split('-')[1])] = False
comment = False
hash_c = False
string = False
far = '{:>60}'
conditions = re.compile('(\".*?\")|(\++\d+\.+\d+|\d+\.+\d+|\-+\d+\.+\d+)|(\-+\d+|\++\d+|\d+)|(==|!=|<=|>=|<-|::)|(//|/\*|\*/|##|#|\"|;|:|\[|\]|<|>|!|\+|-|\*|/|{|}|\||&|\(|\)|\.|`|~|\@|=|,)|([a-z][a-zA-Z0-9_]*)|([a-z]+)|([A-Z]+)')
input_file = raw_input("Enter filename: ")
try:
input_data = open(input_file)
except:
print "File not present"
exit()
queue = []
for input_line in input_data:
if variables.flag[1] == True:
print input_line
line_check = conditions.findall(input_line)
for each_line in line_check:
i=0;
for each in each_line:
if(each!=''):
break;
else:
i=i+1;
dat = ''
if each_line[i] == '/*' :
if comment == False :
comment = True
if each_line[i] == '*/' :
if comment == True :
comment = False
if each_line[i] == '//':
break
if each_line[i] == '##' and comment == False:
if hash_c == False :
hash_c = True
else :
hash_c = False
if (hash_c == True and each_line[i] != "##"):
if re.search('\+[0-9]+|\-[0-9]+', each_line[i]) :
flags(each_line[i],variables.flag)
if (hash_c == False and re.search('\+', each) and comment == False):
if (i==1 or i==2):
dat = "Token : + Code : 79"
queue.append(79)
variables.var.append('+')
if(variables.flag[2] == True):
print far.format(dat)
each = each.split('+')[1]
if (hash_c == False and comment == False and not re.search('##', each_line[i]) and each != '*/'):
if i == 0:
dat = "Token : "+each+" Code : "+code[i];
queue.append(int(code[i]))
variables.var.append(each)
if(variables.flag[2] == True):
print far.format(dat)
if i == 1:
cr = re.compile('[1-9].*[1-9]')
try :
fr = cr.findall(each)[0]
except:
fr =''
if(re.search('\.', fr)) and len(fr)<9 :
dat = "Token : "+each+" Code : "+code[i];
queue.append(int(code[i]))
variables.var.append(each)
if(variables.flag[2] == True):
print far.format(dat)
elif len(fr)<8 :
dat = "Token : "+each+" Code : "+code[i];
queue.append(int(code[i]))
variables.var.append(each)
if(variables.flag[2] == True):
print far.format(dat)
else:
dat = "Invalid Real Number : "+each
if(variables.flag[2] == True):
print far.format(dat)
if i == 2 and len(each)<10:
dat = "Token : "+each+" Code : "+code[i];
queue.append(int(code[i]))
variables.var.append(each)
if(variables.flag[2] == True):
print far.format(dat)
elif i == 2 and len(each)<11 and re.search('\-', each):
dat = "Token : "+each+" Code : "+code[i];
queue.append(int(code[i]))
variables.var.append(each)
if(variables.flag[2] == True):
print far.format(dat)
elif i==2:
dat = "Invalid Number : "+each
if(variables.flag[2] == True):
print far.format(dat)
if i == 3 and each in multi_ascii:
dat = "Token : "+each+" Code : "+multi_ascii[each];
queue.append(int(multi_ascii[each]))
variables.var.append(each)
if(variables.flag[2] == True):
print far.format(dat)
elif i == 3:
dat = "Invalid Multi Ascii : "+each;
if(variables.flag[2] == True):
print far.format(dat)
if i == 4 and each in single_ascii:
dat = "Token : "+each+" Code : "+single_ascii[each];
queue.append(int(single_ascii[each]))
variables.var.append(each)
if(variables.flag[2] == True):
print far.format(dat)
elif i == 4:
dat = "Invalid Single Ascii : "+each;
if(variables.flag[2] == True):
print far.format(dat)
if i == 5 and len(each)<17:
dat = "Token : "+each+" Code : "+code[i];
queue.append(int(code[i]))
variables.var.append(each)
if(variables.flag[2] == True):
print far.format(dat)
elif i == 5:
dat = "Invalid Identifier : "+each;
if(variables.flag[2] == True):
print far.format(dat)
if i == 7 and each in keyword:
dat = "Token : "+each+" Code : "+keyword[each];
queue.append(int(keyword[each]))
variables.var.append(each)
if(variables.flag[2] == True):
print far.format(dat)
elif i == 7:
dat = "Invalid Keyword : "+each;
if(variables.flag[2] == True):
print far.format(dat)
if (len(queue) > 0):
parser.reduction(queue[:]) # Sending symbol queue for reduction if the queue is not empty
queue = [] # Resetting the queue
if comment == False:
print
#print
parser.reduction([])
'''print "\nLocal symbol table: "
for each in range(0,len(variables.localSymbolTable),2):
print '\n',variables.localSymbolTable[each],variables.localSymbolTable[each+1]'''
print '\n',variables.globalSymbolTable
print '\n',variables.localSymbolTable