npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-c-parser

v1.0.11

Published

NodeJS library to parse C code.

Readme

node-c-parser

What is it?

Syntax parsing library for C programming language in Node.js. This node module uses node-c-lexer for lexical analysis purpose and accepts token stream formatted in the exact same format as it is in node-c-lexer. The parser takes this token stream as input and gives the parse tree as output. The parse tree is actually a javascript object. Further analysis on this JSON formatted parse tree can be run if necessary.

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.

Installation

Can be installed through npm with npm install node-c-parser.

Usage

There is only one API endpoint to use for parsing if token stream is ready. Otherwise it is necessary to generate token stream before parsing. To generate token stream, lexical analyzer unit of this module have to be used.

Let following code to be parsed -

#include <stdio.h>

int main(){
    printf("Hello World!");
    return 0;
}
  1. Require the module:

    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.

    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:

    var tokens = parser.lexer.lexUnit.tokenize(codeText);
  4. Parse:

    var parse_tree = parser.parse(tokens);

Parse tree of the above C code would be like this.

Parse Tree Structure

As it is said earlier that the parse tree is actually a javascript object. Structure of parse tree is defined in following JSON-Schema.

{
    "$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": "#"}
                ]
            }
        }
    }
}

Bug Report

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.