Skip to content

Commit

Permalink
fix the bug of calc DR set
Browse files Browse the repository at this point in the history
  • Loading branch information
keyuchang committed Feb 8, 2022
1 parent 173a172 commit 79e2ea9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
10 changes: 8 additions & 2 deletions LALR/LALR.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-------------
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 12 additions & 2 deletions LALR/Utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,25 @@ 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)
}
}

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)
}
}

Expand Down
74 changes: 74 additions & 0 deletions examples/ladd.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// language: go

%{

package main

import (

"fmt"

)

%}

%union {
val int
}

%type <val> E
%token PLUS NL
%token <val> 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)
}

0 comments on commit 79e2ea9

Please sign in to comment.