A parser generator with Bison's API for creating bottom-up parsers from grammar definitions
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core functionality for creating parsers from grammar definitions. Supports both JSON and Bison-style grammar formats with automatic algorithm selection.
Creates a parser from a grammar definition with optional configuration.
/**
* Create a parser from a grammar definition
* @param grammar - Grammar definition (object or string)
* @param options - Parser generation options
* @returns Parser instance ready for parsing
*/
function Parser(grammar, options);The Parser constructor accepts:
Usage Examples:
const jison = require("jison");
const Parser = jison.Parser;
// Simple calculator grammar
const calculatorGrammar = {
"lex": {
"rules": [
["\\s+", "/* skip whitespace */"],
["[0-9]+", "return 'NUMBER';"],
["\\+", "return '+';"],
["\\-", "return '-';"],
["\\*", "return '*';"],
["\\/", "return '/';"],
["\\(", "return '(';"],
["\\)", "return ')';"],
["$", "return 'EOF';"]
]
},
"bnf": {
"expressions": [["e EOF", "return $1;"]],
"e": [
["e + e", "$$ = $1 + $3;"],
["e - e", "$$ = $1 - $3;"],
["e * e", "$$ = $1 * $3;"],
["e / e", "$$ = $1 / $3;"],
["( e )", "$$ = $2;"],
["NUMBER", "$$ = Number(yytext);"]
]
}
};
const parser = new Parser(calculatorGrammar);Factory function for creating parser generators with specific algorithms.
/**
* Factory function for creating parser generators
* @param grammar - Grammar definition
* @param options - Generation options including parser type
* @returns Appropriate generator instance based on options.type
*/
function Generator(grammar, options);The Generator function returns different generator classes based on the options.type:
'lr0' - Returns LR0Generator'slr' - Returns SLRGenerator'lalr' - Returns LALRGenerator (default)'lr' - Returns LR1Generator'll' - Returns LLGeneratorUsage Examples:
const jison = require("jison");
// Create LALR(1) generator (default)
const generator = new jison.Generator(grammar);
// Create specific algorithm generator
const lr1Generator = new jison.Generator(grammar, { type: 'lr' });
const llGenerator = new jison.Generator(grammar, { type: 'll' });
// Generate parser
const parser = generator.createParser();Direct constructors for specific parsing algorithms.
/**
* LR(0) parser generator constructor
* @param grammar - Grammar definition
* @param options - Generation options
*/
function LR0Generator(grammar, options);
/**
* SLR(1) parser generator constructor
* @param grammar - Grammar definition
* @param options - Generation options
*/
function SLRGenerator(grammar, options);
/**
* LALR(1) parser generator constructor
* @param grammar - Grammar definition
* @param options - Generation options
*/
function LALRGenerator(grammar, options);
/**
* Canonical LR(1) parser generator constructor
* @param grammar - Grammar definition
* @param options - Generation options
*/
function LR1Generator(grammar, options);
/**
* LL(1) parser generator constructor
* @param grammar - Grammar definition
* @param options - Generation options
*/
function LLGenerator(grammar, options);Usage Examples:
const jison = require("jison");
const { LALRGenerator, LR1Generator } = jison;
// Direct construction
const lalrGen = new LALRGenerator(grammar, { debug: true });
const lr1Gen = new LR1Generator(grammar);
// Create parsers
const lalrParser = lalrGen.createParser();
const lr1Parser = lr1Gen.createParser();Methods available on generated parser instances.
/**
* Parse input string and return result
* @param input - String to parse
* @returns Parse result or throws error on failure
*/
parse(input);
/**
* Generate parser source code
* @param options - Code generation options
* @returns Generated parser source as string
*/
generate(options);
/**
* Generate CommonJS module source
* @param options - Module generation options
* @returns CommonJS module source code
*/
generateCommonJSModule(options);
/**
* Generate AMD module source
* @param options - AMD generation options
* @returns AMD module source code
*/
generateAMDModule(options);Usage Examples:
const parser = new Parser(grammar);
// Parse input
try {
const result = parser.parse("2 + 3 * 4");
console.log(result); // 14
} catch (error) {
console.error("Parse error:", error.message);
}
// Generate standalone parser
const parserSource = parser.generate({
moduleName: "MyParser",
moduleType: "commonjs"
});
// Write to file
require('fs').writeFileSync('my-parser.js', parserSource);Structured JavaScript object format for defining grammars.
const jsonGrammar = {
"lex": {
"rules": [
["pattern", "action"],
// ... more rules
]
},
"bnf": {
"nonterminal": [
["production rule", "semantic action"],
// ... more productions
]
},
"operators": [
["associativity", "operator1", "operator2"],
// ... more operator declarations
]
};String format compatible with Bison grammar files.
const bisonGrammar = `
%lex
%%
\\s+ /* skip whitespace */
[0-9]+ return 'NUMBER'
"+" return '+'
%%
%left '+'
%left '*'
%%
expressions: e EOF { return $1; };
e: e '+' e { $$ = $1 + $3; }
| NUMBER { $$ = Number(yytext); }
;
`;
const parser = new Parser(bisonGrammar);Parser creation can throw errors for invalid grammars or unsupported configurations.
try {
const parser = new Parser(invalidGrammar);
} catch (error) {
if (error.message.includes('Grammar error')) {
console.error('Invalid grammar:', error.message);
} else {
console.error('Parser creation failed:', error.message);
}
}Common error conditions: