Registry / serialization / bnf
library1.0.1jsnpmunverified

BNF is a JavaScript library for compiling, parsing, and interpreting BNF and ABNF grammars. Version 1.0.1 uses ECMAScript 6+ and supports inline compilation of grammars, predefined rules (e.g., DIGIT, CRLF, LITERAL), rule events, and multi-language scripts (planned). It runs on Node.js. Unlike other parser generators (e.g., PEG.js, ANTLR), BNF focuses on simplicity and direct embedding of BNF/ABNF syntax without a separate generation step. The library is in early development, with custom rule APIs subject to change.

npm install bnf
INSTALL
IMPORT
SIG · BNF
B
bnf
serializationjavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Compiler
import { Compiler } from 'bnf'
const Compiler = require('bnf').Compiler
ESM import path is 'bnf'. The package may also support CommonJS require, but ESM is preferred for v1.
BNF
import { BNF } from 'bnf'
const bnf = require('bnf')
BNF is a named export. Do not default-import.
ABNF
import { ABNF } from 'bnf'
import ABNF from 'bnf'
ABNF is a named export, not default.

Creates a compiler, adds a calculator grammar, sets rule events, and parses expressions.

import { Compiler } from 'bnf'; const compiler = new Compiler(); compiler.AddLanguage(` <SYNTAX> ::= <evaluation> | <evaluation> <CRLF> <SYNTAX> <evaluation> ::= <number> <OWSP> <type> <OWSP> <number> <number> ::= <DIGITS> <type> ::= "+" | "-" | "/" | "*" `, "testLang"); compiler.SetRuleEvents({ evaluation(token) { console.log("evaluation token found answer:", eval(token.value)); } }); compiler.ParseScript(` 5456 / 13 11 + 3 10 * 8 `.trim());
Debug
Known issues
breakingCustom BNF rules API is unstable and will change during minor version updates.
fix
Avoid writing custom rules until v2 stabilizes, or pin to a specific minor version.
affects: ~1.0.0
deprecatedThe prior version used a custom JavaScript markup for script files; this feature is removed in v1.
fix
Migrate to inline compiling with AddLanguage and ParseScript.
affects: <1.0.0
gotchaMethod names are PascalCase (e.g., AddLanguage, ParseScript) unlike typical JavaScript camelCase.
fix
Use exactly the documented casing: AddLanguage, SetRuleEvents, ParseScript.
affects: >=1.0.0
gotchaThe predefined rule SYNTAX must be declared in every grammar; missing it causes parse failure.
fix
Always include a rule named SYNTAX as the entry point.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: compiler.AddLanguage is not a function
Importing the library incorrectly (e.g., default import or wrong path).
fix
Use `import { Compiler } from 'bnf';` and instantiate with `new Compiler()`.
Error: No language registered
Calling ParseScript before AddLanguage.
fix
Ensure AddLanguage is called with a valid grammar before parsing.
SyntaxError: Unexpected token
Invalid BNF/ABNF syntax (e.g., missing angle brackets or incorrect repeat notation).
fix
Validate grammar using BNF syntax: `<rule> ::= <expression> | "literal"` or ABNF: `rule = expression / "literal"`.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources
bnf — npm install bnf · libregistry