Class: BNF

page. BNF

Represents a grammar as ordered pairs; can compute first and follow sets and generate a tokenizer.


new BNF()

Creates a new grammar representation; creates the $accept non-terminal, $eof end of file literal, and error token.

Properties:
Name Type Description
lits Array.<page.BNF.T>

list of unique literals, can be pushed.

litsByValue Map

maps value to unique literal; transient, maintained by page.BNF#lit.

tokens Array.<page.BNF.T>

list of unique tokens, can be pushed.

tokensByName Map

maps name to unique token; transient, maintained by page.BNF#token.

nts Array.<page.BNF.NT>

list of unique non-terminals, can be pushed.

ntsByName Map

maps name to unique non-terminal; transient, maintained by page.BNF#nt.

rules Array.<page.BNF.Rule>

list of grammar rules, can be pushed.

errors number

counted by page.error method; transient.

Source:

Classes

Lit
NT
Rule
T
Token

Members


<static> builder

An object with action functions corresponding to page.BNF.grammar for page.LL1#reflect which require a new page.BNF as environment to represent a grammar in BNF notation using this environment.

Source:

<static> grammar

The right-recursive BNF grammar for BNF.

Source:

Methods


<static> ctor()

The BNF grammar manually represented with page.BNF.

Source:
Returns:

the right-recursive BNF grammar for BNF, hand-crafted.

Type
page.BNF

<static> from(_grammar)

Factory method: Represent a grammar described in BNF. This is bootstrapped using page.BNF.ctor and page.BNF.builder to create an LL(1) compiler for BNF.

Parameters:
Name Type Description
_grammar string

description in BNF; comments extend from # to the end of a line.

Source:
Returns:

a new grammar representation; check .errors.

Type
page.BNF

dump()

Displays a grammar and all non-terminals with name and contents of all sets.

Source:
Returns:
Type
string

getByOrd(_ord)

Returns symbol by ord.

Parameters:
Name Type Description
_ord number

page.BNF.NT or page.BNF.T's ord.

Source:
Returns:
Type
Node

init( [_start] [, _rules])

Must be called once to complete BNF grammar initialization.

Completes rule 0 with a new start symbol $accept to accept the grammar's original start symbol and then $eof.

Sets page.BNF.Rule.index as index into the rules array. Sets empty for each rule and its non-terminal which has an empty right-hand side.

Checks that all non-terminals are defined, can be reached, and cannot expand infinitely without generating terminals.

Computes first and follow for all rules and non-terminals.

Parameters:
Name Type Argument Description
_start page.BNF.NT <optional>

start symbol of grammar, default is the non-terminal of rule 1.

_rules Array.<page.BNF.Rule> <optional>

(additional) grammar rules.

Source:
See:

<private> initTree( [_rules])

Must be called once; common code to complete grammar initialization.

Adds rules passed as a parameter to page.BNF.rules.

Sorts literals in reverse order, i.e., longer ones before shorter prefixes. Defines page.BNF.T.ord and page.BNF.NT.ord as globally unique index (literals before tokens before non-terminals). Sets page.BNF.T.first for each terminal to contain itself.

Parameters:
Name Type Argument Description
_rules Array.<page.BNF.Rule> <optional>

(additional) grammar rules.

Source:

lit( [_literal])

Factory method to create a unique literal, maintains page.BNF.lit and page.BNF.litsByValue.

Parameters:
Name Type Argument Description
_literal string <optional>

literal's representation; if omitted represents the $eof literal terminal.

Source:
Returns:

a unique literal.

Type
page.BNF.Lit

nt( [_name])

Factory method to create a unique non-terminal representation, maintains page.BNF.nts and page.BNF.ntsByName, checks against page.BNF.tokensByName.

Parameters:
Name Type Argument Description
_name string <optional>

non-terminal's name; cannot start with $. If $ creates a unique name which cannot be searched for. If omitted represents the $accept non-terminal.

Source:
Returns:

a unique non-terminal representation.

Type
page.BNF.NT

rule(_nt [, _symbols])

Factory method to create a rule representation for BNF. Adds to rule's non-terminal's page.BNF.NT.rules.

Parameters:
Name Type Argument Description
_nt page.BNF.NT

left-hand side, non-terminal.

_symbols Array.<Node> <optional>

right-hand side, may be empty array; if omitted, creates empty rule 0 for $accept.

Source:
See:
  • page.BNF.init
Returns:

a new rule representation.

Type
page.BNF.Rule

token( [_name] [, _pattern])

Factory method to create a unique token, maintains page.BNF.tokens and page.BNF.tokensByName, checks against page.BNF.ntsByName.

Parameters:
Name Type Argument Description
_name string <optional>

token's name; cannot start with $. If omitted represents the $error token.

_pattern string <optional>

pattern to match values representing the token in input; must be specified if and only if the token is created.

Source:
Returns:

a unique terminal category representation.

Type
page.BNF.Token

tokenizer( [_skip])

Creates a function which tokenizes a string.

For literals with common prefixes the longer literal will be matched first. Token patterns are matched after literals and in the order the tokens were defined.

Parameters:
Name Type Argument Description
_skip string | RegExp <optional>

a pattern to define ignorable character sequences; this pattern must not accept empty input and it must use (:? ) rather than ( ) for grouping; the default is to skip white space.

Source:
Returns:

a function which takes a string and returns a list of page.Tuple elements. The list contains one tuple with the error token for each character which is neither ignorable nor a literal or part of a token. The function is based on a regular expression which can be displayed as a member .pattern.

Type
Tokenizer

toString()

Displays description of grammar and number of errors if any.

Source:
Returns:
Type
string