Skip to content

Commit

Permalink
*: Add Docs and modify CodeGen problem
Browse files Browse the repository at this point in the history
  • Loading branch information
keyuchang committed Dec 8, 2021
1 parent 1079dcd commit 7c69edb
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Builder/GoGenCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func ReduceFunc(reduceIndex int) *StateSym {
caseCode += fmt.Sprintf("case %d: \n", i)
rightPartlen := len(productionRule.RighPart)
caseCode += fmt.Sprintf("\tdollarDolar.YySymIndex = %d\n", productionRule.LeftPart.ID)
caseCode += fmt.Sprintf("\tDollar := StateSymStack[topIndex-%d : StackPointer]\n", rightPartlen)
caseCode += fmt.Sprintf("\tDollar := StateSymStack[topIndex-%d : StackPointer]\n\t_ = Dollar\n", rightPartlen)
//fetch the action code here
caseCode += actionCodeReplace(b.vnode, i, productionRule)
caseCode += fmt.Sprintf("\tPopStateSym(%d)\n", rightPartlen)
Expand Down
4 changes: 2 additions & 2 deletions Cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
)

var rootCmd = &cobra.Command{
Use: "github.com/acekingke/yaccgo",
Short: "github.com/acekingke/yaccgo is a yacc generator",
Use: "yaccgo",
Short: "yaccgo is a yacc generator",
Long: "Understandable yacc generator , it can generate go/js/rust code",
Run: nil,
}
Expand Down
169 changes: 169 additions & 0 deletions Docs/manual.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# Command
If you run yaccgo , it will show as follow:
```
Understandable yacc generator , it can generate go/js/rust code
Usage:
yaccgo [command]
Available Commands:
completion generate the autocompletion script for the specified shell
debug open debug mode
generate generate filetype input.y output.go
help Help about any command
Flags:
-h, --help help for yaccgo
Use "yaccgo [command] --help" for more information about a command.
```
It has sub command like `generate, debug, help`
## sub command
### generate
The command use as follow:
```
yaccgo generate {filetype} {inputfile} {outputfile}
```
filetype it has `go` `typescript`, and `rust` will support in feature.

**example:**
```
yaccgo generate go examples/expr.y examples/e.go
```
it will generate golang file in `examples/e.go` , and the input file is `examples/expr.y`, file type is `go`

**example**
```
yaccgo generate typescript examples/exprts.y examples/e.ts
```
It will create a typescript file in `examples/e.ts`, input file is `examples/exprts.y`

### Debug
command format as follow:
```
yaccgo debug {inputfile}
```
it will generate yacc debug information.

# grammar file specification
grammar file is '.y' as postfix files, use as input file for yaccgo.
`examples/expr.y` and `examples/exprts.y` above is grammar files
## Sections
grammar file consists of the following sections:

- Declarations
- Rules
- Programs

and sections are seperated by `%%` each other, so a complete grammar file is like this:
```
Declarations
%%
Rules
%%
Programs
```

### Declarations
they are consist of like that:
```
%{
Import codes
const variables define
global variables define
other header codes
%}
start define
sematics value type define
terminal symbol define
precedence define
```
all parts will persent as follow:

* header codes

header codes are wrap by `%{ %}`,Usually they contain import codes, const defines , global variables define and so on.

**example**
```
%{
package main
import (
"fmt"
)
%}
```
* start define

start define specify nonterminal symbol start in grammar.
```
%start nonterminal
```

**example**
```
%start E
```
indicate E is the start symbol in grammar.

* sematics value type define

Every terminal symbol or nonterminal symbol has a sematics value type, use `%union ` to define the value type,
use `%token` to specify a value type to terminal symbol,
or use `%type` to specify a value type to nonterminal symbol.

`%token`'s form is:

```
%token <tag> name
```
`%type`'s form is:

```
%type <tag> name
```
`%union` form is:

```
%union {
XXX
}
```
**examples**

```
%union {
//it is go code form
val int
}
%type <val> E
%token <val> NUM
```

* terminal symbol define

define symbol is terminal symbol, the forms are:

```
%token name
%token name interger-value
```

if `%token` is not following with interger-value, the system specify identification number automatically, or following with interger-value, specify identification number by user.

* precedence define

### Rules
wating more...

### Programs
Wating more...
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bin/yaccgo generate typescript examples/exprts.y expts.ts
```
at your `y `file You should do as follower

more in [manual](./Docs/manual.md)
# Design

LALR1 Algorithm Base on
Expand Down
30 changes: 30 additions & 0 deletions examples/e.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%{
package main
import "fmt"
%}

%token 'n'
%start L
%%
L : /*empty*/
|E L
E: 'n'
%%
func GetToken(input string, valTy *ValType, pos *int) int {
if *pos >= len(input) {
return -1
}
c := input[*pos]
*pos++
switch c {
case 'n':
*valTy = ValType{}
return 'n'
default:
return 0
}
}
func main() {
v := Parser("nnn")
fmt.Println(v)
}

0 comments on commit 7c69edb

Please sign in to comment.