{"id":22663099,"url":"https://github.com/taufique71/node-c-parser","last_synced_at":"2025-04-12T07:22:07.565Z","repository":{"id":65424632,"uuid":"48035653","full_name":"taufique71/node-c-parser","owner":"taufique71","description":"A recursive decent parser for C programming language codes","archived":false,"fork":false,"pushed_at":"2017-07-30T11:04:01.000Z","size":780,"stargazers_count":38,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-03T19:51:43.025Z","etag":null,"topics":["c","grammar","parsing","programming-language"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/taufique71.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-15T10:21:17.000Z","updated_at":"2024-11-01T00:58:59.000Z","dependencies_parsed_at":"2023-01-23T10:55:20.860Z","dependency_job_id":null,"html_url":"https://github.com/taufique71/node-c-parser","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taufique71%2Fnode-c-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taufique71%2Fnode-c-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taufique71%2Fnode-c-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taufique71%2Fnode-c-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taufique71","download_url":"https://codeload.github.com/taufique71/node-c-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247657275,"owners_count":20974344,"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","grammar","parsing","programming-language"],"created_at":"2024-12-09T12:17:14.585Z","updated_at":"2025-04-12T07:22:07.546Z","avatar_url":"https://github.com/taufique71.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# node-c-parser\n\n### What is it?\nSyntax parsing library for C programming language in Node.js.\nThis node module uses [`node-c-lexer`](https://github.com/taufique71/node-c-lexer) for lexical analysis purpose and accepts token stream formatted in the exact same format as it is in `node-c-lexer`.\nThe parser takes this token stream as input and gives the parse tree as output. \nThe parse tree is actually a javascript object. \nFurther analysis on this JSON formatted parse tree can be run if necessary.\n\n\nC programming language grammars are taken from [here](https://gist.github.com/taufique71/a8e0950b0ca21a9564675723c7998052). \nBefore implementation left recursion is removed from this grammar set. \nFinal grammar set on which this parser is implemented can be found in [GRAMMARS.md](https://github.com/taufique71/node-c-parser/blob/master/GRAMMARS.md) file of this project.\n\n### Installation\nCan be installed through npm with `npm install node-c-parser`.\n\n### Usage\nThere is only one API endpoint to use for parsing if token stream is ready. \nOtherwise it is necessary to generate token stream before parsing. \nTo generate token stream, lexical analyzer unit of this module have to be used.\n\nLet following code to be parsed - \n\n```c\n#include \u003cstdio.h\u003e\n\nint main(){\n    printf(\"Hello World!\");\n    return 0;\n}\n```\n1. **Require the module**: \n    ```js\n    var parser = require(\"node-c-parser\");\n    ```\n\n2. **Remove preprocessors**: Before doing anything on source code at first preprocessors \n    need to be removed. \n    Suppose the code is saved to a file named `a.c` and the file resides in the    same directory from where the script is run.\n    ```js\n    parser.lexer.cppUnit.clearPreprocessors(\"./a.c\", function(err, codeText){\n        if(err){\n            // Error occured during preprocessor removal. Handle it.\n        }\n        else{\n            // codeText variable contains preprocessor free code. Do something with it.\n        }\n    });\n    ```\n\n3. **Tokenize**:\n    ```js\n    var tokens = parser.lexer.lexUnit.tokenize(codeText);\n    ```\n\n4. **Parse**:\n    ```js\n    var parse_tree = parser.parse(tokens);\n    ```\n\nParse tree of the above C code would be like [this](http://paste.ubuntu.com/17739375/).\n\n### Parse Tree Structure\nAs it is said earlier that the parse tree is actually a javascript object.\nStructure of parse tree is defined in following JSON-Schema.\n```js\n{\n    \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n    \"definitions\": {\n        \"token\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"lexeme\": {\"type\": \"string\"},\n                \"row\": {\"type\": \"integer\", \"minimum\": 0},\n                \"col\": {\"type\": \"integer\", \"minimum\": 0},\n                \"tokenClass\": {\"type\": \"string\"},\n                \"parent\": {\"type\": \"null\"},\n                \"children\": {\"type\": \"null\"},\n                \"keyword\": {\"type\": \"boolean\"}\n            },\n            \"required\": [\"lexeme\", \"row\", \"col\", \"tokenClass\", \"parent\", \"children\"]\n        }\n    },\n    \"type\": \"object\",\n    \"properties\": {\n        \"title\": {\"type\": \"string\"},\n        \"children\": {\n            \"type\": \"array\",\n            \"items\": {\n                \"oneOf\": [\n                    {\"$ref\": \"#/definitions/token\"},\n                    {\"$ref\": \"#\"}\n                ]\n            }\n        }\n    }\n}\n```\n\n### Bug Report\nThe module is still very naive. \nThere must be lots of bugs lurking in the code. \nPlease report any bug by creating an issue with details. \nOr it would be better if you could create a pull request with the failed test case added to the unit tests.\nIf you think the bug is in `node-c-lexer` then report it that repository in the mentioned way.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaufique71%2Fnode-c-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaufique71%2Fnode-c-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaufique71%2Fnode-c-parser/lists"}