{"id":13764148,"url":"https://github.com/goccmack/gocc","last_synced_at":"2025-05-14T20:02:15.884Z","repository":{"id":33288501,"uuid":"36933220","full_name":"goccmack/gocc","owner":"goccmack","description":"Parser / Scanner Generator","archived":false,"fork":false,"pushed_at":"2025-02-20T14:13:36.000Z","size":2489,"stargazers_count":634,"open_issues_count":25,"forks_count":49,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-04-07T18:05:20.998Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/goccmack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-06-05T13:08:21.000Z","updated_at":"2025-04-06T06:02:38.000Z","dependencies_parsed_at":"2024-01-05T21:45:44.260Z","dependency_job_id":"a8110588-cb54-430c-bd80-1d654642d6c2","html_url":"https://github.com/goccmack/gocc","commit_stats":{"total_commits":212,"total_committers":19,"mean_commits":"11.157894736842104","dds":0.7264150943396226,"last_synced_commit":"2292f9e40198d1d43db1e951089d4edd34079619"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goccmack%2Fgocc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goccmack%2Fgocc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goccmack%2Fgocc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goccmack%2Fgocc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/goccmack","download_url":"https://codeload.github.com/goccmack/gocc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248960326,"owners_count":21189982,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T15:01:16.601Z","updated_at":"2025-04-14T20:44:45.090Z","avatar_url":"https://github.com/goccmack.png","language":"Go","funding_links":[],"categories":["开源类库","Software Packages","Go","Golang","软件包","Go Tools","Go 工具","Other Software"],"sub_categories":["解释器","Other Software","其他软件","其他软件库和软件包","Contents"],"readme":"# New\nHave a look at [https://github.com/goccmack/gogll](https://github.com/goccmack/gogll) for scannerless GLL parser generation.\n# Gocc\n\n![Build Status](https://github.com/goccmack/gocc/workflows/build/badge.svg)\n[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\u0026style=flat-square)](https://pkg.go.dev/github.com/goccmack/gocc)\n[![Go Report Card](https://goreportcard.com/badge/github.com/goccmack/gocc)](https://goreportcard.com/report/github.com/goccmack/gocc)\n\n## Introduction\n\nGocc is a compiler kit for Go written in Go.\n\nGocc generates lexers and parsers or stand-alone DFAs or parsers from a BNF.\n\nLexers are DFAs, which recognise regular languages. Gocc lexers accept UTF-8 input.\n\nGocc parsers are PDAs, which recognise LR-1 languages. Optional LR1 conflict\nhandling automatically resolves shift / reduce and reduce / reduce conflicts.\n\nGenerating a lexer and parser starts with creating a bnf file. Action expressions\nembedded in the BNF allows the user to specify semantic actions for syntax productions.\n\nFor complex applications the user typically uses an abstract syntax tree (AST)\nto represent the derivation of the input. The user provides a set of functions\nto construct the AST, which are called from the action expressions specified\nin the BNF.\n\nSee the [README](example/bools/README) for an included example.\n\n[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)\n\n## Installation\n\n* First download and Install Go From http://golang.org/\n* Setup your GOPATH environment variable.\n* Next in your command line run: go get github.com/goccmack/gocc (go get will\n  git clone gocc into GOPATH/src/github.com/goccmack/gocc and run go install)\n* Alternatively clone the source: https://github.com/goccmack/gocc . Followed\n  by go install github.com/goccmack/gocc\n* Finally, make sure that the bin folder where the gocc binary is located is\n  in your PATH environment variable.\n\n## Getting Started\n\nOnce installed, start by creating your BNF in a package folder.\n\nFor example GOPATH/src/foo/bar.bnf:\n\n```\n/* Lexical Part */\n\nid : 'a'-'z' {'a'-'z'} ;\n\n!whitespace : ' ' | '\\t' | '\\n' | '\\r' ;\n\n/* Syntax Part */\n\n\u003c\u003c import \"foo/ast\" \u003e\u003e\n\nHello:  \"hello\" id \u003c\u003c ast.NewWorld($1) \u003e\u003e ;\n```\n\nNext to use gocc, run:\n\n```sh\ncd $GOPATH/src/foo\ngocc bar.bnf\n```\n\nThis will generate a scanner, parser and token package inside GOPATH/src/foo\nFollowing times you might only want to run gocc without the scanner flag,\nsince you might want to start making the scanner your own. Gocc is after all\nonly a parser generator even if the default scanner is quite useful.\n\nNext create ast.go file at $GOPATH/src/foo/ast with the following contents:\n\n```go\npackage ast\n\nimport (\n    \"foo/token\"\n)\n\ntype Attrib interface {}\n\ntype World struct {\n    Name string\n}\n\nfunc NewWorld(id Attrib) (*World, error) {\n    return \u0026World{string(id.(*token.Token).Lit)}, nil\n}\n\nfunc (this *World) String() string {\n    return \"hello \" + this.Name\n}\n```\n\nFinally, we want to parse a string into the ast, so let us write a test at\n$GOPATH/src/foo/test/parse_test.go with the following contents:\n\n```go\npackage test\n\nimport (\n    \"foo/ast\"\n    \"foo/lexer\"\n    \"foo/parser\"\n    \"testing\"\n)\n\nfunc TestWorld(t *testing.T) {\n    input := []byte(`hello gocc`)\n    lex := lexer.NewLexer(input)\n    p := parser.NewParser()\n    st, err := p.Parse(lex)\n    if err != nil {\n        panic(err)\n    }\n    w, ok := st.(*ast.World)\n    if !ok {\n        t.Fatalf(\"This is not a world\")\n    }\n    if w.Name != `gocc` {\n        t.Fatalf(\"Wrong world %v\", w.Name)\n    }\n}\n```\n\nFinally, run the test:\n\n```sh\ncd $GOPATH/src/foo/test\ngo test -v\n```\n\nYou have now created your first grammar with gocc. This should now be relatively\neasy to change into the grammar you actually want to create or use an existing\nLR1 grammar you would like to parse.\n\n## BNF\n\nThe Gocc BNF is specified [here](spec/gocc2.ebnf)\n\nAn example bnf with action expressions can be found [here](example/bools/example.bnf)\n\n## Action Expressions and AST\n\nAn action expression is specified as \"\u003c\", \"\u003c\", goccExpressionList , \"\u003e\", \"\u003e\" .\nThe goccExpressionList is equivalent to a [goExpressionList](https://golang.org/ref/spec#ExpressionList).\nThis expression list should return an Attrib and an error. Where Attrib is:\n\n```go\ntype Attrib interface {}\n```\n\nAlso, parsed elements of the corresponding bnf rule can be represented in the expressionList as \"$\", digit.\n\nSome action expression examples:\n\n```\n\u003c\u003c $0, nil \u003e\u003e\n\u003c\u003c ast.NewFoo($1) \u003e\u003e\n\u003c\u003c ast.NewBar($3, $1) \u003e\u003e\n\u003c\u003c ast.TRUE, nil \u003e\u003e\n```\n\nConstants, functions, etc. that are returned or called should be programmed by\nthe user in his ast (Abstract Syntax Tree) package. The ast package requires\nthat you define your own Attrib interface as shown above. All parameters\npassed to functions will be of this type.\n\nFor 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:\n\n```\n\u003c\u003c $3.(*token.Token), nil \u003e\u003e\n\u003c\u003c $T3, nil \u003e\u003e\n```\n\nSome example of functions:\n\n```go\nfunc NewFoo(a Attrib) (*Foo, error) { ... }\nfunc NewBar(a, b Attrib) (*Bar, error) { ... }\n```\n\nAn example of an ast can be found [here](example/bools/ast/ast.go)\n\n## Users\n\nThese projects use gocc:\n\n* [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\n* [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)\n* [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\n* [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\n* [gographviz](https://github.com/awalterschulze/gographviz) - [BNF file](https://github.com/awalterschulze/gographviz/blob/main/dot.bnf) - Parses the Graphviz DOT language in golang\n* [katydid](http://katydid.github.io/) - [BNF file](https://github.com/katydid/validator-go/blob/main/validator/bnf/all.bnf) - Encoding agnostic validation language\n* [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\n* [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.\n* [nesgo](https://github.com/retroenv/nesgo) - [BNF file](https://github.com/retroenv/nesgo/blob/main/internal/gocc/lang.bnf) - A Go compiler for NES\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoccmack%2Fgocc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoccmack%2Fgocc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoccmack%2Fgocc/lists"}