Skip to content

Commit

Permalink
*: fix the bug none use nonTerminal symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
keyuchang committed Aug 2, 2022
1 parent 0500f80 commit 450337d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
54 changes: 54 additions & 0 deletions LALR/LALR.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,20 @@ func ComputeLALR(g *grammar.Grammar) *LALR1 {
panic(err.Error())
} else {
// try to split the table and pack it
if utils.DebugPackTab {
fmt.Print("/* ")
for _, sy := range lalr.G.Symbols {
fmt.Printf("%s\t", sy.Name)
}
fmt.Println("*/")
for index, row := range tab {
fmt.Printf("/* %d */ {", index)
for _, val := range row {
fmt.Printf("%d,\t", val)
}
fmt.Println("},")
}
}
if err := lalr.TrySplitTable(tab); err != nil {
fmt.Println(err.Error())
}
Expand Down Expand Up @@ -320,6 +334,7 @@ func (lalr *LALR1) TrySplitTable(tab [][]int) error {
actTab, goTab := lalr.SplitActionAndGotoTable(tab)
// try to pack the table
actdef := make([]int, len(actTab))

for i := range actTab {
actdef[i] = findMaxOccurence(actTab[i])
for j := range actTab[i] {
Expand All @@ -344,7 +359,21 @@ func (lalr *LALR1) TrySplitTable(tab [][]int) error {
actTab[j] = append(actTab[j], v)
}
}
if utils.DebugPackTab {
fmt.Println("==========Show Action Table===============")
for i, r := range actTab {
fmt.Printf("%d:%v\n", i, r)
}
fmt.Println("==========Show Action def===============")
for i, r := range actdef {
fmt.Printf("%d:%v\n", i, r)
}
fmt.Println("==========Show Goto Table===============")
for i, r := range gtdef {
fmt.Printf("%d:%v\n", i, r)
}

}
act, off, check := utils.PackTable(actTab)
if len(act)+len(off)+len(actdef)+len(gtdef) > len(tab)*len(tab[0]) {
return errors.New("the table is no need to pack")
Expand All @@ -353,6 +382,31 @@ func (lalr *LALR1) TrySplitTable(tab [][]int) error {
lalr.NeedPacked = true
lalr.ActionTable, lalr.OffsetTable, lalr.CheckTable = act, off, check
lalr.ActionDef, lalr.GoToDef = actdef, gtdef
if utils.DebugPackTab {
// Check the packed table is correct
res := 0
nTerminals := len(lalr.G.VtSet)
//nNonTerminals := len(lalr.G.VnSet) - 1 // skip the start symbol
for state, v := range tab {
for lookahead, val := range v {
if off[state]+lookahead < 0 {
res = lalr.GenErrorCode()
} else if off[state]+lookahead >= len(check) ||
check[off[state]+lookahead] != state {
if lookahead > nTerminals {
res = gtdef[lookahead-nTerminals-1]
} else {
res = actdef[state]
}
} else {
res = act[off[state]+lookahead]
}
if res != val {
panic(fmt.Sprintf("state:%d, lookahead:%d, the packed table is not correct", state, lookahead))
}
}
}
}
return nil
}

Expand Down
5 changes: 2 additions & 3 deletions LALR/Table.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (lalr *LALR1) GenTable() ([][]int, error) {

func (lalr *LALR1) SplitActionAndGotoTable(tab [][]int) ([][]int, [][]int) {
nTerminals := len(lalr.G.VtSet)
nNonTerminals := len(lalr.G.VnSet)
nNonTerminals := len(lalr.G.VnSet) - 1 // skip the start symbol
fmt.Println("nTerminals", nTerminals, "nNonTerminals", nNonTerminals)
actionTable := [][]int{}
for i := 0; i < len(tab); i++ {
Expand All @@ -204,12 +204,11 @@ func (lalr *LALR1) SplitActionAndGotoTable(tab [][]int) ([][]int, [][]int) {
for i := 0; i < nNonTerminals; i++ {
row := make([]int, len(tab))
for j := 0; j < len(row); j++ {
row[j] = tab[j][nTerminals+i+1]
row[j] = tab[j][nTerminals+i+1] // 1 is the start symbol
}
gotoTable = append(gotoTable, row)
}
return actionTable, gotoTable

}

func getActionTypeName(act E_ActionType) string {
Expand Down
8 changes: 8 additions & 0 deletions Parser/Vistor.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ func (w *Walker) BuildLALR1() *lalr.LALR1 {

}
// resolve symbol
// check the nonTerminal symbol is at left side
for _, sym := range g.Symbols {
if sym.IsNonTerminator {
if _, ok := g.VnSet[sym]; !ok {
panic(fmt.Sprintf("Check the nonterminal %s in left part of rules", sym.Name))
}
}
}
g.ResolveSymbols()
g.CalculateEpsilonClosure()
item_var := item.NewItem(0, 0)
Expand Down
1 change: 1 addition & 0 deletions Utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ package utils
var DebugFlags bool = false
var PackFlags bool = true
var HttpDebug bool = false
var DebugPackTab bool = true

0 comments on commit 450337d

Please sign in to comment.