diff --git a/LALR/LALR.go b/LALR/LALR.go index f9f73c1..44fcbe0 100644 --- a/LALR/LALR.go +++ b/LALR/LALR.go @@ -162,7 +162,7 @@ func (lalr *LALR1) CalcDR() { //set the start -> S' Dr is $ sym // 0 index transistor is I0--S'--> // 1 is dollor symbol - lalr.DRSet[0] = []int{1} + lalr.DRSet[0] = append(lalr.DRSet[0], []int{1}...) } //-----------------Caculate the Includes relation------------- @@ -262,8 +262,14 @@ func ComputeLALR(g *grammar.Grammar) *LALR1 { for k := range lalr.trans { fmt.Println(lalr.showTrans(k)) } - fmt.Println("==========LOOKAHEAD SET===============") + fmt.Println("==========Show Direct Read SET===============") + lalr.ShowDrSet() + fmt.Println("==========Show Reads SET===============") + lalr.ShowReadSet() + fmt.Println("==========Show FollowSet SET===============") lalr.ShowFollowSet() + fmt.Println("==========Show LookAhead SET===============") + lalr.ShowLookAheadSet() } if tab, err := lalr.GenTable(); err != nil { diff --git a/LALR/Utils.go b/LALR/Utils.go index f1bd3cf..988895e 100644 --- a/LALR/Utils.go +++ b/LALR/Utils.go @@ -41,7 +41,12 @@ func (lalr *LALR1) ShowDrSet() { for trIndex, set := range lalr.DRSet { q := lalr.trans[trIndex].q symName := lalr.fetchSymbol(int(lalr.trans[trIndex].sym_or_rule)).Name - fmt.Printf("%d--%s--> %v\n", q, symName, set) + var str_set string = "[" + for _, v := range set { + str_set += lalr.fetchSymbol(v).Name + " " + } + str_set += " ]" + fmt.Printf("%d--%s--> %s\n", q, symName, str_set) } } @@ -49,7 +54,12 @@ func (lalr *LALR1) ShowReadSet() { for trIndex, set := range lalr.ReadSet { q := lalr.trans[trIndex].q symName := lalr.fetchSymbol(int(lalr.trans[trIndex].sym_or_rule)).Name - fmt.Printf("%d--%s--> %v\n", q, symName, set) + var str_set string = "[" + for _, v := range set { + str_set += lalr.fetchSymbol(v).Name + " " + } + str_set += " ]" + fmt.Printf("%d--%s--> %s\n", q, symName, str_set) } } diff --git a/examples/ladd.y b/examples/ladd.y new file mode 100644 index 0000000..8702616 --- /dev/null +++ b/examples/ladd.y @@ -0,0 +1,74 @@ +// language: go + + %{ + + package main + + import ( + + "fmt" + + ) + + %} + + %union { + val int + } + + %type E + %token PLUS NL + %token NUM + %token NUM 100 + %start PROG + %left PLUS +%% +PROG: + /*empty*/ + | PROG E NL +E: + E PLUS E { + $$ = $1 + $3 + } + | NUM { + $$ = $1 + } + +%% + const EOF = -1 + // The parser expects the lexer to return 0 on EOF. Give it a name + // for clarity. + func GetToken(input string, valTy *ValType, pos *int) int { + if *pos >= len(input) { + return -1 + } else { + *valTy = ValType{0} + loop: + if *pos >= len(input) { + return EOF + } + c := input[*pos] + *pos++ + switch c { + case '+': + return PLUS + case '\n': + return NL + default: + if c >= '0' && c <= '9' { // is digit + valTy.val = (valTy.val)*10 + int(c) - '0' + // next is digit + if *pos < len(input) && input[*pos] >= '0' && input[*pos] <= '9' { + goto loop + } + return NUM + } + + } + return 0 + } + } +func main() { + v := Parser("1+2\n").val + fmt.Println(v) +}