-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducer.lua
169 lines (135 loc) · 4.34 KB
/
reducer.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
--[[
MIT License
Copyright (c) 2019 emmachase
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
local dr = {}
local pprint = require("pprint")
local util = require("util")
local function isEpsilon(condition)
return type(condition) == "table" and condition.type == "epsilon"
end
local function traverseEpsilon(machine, state, seen)
seen = seen or {}
local func = {state}
local edges = machine.states[state].edges
for i = 1, #edges do
local edge = edges[i]
if isEpsilon(edge.condition) then
if not seen[edge.dest] then
seen[edge.dest] = true
-- Epsilon transition, add to E function
func[#func + 1] = edge.dest
-- Traverse through its destination and fully evaluate the epsilon path
local extraFn = traverseEpsilon(machine, edge.dest, seen)
-- Append those new states to this E function
local pos = #func
for j = 1, #extraFn do
func[pos + 1] = extraFn[j]
pos = pos + 1
end
end
end
end
return util.nub(func)
end
local function nameFromST(st)
table.sort(st)
return table.concat(st)
end
function dr.reduceNFA(nfa)
-- Construct E function
local eFunc = {}
for k in pairs(nfa.states) do
local eI = 1
eFunc[k] = traverseEpsilon(nfa, k)
end
local newMachine = {
states = {},
startState = nameFromST(eFunc[nfa.startState]),
acceptStates = {},
properties = nfa.properties
}
local todoStates = {eFunc[nfa.startState]}
local completeStates = {}
while todoStates[1] do -- while todoStates is not empty
local workingState = table.remove(todoStates, 1)
local newState = {
edges = {},
enter = {}
}
local lang = {}
local isAccepted = false
-- Get all possible inputs by traversing states
for i = 1, #workingState do
local stateName = workingState[i]
if nfa.acceptStates[stateName] then
isAccepted = true
end
local state = nfa.states[stateName]
for j = 1, #state.edges do
local cond = state.edges[j].condition
if type(cond) == "string" then
lang[cond] = lang[cond] or {}
lang[cond][#lang[cond] + 1] = state.edges[j].dest
end
end
end
-- For each possible input, compute the resultant state, and create an edge
for k, v in pairs(lang) do
local st, si = {}, 1
for i = 1, #v do
local throughput = eFunc[v[i]]
for j = 1, #throughput do
st[si] = throughput[j]
si = si + 1
end
end
st = util.nub(st)
table.sort(st)
local destState = nameFromST(st)
newState.edges[#newState.edges + 1] = {
condition = k,
dest = destState
}
if not completeStates[destState] then
todoStates[#todoStates + 1] = st
completeStates[destState] = true
end
end
-- Append each enter condition
local ei = 1
for i = 1, #workingState do
local stateName = workingState[i]
local state = nfa.states[stateName]
if state.enter then
for j = 1, #state.enter do
newState.enter[ei] = state.enter[j]
ei = ei + 1
end
end
end
local stateName = nameFromST(workingState)
newMachine.states[stateName] = newState
if isAccepted then
newMachine.acceptStates[stateName] = true
end
completeStates[stateName] = true
end
return newMachine
end
return dr