Class: EBNF

page. EBNF

Represents a grammar as a tree; supports choices and iterations.


new EBNF()

Creates a new grammar representation.

Properties:
Name Type Description
bnf page.BNF

grammar represented in BNF, terminals are shared here. This object is created here and completed in page.EBNF#init.

Source:

Extends

Classes

Alt
Lst
NT
Rep
Rule
Seq

Members


<static> builder

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

Source:

<static> grammar

The EBNF grammar for EBNF.

Source:

Methods


<static> ctor()

The EBNF grammar manually represented with page.EBNF.

Source:
Returns:

the EBNF grammar for EBNF, hand-crafted.

Type
page.EBNF

<static> from(_grammar)

Factory method: Represent a grammar described in EBNF.

This is bootstrapped using page.EBNF.ctor to create an LL(1) compiler for EBNF which uses page.EBNF.builder.

Parameters:
Name Type Description
_grammar string

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

Source:
Returns:

a new grammar representation; check .errors.

Type
page.EBNF

alt(_nodes)

Factory method to create a new representation of a choice for EBNF.

Parameters:
Name Type Description
_nodes Array.<eNode>

alternatives, not empty.

Source:
Returns:

a new representation of a choice for EBNF; flattens if there is a single descendant.

Type
eNode

dump()

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

Inherited From:
Source:
Returns:
Type
string

dump()

Displays EBNF grammar, appends page.EBNF#bnf and a dump.

Source:
Returns:
Type
string

<private> eNode(_marker, _node)

Asserts that a node has a type suitable for EBNF.

Parameters:
Name Type Description
_marker string

to identify position across a re-throw.

_node eNode

to test.

Source:
See:
Throws:

if type is not suitable.

Type
Error

getByOrd(_ord)

Returns symbol by ord.

Parameters:
Name Type Description
_ord number

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

Inherited From:
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.

Inherited From:
Source:
See:

init( [_start] [, _rules])

Must be called once to complete EBNF 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.

Uses .bnf to represent the grammar in BNF and imports empty and the first and follow sets. The algorithm is careful to create new non-terminals for concealed BNF rules to make the EBNF non-terminals visible in the BNF grammar.

Parameters:
Name Type Argument Description
_start page.EBNF.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.

Inherited From:
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.

Inherited From:
Source:
Returns:

a unique literal.

Type
page.BNF.Lit

many(_node)

Factory method to create a new representation of an iteration for EBNF; represents 0 or more occurrences.

Parameters:
Name Type Description
_node eNode

body.

Source:
Returns:

a new representation of an iteration for EBNF.

Type
page.EBNF.Rep

manyList(_node, _delim)

Factory method to create a new representation of a delimited iteration for EBNF; represents 0 or more occurrences.

Parameters:
Name Type Description
_node eNode

body.

_delim eNode

delimiter.

Source:
Returns:

a new representation of a delimited iteration for EBNF.

Type
page.EBNF.Lst

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.

Inherited From:
Source:
Returns:

a unique non-terminal representation.

Type
page.BNF.NT

nt( [_name])

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

Parameters:
Name Type Argument Description
_name string <optional>

non-terminal's name, cannot start with $. If omitted represents the $accept non-terminal.

Source:
Returns:

a unique non-terminal representation.

Type
page.EBNF.NT

opt(_node)

Factory method to create a new representation of an iteration for EBNF; represents 0 to 1 occurence.

Parameters:
Name Type Description
_node eNode

body.

Source:
Returns:

a new representation of an iteration for EBNF.

Type
page.EBNF.Rep

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.

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

a new rule representation.

Type
page.BNF.Rule

rule(_nt [, _symbol])

Factory method to create a rule representation for EBNF. Sets it as rule's non-terminal's page.EBNF.NT#rule

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

left-hand side, non-terminal, may not yet have a rule.

_symbol eNode <optional>

single symbol for right-hand side. if omitted, must be rule 0 for $accept.

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

a new rule representation.

Type
page.EBNF.Rule

seq(_nodes)

Factory method to create a new representation of a sequence for EBNF.

Parameters:
Name Type Description
_nodes Array.<eNode>

sequence, not empty.

Source:
Returns:

a new representation of a sequence for EBNF; flattens if there is a single descendant.

Type
eNode

some(_node)

Factory method to create a new representation of an iteration for EBNF; represents 1 or more occurrences.

Parameters:
Name Type Description
_node eNode

body.

Source:
Returns:

a new representation of an iteration for EBNF.

Type
page.EBNF.Rep

someList(_node, _delim)

Factory method to create a new representation of a delimited iteration for EBNF; represents 1 or more occurrences.

Parameters:
Name Type Description
_node eNode

body.

_delim eNode

delimiter.

Source:
Returns:

a new representation of a delimited iteration for EBNF.

Type
page.EBNF.Lst

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.

Inherited From:
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.

Inherited From:
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.

Inherited From:
Source:
Returns:
Type
string