[![Build status](https://img.shields.io/travis/pegjs/pegjs.svg)](https://travis-ci.org/pegjs/pegjs) [![npm version](https://img.shields.io/npm/v/pegjs.svg)](https://www.npmjs.com/package/pegjs) [![Bower version](https://img.shields.io/bower/v/pegjs.svg)](https://github.com/pegjs/bower) [![License](https://img.shields.io/badge/license-mit-blue.svg)](https://opensource.org/licenses/MIT) PEG.js ====== PEG.js is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting. You can use it to process complex data or computer languages and build transformers, interpreters, compilers and other tools easily. Features -------- * Simple and expressive grammar syntax * Integrates both lexical and syntactical analysis * Parsers have excellent error reporting out of the box * Based on [parsing expression grammar](http://en.wikipedia.org/wiki/Parsing_expression_grammar) formalism — more powerful than traditional LL(*k*) and LR(*k*) parsers * Usable [from your browser](http://pegjs.org/online), from the command line, or via JavaScript API Getting Started --------------- [Online version](http://pegjs.org/online) is the easiest way to generate a parser. Just enter your grammar, try parsing few inputs, and download generated parser code. Installation ------------ ### Node.js To use the `pegjs` command, install PEG.js globally: ```console $ npm install -g pegjs ``` To use the JavaScript API, install PEG.js locally: ```console $ npm install pegjs ``` If you need both the `pegjs` command and the JavaScript API, install PEG.js both ways. ### Browser [Download](http://pegjs.org/#download) the PEG.js library (regular or minified version) or install it using Bower: ```console $ bower install pegjs ``` Generating a Parser ------------------- PEG.js generates parser from a grammar that describes expected input and can specify what the parser returns (using semantic actions on matched parts of the input). Generated parser itself is a JavaScript object with a simple API. ### Command Line To generate a parser from your grammar, use the `pegjs` command: ```console $ pegjs arithmetics.pegjs ``` This writes parser source code into a file with the same name as the grammar file but with “.js” extension. You can also specify the output file explicitly: ```console $ pegjs arithmetics.pegjs arithmetics-parser.js ``` If you omit both input and output file, standard input and output are used. The generated parser is in the [UMD](https://github.com/umdjs/umd) format, which means it works as a Node.js or AMD module. You can also use the `-e`/`--export-var` option to define a global variable into which the parser object is assigned to when no module loader is detected. You can tweak the generated parser with several options: * `--cache` — makes the parser cache results, avoiding exponential parsing time in pathological cases but making the parser slower * `--allowed-start-rules` — comma-separated list of rules the parser will be allowed to start parsing from (default: the first rule in the grammar) * `--plugin` — makes PEG.js use a specified plugin (can be specified multiple times) * `--extra-options` — additional options (in JSON format) to pass to `peg.generate` * `--extra-options-file` — file with additional options (in JSON format) to pass to `peg.generate` * `--trace` — makes the parser trace its progress ### JavaScript API In Node.js, require the PEG.js parser generator module: ```javascript var peg = require("pegjs"); ``` In browser, include the PEG.js library in your web page or application using the `