-
Notifications
You must be signed in to change notification settings - Fork 0
/
first.lua
379 lines (336 loc) · 7.75 KB
/
first.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
local lpeg = require"lpeg"
local compile = require'peggrammar'
local FIRST
local FOLLOW
local empty = ''
local calcf
local makelit = compile.makelit
local makeord = compile.makeord
local printfirst, calcfirst, calck
local function disjoint (s1, s2)
for k, _ in pairs(s1) do
if s2[k] then
return false
end
end
return true
end
local function equalSet (s1, s2)
for k, _ in pairs(s1) do
if not s2[k] then
return false
end
end
for k, _ in pairs(s2) do
if not s1[k] then
return false
end
end
return true
end
local function union (s1, s2, notEmpty)
local s3 = {}
local eq = true
for k, _ in pairs(s1) do
s3[k] = true
if not s2[k] then
eq = false
end
end
for k, _ in pairs(s2) do
s3[k] = true
if not s1[k] then
eq = false
end
end
if notEmpty then
s3[empty] = nil
end
return s3, eq
end
local function sortset(s)
local r = {}
for k, _ in pairs(s) do
table.insert(r, k)
end
table.sort(r)
return r
end
local function set2choice (s)
local p
local r = sortset(s)
for i, v in ipairs(r) do
if not p then
p = makelit(v)
else
p = makeord(makelit(v), p)
end
end
return p
end
local function matchEmpty (p)
if p.kind == 'empty' or p.kind == 'star' or
p.kind == 'not' or p.kind == 'and' or p.kind == 'opt' then
return true
elseif p.kind == 'char' or p.kind == 'plus' or p.kind == 'any' then
return false
elseif p.kind == 'ord' then
return matchEmpty(p.p1) or matchEmpty(p.p2)
elseif p.kind == 'con' then
if matchEmpty(p.p1) then
return matchEmpty(p.p2)
else
return false
end
elseif p.kind == 'var' then
return FIRST[p.v][empty]
else
error("Unknown kind " .. tostring(p.kind))
end
end
local function writepeg (p, iscon)
if p.kind == 'char' then
return "'" .. p.v .. "'"
elseif p.kind == 'empty' then
return "''"
elseif p.kind == 'any' then
return "."
elseif p.kind == 'var' then
return p.v
elseif p.kind == 'ord' then
local s1 = writepeg(p.p1, false)
local s2 = writepeg(p.p2, false)
if iscon then
return '(' .. s1 .. " / " .. s2 .. ')'
else
return s1 .. " / " .. s2
end
elseif p.kind == 'con' then
return writepeg(p.p1, true) .. " " .. writepeg(p.p2, true)
elseif p.kind == 'and' then
return '&(' .. writepeg(p.p1) .. ')'
elseif p.kind == 'not' then
return '!(' .. writepeg(p.p1) .. ')'
elseif p.kind == 'opt' then
return writepeg(p.p1) .. '?'
elseif p.kind == 'star' then
return writepeg(p.p1) .. '*'
elseif p.kind == 'plus' then
return writepeg(p.p1) .. '+'
else
error("Unknown kind: " .. p.kind)
end
end
local function makepeg (p)
if p.kind == 'char' then
return lpeg.P(p.v)
elseif p.kind == 'empty' then
return lpeg.P""
elseif p.kind == 'ord' then
return makepeg(p.p1) + makepeg(p.p2)
elseif p.kind == 'con' then
return makepeg(p.p1) * makepeg(p.p2)
elseif p.kind == 'var' then
return lpeg.V(p.v)
elseif p.kind == 'and' then
return #makepeg(p.p1)
else
error("Unknown kind: " .. p.kind)
end
end
local function printfollow (g)
for k, v in pairs(g) do
local s = k .. ':'
local fst = calcfirst(v)
local r = sortset(FOLLOW[k])
for _, v1 in ipairs(r) do
s = s .. ' ' .. v1
end
print(s)
print("FIRST")
printfirst(fst)
end
end
function printfirst (t)
local s = ''
for k, _ in pairs(t) do
s = s .. ' ' .. k
end
print(s)
end
function calcfirst (p)
if p.kind == 'empty' then
return { [empty] = true }
elseif p.kind == 'char' then
return { [p.v] = true }
elseif p.kind == 'ord' then
return union(calcfirst(p.p1), calcfirst(p.p2))
elseif p.kind == 'con' then
local s1 = calcfirst(p.p1)
local s2 = calcfirst(p.p2)
if s1[empty] then
return union(s1, s2, not s2[empty])
else
return s1
end
elseif p.kind == 'var' then
return FIRST[p.v]
elseif p.kind == 'throw' then
return { }
elseif p.kind == 'any' then
return { ["any"] = true }
elseif p.kind == 'not' then
return { [empty] = true }
-- in case of a well-formed PEG, in a repetition p*, we know p does not match the empty string
elseif p.kind == 'opt' or p.kind == 'star' then
--if p.kind == 'plus' and p.p1.v == 'recordfield' then
-- print ('danado', p.p1.v)
--end
return union(calcfirst(p.p1), { [empty] = true})
elseif p.kind == 'plus' then
return calcfirst(p.p1)
else
error("Unknown kind: " .. p.kind)
end
end
local function updateFollow (p, k)
if p.kind == 'var' then
local v = p.v
FOLLOW[v] = union(FOLLOW[v], k, true)
elseif p.kind == 'con' then
if p.p1.kind == 'var' and matchEmpty(p.p2) then
updateFollow(p.p1, k)
end
updateFollow(p.p2, k)
elseif p.kind == 'ord' then
updateFollow(p.p1, k)
updateFollow(p.p2, k)
end
end
function calck (g, p, k)
if p.kind == 'empty' then
return k
elseif p.kind == 'char' then
return { [p.v]=true }
elseif p.kind == 'ord' then
local k1 = calck(g, p.p1, k)
local k2 = calck(g, p.p2, k)
return union(k1, k2, true)
elseif p.kind == 'con' then
local k2 = calck(g, p.p2, k)
return calck(g, p.p1, k2)
elseif p.kind == 'var' then
if matchEmpty(p) then
return union(FIRST[p.v], k, true)
else
return FIRST[p.v]
end
elseif p.kind == 'throw' then
return k
elseif p.kind == 'any' then
return { ['.']=true }
elseif p.kind == 'not' then
return k
elseif p.kind == 'opt' then
return union(calck(g, p.p1, k), k, true)
-- in case of a well-formed PEG a repetition does not match the empty string
elseif p.kind == 'star' then
updateFollow(g, p.p1, calcfirst(p))
--if p.p1.kind == 'var' then
-- local v = p.p1.v
-- FOLLOW[v] = union(FOLLOW[v], calcfirst(g, g[v], {}), true)
--end
return union(calck(g, p.p1, k), k, true)
elseif p.kind == 'plus' then
--return calck(g, p.p1, k)
updateFollow(g, p.p1, calcfirst(p))
--[[if p.p1.kind == 'var' then
local v = p.p1.v
FOLLOW[v] = union(FOLLOW[v], calcfirst(g, g[v], {}), true)
end]]
--return union(calck(g, p.p1, k), k)
return union(calck(g, p.p1, k), k, true)
else
error("Unknown kind: " .. p.kind)
end
end
local function initFst (g)
FIRST = {}
for k, v in pairs(g) do
FIRST[k] = {}
end
end
local function calcFst (g)
local update = true
local equal
initFst(g)
while update do
update = false
for k, v in pairs(g) do
FIRST[k], equal = union(FIRST[k], calcfirst(v))
if not equal then
update = true
end
end
end
return FIRST
end
local function initFlw(g, init)
FOLLOW = {}
for k, v in pairs(g) do
FOLLOW[k] = {}
end
FOLLOW[init] = { ['$'] = true }
end
local function calcFlwAux (p, flw)
if p.kind == 'var' then
FOLLOW[p.v] = union(FOLLOW[p.v], flw)
elseif p.kind == 'con' then
calcFlwAux(p.p2, flw)
local k = calcfirst(p.p2)
assert(not k[empty] == not matchEmpty(p.p2), tostring(k[empty]) .. ' ' .. tostring(matchEmpty(p.p2)) .. ' ' .. writepeg(p.p2, p.p2.kind == 'con'))
if matchEmpty(p.p2) then
--if k[empty] then
calcFlwAux(p.p1, union(k, flw, true))
else
calcFlwAux(p.p1, k)
end
elseif p.kind == 'star' or p.kind == 'plus' then
calcFlwAux(p.p1, union(calcfirst(p.p1), flw, true))
elseif p.kind == 'opt' then
calcFlwAux(p.p1, flw)
elseif p.kind == 'ord' then
calcFlwAux(p.p1, flw)
calcFlwAux(p.p2, flw)
end
end
local function calcFlw (g, init)
local update = true
initFlw(g, init)
while update do
local tmp = {}
for k, v in pairs(FOLLOW) do
tmp[k] = v
end
for k, v in pairs(g) do
calcFlwAux(v, FOLLOW[k])
end
update = false
for k, v in pairs(g) do
if not equalSet(FOLLOW[k], tmp[k]) then
update = true
end
end
end
return FOLLOW
end
return {
calcFlw = calcFlw,
calcFst = calcFst,
calcfirst = calcfirst,
printfollow = printfollow,
disjoint = disjoint,
set2choice = set2choice,
calck = calck,
matchEmpty = matchEmpty
}