{"id":20859373,"url":"https://github.com/engineeringsoftware/gocgo","last_synced_at":"2025-07-16T05:36:59.607Z","repository":{"id":239221867,"uuid":"798894028","full_name":"EngineeringSoftware/gocgo","owner":"EngineeringSoftware","description":"C language lexer, parser, and visitors","archived":false,"fork":false,"pushed_at":"2024-05-10T17:49:20.000Z","size":2023,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-12T14:15:51.225Z","etag":null,"topics":["c","go","lexer","parser","visitors"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EngineeringSoftware.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":"2024-05-10T17:44:09.000Z","updated_at":"2024-05-11T12:31:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"a7f56b7c-f4e0-4fca-b975-9425f6c8f2e1","html_url":"https://github.com/EngineeringSoftware/gocgo","commit_stats":null,"previous_names":["engineeringsoftware/gocgo"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/EngineeringSoftware/gocgo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineeringSoftware%2Fgocgo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineeringSoftware%2Fgocgo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineeringSoftware%2Fgocgo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineeringSoftware%2Fgocgo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EngineeringSoftware","download_url":"https://codeload.github.com/EngineeringSoftware/gocgo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineeringSoftware%2Fgocgo/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265485026,"owners_count":23774419,"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":["c","go","lexer","parser","visitors"],"created_at":"2024-11-18T04:49:39.459Z","updated_at":"2025-07-16T05:36:59.581Z","avatar_url":"https://github.com/EngineeringSoftware.png","language":"Go","readme":"## gocgo\n\ngocgo (go-c-go), implemented in Go, includes lexer, parser, and\nvisitors for the C programming language.\n\nThis small project started as a demo of ANTLR and the visitor design\npattern.  It turned out that having a C parser available was valuable\nfor a few other cases on our side, thus, we decided to make it public\nas it might be of interest to others as well.\n\ngocgo is (currently) suitable for anlaysis of C code.  While some\ntransformation is possible, having a flexible transformation API could\nbe added later.\n\n\n## A Quick Example\n\nBelow is an example of a visitor that collects C code into a buffer\nand along the way removes function bodies.\n\n```go\ntype FuncDeleteVisitor struct {\n\t*BaseVisitorImpl\n\tin   bool\n\tBuff bytes.Buffer\n}\n\nfunc NewFuncDeleteVisitor() *FuncDeleteVisitor {\n\treturn \u0026FuncDeleteVisitor{in: false}\n}\n\nfunc (v *FuncDeleteVisitor) VisitTerminal(n antlr.TerminalNode) error {\n\tv.Buff.WriteString(n.GetSymbol().GetText())\n\tv.Buff.WriteString(\" \")\n\treturn nil\n}\n\nfunc (v *FuncDeleteVisitor) VisitFunctionDefinition(ctx *parsing.FunctionDefinitionContext) (bool, error) {\n\tv.in = true\n\treturn true, nil\n}\n\nfunc (v *FuncDeleteVisitor) VisitFunctionDefinitionEnd(ctx *parsing.FunctionDefinitionContext) error {\n\tv.in = false\n\treturn nil\n}\n\nfunc (v *FuncDeleteVisitor) VisitBlockItemList(ctx *parsing.BlockItemListContext) (bool, error) {\n\tif v.in {\n\t\treturn false, nil\n\t} else {\n\t\treturn true, nil\n\t}\n}\n```\n\n\n## Usage\n\nWe provide a complete example of running the aforementioned visitor as\na command in this repo.  You can run the following:\n\n```\ngo run ./cmd/gocgo/gocgo.go cprogs/for.c \n```\n\nYou can take a look at the command for details, but it is rather\nsimple code.\n\n\n## Architecture and Design\n\nEach visitor should embed a pointer to `BaseVisitorImpl`, which\nprovides the default implementation of `Visit` methods.\n\nThere is a `VisitX` method for each type of AST node (e.g.,\n`VisitFunctionDefinition` for a function definition).  Consider this\nexample:\n\n```go\nfunc (v *FuncDeleteVisitor) VisitFunctionDefinition(ctx *parsing.FunctionDefinitionContext) (bool, error) {\n\tv.in = true\n\treturn true, nil\n}\n```\n\nThis `Visit` method is invoked for every function definition in parsed\nC code.  There are two return values.  The first value (`bool`) says\nif children should be visited.  The second value (`error`) is the\n`error` Go interface.\n\nEach `VisitX` method has a corresponding `VisitXEnd` method invoked\nonce all children of the node are visited.  From our earlier example:\n\n```go\nfunc (v *FuncDeleteVisitor) VisitFunctionDefinitionEnd(ctx *parsing.FunctionDefinitionContext) error {\n\tv.in = false\n\treturn nil\n}\n```\n\n`VisitXEnd` methods have a single return value (`error`).\n\nThis architecture is common in many tools, e.g., visitors in the\nEclipse implementation.\n\n\n## Implementation\n\ngocgo cuts a few steps for you if you were planning to use ANTLR to\nobtain a parser for the C programming language.\n\nWe used the grammar from the [ANTLR\nrepo](https://github.com/antlr/grammars-v4/blob/19de2a44eaef0de599044b76ede8492fb8e07b4d/c/C.g4).\n\nIf you wish to reproduce the steps to get lexer, parser, and the\noriginal visitors, you can run the following command in the root of\nthis repo:\n\n```bash\ngo generate ./...\n```\n\nWe made a few changes to simplify the usage of visitors and address\nsome issues (e.g., [Issue\n4398](https://github.com/antlr/antlr4/issues/4398)).  Below is the\nsummary of key changes / additions:\n\n* Addressed the issue 4398, by introducing (locally in this repo)\n  `VisitChildren`.\n\n* Removed the generated `c_base_visitor.go`, because it limits\n  changing behavior of any `Visit` method.\n\n* Introduced our own visitor interface (`BaseVisitor`) and its\n  implementation (`BaseVisitorImpl`) that enable proper struct\n  embedding.  Your visitor can now change behavior of (a subset of)\n  `Visit` methods.\n\n* Updated API design, such that each `Visit` method returns `bool` and\n  `error` (rather than `interface{}`). We find the new approach more\n  Go appropriate.\n\n\n## License\n\n[BSD-3-Clause license](LICENSE).\n\n\n## Contact\n\nFeel free to get in touch if you have any comments: Milos Gligoric\n`\u003cmilos.gligoric@gmail.com\u003e`.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fengineeringsoftware%2Fgocgo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fengineeringsoftware%2Fgocgo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fengineeringsoftware%2Fgocgo/lists"}