-
Notifications
You must be signed in to change notification settings - Fork 1
/
lex.py
173 lines (124 loc) · 3.96 KB
/
lex.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
import re
from sys import argv
#Removing comments
def removecomments():
with open ("input.c", "r") as myfile:
string=myfile.read()
string = re.sub(re.compile("[^\"]/\*.*?\*/",re.DOTALL ) ,"" ,string) # remove all occurance streamed comments (/*COMMENT */) from string
string = re.sub(re.compile("//.*?\n" ) ,"" ,string) # remove all occurance singleline comments (//COMMENT\n ) from string
#string = re.sub(re.compile("\n{2,}|[\t]+\n|[\t]+") ,"" ,string) # remove multiple spaces
string=string.split("\n")
with open("output","w+") as f:
for i in string:
i+="\n"
f.write(i.lstrip())
removecomments()
valueStack = []
def getNextToken():
with open ("output", "r") as myfile:
string=myfile.read()
string=string.replace(';'," ; ").replace("("," ( ").replace(")"," ) ").replace(","," , ").replace("--"," -- ").replace("++"," ++ ").replace("&"," & ").replace("|"," || ").replace("}"," } ").replace("{"," { ").replace("["," ] ")
if(re.findall("\s*\+\s*\+\s*\+\s*",string)):
x= re.findall("\s*\+\s*\+\s*\+\s*",string)
for i in x:
string=string.replace(i," ++ + ");
if(re.findall("\s*-\s*-\s*-\s*",string)):
x= re.findall("\s*-\s*-\s*-\s*",string)
for i in x:
string=string.replace(i," -- - ");
if(re.findall("\s*<\s*=\s*",string)):
x= re.findall("\s*<\s*=\s*",string)
for i in x:
string=string.replace(i," <= ");
if(re.findall("\s*>\s*=\s*",string)):
x= re.findall("\s*>\s*=\s*",string)
for i in x:
string=string.replace(i," >= ");
if(re.findall("\s*&\s*&\s*",string)):
x= re.findall("\s*&\s*&\s*",string)
for i in x:
string=string.replace(i," && ");
program = string.strip().split('\n')
#Token Names
header = set(['#include'])
datatype = {'int','float', 'char','long'}
symbols = set(['_','/','*','`','~','!','@','#','$','%','^','&','(',')','|','"',':',';','{'
,'}','[',']','<','>','?','/'])
operator = { '=' , '+', '-', '/' , '*', '++' , '--','<=','<' ,">=","&&" ,'||','|','&'}
keyword=set(["return"])
startQuote = "\""
lineno =[]
for i in range(len(program)):
lis=list(program[i])
loop=0;
lol=False
change = False;
while(loop != len(lis)):
#print(lis[loop],lis[loop]=="\"",lol)
if(lis[loop]=="\""):
if(lol==False and change != True ):
lol=True
change=True;
if(lol==True and change != True):
lol=False
change = True
if(lol==True):
if(lis[loop]==" "):
lis[loop]="_"
if(change == True):
change=False
loop +=1
program[i] = "".join(lis);
sp = program[i].split(' ')
sp = list(map(lambda x: (x,i) ,sp))
lineno += sp;
x=len(lineno)
cnt=0;
symbolTable=[]
idNo=1;
while(cnt != x):
tok=lineno[cnt][0];
if(tok in header):
cont=tok;
if(cnt+1 < x and re.search('<.*>',lineno[cnt+1][0]).group(0) ):
cnt +=1;
cont += lineno[cnt][0];
elif(cnt+1 < x and '<' in lineno[cnt+1][0] ):
cnt +=1;
cont += lineno[cnt][0];
if(cnt+1 < x and re.search('.*\.h',lineno[cnt+1][0]).group(0) ):
cnt +=1;
cont += lineno[cnt][0];
if(cnt+1 < x and '>' in lineno[cnt+1][0] ):
cnt +=1;
cont += lineno[cnt][0];
symbolTable.append(("HEADER",cont,lineno[cnt][1]));
elif(tok in datatype):
symbolTable.append(("DATA_TYPE",tok,lineno[cnt][1]))
elif(tok in symbols):
symbolTable.append(("SYMBOL",tok,lineno[cnt][1]));
elif(tok in operator):
symbolTable.append(("OPERATOR",tok,lineno[cnt][1]));
elif("\"" in tok):
symbolTable.append(("STRING",tok.replace("_"," "),lineno[cnt][1]))
elif(tok in keyword):
symbolTable.append(("KEYWORD",tok.replace("_"," "),lineno[cnt][1]))
else:
if(tok != ""):
idn = idNo
Found=False
for i in symbolTable:
if(i[0] == "ID"):
if(tok == i[1]):
Found=True
idn=i[2]
symbolTable.append( (("ID"),tok,idn,lineno[cnt][1] ))
if(Found==True):
Found =False
else:
idNo += 1
cnt +=1;
for x in range(len(symbolTable)):
print(symbolTable[x])
getNextToken();
#Hi!