-
Notifications
You must be signed in to change notification settings - Fork 24
/
states.go
36 lines (34 loc) · 1.15 KB
/
states.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
package knuth
import (
"github.com/goccmack/gogll/v3/lr1/first"
"github.com/goccmack/gogll/v3/lr1/items"
"github.com/goccmack/gogll/v3/lr1/states"
"github.com/goccmack/gogll/v3/lr1/symbolsuccessors"
)
func States(symbols []string, lr0items *items.Items, first *first.First) *states.States {
s := &states.States{
List: make([]*states.State, 0, 64),
}
s.NewState0(symbols, lr0items, first, states.NewConfigGroup(lr0items.List[0], "$"))
symSuccessors := symbolsuccessors.NewSymbolSuccessors()
for si := 0; si < len(s.List); si++ {
st_trans := make([]states.Transition, 0, 4)
for _, trans := range s.List[si].Next().List() {
newState := true
for _, snum := range symSuccessors[trans.Sym] {
if s.List[snum].Equal(trans.State) {
st_trans = append(st_trans, states.Transition{trans.Sym, s.List[snum]})
newState = false
}
}
if newState {
st_trans = append(st_trans, trans)
s.List = append(s.List, trans.State)
trans.State.Number = len(s.List) - 1
symSuccessors[trans.Sym] = append(symSuccessors[trans.Sym], trans.State.Number)
}
}
s.List[si].Transitions = states.NewTransitionsList(st_trans, symbols)
}
return s
}