nearley vs pegjs vs jison
Parsing Libraries for JavaScript Comparison
3 Years
nearleypegjsjisonSimilar Packages:
What's Parsing Libraries for JavaScript?

Parsing libraries in JavaScript are tools that help developers analyze and process text or code by breaking it down into its component parts. These libraries are essential for tasks such as interpreting programming languages, processing structured data formats (like JSON or XML), or implementing domain-specific languages (DSLs). They typically use techniques like lexical analysis (tokenization) and syntactic analysis (parsing) to convert input text into a structured format, such as an abstract syntax tree (AST), which can then be manipulated or analyzed programmatically. Parsing libraries are widely used in compilers, interpreters, code editors, and any application that needs to understand and process text-based input. jison is a JavaScript library that generates parsers from a BNF-like grammar, making it easy to create custom parsers for various text formats. nearley is a fast, powerful, and easy-to-use parsing library that supports context-free grammars and allows for the creation of parsers using a simple, intuitive syntax. pegjs is a parser generator for JavaScript that uses Parsing Expression Grammars (PEGs) to create efficient parsers, offering a clear and expressive way to define grammars for complex languages and data formats.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
nearley3,291,024
3,707-1985 years agoMIT
pegjs636,104
4,903-1179 years agoMIT
jison65,114
4,381-1628 years agoMIT
Feature Comparison: nearley vs pegjs vs jison

Grammar Definition

  • nearley:

    nearley supports context-free grammars and allows for more expressive grammar definitions, including the use of multiple rules for the same non-terminal. This flexibility enables you to define complex grammatical structures with ease.

  • pegjs:

    pegjs uses Parsing Expression Grammar (PEG) notation, which provides a clear and unambiguous way to define grammars. PEGs allow for more precise control over parsing behavior, making it easier to handle ambiguities and define complex parsing rules.

  • jison:

    jison uses a BNF-like syntax for grammar definition, allowing you to specify production rules, tokens, and actions directly in the grammar file. This approach provides a clear and structured way to define how input text should be parsed.

Parser Generation

  • nearley:

    nearley generates parsers that are fast and memory-efficient, with a focus on performance for context-free grammars. The generated parsers can handle large inputs and complex grammatical structures with minimal overhead.

  • pegjs:

    pegjs generates parsers from PEG grammars, producing compact and efficient JavaScript code. The generated parsers are easy to use and integrate, with built-in support for error handling and debugging.

  • jison:

    jison generates parsers as JavaScript code from the defined grammar, allowing for easy integration into web applications or Node.js projects. The generated parsers are efficient and can handle both lexical and syntactic analysis.

Error Handling

  • nearley:

    nearley includes built-in error handling for syntax errors, with support for customizable error messages and recovery. The library provides tools for debugging and visualizing parsing errors, making it easier to identify and fix issues in the grammar.

  • pegjs:

    pegjs offers robust error handling, including detailed error messages and support for custom error types. The generated parsers provide clear feedback on parsing failures, which helps developers diagnose and resolve issues quickly.

  • jison:

    jison provides customizable error handling, allowing you to define how parsing errors are reported and handled. You can specify error messages, recovery strategies, and actions to take when errors occur during parsing.

Performance

  • nearley:

    nearley is known for its high performance, especially with context-free grammars. It uses a packrat parsing algorithm that ensures linear time parsing for many grammars, making it one of the fastest parsing libraries available.

  • pegjs:

    pegjs generates efficient parsers that perform well for a wide range of grammars. However, the performance can vary depending on the complexity of the PEG grammar and the presence of backtracking.

  • jison:

    jison generates parsers that are optimized for performance, but the speed depends on the complexity of the grammar and the efficiency of the parsing algorithms used. For most use cases, jison provides fast parsing with minimal overhead.

