Skip to content

Commit

Permalink
Builder,README: add feature for parse tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
keyuchang committed Mar 31, 2022
1 parent 18bace1 commit 18286ed
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
63 changes: 60 additions & 3 deletions Builder/GoGenCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ type GoBuilder struct {
StateFunc string
ReduceFunc string
Translate string
// ReduceTrace funciton
ReduceTrace string
}

func NewGoBuilder(w *parser.Walker) *GoBuilder {
return &GoBuilder{
HeaderPart: `/*Generator Code , do not modify*/\n`,
HeaderPart: `/*Generator Code , do not modify*/\n import "strings"\n`,
vnode: w.VistorNode.(*parser.RootVistor),
}
}
Expand All @@ -49,6 +51,7 @@ func (b *GoBuilder) buildConstPart() {

func (b *GoBuilder) buildUionAndCode() {
str := `
var IsTrace bool = false
var StateSymStack = []StateSym{}
var StackPointer = 0
type Context struct {
Expand All @@ -71,7 +74,7 @@ type StateSym struct {
}`
str = fmt.Sprintf(str, b.vnode.GetUion())
b.UnionPart = str
b.CodeHeader = b.vnode.GetCode()
b.CodeHeader = b.vnode.GetCode() + "\n import \"strings\"\n"
b.CodeLast = b.vnode.GetCodeCopy()
}

Expand Down Expand Up @@ -101,6 +104,7 @@ func (b *GoBuilder) buildStateFunc() {
b.StateFunc = `
// Push StateSym
func PushStateSym(state *StateSym) {
TraceShift(state)
if StackPointer >= len(StateSymStack) {
StateSymStack = append(StateSymStack, *state)
} else {
Expand Down Expand Up @@ -157,7 +161,8 @@ func Parser(input string) *ValType {
s := &StateSymStack[StackPointer-1]
a := s.Action(lookAhead)
if a == ERROR_ACTION {
panic("Grammar parse error")
lines := strings.Split(input[:currentPos], "\n")
panic("Grammar parse error near :" + lines[len(lines)-1])
} else if a == ACCEPT_ACTION {
return &s.ValType
} else {
Expand All @@ -175,6 +180,7 @@ func Parser(input string) *ValType {
s := &StateSymStack[StackPointer-1]
gotoState := s.Action(SymTy.YySymIndex)
SymTy.Yystate = gotoState
TraceReduce(reduceIndex, gotoState, TraceTranslate(lookAhead))
PushStateSym(SymTy)
}
}
Expand Down Expand Up @@ -205,6 +211,55 @@ func translate(c int) int {
}
}
b.Translate = fmt.Sprintf(str, caseCodes)
// build Trace info
traceFun := `
func TraceShift(s *StateSym) {
if IsTrace {
fmt.Printf("Shift %%s, push state %%d\n", TraceTranslate(s.YySymIndex), s.Yystate)
}
}
func TraceTranslate(c int) string {
var conv string = ""
switch c {
%s
}
return conv
}
`
caseCodes = ""
for _, sy := range b.vnode.G.Symbols {
caseCodes += fmt.Sprintf("\tcase %d:\n \tconv = \"%s\"\n", sy.ID, parser.RemoveTempName(sy.Name))
}
b.Translate += fmt.Sprintf(traceFun, caseCodes)
}

func (b *GoBuilder) buildReduceTrace() {
str := `
func TraceReduce(reduceIndex, s int, look string) {
if IsTrace {
switch reduceIndex {
%s
}
}
}
`

caseCode := ""
for i := 1; i < len(b.vnode.G.ProductoinRules); i++ {
caseCode += fmt.Sprintf("\t\tcase %d: \n", i)
oneRule := b.vnode.GetRules(i - 1)
leftPartString := "use Reduce:" + parser.RemoveTempName(oneRule.LeftPart.Name)
var rightPartString string = ""
for _, rightPart := range oneRule.RighPart {
rightPartString += parser.RemoveTempName(rightPart.Name) + " "
}
strTrace := fmt.Sprintf("%s -> %s",
leftPartString, rightPartString)
caseCode += fmt.Sprintf("\n\t\tfmt.Printf(\"look ahead %%s, %s, go to state %%d\\n\", look, s)\n", strTrace)
}
str = fmt.Sprintf(str, caseCode)
b.ReduceTrace = str
}

// make ReduceFunc
Expand Down Expand Up @@ -278,6 +333,7 @@ func GoGenFromString(input string, file string) error {
}

b.buildReduceFunc()
b.buildReduceTrace()
b.buildTranslate()
// Create file and write to it
f, err := os.Create(file)
Expand All @@ -292,6 +348,7 @@ func GoGenFromString(input string, file string) error {
f.WriteString(b.StateFunc)
f.WriteString(b.ReduceFunc)
f.WriteString(b.Translate)
f.WriteString(b.ReduceTrace)
f.Close()
return nil
}
5 changes: 4 additions & 1 deletion Builder/GoGenPackTableCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (b *GoBuilder) buildPackStateFunc() {
b.StateFunc = `
// Push StateSym
func PushStateSym(state *StateSym) {
TraceShift(state)
if StackPointer >= len(StateSymStack) {
StateSymStack = append(StateSymStack, *state)
} else {
Expand Down Expand Up @@ -94,7 +95,8 @@ func Parser(input string) *ValType {
s := &StateSymStack[StackPointer-1]
a := s.Action(lookAhead)
if a == ERROR_ACTION {
panic("Grammar parse error")
lines := strings.Split(input[:currentPos], "\n")
panic("Grammar parse error near :" + lines[len(lines)-1])
} else if a == ACCEPT_ACTION {
return &s.ValType
} else {
Expand All @@ -112,6 +114,7 @@ func Parser(input string) *ValType {
s := &StateSymStack[StackPointer-1]
gotoState := s.Action(SymTy.YySymIndex)
SymTy.Yystate = gotoState
TraceReduce(reduceIndex, gotoState, TraceTranslate(lookAhead))
PushStateSym(SymTy)
}
}
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,37 @@ bin/yaccgo generate typescript examples/exprts.y expts.ts
```
at your `y `file You should do as follower
more in [manual](./Docs/manual.md)

# Trace
After generate the parse code, modify the `var IsTrace bool = false` to `var IsTrace bool = true` , then you can see the trace of the parser. feature completed for go, other are WIP.
for example:
```
bin/yaccgo generate go examples/ladd.y out/ladd.go
```
then you modify the ladd.go file, change code `var IsTrace bool = false` to `var IsTrace bool = true`, then
```
go run out/ladd.go
```
You can see trace information
```
look ahead NUM, use Reduce:PROG -> , go to state 1
Shift PROG, push state 1
Shift NUM, push state 3
look ahead PLUS, use Reduce:E -> NUM , go to state 2
Shift E, push state 2
Shift PLUS, push state 5
Shift NUM, push state 3
look ahead NL, use Reduce:E -> NUM , go to state 6
Shift E, push state 6
look ahead NL, use Reduce:E -> E PLUS E , go to state 2
Shift E, push state 2
Shift NL, push state 4
3
look ahead $, use Reduce:PROG -> PROG E NL , go to state 1
Shift PROG, push state 1
0
```

# Design

LALR1 Algorithm Base on
Expand Down

0 comments on commit 18286ed

Please sign in to comment.