CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jison

A parser generator with Bison's API for creating bottom-up parsers from grammar definitions

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

grammar-processing.mddocs/

Grammar Processing

Internal system for processing and analyzing grammar definitions including production rules, nonterminals, and operator precedence. These APIs are primarily used internally by Jison but may be useful for advanced grammar analysis.

Capabilities

Grammar Processing

Core method for processing raw grammar definitions into internal representation.

/**
 * Process a grammar definition into internal representation
 * @param grammar - Raw grammar object containing lex, bnf/ebnf, operators, etc.
 */
processGrammar(grammar);

This method:

  • Converts EBNF to BNF if needed
  • Processes operator precedence declarations
  • Builds symbol tables
  • Creates production rules
  • Augments grammar with accept production

Usage Examples:

const generator = new jison.Generator(grammar);
// processGrammar is called internally during construction

// Access processed components
console.log(generator.nonterminals);  // Nonterminal objects
console.log(generator.productions);   // Production rule array
console.log(generator.symbols);       // All grammar symbols

Production Building

Builds production rules from BNF grammar definitions with semantic actions.

/**
 * Build production rules from BNF grammar
 * @param bnf - BNF grammar object with nonterminal -> productions mapping
 * @param productions - Production array to populate
 * @param nonterminals - Nonterminal symbol objects
 * @param symbols - All grammar symbols array
 * @param operators - Operator precedence rules
 */
buildProductions(bnf, productions, nonterminals, symbols, operators);

This method processes:

  • Production rules from BNF definitions
  • Semantic actions associated with productions
  • Named semantic values (e.g., $variable)
  • Operator precedence assignments
  • Error recovery rules

Grammar Augmentation

Adds the accept production and required symbols to complete the grammar.

/**
 * Augment grammar with accept production and required symbols
 * @param grammar - Original grammar definition
 */
augmentGrammar(grammar);

This method:

  • Adds $accept production as grammar start
  • Adds $end (EOF) terminal symbol
  • Sets up initial symbol mappings
  • Validates start symbol exists

Grammar Analysis Classes

Production Class

Represents individual production rules in the grammar.

/**
 * Production rule constructor
 * @param symbol - Left-hand side nonterminal
 * @param handle - Right-hand side symbol array
 * @param id - Unique production identifier
 */
function Production(symbol, handle, id);

/**
 * Production instance properties and methods
 */
class Production {
  symbol: string;        // Left-hand side nonterminal
  handle: string[];      // Right-hand side symbols
  id: number;           // Production identifier
  nullable: boolean;    // Whether production can derive epsilon
  first: string[];      // First set for this production
  precedence: number;   // Operator precedence level
  
  /**
   * Convert production to string representation
   * @returns String in "A -> B C D" format
   */
  toString(): string;
}

Usage Examples:

// Productions are created internally during grammar processing
const generator = new jison.Generator(grammar);

generator.productions.forEach(production => {
  console.log(`${production.symbol} -> ${production.handle.join(' ')}`);
  console.log(`Precedence: ${production.precedence}`);
  console.log(`Nullable: ${production.nullable}`);
});

Nonterminal Class

Represents nonterminal symbols with their associated productions and computed sets.

/**
 * Nonterminal symbol constructor
 * @param symbol - Symbol name
 */
function Nonterminal(symbol);

/**
 * Nonterminal instance properties and methods
 */
class Nonterminal {
  symbol: string;          // Symbol name
  productions: Set;        // Associated production rules
  first: string[];         // First set
  follows: string[];       // Follow set  
  nullable: boolean;       // Whether symbol can derive epsilon
  
  /**
   * Convert nonterminal to string with debug information
   * @returns Detailed string representation
   */
  toString(): string;
}

Usage Examples:

const generator = new jison.Generator(grammar);

// Access nonterminal information
for (const symbolName in generator.nonterminals) {
  const nt = generator.nonterminals[symbolName];
  console.log(`Symbol: ${nt.symbol}`);
  console.log(`Nullable: ${nt.nullable}`);
  console.log(`First: [${nt.first.join(', ')}]`);
  console.log(`Follow: [${nt.follows.join(', ')}]`);
  console.log(`Productions: ${nt.productions.size()}`);
}

Lookahead Computation

Methods for computing First, Follow, and Nullable sets used in parsing algorithms.

First Sets

Compute First sets for symbols and symbol sequences.

/**
 * Compute First sets for all productions and nonterminals
 */
firstSets();

/**
 * Get First set for a symbol or sequence of symbols
 * @param symbol - Single symbol (string) or array of symbols
 * @returns Array of terminal symbols that can begin derivations
 */
first(symbol);

Follow Sets

Compute Follow sets for nonterminal symbols.

/**
 * Compute Follow sets for all nonterminals
 */
followSets();

Nullable Analysis

Determine which symbols and productions can derive the empty string.

/**
 * Compute nullable properties for all symbols and productions
 */
nullableSets();

/**
 * Check if a symbol or sequence can derive epsilon
 * @param symbol - Single symbol (string) or array of symbols
 * @returns True if symbol/sequence is nullable
 */
nullable(symbol);

Usage Examples:

const generator = new jison.Generator(grammar);
// Lookahead sets are computed automatically for LR/LL generators

// Manual computation (if extending generators)
generator.computeLookaheads();

// Check specific symbols
const firstOfE = generator.first('e');
const isENullable = generator.nullable('e');
const firstOfSequence = generator.first(['e', '+', 'e']);

Operator Processing

Process operator precedence and associativity declarations.

/**
 * Process operator precedence declarations
 * @param ops - Array of operator declarations
 * @returns Object mapping operators to precedence/associativity
 */
function processOperators(ops);

Operator declarations format:

const operators = [
  ['left', '+', '-'],        // Left associative, precedence 1
  ['left', '*', '/'],        // Left associative, precedence 2  
  ['right', '^'],            // Right associative, precedence 3
  ['nonassoc', '==', '!=']   // Non-associative, precedence 4
];

Error Handling and Debugging

Debug Output

Enable detailed processing information during grammar analysis.

/**
 * Output trace information (no-op unless debug mode enabled)
 * @param ...args - Arguments to trace
 */
trace(...args);

/**
 * Output warning messages
 * @param ...args - Warning message components
 */
warn(...args);

/**
 * Throw formatted error
 * @param msg - Error message
 * @throws Error with formatted message
 */
error(msg);

Usage Examples:

// Enable debug mode during construction
const generator = new jison.Generator(grammar, { debug: true });

// Debug output will show:
// - Grammar processing steps
// - Symbol table construction
// - First/Follow set computation
// - Conflict resolution details

Common Processing Errors

Grammar processing can fail with various error conditions:

  • "Grammar error: must have at least one rule" - Empty BNF section
  • "Grammar error: startSymbol must be a non-terminal" - Invalid start symbol
  • "Could not parse jison grammar" - Malformed grammar syntax
  • "Could not parse lex grammar" - Invalid lexical rules
try {
  const parser = new jison.Parser(grammar);
} catch (error) {
  if (error.message.includes('Grammar error')) {
    // Handle grammar-specific errors
    console.error('Grammar processing failed:', error.message);
  }
}

docs

algorithm-selection.md

cli.md

grammar-processing.md

index.md

parser-creation.md

parser-generation.md

tile.json