Class: EBNFP

page. EBNFP

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


new EBNFP( [_recover])

Creates a new grammar representation.

Parameters:
Name Type Argument Description
_recover boolean <optional>

if true add error rules to BNF; this is implied by %right error in a grammar.

Properties:
Name Type Description
bnf page.BNFP

grammar represented in BNF with precedences, terminals are shared.

levels Array.<page.BNFP.Precedence>

list of lists of terminals with equal precedence and associativity in order of increasing precedence, shared with .bnf.

recover boolean

if true add error rules to BNF.

Source:
See:

Extends

Classes

Lst
Rep
Seq

Members


<static> builder

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

Source:

<static> grammar

The left-recursive BNFP SLR(1) grammar for EBNFP.

error causes this grammar to have two shift-reduce conflicts which can be hidden through precedence in BNFP but not EBNFP.

Source:

Methods


<static> from(_grammar [, _recover])

Factory method: Represent a grammar described in EBNFP.

This is bootstrapped using page.BNFP.from and page.EBNFP.grammar to create an SLR(1) compiler for EBNFP which uses page.EBNFP.builder.

Parameters:
Name Type Argument Description
_grammar string

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

_recover boolean <optional>

if true add error rules to BNF; this is implied by %right error in a grammar.

Source:
Returns:

a new grammar representation; check .errors.

Type
page.EBNFP

alt(_nodes)

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

Parameters:
Name Type Description
_nodes Array.<eNode>

alternatives, not empty.

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

Inherited From:
Source:
Returns:
Type
string

dump()

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

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.

Inherited From:
Source:
See:
Throws:

if type is not suitable.

Type
Error

<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 epNode

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.

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

Inherited From:
Source:
Returns:

a new representation of an iteration for EBNF.

Type
page.EBNF.Rep

many(_node)

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

Parameters:
Name Type Description
_node epNode

body.

Source:
Returns:

a new representation of an iteration for EBNFP.

Type
page.EBNFP.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.

Inherited From:
Source:
Returns:

a new representation of a delimited iteration for EBNF.

Type
page.EBNF.Lst

manyList(_node, _delim)

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

Parameters:
Name Type Description
_node epNode

body.

_delim epNode

delimiter.

Source:
Returns:

a new representation of a delimited iteration for EBNFP.

Type
page.EBNFP.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.

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

Inherited From:
Source:
Returns:

a new representation of an iteration for EBNF.

Type
page.EBNF.Rep

opt(_node)

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

Parameters:
Name Type Description
_node epNode

body.

Source:
Returns:

a new representation of an iteration for EBNFP.

Type
page.EBNFP.Rep

precedence(_assoc, _terminals)

Factory method to represent a list of terminals with equal precedence level and equal associativity.

Creates a new page.BNFP.Precedence object, adds it to page.EBNFP.levels, adds .prec.level and .prec.assoc to all terminals in the list, and checks for duplicates.

Parameters:
Name Type Description
_assoc string

associativity: 'left', 'right', or 'nonassoc'.

_terminals Array.<page.BNF.T>

list of terminals.

Source:
Throws:

an error message if there is duplication.

Type
string
Returns:

representing the set.

Type
page.BNFP.Precedence

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.

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

Inherited From:
Source:
Returns:

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

Type
eNode

seq(_nodes [, _prec])

Factory method to create a new representation of a sequence with optional precedence for EBNFP.

Parameters:
Name Type Argument Description
_nodes Array.<epNode>

sequence, not empty.

_prec page.BNF.T <optional>

terminal defining precedence explicitly; by default last terminal in sequence, if any.

Source:
Throws:

an error message if an explicit precedence cannot be resolved.

Type
string
Returns:

a new representation of a sequence for EBNFP; flattened if there is a single descendant and no precedence.

Type
epNode

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.

Inherited From:
Source:
Returns:

a new representation of an iteration for EBNF.

Type
page.EBNF.Rep

some(_node)

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

Parameters:
Name Type Description
_node epNode

body.

Source:
Returns:

a new representation of an iteration for EBNFP.

Type
page.EBNFP.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.

Inherited From:
Source:
Returns:

a new representation of a delimited iteration for EBNF.

Type
page.EBNF.Lst

someList(_node, _delim)

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

Parameters:
Name Type Description
_node epNode

body.

_delim epNode

delimiter.

Source:
Returns:

a new representation of a delimited iteration for EBNFP.

Type
page.EBNFP.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

toString()

Displays description of grammar and number of errors if any.

Source:
Returns:
Type
string