1

  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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
require 'set'
class Grammardef
def initialize
@rules = Hash.new()
end
def setexiom(axiom)
@axiom = axiom
print "axiom:\n", @axiom, "\n", "setted\n"
end
def setnterm(nterms)
@nterms = nterms.split(/ +/).delete_if { |x| x == "" }
@nterms << @axiom
#@nterms = @nterms.sort { |x,y| y <=> x }
print "nterms:\n", @nterms, "\n", "setted\n"
end
def setterm(terms)
@terms = terms.split(/ +|\"/).delete_if { |x| x == "" }
print "terms:\n", @terms, "\n", "setted\n"
end
def setrules(rules)
#print "rules:\n", rules.split(/( *|\\n)\$RULE /).delete_if {|x| (x == "") || (x == "\\n")}, "\n", "setted\n"
rulesregexp = Regexp.new(/\$RULE (([A-Z]\'{0,1}) =(([( [A-Z]\'{0,1})( \"[a-z]\"| \"\*\"| \"\+\"| \"\(\"| \"\)\")]+\\n| \$EPS\\n)+))/)
a = rulesregexp.match(rules)[0]
#print "rules:\n", a, "\n"
self.setrule(a)
while rulesregexp.match(rules) != nil
rules = rules[rulesregexp.match(rules)[0].length, rules.length]
if (rulesregexp.match(rules) != nil)
a = rulesregexp.match(rules)[0]
#print a, "\n"
self.setrule(a)
end
end
print "rules hash:\n", @rules, "\n"
end
def setrule(rule)
ruleregexp = Regexp.new(/\$RULE (([A-Z]\'{0,1}) =(([( [A-Z]\'{0,1})( \"[a-z]\"| \"\*\"| \"\+\"| \"\(\"| \"\)\")]+\\n| \$EPS\\n)+))/)
leftpart = ruleregexp.match(rule)[2]
rightparts = ruleregexp.match(rule)[3]
#print "rule left:\n", leftpart, "\nrule rights:\n", rightparts, "\n"
rightpartsarr = rightparts.split(/\\n/).delete_if { |x| x == "" }
#print "right parts array\n", rightpartsarr, "\n******************************************************\n"
rightpartsset = Set.new()
(rightpartsarr).each do |x|
onealt = x.split(/ +|\"/).delete_if { |x| x == "" }
rightpartsset.add(onealt)
#print onealt, "\n******************************************************\n"
@rules[leftpart] = rightpartsset
end
end
def axiom
@axiom
end
def nterms
@nterms
end
def terms
@terms
end
def rules
@rules
end
def rulebynterm(nterm)
@rules[nterm]
end
def arrtostr(x)
#print x, " in arrtostr\n"
str = ""
x.each { |x| str += x }
str
end
def phi(seq)
#print "in phi", seq, "\n"
if @nterms.include?(seq[0]) && seq.length == 1
return @FIRST[seq[0]]
end
if seq[0] == "$EPS"
retset = (Set.new() << "$EPS")
return retset
end
if @terms.include?(seq[0])
retset = (Set.new() << seq[0])
return retset
end
if @nterms.include?(seq[0])
if !@FIRST[seq[0]].include?("$EPS")
return @FIRST[seq[0]]
else
epsset = (Set.new() << "$EPS")
subarr = seq[1 .. seq.length - 1]
#print "subarr in phi: ", subarr, "\n"
if subarr.length == 0
retset = @FIRST[seq[0]] - epsset
else
retset = (@FIRST[seq[0]] - epsset) + phi(subarr)
end
return retset
end
end
print "no arr in phi"
end
def FIRSTF()
@FIRST = Hash.new
(@nterms).each do |x|
@FIRST[x] = Set.new
end
begin
@FIRSTold = @FIRST.clone
(@nterms).each do |x|
(@rules[x]).each do |y|
@FIRST[x] += self.phi(y)
end
end
end while @FIRSTold != @FIRST
#print "FIRST\n"
#@FIRST.each {|key, value| @FIRST[key] ; print "#{key} is ", value.inspect, "\n" }
(@nterms).each do |x|
test1 = @FIRST[x]
x1 = [x]
test2 = phi(x1)
#print test1.inspect, " ", test2.inspect, "\n"
end
print "first hash:\n", @FIRST, "\n"
end
def FOLLOWF()
@FOLLOW = Hash.new
(@nterms).each do |x|
@FOLLOW[x] = Set.new
end
@FOLLOW[@axiom] << "$"
(@nterms).each do |x|
(@rules[x]).each do |y|
#print x, ":", y, "\n"
(y).each_index do |z|
#print z, "\n"
if @nterms.include?(y[z])
if z != y.length - 1
#print "Y: ", y[z], " v: ", y[z+1 .. y.length - z], "\n"
@FOLLOW[y[z]] += (phi(y[z+1 .. y.length - z]) - ["$EPS"])
else
#print "Y: ", y[z], " v: ", [], "\n"
end
end
end
end
end
begin
@FOLLOWold = @FOLLOW.clone
#print "iter\n"
(@nterms).each do |x|
(@rules[x]).each do |y|
(y).each_index do |z|
if @nterms.include?(y[z])
if z != y.length - 1
#print "Y: ", y[z], " v: ", y[z+1 .. y.length - z], "\n"
#@FOLLOW[y[z]] += (phi(y[z+1 .. y.length - z]) - ["$EPS"])
#print phi(y[z+1 .. y.length - z]).inspect, "beforeepsif\n"
if phi(y[z+1 .. y.length - z]).include?("$EPS")
#print phi(y[z+1 .. y.length - z]).inspect, "\n"
#print "INCLUDE EPS follow y: ", @FOLLOW[y[z]].inspect, "follow x: ", @FOLLOW[x].inspect, "\n"
@FOLLOW[y[z]] += @FOLLOW[x]
end
else
#print "Y: ", y[z], " v: ", [], "\n"
#print"y: ",y[z], "follow y: ", @FOLLOW[y[z]].inspect, "follow x: ", @FOLLOW[x].inspect, "\n"
@FOLLOW[y[z]] += @FOLLOW[x]
end
end
end
end
end
end while @FOLLOWold != @FOLLOW
print "follow hash:\n", @FOLLOW, "\n"
end
def settable
#print "starting settable\n"
@table = Hash.new()
(@nterms).each do |x|
(@terms).each do |y|
#print "nterm: ", x, " term: ", y, "\n"
@table[[x, y]] = ["ERROR"]
end
@table[[x, "$"]] = ["ERROR"]
end
(@nterms).each do |x|
(@rules[x]).each do |y|
#print y, "--FIRST--", self.phi(y).inspect, "\n"
(self.phi(y)).each do |z|
#print @table[[x, z]], "\n"
#@table[[x, z]] = y
if self.phi(y).include?("$EPS")
#print "include", self.phi(y).inspect, "\n"
(@FOLLOW[x]).each do |b|
#print b, "\n"
if @table[[x, b]] == ["ERROR"]
@table[[x, b]] = y
else
@table[[x, b]] += y
end
end
else
#print "not include", self.phi(y).inspect, "\n"
if @table[[x, z]] == ["ERROR"]
@table[[x, z]] = y
else
@table[[x, z]] += y
end
end
end
end
end
#print "table:\n", @table, "\n"
mas2 = ["+", "*", "n", "(", ")", "$"]
mas1 = ["E", "E'", "T", "T'", "F"]
(mas1).each do |x|
(mas2).each do |y|
#print x, " ", y, " ", @table[[x, y]], "\n"
end
end
end
def topdownparse(x)
print "\n\n\nstarting topdown\n\n\n"
@terms << "$EPS"
mag = []
mag.push("$")
mag.push(@axiom)
a = x.shift
x1 = "random"
while x1 != "$" do
x1 = mag.pop
if x1 == "$"
if x == []
break
else
print "error0\n"
break
end
end
if (@terms).include?(x1)
if x1 == a
a = x.shift
else
if x1 != "$EPS"
print "error1\n"
break
else
next
end
end
else
if a == nil
a = "$"
end
if @table[[x1, a]] != ["ERROR"]
mas = @table[[x1, a]]
mas.reverse!
mag += mas
print "rule: ", x1, " -> ", mas.reverse!, "\n"
else
print "error2\n"
break
end
end
end
end
end
class Progreader
def initialize(exampleprog)
#exampleprog = "$AXIOM E\\n$NTERM E' T T' F\\n$TERM \"+\" \"*\" \"(\" \")\" \"n\"\\n$RULE E = T\\n$RULE E' = \"+\" T E'\\n $EPS\\n"
progregexp = Regexp.new(/\$AXIOM ([A-Z0-9']+)\\n\$NTERM(( [A-Z0-9']+)+)\\n\$TERM( ".+")+\\n((\$RULE [A-Z0-9']+ =(( ([A-Z0-9'\$]|(".+"))+)+\\n)+)+)/)
prog = progregexp.match(exampleprog)
@arifmGrammar = Grammardef.new
#print "\n$$$allprogram:\n", prog, "\n"
#print "axiom:\n", prog[1], "\n"
@arifmGrammar.setexiom(prog[1])
#print "nterm:\n", prog[2], "\n"
@arifmGrammar.setnterm(prog[2])
#print "term:\n", prog[4], "\n"
@arifmGrammar.setterm(prog[4])
#print "rules:\n", prog[6], "\n"
@arifmGrammar.setrules(prog[5])
@arifmGrammar.FIRSTF
@arifmGrammar.FOLLOWF
@arifmGrammar.settable
#@arifmGrammar.topdownparse(["(" ,"n", "+", "n", ")"])
end
def arifmGrammar
@arifmGrammar
end
end
class Filecustomreader
def initialize(filename)
#read from file
file = File.new(filename, "r")
@mygram = "";
while line = file.gets
@mygram += line.chomp + "\\n"
end
@mygram = @mygram[0, @mygram.length - 0]
#print @mygram
#print "\nqqqqq\n"
end
def mygram
@mygram
end
end
#exampleprog = "$AXIOM E\\n$NTERM E' T T' F\\n$TERM \"+\" \"*\" \"(\" \")\" \"n\"\\n$RULE E = T E'\\n$RULE E' = \"+\" T E'\\n $EPS\\n$RULE T = F T'\\n$RULE T' = \"*\" F T'\\n $EPS\\n$RULE F = \"n\"\\n \"(\" E \")\"\\n"
#progreader = Progreader.new(exampleprog)
reader = Filecustomreader.new("/Users/vladimir/Desktop/tablelab7/gramgram.txt")
progreader = Progreader.new(reader.mygram)
arifmGrammar = progreader.arifmGrammar
#print arifmGrammar, "\n"