- Simple and expressive grammar syntax
- No separate lexical analysis step — both lexical and syntactical analysis are handled by one tool
- Handles wide class of grammars (superset of LL(k) and LR(k))
- Precise and human-friendly error reporting
Using PEG.js is easy:
var parser = PEG.buildParser("start = ('a' / 'b')+"); parser.parse("abba"); // returns ["a", "b", "b", "a"] parser.parse("abcd"); // throws an exception with details about the error
Basically, you need to generate a parser from your grammar and then use it to parse the input.
Generating a Parser
There are three ways how to generate the parser:
- Using the online generator
- Using the
- Using the command line
The online generator is easiest to use — you just enter your grammar and download the generated parser code. The parser object will be available in a global variable you specify (
parser by default).
lib/compiler.js file and use the
PEG.buildParser function. This function accepts a string with a grammar and either returns the built parser object or throws an exception if the grammar is invalid.
To generate the parser from a command line, you need to have Java installed (so that Rhino — which is included in PEG.js — can run). Use the
bin/pegjs script on Unix or
bin/pegjs.bat batch file on Windows:
$ bin/pegjs arithmeticsParser examples/arithmetics.pegjs
This command will create the parser from the
examples/arithmetics.pegjs file and put in into the
examples/arithmetics.js file. The parser object will be available in the
arithmeticsParser global variable. To learn more about the generator usage, use the
Using the Generated Parser
PEG.buildParser) and use the
parse method on the parser object. This method accepts an input string and either returns the parse result (dependent on the actions you specified in the grammar) or throws
PEG.grammarParser.SyntaxError exception if the input contains a syntax error. The exception has properties
column, which contain details about the error.
The parser object also has the
toSource method that returns its textual representation.
For detailed description of the grammar see the online documentation.
Both the parser generator and generated parsers should run well in the following environments: