prettybnf provides a parser for Backus-Naur Form grammars and a stringifier
for pretty-printing BNF abstract syntax trees. It uses the traditional BNF
syntax as opposed to
You can use
prettybnf both in Node.js apps and in modern browsers. Only
browsers that implement ECMAScript 5.1 with strict mode are supported. This
includes Chrome 13+, Firefox 4+, Safari 5.1+, IE 10+, and Opera 12+
To get the library:
npm install prettybnfand add
var prettybnf = require('prettybnf')to your scripts.
prettybnf.jssomewhere and add
var prettybnf = require('./path/to/prettybnf.js')to your scripts.
prettybnf.jssomewhere and add the usual
<script src="path/to/prettybnf.js"></script>tag to your HTML. This will create a global object named
If you're not using
npm you can download the latest version of
from GitHub here.
There are four top-level exports on the
prettybnf.version: string defining your version of the library
prettybnf.parse(grammar): parses a BNF string and returns the AST
prettybnf.stringify(ast): serializes an AST to a BNF string
prettybnf.Parser(grammar): internals of the parser for testing and hacking
The parser constructs an AST, composed of AST nodes, from a grammar. Each node
is an object with a
type property that specifies the type of node. The node
types, which correspond to the BNF syntax recognized by the parser, are as
prettybnf.parse returns a
Each node has other relevant properties, described as follows:
text: the terminal
Stringspecifying the name of the nonterminal
The BNF syntax recognized by the parser is defined in the file
which is itself written in BNF.
Written by Daniel Connelly email@example.com (http://dhconnelly.com).
Released under the 2-clause BSD license; see LICENSE for more details.