chevrotain vs jison vs nearley vs peggy
JavaScript Parser Generators
chevrotainjisonnearleypeggySimilar Packages:

JavaScript Parser Generators

Parser generators are tools that automatically generate parsers from a formal grammar specification. They are used to analyze and process structured text, such as programming languages, data formats, or domain-specific languages. Parser generators take a grammar definition as input and produce code that can parse input text according to the specified rules. This process is known as parsing, and the generated parsers can be used for tasks such as syntax analysis, code compilation, data validation, and transformation. Parser generators are widely used in compiler construction, interpreters, and various applications that require structured text processing. They help automate the creation of parsers, ensuring correctness and efficiency while reducing the manual effort required to implement parsing logic. chevrotain is a modern, fast, and feature-rich parser-building toolkit for JavaScript that allows developers to create custom parsers with fine-grained control over the parsing process. It is designed for performance and flexibility, making it suitable for complex parsing tasks and language implementations. jison is a parser generator for JavaScript that takes a grammar specification (similar to Yacc/Bison) and generates a parser in JavaScript. It is useful for creating parsers for domain-specific languages, interpreters, or compilers directly in JavaScript. nearley is a simple, fast, and powerful parser generator for JavaScript that supports ambiguous grammars and allows for more flexible parsing strategies. It is designed to be easy to use and is particularly well-suited for parsing complex or non-deterministic grammars. peggy is a modern parser generator for JavaScript based on the Parsing Expression Grammar (PEG) formalism. It is a fork of the popular PEG.js project, offering improved performance, better error handling, and additional features. Peggy allows developers to define grammars using a clear and concise syntax, making it easy to create parsers for a wide range of applications.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
chevrotain02,7831.23 MB314 months agoApache-2.0
jison04,386-1629 years agoMIT
nearley03,741-1986 years agoMIT
peggy01,170592 kB364 months agoMIT

Feature Comparison: chevrotain vs jison vs nearley vs peggy

Parsing Strategy

  • chevrotain:

    chevrotain uses a top-down parsing approach with support for recursive descent and custom parsing strategies. It allows developers to implement their own parsing logic while providing tools for building efficient parsers.

  • jison:

    jison uses a bottom-up parsing approach (LALR(1)) similar to Yacc/Bison. It generates parsers based on the grammar specification, making it suitable for deterministic context-free grammars.

  • nearley:

    nearley uses the Earley parsing algorithm, which can handle all context-free grammars, including ambiguous ones. This makes it more flexible than traditional parsers, especially for complex or non-deterministic grammars.

  • peggy:

    peggy uses the Parsing Expression Grammar (PEG) formalism, which is a top-down parsing approach with backtracking. PEGs are deterministic, meaning they do not allow ambiguity, which can lead to more predictable parsing behavior.

Grammar Specification

  • chevrotain:

    chevrotain allows grammar to be defined programmatically using JavaScript, providing flexibility and control over the parsing process. This approach enables dynamic grammar construction and customization.

  • jison:

    jison uses a BNF-like syntax for grammar specification, similar to Yacc/Bison. This makes it familiar to developers with experience in traditional parser generators.

  • nearley:

    nearley uses a simple and intuitive syntax for defining grammars, which supports both deterministic and ambiguous rules. It also allows for the inclusion of custom JavaScript code within the grammar.

  • peggy:

    peggy uses a PEG-based syntax for grammar definition, which is clear and concise. PEGs are designed to be easy to read and write, making peggy a good choice for developers who prefer this formalism.

Error Handling

  • chevrotain:

    chevrotain provides tools for implementing custom error handling and recovery strategies. It allows developers to create detailed error messages and handle parsing errors in a way that best suits their application.

  • jison:

    jison generates parsers with basic error handling capabilities, including the ability to define custom error messages and recovery rules. However, error handling is limited compared to more modern tools.

  • nearley:

    nearley has limited built-in error handling, but it allows developers to implement their own error reporting and recovery mechanisms. The flexibility of the Earley algorithm can make error handling more complex.

  • peggy:

    peggy offers improved error handling compared to its predecessor, PEG.js, with better support for reporting syntax errors and providing meaningful messages. It also allows for custom error handling within the grammar.

