EBNF Grammar Parser for Jison
ebnf-parser is a utility library designed to parse BNF and EBNF grammars, primarily used as a pre-processor for the Jison parser generator. It takes either a string representation of a grammar or a JSON grammar and transforms EBNF constructs into standard BNF, producing a JSON grammar format that Jison can consume. The current stable version is 0.1.10. Due to its specific role as a dependency for Jison and its low version number, its release cadence is slow and driven by Jison's needs. Key differentiators include its tight integration with Jison's grammar format and its focus solely on grammar transformation rather than full parsing engine capabilities, making it a specialized tool in the parser generator ecosystem.
Common errors
-
TypeError: ebnfParser.parse is not a function
cause The `parser.js` file, which contains the actual parsing logic and the `parse` method, has not been generated or is not accessible.fixIf installing from source (e.g., Git clone), ensure you run `make` in the package root to generate `parser.js`. If installed via npm, verify the installation integrity. -
SyntaxError: Cannot use import statement outside a module
cause You are attempting to use `import ebnfParser from 'ebnf-parser';` in a CommonJS context (e.g., a `.js` file without `"type": "module"` in `package.json`, or an older Node.js version).fixChange your import statement to `const ebnfParser = require('ebnf-parser');`. Alternatively, configure your project for ESM, but note that `ebnf-parser` itself is CommonJS and will require interop.
Warnings
- gotcha This package is CommonJS-only. Attempting to use `import` statements directly in an ESM module will result in a runtime error unless your build setup handles CommonJS interoperability.
- breaking The parser relies on a generated `parser.js` file. If cloning from Git, you must run `make` in the package's root directory to generate this file, otherwise the package will fail to load or function correctly.
- gotcha The package version 0.1.10 indicates an early stage of development or maintenance mode. It might not receive frequent updates or extensive community support compared to more mature libraries.
Install
-
npm install ebnf-parser -
yarn add ebnf-parser -
pnpm add ebnf-parser
Imports
- ebnfParser
import ebnfParser from 'ebnf-parser';
const ebnfParser = require('ebnf-parser'); - parse
import { parse } from 'ebnf-parser';const ebnfParser = require('ebnf-parser'); ebnfParser.parse("%start ... %"); - transform
import { transform } from 'ebnf-parser';const ebnfParser = require('ebnf-parser'); ebnfParser.transform({"ebnf": "..."});
Quickstart
const ebnfParser = require('ebnf-parser');
// Example 1: Parsing a simple EBNF string grammar
const grammarString = `
%start program
program: statement+;
statement: 'id' '=' expression ';';
expression: term ('+' term)*;
term: 'number' | '(' expression ')';
`;
try {
const parsedGrammar = ebnfParser.parse(grammarString);
console.log('Parsed Grammar (string input):', JSON.stringify(parsedGrammar, null, 2));
} catch (error) {
console.error('Error parsing string grammar:', error.message);
}
// Example 2: Transforming an EBNF JSON grammar (conceptual, actual JSON might vary)
const ebnfJson = {
"ebnf": {
"program": ["statement+"],
"statement": ["'id' '=' expression ';'"],
"expression": ["term ('+' term)*"],
"term": ["'number' | '(' expression ')'"]
}
};
try {
const transformedGrammar = ebnfParser.transform(ebnfJson);
console.log('Transformed Grammar (JSON input):', JSON.stringify(transformedGrammar, null, 2));
} catch (error) {
console.error('Error transforming JSON grammar:', error.message);
}