-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexicalAnalyzer.rb
254 lines (236 loc) · 6.92 KB
/
LexicalAnalyzer.rb
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
#Code_by_HamzaEhteshamFarooq_aka_EhtesHamXa
DataType = ['int','char','string','double','bool']
Keywords = ['class','new','while','if','do','switch','default','else','case',
'break','goto','return','interface','sealed','static','continue',
'override','virtual','void','main','abstract','try','as','catch','throw','for']
Encap = ['public','private','proteced']
Access = ['this','base']
Seperator = [';','(',')','{','}','[',']']
Boolean = ['true','false']
Token = Array.new
Linea=1
def num (chk)
chk=~/[0-9]/
end
def ids (chak)
chak=~/[QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm_]/
end
def id (iden)
iden =~ /[_QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0-9]/
end
def ssss(stri)
ioa=0
#Code_by_HamzaEhteshamFarooq
ioa+=1
counti = stri.length()
while ioa<counti
if id (stri[ioa])
ioa+=1
if ioa == counti
Token.push("ID,#{stri},#{Linea}")
end
else
Token.push("Invalid Token,#{stri},#{Linea}")
break
end
end
end
def Analyzer(check)
if DataType.select{|word| word == check} !=[]
Token.push("DataType,#{check},#{Linea}")
elsif Keywords.select{|word| word == check} !=[]
Token.push("#{check},#{check},#{Linea}")
elsif Boolean.select{|word| word == check} !=[]
Token.push("Bool,#{check},#{Linea}")
#Code_by_HamzaEhteshamFarooq
elsif Access.select{|word| word == check} !=[]
Token.push("Access,#{check},#{Linea}")
elsif Encap.select{|word| word == check} !=[]
Token.push("Encap,#{check},#{Linea}")
elsif Seperator.select{|word| word == check} !=[]
Token.push("#{check},#{check},#{Linea}")
else
case check
when '++','--'
Token.push("Incdec,#{check},#{Linea}")
when '+','-'
Token.push("PM,#{check},#{Linea}")
when '*','/','%'
Token.push("MDM,#{check},#{Linea}")
when '=','+=','-=','*=','%=','/='
Token.push("ASSIOP,#{check},#{Linea}")
when '==','<','>','<=','>=','!='
Token.push("ROP,#{check},#{Linea}")
when '&&','||'
Token.push("GOP,#{check},#{Linea}")
when '&'
Token.push("ANO,#{check},#{Linea}")
when '|'
Token.push("ORO,#{check},#{Linea}")
when '!'
Token.push("NOP,#{check},#{Linea}")
when /\<\</
Token.push("SOP,#{check},#{Linea}")
when /\>\>/
Token.push("SOP,#{check},#{Linea}")
when ','
Token.push("\",\",\",\",#{Linea}")
else
ii=0
genti = check.length()
if num(check[0])
if genti==1
Token.push("Interger,#{check},#{Linea}")
else
ii+=1
while ii<genti
if num(check[ii])
ii+=1
if ii==genti
Token.push("Interger,#{check},#{Linea}")
end
else
Token.push("Invalid Token,#{check},#{Linea}")
break
end
end
end
elsif ids(check[0])
if check.length()==1
Token.push("ID,#{check},#{Linea}")
else
ssss(check)
end
else
Token.push("Invalid Token,#{check},#{Linea}")
end
end
end
end
def period(tokenback,tokenup)
Analyzer(tokenup)
tokenup=Token.pop()
takenup=tokenup.split(',')
tampa=tokenback.split(',')
if takenup[0]==tampa[0]
case tampa[0]
when 'Interger'
tokenup=tokenup.split(',')
tempa=tokenback.split(',')
Token.pop()
tempr = tempa[1]+'.'+tokenup[1]
Token.push("Decimal,#{tempr},#{Linea}")
else
Token.push("period,.,#{Linea}")
Token.push(tokenup)
end
else
Token.push("period,.,#{Linea}")
Token.push(tokenup)
end
end
code = File.read(%|C:\\Users\\Ehtes\\Documents\\class pro\\code.txt|)
text = code.split(/(\n)|(\\[\"\'rnbst\\])|(\/\/)|(\/\*)|(\*\/)|(\\)|( )|(\t)|(\&\&)|(\|\|)|(\+\+)|(\-\-)|([\+\-\*\/\%]\=)|(\!\=)|(\<\<)|(\>\>)|([\<\>]\=?)|(\=\=)|([\&\|\.\,\;\(\)\[\]\{\}\+\*\-\=\%\!\:\"\'\/])/)
puts text
count = text.length()
i=0
while i<count
temp =""
temp= text[i]
next i+=1 if temp==" " or temp == ""
next i+=1 if temp =="\t"
if temp =="\n"
Linea+=1
i+=1
next
elsif temp=="//"
i+=1
temp=""
while text[i]!="\n"
temp+=text[i]
i+=1
if i==count
break
end
end
i+=1
elsif temp=="/*"
i+=1
temp=""
while text[i]!="*/"
if text[i]=="\n"
Linea+=1
end
temp+=text[i]
i+=1
if i==count
break
end
end
i+=1
elsif temp=='\''
temp=""
ini = i
i+=1
while text[i]!='\'' and text[i]!="\n"
next i+=1 if text[i]==""
if temp==""
temp += text[i]
i+=1
else
break
end
end
if text[i]=='\''
Token.push("character,#{temp},#{Linea}")
i+=1
else
i=ini
Token.push("Invalid Token,#{text[i]},#{Linea}")
i+=1
end
elsif temp=="\""
temp=""
ini = i
i+=1
while text[i]!="\"" and text[i]!="\n"
if text[i]=="\\"
Token.push("Invalid Token,\\,#{Linea}")
end
temp +=text[i]
i+=1
if i>=count
break
end
end
if text[i]=="\""
Token.push("string,#{temp},#{Linea}")
i+=1
else
i=ini
Token.push("Invalid Token,#{text[i]},#{Linea}")
i+=1
end
elsif temp=='.'
i+=1
while text[i]=="" or text[i]==" " or text[i]=="\t"
i+=1
end
if text[i]=="."
puts "aasakdopaskdasd"
Token.push("period,.,#{Linea}")
Token.push("period,.,#{Linea}")
i+=1
else
period(Token[-1],text[i])
i+=1
end
else
Analyzer(temp)
i+=1
end
end
puts Token
puts text.length()
File.open("Token.csv", "w") { |f| f.write "#{Time.now} - User logged in\n" }
File.write("Token.csv", Token.join("\n"))