-
Notifications
You must be signed in to change notification settings - Fork 0
/
table-itemset.go
166 lines (148 loc) · 3.79 KB
/
table-itemset.go
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
package lr0
import (
"github.com/pkg/errors"
)
func newTableItemset(items []tableItem, g *grammar) tableItemset {
allItems := expandAllPossibleTableItems(items, g)
validateTableItemsetDeterministic(allItems, g)
return tableItemset{items: allItems}
}
// expandAllPossibleTableItems returns possible items by expansion all the given items
// by Grammar
func expandAllPossibleTableItems(items []tableItem, g *grammar) []tableItem {
var final []tableItem
knownNext := newIdSet()
newItems := items
for len(newItems) != 0 {
var nextNew []Id
NewItems:
for _, newIt := range newItems {
for _, have := range final {
if newIt == have {
continue NewItems
}
}
final = append(final, newIt)
nextId := newIt.Expected()
if nextId == InvalidId || g.IsTerminal(nextId) {
continue
}
if knownNext.Has(nextId) {
continue
}
nextNew = append(nextNew, nextId)
}
knownNext.Add(nextNew...)
newItems = nil
for _, id := range nextNew {
for _, r := range g.RulesFor(id) {
newItems = append(newItems, newTableItem(r))
}
}
}
return final
}
// validateTableItemsetDeterministic checks for bad state in this tableItemset.
// A problem is reported by panic since it's grammar definition problem
//
// - ErrConflictReduceReduce
//
// - ErrConflictShiftReduce
//
// https://en.wikipedia.org/wiki/LR_parser#Conflicts_in_the_constructed_tables
func validateTableItemsetDeterministic(items []tableItem, g *grammar) {
var finite, terminals, nonTerminals []tableItem
for _, it := range items {
if !it.HasFurther() {
finite = append(finite, it)
continue
}
if g.IsTerminal(it.Expected()) {
terminals = append(terminals, it)
continue
}
nonTerminals = append(nonTerminals, it)
}
// Check for Shift-Reduce conflicts
if len(finite) != 0 && len(terminals) != 0 {
restTerminals := g.GetTerminalIdsSet()
for _, it := range terminals {
restTerminals.Remove(it.Expected())
}
if restTerminals.Count() == 0 {
panic(errors.WithStack(ErrConflictShiftReduce))
}
}
// Check for Reduce-Reduce conflicts
if len(finite) > 1 {
panic(errors.WithStack(ErrConflictReduceReduce))
}
}
type tableItemset struct {
items []tableItem
}
// HasFinalItem checks if this set has tableItem where the next token must be EOF
func (s tableItemset) HasFinalItem() bool {
for _, it := range s.items {
if it.HasEOF() && !it.HasFurther() {
return true
}
}
return false
}
// ReduceRule returns reduction rule of this set if any, nil otherwise
func (s tableItemset) ReduceRule() Rule {
for _, it := range s.items {
if !it.HasFurther() {
// this is the only due to Reduce-Reduce conflicts validation
return it.Rule
}
}
return nil
}
// IsEqual checks equality with another tableItemset
func (s tableItemset) IsEqual(to tableItemset) bool {
if len(s.items) != len(to.items) {
return false
}
my := make(map[tableItem]struct{})
for _, it := range s.items {
my[it] = struct{}{}
}
for _, it := range to.items {
if _, ok := my[it]; !ok {
return false
}
}
return true
}
// GetNextItemsets creates next sets for next state by shifting current position
// in items
func (s tableItemset) GetNextItemsets(g *grammar) map[Id]tableItemset {
ret := make(map[Id]tableItemset)
for id, items := range s.getNextSetsMap() {
ret[id] = newTableItemset(items, g)
}
return ret
}
// getNextSetsMap creates input data to create next sets for next states by
// shifting current position in items
func (s tableItemset) getNextSetsMap() map[Id][]tableItem {
nextM := make(map[Id][]tableItem)
SOURCE:
for _, it := range s.items {
if !it.HasFurther() {
continue
}
nextId := it.Expected()
nextItem := it.Shift()
has := nextM[nextId]
for _, hasItem := range has {
if hasItem == nextItem {
continue SOURCE
}
}
nextM[nextId] = append(has, nextItem)
}
return nextM
}