Module: BNF

A module which extends the Base module and supports creating scanners and SLR(1) parsers from BNF grammars. Parsers employ the observer pattern to be traced and to call actions written in JavaScript.

The fundamental object created by this module is a Grammar which could be prepared from grammar rules using the syntax defined here. Grammars are written as ordered pairs and can define precedences.

The state table is created from sets of positions in rules, and conflicts are resolved using the lookahead and follow sets, and precedences, if available, i.e., it is a simplified implementation of LR(1). The table is not optimized.

An EBNF grammar, optionally with precedences and $error, can be imported for SLR(1) parsing, but the BNF and EBNF modules only depend on the Base module and not on each other.

The grammar rules, precedence levels, and states are represented using a number of classes summarized below. check() creates a state table which controls a Parser. A Scanner is created from the grammar's terminals.

Objects are created using factory methods which check parameters and maintain inventories. All factory methods are defined in Grammar and the factory methods for all but Message have the same name as the classes (with lower-case initials).

All properties have getters, very few have setters, i.e., there is no assignment outside class boundaries.

class main properties main methods
Grammar: Factory config, ebnf, sr, rr, rules: Array<Rule>,
states: Array<State>
check(), parser(), build(), trace()
Rule nt, symbols: Array<Symbol>, prec, empty, reached, finite, first, reduced
NT: NT: Symbol name, ord, rules: Array<Rule>, empty, reached, finite, first, follow
Lit: Lit: T: Symbol name, value, ord, prec, used, first unescape(s)
Token: Token: T: Symbol name, pat, ord, prec, used, first
State marks: Array<Mark>, core,
messages: Object<ord, Message>
advance(state), equals(core)
Mark rule, position, complete advance(), equals(mark)
Message message, symbol, info
Parser grammar, stack[], state: State, values[] parse(input)
Version:
  • 2024-07-25
Author:
Source:
See:

Classes

Actions
Grammar
Lit
Mark
Message
NT
Parser
Rule
State
Token

Mixins

T