-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.go
40 lines (33 loc) · 984 Bytes
/
predict.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
package grammar
import (
"os"
)
type PredictTable struct {
terminals []Symbol
nonterminal []Symbol
contents []*Production
}
func newPredictTable(terminals []Symbol, nonterminals []Symbol) *PredictTable {
terminals = SortSymbol(terminals)
if terminals[len(terminals)-1] != rightEndMarkerS {
terminals = append(terminals, rightEndMarkerS)
}
return &PredictTable{
terminals: terminals,
nonterminal: nonterminals,
contents: make([]*Production, len(terminals)*len(nonterminals)),
}
}
func (a *PredictTable) add(nonterminal Symbol, terminal Symbol, content *Production) {
row := indexOfSymbolList(nonterminal, a.nonterminal)
col := indexOfSymbolList(terminal, a.terminals)
a.addContent(row, col, content)
}
func (a *PredictTable) addContent(row int, col int, content *Production) {
// rowCount := len(a.nonterminal)
colCount := len(a.terminals)
a.contents[row*colCount+col] = content
}
func (a *PredictTable) dump() {
PrintPredictTable(os.Stdout, a)
}