jison

repository·master·Indexed 26 days ago

https://github.com/zaach/jison

A parser generator with Bison's API for creating bottom-up parsers in JavaScript. It converts JSON-encoded or Bison-style grammars into JavaScript files. Jison provides a command-line interface for generating parsers and a Parser class for programmatic use. It supports various parser algorithms including lr0, slr, lalr, and lr, and offers a PHP port for generating PHP parsers from .jison files.

Tokens
1.9K
Snippets
6
Records
12
Agent score
88%

What's inside jison

  1. Generate a parser using the Jison CLI

    master

    Use the jison command to generate a JavaScript parser from a grammar file. By default, this creates a .js file in your current working directory named after the input file.

    jison calculator.jison
  2. Generate a parser from a Jison file using the CLI

    master

    You can use the Jison command-line interface to generate a parser from a .jison definition file. This is useful for quick prototyping or when you want to generate a parser file directly from your terminal.

    $ node ../lib/cli.js basic_lex.jison
  3. Install and run the Jison PHP port

    master

    To use this port, you must have jison and node.js installed on your system. Navigate to the directory containing your .jison file in your command line interface and execute the php.js script using node.

    Run the following command structure: nodejs /location_of_jison/ports/php/php.js my_jison_file.jison

    nodejs /location_of_jison/ports/php/php.js my_jison_file.jison
  4. Use //js and //php comments to manage multi-language parser sections

    master

    The PHP port allows you to include both JavaScript and PHP logic within the same .jison file. The wrapper uses specific comment markers to decide which code to strip and which to uncomment for the PHP output:

    • //js: Starts a JavaScript area. This block is removed from the final PHP parser.
    • //: Ends the current commenting state (used to close a //js block).
    • //php: Starts a PHP area. The comment is removed in the PHP output so the code becomes executable.
    • No comment: The line is left as-is (treated as standard Jison/JavaScript).

    Note: Comments inside /*php ... */ blocks are also uncommented for the PHP parser.

     : content
    	{$$ = $1;} //<--this is left alone
     | contents content
    	{
    	    //js
    		$$ = join($1, $2); //<--this is stripped in the php parser
    
    	    //php $$ = $SomeClass->someMethod($1, $2); //<--this is uncommented in the php parser
    	}
  5. Generate parsers programmatically with the Parser class

    master

    You can use Jison as a CommonJS module to generate parsers from a JSON grammar or a grammar string.

    1. Import the Parser class from jison.
    2. Instantiate new Parser(grammar) where grammar is a JSON object or a string.
    3. Call .generate() to get the source code as a string, or call .parse(input) to use the parser directly in memory.
    var Parser = require("jison").Parser;
    
    // a grammar in JSON
    var grammar = {
        "lex": {
            "rules": [
               ["\\s+", "/* skip whitespace */"],
               ["[a-f0-9]+", "return 'HEX';"]
            ]
        },
    
        "bnf": {
            "hex_strings" :[ "hex_strings HEX",
                             "HEX" ]
        }
    };
    
    var parser = new Parser(grammar);
    
    // generate source, ready to be written to disk
    var parserSource = parser.generate();
    
    // use the parser directly from memory
    // returns true
    parser.parse("adfe34bc e82a");
    
    // throws lexical error
    parser.parse("adfe34bc zxg");
  6. Configure the PHP parser output via //option comments

    master

    You can configure the generated PHP file by adding comments starting with //option inside your .jison file. The syntax is //option optionName:value.

    Supported options:

    • namespace: Sets the PHP namespace (default: Jison)
    • class: Sets the PHP class name (default: the .jison filename without extension)
    • fileName: Sets the output filename (default: the .jison filename without extension followed by .php)
  7. Jison CLI Reference

    master

    The Jison command line interface follows this usage pattern:

    jison [file] [lexfile] [options]

    Arguments:

    • file: file containing a grammar
    • lexfile: file containing a lexical grammar

    Options:

    • -j, --json: force jison to expect a grammar in JSON format
    • -o FILE, --outfile FILE: Filename and base module name of the generated parser
    • -t, --debug: Debug mode
    • -m TYPE, --module-type TYPE: The type of module to generate (commonjs, amd, js)
    • -p TYPE, --parser-type TYPE: The type of algorithm to use for the parser (lr0, slr, lalr, lr)
    • -V, --version: print version and exit
  8. Create and run a parser instance

    master
    Once a Jison.Generator has been initialized, you can create a functional parser instance using the .createParser() method. Use the .parse(source) method on the parser instance to process a source string. The .parse() method will return the parsed result or throw an error if the source does not conform to the grammar.
  9. Generate a parser using Jison.Generator

    master

    To create a parser instance, instantiate Jison.Generator with a grammar configuration object and an options object. The configuration object can be a JSON representation of the grammar or a BNF-parsed object.

    Supported parsing types include slr, ll, and others. If the generator supports lookahead computation, you should call .computeLookaheads() on the resulting instance.

    Note: The Jison.print method is explicitly disabled in this web entrypoint environment.

  10. Reference: Jison CLI Options

    master

    The following options are available when using the jison command line interface:

    --file <file>          file containing a grammar
    --lexfile <file>       file containing a lexical grammar
    --json, -j             force jison to expect a grammar in JSON format
    --outfile, -o <FILE>    Filename and base module name of the generated parser
    --debug, -t             Debug mode
    --module-type, -m <TYPE> The type of module to generate (commonjs, amd, js)
    --parser-type, -p <TYPE> The type of algorithm to use for the parser (lr0, slr, lalr, lr)
    --version, -V           print version and exit
  11. Use the Jison CLI to generate parsers

    master

    The Jison CLI allows you to generate parser code from a grammar file (or via stdin) and an optional lexical grammar file. If no file is provided, it reads from stdin.

    Command Line Options

    FlagAbbrArgumentDescription
    --fileFILEFile containing a grammar (positional 0)
    --lexfileFILEFile containing a lexical grammar (positional 1)
    --json-jForce Jison to expect a grammar in JSON format
    --outfile-oFILEFilename and base module name of the generated parser
    --debug-tEnable debug mode
    --module-type-mTYPEThe type of module to generate (commonjs, amd, or js). Defaults to commonjs
    --parser-type-pTYPEThe type of algorithm to use (lr0, slr, lalr, or lr). Defaults to lalr
    --version-VPrint version and exit

    Usage Examples

    Generate a parser from a grammar file:

    jison my_grammar.jison

    Generate a parser with a specific output filename and module type:

    jison my_grammar.jison --lexfile my_lexer.l --outfile my_parser.js --module-type amd

    Generate a parser from a JSON grammar file:

    jison my_grammar.json --json