Ease of Use: Code Examples

  • nearley:

    nearley Example

    // Define a simple grammar in Nearley
    const grammar = `
    @{%
    const { Parser } = require('nearley');
    const grammar = require('./my-grammar.ne'); // Import your Nearley grammar
    %}
    
    // Create a parser instance
    const parser = new Parser(grammar);
    
    // Parse some input
    parser.feed('your input here');
    
    // Get the parse result
    const result = parser.results;
    console.log(result);
    
  • pegjs:

    pegjs Example

    // Define a simple PEG grammar
    const grammar = `
    start
      = expression
    
    expression
      = left:term op:('+' / '-') right:term {
          return { type: 'expression', operator: op, left, right };
        }
    
    term
      = left:factor op:('*' / '/') right:factor {
          return { type: 'term', operator: op, left, right };
        }
    
    factor
      = '(' expr:expression ')' {
          return expr;
        }
      / number:Number
    
    Number
      = digits:[0-9]+ {
          return parseInt(digits.join(''), 10);
        }
    `;
    
    // Create a parser from the PEG grammar
    const parser = peg.generate(grammar);
    
    // Parse an expression
    const result = parser.parse('3 + 4 * 2');
    console.log(result);
    
  • jison:

    jison Example

    // Define a simple grammar in Jison
    const grammar = `
    %lex
    %%
    
    %start start
    
    %%
    
    start
      : expression
      ;
    
    expression
      : expression '+' expression
      | expression '-' expression
      | expression '*' expression
      | expression '/' expression
      | NUMBER
      ;
    
    %%
    
    // Create a parser from the grammar
    const parser = new Parser(grammar);
    
    // Parse an expression
    const result = parser.parse('3 + 4 * 2');
    console.log(result);
    
How to Choose: nearley vs pegjs vs jison
  • nearley:

    Choose nearley if you want a fast and flexible parser that supports context-free grammars and allows for easy integration with existing projects. It is ideal for applications that require high-performance parsing and support for complex grammatical structures.

  • pegjs:

    Choose pegjs if you prefer a parser generator that uses PEGs for grammar definition and you need a simple, yet powerful tool for creating parsers. It is well-suited for projects that require clear and unambiguous grammar definitions, making it easier to understand and maintain the parsing logic.

  • jison:

    Choose jison if you need a parser generator that creates parsers from BNF-like grammars and you want fine-grained control over the parsing process. It is suitable for projects where you need to implement custom parsing logic and handle ambiguities in the grammar.

README for nearley

nearley ↗️

JS.ORG npm version

nearley is a simple, fast and powerful parsing toolkit. It consists of:

  1. A powerful, modular DSL for describing languages
  2. An efficient, lightweight Earley parser
  3. Loads of tools, editor plug-ins, and other goodies!

nearley is a streaming parser with support for catching errors gracefully and providing all parsings for ambiguous grammars. It is compatible with a variety of lexers (we recommend moo). It comes with tools for creating tests, railroad diagrams and fuzzers from your grammars, and has support for a variety of editors and platforms. It works in both node and the browser.

Unlike most other parser generators, nearley can handle any grammar you can define in BNF (and more!). In particular, while most existing JS parsers such as PEGjs and Jison choke on certain grammars (e.g. left recursive ones), nearley handles them easily and efficiently by using the Earley parsing algorithm.

nearley is used by a wide variety of projects:

nearley is an npm staff pick.

Documentation

Please visit our website https://nearley.js.org to get started! You will find a tutorial, detailed reference documents, and links to several real-world examples to get inspired.

Contributing

Please read this document before working on nearley. If you are interested in contributing but unsure where to start, take a look at the issues labeled "up for grabs" on the issue tracker, or message a maintainer (@kach or @tjvr on Github).

nearley is MIT licensed.

A big thanks to Nathan Dinsmore for teaching me how to Earley, Aria Stewart for helping structure nearley into a mature module, and Robin Windels for bootstrapping the grammar. Additionally, Jacob Edelman wrote an experimental JavaScript parser with nearley and contributed ideas for EBNF support. Joshua T. Corbin refactored the compiler to be much, much prettier. Bojidar Marinov implemented postprocessors-in-other-languages. Shachar Itzhaky fixed a subtle bug with nullables.

Citing nearley

If you are citing nearley in academic work, please use the following BibTeX entry.

@misc{nearley,
    author = "Kartik Chandra and Tim Radvan",
    title  = "{nearley}: a parsing toolkit for {JavaScript}",
    year   = {2014},
    doi    = {10.5281/zenodo.3897993},
    url    = {https://github.com/kach/nearley}
}