| 1 | # New |
| 2 | Have a look at [https://github.com/goccmack/gogll](https://github.com/goccmack/gogll) for scannerless GLL parser generation. |
| 3 | # Gocc |
| 4 | |
| 5 |  |
| 6 | [](https://pkg.go.dev/github.com/goccmack/gocc) |
| 7 | [](https://goreportcard.com/report/github.com/goccmack/gocc) |
| 8 | |
| 9 | ## Introduction |
| 10 | |
| 11 | Gocc is a compiler kit for Go written in Go. |
| 12 | |
| 13 | Gocc generates lexers and parsers or stand-alone DFAs or parsers from a BNF. |
| 14 | |
| 15 | Lexers are DFAs, which recognise regular languages. Gocc lexers accept UTF-8 input. |
| 16 | |
| 17 | Gocc parsers are PDAs, which recognise LR-1 languages. Optional LR1 conflict |
| 18 | handling automatically resolves shift / reduce and reduce / reduce conflicts. |
| 19 | |
| 20 | Generating a lexer and parser starts with creating a bnf file. Action expressions |
| 21 | embedded in the BNF allows the user to specify semantic actions for syntax productions. |
| 22 | |
| 23 | For complex applications the user typically uses an abstract syntax tree (AST) |
| 24 | to represent the derivation of the input. The user provides a set of functions |
| 25 | to construct the AST, which are called from the action expressions specified |
| 26 | in the BNF. |
| 27 | |
| 28 | See the [README](example/bools/README) for an included example. |
| 29 | |
| 30 | [User Guide (PDF): Learn You a gocc for Great Good](https://raw.githubusercontent.com/goccmack/gocc/master/doc/gocc_user_guide.pdf) (gocc3 user guide will be published shortly) |
| 31 | |
| 32 | ## Installation |
| 33 | |
| 34 | * First download and Install Go From http://golang.org/ |
| 35 | * Setup your GOPATH environment variable. |
| 36 | * Next in your command line run: go get github.com/goccmack/gocc (go get will |
| 37 | git clone gocc into GOPATH/src/github.com/goccmack/gocc and run go install) |
| 38 | * Alternatively clone the source: https://github.com/goccmack/gocc . Followed |
| 39 | by go install github.com/goccmack/gocc |
| 40 | * Finally, make sure that the bin folder where the gocc binary is located is |
| 41 | in your PATH environment variable. |
| 42 | |
| 43 | ## Getting Started |
| 44 | |
| 45 | Once installed, start by creating your BNF in a package folder. |
| 46 | |
| 47 | For example GOPATH/src/foo/bar.bnf: |
| 48 | |
| 49 | ``` |
| 50 | /* Lexical Part */ |
| 51 | |
| 52 | id : 'a'-'z' {'a'-'z'} ; |
| 53 | |
| 54 | !whitespace : ' ' | '\t' | '\n' | '\r' ; |
| 55 | |
| 56 | /* Syntax Part */ |
| 57 | |
| 58 | << import "foo/ast" >> |
| 59 | |
| 60 | Hello: "hello" id << ast.NewWorld($1) >> ; |
| 61 | ``` |
| 62 | |
| 63 | Next to use gocc, run: |
| 64 | |
| 65 | ```sh |
| 66 | cd $GOPATH/src/foo |
| 67 | gocc bar.bnf |
| 68 | ``` |
| 69 | |
| 70 | This will generate a scanner, parser and token package inside GOPATH/src/foo |
| 71 | Following times you might only want to run gocc without the scanner flag, |
| 72 | since you might want to start making the scanner your own. Gocc is after all |
| 73 | only a parser generator even if the default scanner is quite useful. |
| 74 | |
| 75 | Next create ast.go file at $GOPATH/src/foo/ast with the following contents: |
| 76 | |
| 77 | ```go |
| 78 | package ast |
| 79 | |
| 80 | import ( |
| 81 | "foo/token" |
| 82 | ) |
| 83 | |
| 84 | type Attrib interface {} |
| 85 | |
| 86 | type World struct { |
| 87 | Name string |
| 88 | } |
| 89 | |
| 90 | func NewWorld(id Attrib) (*World, error) { |
| 91 | return &World{string(id.(*token.Token).Lit)}, nil |
| 92 | } |
| 93 | |
| 94 | func (this *World) String() string { |
| 95 | return "hello " + this.Name |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | Finally, we want to parse a string into the ast, so let us write a test at |
| 100 | $GOPATH/src/foo/test/parse_test.go with the following contents: |
| 101 | |
| 102 | ```go |
| 103 | package test |
| 104 | |
| 105 | import ( |
| 106 | "foo/ast" |
| 107 | "foo/lexer" |
| 108 | "foo/parser" |
| 109 | "testing" |
| 110 | ) |
| 111 | |
| 112 | func TestWorld(t *testing.T) { |
| 113 | input := []byte(`hello gocc`) |
| 114 | lex := lexer.NewLexer(input) |
| 115 | p := parser.NewParser() |
| 116 | st, err := p.Parse(lex) |
| 117 | if err != nil { |
| 118 | panic(err) |
| 119 | } |
| 120 | w, ok := st.(*ast.World) |
| 121 | if !ok { |
| 122 | t.Fatalf("This is not a world") |
| 123 | } |
| 124 | if w.Name != `gocc` { |
| 125 | t.Fatalf("Wrong world %v", w.Name) |
| 126 | } |
| 127 | } |
| 128 | ``` |
| 129 | |
| 130 | Finally, run the test: |
| 131 | |
| 132 | ```sh |
| 133 | cd $GOPATH/src/foo/test |
| 134 | go test -v |
| 135 | ``` |
| 136 | |
| 137 | You have now created your first grammar with gocc. This should now be relatively |
| 138 | easy to change into the grammar you actually want to create or use an existing |
| 139 | LR1 grammar you would like to parse. |
| 140 | |
| 141 | ## BNF |
| 142 | |
| 143 | The Gocc BNF is specified [here](spec/gocc2.ebnf) |
| 144 | |
| 145 | An example bnf with action expressions can be found [here](example/bools/example.bnf) |
| 146 | |
| 147 | ## Action Expressions and AST |
| 148 | |
| 149 | An action expression is specified as "<", "<", goccExpressionList , ">", ">" . |
| 150 | The goccExpressionList is equivalent to a [goExpressionList](https://golang.org/ref/spec#ExpressionList). |
| 151 | This expression list should return an Attrib and an error. Where Attrib is: |
| 152 | |
| 153 | ```go |
| 154 | type Attrib interface {} |
| 155 | ``` |
| 156 | |
| 157 | Also, parsed elements of the corresponding bnf rule can be represented in the expressionList as "$", digit. |
| 158 | |
| 159 | Some action expression examples: |
| 160 | |
| 161 | ``` |
| 162 | << $0, nil >> |
| 163 | << ast.NewFoo($1) >> |
| 164 | << ast.NewBar($3, $1) >> |
| 165 | << ast.TRUE, nil >> |
| 166 | ``` |
| 167 | |
| 168 | Constants, functions, etc. that are returned or called should be programmed by |
| 169 | the user in his ast (Abstract Syntax Tree) package. The ast package requires |
| 170 | that you define your own Attrib interface as shown above. All parameters |
| 171 | passed to functions will be of this type. |
| 172 | |
| 173 | For raw elements that you know to be a `*token.Token`, you can use the short-hand: `$T0` etc, leading the following expressions to produce identical results: |
| 174 | |
| 175 | ``` |
| 176 | << $3.(*token.Token), nil >> |
| 177 | << $T3, nil >> |
| 178 | ``` |
| 179 | |
| 180 | Some example of functions: |
| 181 | |
| 182 | ```go |
| 183 | func NewFoo(a Attrib) (*Foo, error) { ... } |
| 184 | func NewBar(a, b Attrib) (*Bar, error) { ... } |
| 185 | ``` |
| 186 | |
| 187 | An example of an ast can be found [here](example/bools/ast/ast.go) |
| 188 | |
| 189 | ## Users |
| 190 | |
| 191 | These projects use gocc: |
| 192 | |
| 193 | * [gogo](https://github.com/shivansh/gogo) - [BNF file](https://github.com/shivansh/gogo/blob/master/src/lang.bnf) - a Go to MIPS compiler written in Go |
| 194 | * [gonum/gonum](https://github.com/gonum/gonum) - [BNF file](https://github.com/gonum/gonum/blob/master/graph/formats/dot/internal/dot.bnf) - DOT decoder (part of the graph library of Gonum) |
| 195 | * [llir/llvm](https://github.com/llir/llvm) - [BNF file](https://github.com/llir/llvm/blob/28149269dab73cc63915a9c2c6c7b25dbd4db027/asm/internal/ll.bnf) - LLVM IR library in pure Go |
| 196 | * [mewmew/uc](https://github.com/mewmew/uc) - [BNF file](https://github.com/mewmew/uc/blob/master/gocc/uc.bnf) - A compiler for the µC language |
| 197 | * [gographviz](https://github.com/awalterschulze/gographviz) - [BNF file](https://github.com/awalterschulze/gographviz/blob/main/dot.bnf) - Parses the Graphviz DOT language in golang |
| 198 | * [katydid](http://katydid.github.io/) - [BNF file](https://github.com/katydid/validator-go/blob/main/validator/bnf/all.bnf) - Encoding agnostic validation language |
| 199 | * [skius/stringlang](https://github.com/skius/stringlang) - [BNF file](https://github.com/skius/stringlang/blob/main/lang.bnf) - An interpreter for the expression-oriented language StringLang |
| 200 | * [miller](https://github.com/johnkerl/miller) - [BNF file](https://github.com/johnkerl/miller/blob/main/internal/pkg/parsing/mlr.bnf) - Miller is like awk, sed, cut, join, and sort for name-indexed data such as CSV, TSV, and tabular JSON. |
| 201 | * [nesgo](https://github.com/retroenv/nesgo) - [BNF file](https://github.com/retroenv/nesgo/blob/main/internal/gocc/lang.bnf) - A Go compiler for NES |