The `ebnf` package provides a JavaScript and TypeScript-compatible library for generating Abstract Syntax Tree (AST) parsers from formal grammars defined in either Backus-Naur Form (BNF) or W3C Extended Backus-Naur Form (EBNF). It is currently at version 1.9.1. The library differentiates itself by offering direct AST generation, browser compatibility, and built-in TypeScript type definitions, making it suitable for both Node.js and client-side applications. Releases appear to be driven by bug fixes and minor improvements, without a strict time-based cadence. It's particularly useful for projects requiring custom language parsing or syntax highlighting, such as Domain Specific Languages (DSLs) or code analysis tools.
npm install ebnfVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a simple arithmetic grammar using BNF, create a parser instance, and then use it to generate an Abstract Syntax Tree (AST) for a given expression.
Upgrade to version 1.9.1 or later to mitigate potential security implications related to `eval` usage. Ensure all grammar definitions, especially those originating from untrusted sources, are thoroughly validated before being passed to the parser.
Explicitly choose either `Grammars.BNF.Parser` or `Grammars.W3C.Parser` and ensure your grammar strictly follows the conventions of the chosen format. Consult the respective specifications (BNF, W3C EBNF) for syntax details.
If you need `ALL_UPPER_AND_SNAKE_CASE` rules to be present in the AST, you can deactivate this default behavior by setting the `keepUpperRules: true` flag in the parser's options (if supported, verify against latest documentation/source). Otherwise, design your grammar to use different naming conventions for rules intended to be part of the AST.
Run `npm install ebnf` (or `yarn add ebnf`) to install the package. If using TypeScript, ensure `tsconfig.json` correctly includes `node_modules/@types` or that the package's bundled types are being picked up.
Verify that `import { Grammars } from 'ebnf';` is used correctly and that the package is installed. If using CommonJS, ensure `const { Grammars } = require('ebnf');` is being used, though ESM imports are preferred.Review your EBNF/BNF grammar string and ensure all referenced non-terminal rules are explicitly defined. Check for typos in rule names.
Inspect the input string at the specified line and column. Compare it against your grammar definition to identify which rule is failing to match. This often points to incorrect grammar syntax, missing terminals, or an input string that doesn't conform to the defined language. Consider simplifying the grammar or the input to isolate the issue.
No dependency data recorded yet.