-
Notifications
You must be signed in to change notification settings - Fork 0
/
peginterpreterLabel.lua
321 lines (288 loc) · 7.33 KB
/
peginterpreterLabel.lua
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
-- Implementa a nova semântica de labels onde uma falha
-- é um par (label, restoDaEntrada)
local m = require "lpeg"
local compile = require 'peggrammar'
local first = require'first'
local makeord = compile.makeord
local makethrow = compile.makethrow
local makecon = compile.makecon
local makenot = compile.makenot
local makestar = compile.makestar
local makeopt = compile.makeopt
local makeplus = compile.makeplus
local makeany = compile.makeany
local makevar = compile.makevar
local fail = compile.fail
local calcfirst = first.calcfirst
local disjoint = first.disjoint
local set2choice = first.set2choice
local matchEmpty = first.matchEmpty
local calck = first.calck
local ierr
local gerr
local function adderror (p, flw)
local s = 'Err_' .. string.format("%03d", ierr)
local pred = makenot(set2choice(flw))
gerr[s] = makestar(makecon(pred, makeany()))
ierr = ierr + 1
return makeord(p, makethrow(s))
end
local function makeFailure (f, s)
return { f = f, s = s }
end
local function isSimple (p)
return p.kind == 'empty' or p.kind == 'char' or
p.kind == 'any' or p.kind == 'var' or
p.kind == 'throw'
end
local function rep_symb (p)
if p.kind == 'star' then
return '*'
elseif p.kind == 'plus' then
return '+'
else
return '?'
end
end
local function addlab_aux (g, p, seq, flw)
if ((p.kind == 'var' and not matchEmpty(p)) or p.kind == 'char' or p.kind == 'any') and seq then
return adderror(p, flw)
elseif p.kind == 'con' then
local newseq = seq or not matchEmpty(p.p1)
return makecon(addlab_aux(g, p.p1, seq, calck(g, p.p2, flw)), addlab_aux(g, p.p2, newseq, flw))
elseif p.kind == 'ord' then
local flagDisjoint = disjoint(calcfirst(p.p1), calck(g, p.p2, flw))
local p1 = p.p1
if flagDisjoint then
p1 = addlab_aux(g, p.p1, false, flw)
end
local p2 = addlab_aux(g, p.p2, false, flw)
if seq and not matchEmpty(p) then
return adderror(makeord(p1, p2), flw)
else
return makeord(p1, p2)
end
elseif (p.kind == 'star' or p.kind == 'opt' or p.kind == 'plus') and disjoint(calcfirst(p.p1), flw) then
local newp
--if seq then
--if true then
if false then
local p1 = addlab_aux(g, p.p1, false, flw)
local s = 'Err_' .. string.format("%03d", ierr) .. '_Flw'
gerr[s] = set2choice(flw)
newp = makecon(makenot(makevar(s)), adderror(p1, flw))
else
newp = addlab_aux(g, p.p1, false, flw)
end
if p.kind == 'star' then
return makestar(newp)
elseif p.kind == 'opt' then
return makeopt(newp)
else --plus
if seq then
return adderror(makeplus(newp), flw)
else
return makeplus(newp)
end
end
else
return p
end
end
local function addlab (g, flw, rules)
local newg = {}
ierr = 1
for i, v in ipairs(rules) do
newg[v] = addlab_aux(g, g[v], false, flw[v])
end
return newg
end
local function printg_aux (p)
if p.kind == 'empty' then
return "''" -- empty.1
elseif p.kind == "char" then
return "'" .. p.v .. "'"
elseif p.kind == 'any' then
return '.'
elseif p.kind == "con" then
local s1 = printg_aux(p.p1)
local s2 = printg_aux(p.p2)
local s = s1
if p.p1.kind == 'ord' and (p.p1.p2.kind ~= 'throw') then
s = '(' .. s .. ')'
end
if p.p2.kind == 'ord' and (p.p2.p2.kind ~= 'throw') then
s = s .. ' (' .. s2 .. ')'
else
s = s .. ' ' .. s2
end
return s
elseif p.kind == "ord" then
local s1 = printg_aux(p.p1)
local s2 = printg_aux(p.p2)
if p.p2.kind == 'throw' then
return '[' .. s1 .. ']^' .. string.sub(s2, 2)
else
return s1 .. ' / ' .. s2
end
elseif p.kind == "star" or p.kind == 'plus' or p.kind == 'opt' then
local s = printg_aux(p.p1)
if isSimple(p.p1) then
return s .. rep_symb(p)
else
return '(' .. s .. ')' .. rep_symb(p)
end
elseif p.kind == "not" then
local s = printg_aux(p.p1)
if isSimple(p.p1) then
return '!' .. s
else
return '!(' .. s .. ')'
end
elseif p.kind == "var" then
return p.v
elseif p.kind == "throw" then
return '%' .. p.v
else
print(p, p.kind)
error ("Regra desconhecida: " .. tostring(p))
end
end
local function printg (g, rules)
if rules then
for i, v in ipairs(rules) do
print(v, "<-", printg_aux(g[v]))
end
else
local t = {}
for k, _ in pairs(g) do
table.insert(t, k)
end
table.sort(t)
for i, v in ipairs(t) do
print(v, "<-", printg_aux(g[v]))
end
end
end
local function match(g, p, s, i)
--print("kind = ", p.kind)
if p.kind == 'empty' then
return i -- empty.1
elseif p.kind == "char" then
local n = #p.v
if string.sub(s, i, i + n - 1) == p.v then
return i + n -- ch.1
else
return nil, makeFailure(fail, s) -- ch.2, ch.3
end
elseif p.kind == 'any' then
if i <= #s then
return i + 1
else
return nil, makeFailure(fail, s)
end
elseif p.kind == "con" then
local j, f = match(g, p.p1, s, i)
if j == nil then
return nil, f -- con.3
end
return match(g, p.p2, s, j) -- con.1, con.2
elseif p.kind == "ord" then
local j, f = match(g, p.p1, s, i)
--print("ord ", j, f, p.f, f == p.f)
--if f ~= nil then print("f.f = ", f.f, f.f == p.f) end
if j ~= nil then
return j -- ord.1
elseif p.f ~= f.f then --ord.2
return nil, f
else
return match(g, p.p2, s, i) -- ord.3
end
elseif p.kind == "star" then
local j, f = match(g, p.p1, s, i)
if j == nil then
if f.f == fail then -- rep.1
return i
else -- rep.3
return nil, f
end
elseif j == i then -- rep.??
return i
else
local k, f = match(g, p, s, j)
-- could be just "return match(g, p, s, j)"
if k == nil then --rep.4
return k, f
else
return k, f--rep.2
end
end
elseif p.kind == "and" then
local j, f = match(g, p.p1, s, i)
if j == nil then
return nil, f -- and.1(?)
end
return i -- and.2(?)
elseif p.kind == "not" then
local j, f = match(g, p.p1, s, i)
if j == nil then
if f.f == fail then
return i -- not.1
else
return nil, f -- not.3
end
end
return nil, makeFailure(fail, s) -- not.2
elseif p.kind == "var" then
local v = g[p.v]
if v == nil then
error ("Gramática não possui regra " .. p.v)
end
return match(g, v, s, i)
elseif p.kind == "throw" then
--print("throw", p.v)
return nil, makeFailure(p.v, s)
else
print(p, p.kind)
error ("Regra desconhecida: " .. tostring(p))
end
end
local t = {}
function entry(e)
table.insert(t, e)
end
local input = ({...})[1] or "grammars.lua"
local f, e = loadfile(input)
if f then f () else error(e) end
-- for every grammar in table t
for _, e in ipairs(t) do
local g = compile.parse(e.g)
local rules = compile.getrules()
gerr = {}
--printg(g)
printg(compile.turnleft(g), rules)
local fst = first.calcFst(g)
local flw = first.calcFlw(g, e.s)
print("\nFOLLOW")
first.printfollow(g)
local newg = addlab(g, flw, rules)
print("NewG -----------")
printg(compile.turnleft(newg), rules)
printg(compile.turnleft(gerr))
--printg(newg)
g = compile.turnleft(g) -- makes "/"left associative
-- let's try to match several inputs
for _, v in ipairs(e.input) do
local inp = v[1] -- the subject
local res = v[2] -- the expected result
local n, f = match(g, compile.makevar(e.s), inp, 1)
print (inp, n)
if f ~= nil then
print("Label: " .. f.f, "Input: " .. f.s)
end
if n == nil then n = 0 end
assert(n == res,
"For input " .. inp .. " the expected result was " .. res .. " but we got " .. tostring(n))
end
print()
end