NodeJS library to parse C code.
npm install node-c-parsernode-c-lexer for lexical analysis purpose and accepts token stream formatted in the exact same format as it is in node-c-lexer.
C programming language grammars are taken from here.
Before implementation left recursion is removed from this grammar set.
Final grammar set on which this parser is implemented can be found in GRAMMARS.md file of this project.
npm install node-c-parser.Let following code to be parsed -
``c
#include
int main(){
printf("Hello World!");
return 0;
}
``
1. Require the module:
js`
var parser = require("node-c-parser");
2. Remove preprocessors: Before doing anything on source code at first preprocessors
need to be removed.
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.`
js`
parser.lexer.cppUnit.clearPreprocessors("./a.c", function(err, codeText){
if(err){
// Error occured during preprocessor removal. Handle it.
}
else{
// codeText variable contains preprocessor free code. Do something with it.
}
});
3. Tokenize:
`js`
var tokens = parser.lexer.lexUnit.tokenize(codeText);
4. Parse:
`js`
var parse_tree = parser.parse(tokens);
Parse tree of the above C code would be like this.
js
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"token": {
"type": "object",
"properties": {
"lexeme": {"type": "string"},
"row": {"type": "integer", "minimum": 0},
"col": {"type": "integer", "minimum": 0},
"tokenClass": {"type": "string"},
"parent": {"type": "null"},
"children": {"type": "null"},
"keyword": {"type": "boolean"}
},
"required": ["lexeme", "row", "col", "tokenClass", "parent", "children"]
}
},
"type": "object",
"properties": {
"title": {"type": "string"},
"children": {
"type": "array",
"items": {
"oneOf": [
{"$ref": "#/definitions/token"},
{"$ref": "#"}
]
}
}
}
}
`$3
The module is still very naive.
There must be lots of bugs lurking in the code.
Please report any bug by creating an issue with details.
Or it would be better if you could create a pull request with the failed test case added to the unit tests.
If you think the bug is in node-c-lexer` then report it that repository in the mentioned way.