Performance

  • chevrotain:

    chevrotain is designed for high performance, especially for large and complex grammars. Its architecture allows for efficient parsing with minimal overhead, making it suitable for real-time applications.

  • jison:

    jison generates efficient parsers based on the LALR(1) algorithm, which is well-suited for deterministic grammars. However, performance can vary depending on the complexity of the grammar and the generated code.

  • nearley:

    nearley is generally slower than LALR parsers due to the Earley algorithm's complexity, especially for ambiguous grammars. However, it offers greater flexibility and can handle a wider range of grammars.

  • peggy:

    peggy is optimized for performance within the constraints of PEG parsing. While PEG parsers can be slower than LALR parsers for certain grammars, peggy is designed to generate efficient code that minimizes backtracking.

Ease of Use: Code Examples

  • chevrotain:

    chevrotain Example

    const { createToken, Lexer, Parser } = require('chevrotain');
    
    // Define tokens
    const NumberLiteral = createToken({ name: 'NumberLiteral', pattern: /\d+/ });
    const Plus = createToken({ name: 'Plus', pattern: /\+/ });
    const WhiteSpace = createToken({ name: 'WhiteSpace', pattern: /\s+/, group: Lexer.SKIPPED });
    
    // Create a lexer
    const allTokens = [WhiteSpace, Plus, NumberLiteral];
    const SimpleLexer = new Lexer(allTokens);
    
    // Create a parser
    class SimpleParser extends Parser {
      constructor() {
        super(allTokens);
        const $ = this;
        $.RULE('expression', () => {
          $.CONSUME(NumberLiteral);
          $.CONSUME(Plus);
          $.CONSUME(NumberLiteral);
        });
        this.performSelfAnalysis();
      }
    }
    
    const parser = new SimpleParser();
    const input = '3 + 4';
    const lexingResult = SimpleLexer.tokenize(input);
    parser.input = lexingResult.tokens;
    parser.expression();
    console.log('Parsing completed successfully');
    
  • jison:

    jison Example

    const jison = require('jison');
    
    // Define grammar
    const grammar = {
      lex: {
        rules: [
          ['\s+', ''],
          ['\d+', 'return "NUMBER";'],
          ['\+', 'return "+";'],
          ['$end', 'return "EOF";'],
        ],
      },
      bnf: {
        expression: [
          ['expression + term', 'return $1 + $3;'],
          ['term', 'return $1;'],
        ],
        term: [['NUMBER', 'return Number(yytext);']],
      },
    };
    
    // Create parser
    const parser = new jison.Parser(grammar);
    
    // Parse input
    const result = parser.parse('3 + 4');
    console.log('Result:', result);
    
  • nearley:

    nearley Example

    const nearley = require('nearley');
    const grammar = require('./simple-grammar.js'); // Assume grammar is compiled
    
    // Create parser
    const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
    
    // Parse input
    parser.feed('3 + 4');
    console.log('Parse results:', parser.results);
    
  • peggy:

    peggy Example

    const peggy = require('peggy');
    
    // Define grammar
    const grammar = `
      Expression
        = head:Term tail:(_ "+" _ Term)* {
            return tail.reduce((result, element) => result + element[3], head);
          }
      Term
        = [0-9]+ { return parseInt(text(), 10); }
      _
        = [ \t\n\r]*
    `;
    
    // Generate parser
    const parser = peggy.generate(grammar);
    
    // Parse input
    const result = parser.parse('3 + 4');
    console.log('Result:', result);
    

How to Choose: chevrotain vs jison vs nearley vs peggy

  • chevrotain:

    Choose chevrotain if you need a highly performant and customizable parser with support for complex grammars. It is ideal for projects that require fine-grained control over the parsing process and need to handle large inputs efficiently.

  • jison:

    Choose jison if you are familiar with traditional parser generator tools like Yacc/Bison and need to create parsers using a similar grammar specification format. It is suitable for projects that require a more formal approach to grammar definition and parsing.

  • nearley:

    Choose nearley if you need to parse ambiguous or non-deterministic grammars and want a simple, easy-to-use tool that supports flexible parsing strategies. It is great for projects that require more flexibility in grammar design and can handle multiple interpretations of the input.

  • peggy:

    Choose peggy if you prefer using Parsing Expression Grammars (PEG) and want a modern, efficient parser generator with a clean syntax. It is ideal for projects that benefit from the PEG formalism and need a tool with good performance and error handling.

README for chevrotain

Chevrotain

For details see:

Install

Using npm:

npm install chevrotain

or using yarn:

yarn add chevrotain