new Grammar( [grammar] [, tokens] [, config])
Creates a grammar representation.
Creates the $accept non-terminal, $eof end of input literal, and $error token,
and reserves rule zero: $accept: start $eof;.
Defines tokens, if any.
Parameters:
| Name | Type | Argument | Description |
|---|---|---|---|
grammar |
string |
<optional> <nullable> |
the grammar to represent, using the BNF grammar and BNF token and literal notation. This can be omitted to construct the rules directly using the factory methods. |
tokens |
Object.<string, RegExp> |
<optional> <nullable> |
maps token names, if any,
in the new grammar to their patterns
which must not accept empty input, must not use |
config |
Object.<string, Object> |
<optional> |
overwrites configurable values' defaults; loaded first but can only be the third parameter. |
- Source:
Properties:
| Name | Type | Argument | Description | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ebnf |
module:EBNF~Grammar |
<nullable> |
set only if created from EBNF Grammar. |
||||||||||||||||||||||||||||||||||||||||
rules |
Array.<module:BNF~Rule> | list of grammar rules, can be pushed. |
|||||||||||||||||||||||||||||||||||||||||
states |
Array.<module:BNF~State> | list of possible states for parser. |
|||||||||||||||||||||||||||||||||||||||||
sr |
number | number of shift/reduce conflicts. |
|||||||||||||||||||||||||||||||||||||||||
rr |
number | number of reduce/reduce conflicts. |
|||||||||||||||||||||||||||||||||||||||||
config |
Object.<string, Object> | maps names to configurable values. Properties
|
|||||||||||||||||||||||||||||||||||||||||
lits |
Array.<module:Base~Lit> | list of unique literals, can be pushed. |
|||||||||||||||||||||||||||||||||||||||||
litsByName |
Object.<string, module:Base~Lit> | maps |
|||||||||||||||||||||||||||||||||||||||||
tokens |
Array.<module:Base~Token> | list of unique tokens, can be pushed. |
|||||||||||||||||||||||||||||||||||||||||
tokensByName |
Object.<string, module:Base~Token> | maps name to unique token. |
|||||||||||||||||||||||||||||||||||||||||
levels |
Array.<module:Base~Precedence> | list of precedence levels, can be pushed. |
|||||||||||||||||||||||||||||||||||||||||
nts |
Array.<module:Base~NT> | list of unique non-terminals, can be pushed. |
|||||||||||||||||||||||||||||||||||||||||
ntsByName |
Object.<string, module:Base~NT> | maps name to unique non-terminal. |
|||||||||||||||||||||||||||||||||||||||||
errors |
number | incremented by |
Throws:
-
an error for bad token definitions or syntax errors in the grammar.
- Type
- Error
Examples
LL(1) recursive descent parsing
const e = new EBNF.Grammar(' ... grammar ... ')
e.parser(/ ... skip .../)(' ... input ... ')
e.parser(/ ... skip .../)(' ... input ... ', { ... actions ... })
equivalent SLR(1) stack-based parsing
const b = BNF.Grammar.fromEBNF(e)
b.parser(/ ... skip .../)(' ... input ... ')
b.parser(/ ... skip .../)(' ... input ... ', { ... actions ... })
details
const b = BNF.Grammar.fromEBNF(e)
const s = b.scanner(/ ... skip ... /)
new BNF.Parser(b).parse(s.scan(' ... input ... ').concat(null), b.build())
new BNF.Parser(b).parse(s.scan(' ... input ... ').concat(null), b.build({ ... actions ... }))
Extends
Members
-
<static, constant> bnf :string
-
Grammar describing the BNF notation accepted by
new Grammar():A *grammar* consists of an optional sequence of *precedence levels* followed by one or more rules.
Each *precedence level* consists of an associativity followed by one or more literals or token names and terminated with a semicolon. Precedence levels are increasing.
Each *rule* consists of a non-terminal name on the left-hand side, a colon, a *symbol sequence* on the right-hand side, and a semicolon. Rules with the same names are alternatives.
A *symbol sequence* contains zero or more items, such as a non-terminal name, a self-defining literal, or the name of a token.
Type:
- string
Example
BNF grammars' grammar
grammar: precedences rules; precedences: ; precedences: precedences '%left' terminals ';'; precedences: precedences '%right' terminals ';'; precedences: precedences '%nonassoc' terminals ';'; terminals: terminal; terminals: terminals terminal; terminal: lit; terminal: name; rules: rule; rules: rules rule; rule: Name ':' symbols ';'; rule: Name ':' symbols '%prec' terminal ';'; symbols: ; symbols: symbols name; symbols: symbols lit; name: Name; lit: Lit;
-
<static, constant> fromEBNF :function
-
Factory method to represent an EBNF grammar as a BNF grammar and check it.
Type:
- function
Examples
Translating
[ s | t | ... ]$-#: ; $-#: s; $-#: t; ...
Translating
{ s | t | ... }$-##: $-#; $-##: $-## $-#; $-##: $error; $-##: $-## $error; $-#: s; $-#: t; ...
-
<private, static, constant> grammar :module:BNF~Grammar
-
The BNF grammars' grammar; created when the module is loaded and used internally in
new Grammar().Type:
-
<static, constant> terminals :Object.<string, RegExp>
-
Token definitions for
LitandNameinGrammar#bnf.*Literals* represent themselves and are single-quoted strings using `\` only to escape single quotes and `\` itself.
A *Name* either represents a non-terminal or a *token*.
*Tokens* represent sets of inputs, such as names or numbers, and are alphanumeric names which must start with a letter and may include underscores.
`$error` is a special token to control error recovery.
Type:
- Object.<string, RegExp>
- Source:
- See:
Example
BNF grammars' tokens
{ Lit: /'(?:[^'\\]|\\['\\])+'/, Name: /[A-Za-z][A-Za-z0-9_]*|\$error/ }
Methods
-
accept()
-
Factory method to create the
acceptmessage for$eof.Returns:
an object representing the message.
- Type
- module:BNF~Message
-
add(item)
-
Adds a new symbol to the proper inventory or creates and adds new tokens. Must be called with a new, unique symbol or with a map of token names to patterns. Validates item names against
.config. Token patterns must not accept empty input, must not used,g, oryflag, should not be anchored, and should use(:? )rather than( )for grouping.Parameters:
Name Type Description itemSymbol | Object.<string, RegExp> to add to the proper inventory or create and add.
- Inherited From:
- Overrides:
- Source:
-
assert(condition, s)
-
Displays a message and throws an error if a condition is not met; primarily used for stronger argument typing.
Parameters:
Name Type Argument Description conditionboolean should be true.
sArray.<?object> <repeatable>
message, to be displayed; joined by blanks.
- Inherited From:
- Overrides:
- Source:
Throws:
-
message if condition is not met.
- Type
- string
-
check(start)
-
Completes the grammar representation and reports if there are errors; call exactly once. Creates rule zero:
$accept:start$eof;. Sets ordinal number for literals, then tokens, then non-terminals. Checks that all rules are reached and all non-terminals can reduce to a terminal. Computes first and follow sets. Creates state table. Checks that all rules can be reduced.Parameters:
Name Type Description startmodule:BNF~NT the start non-terminal.
-
dump(a, states)
-
Displays grammar, terminals, and non-terminals with name and contents of all sets. Displays number of errors if any.
Parameters:
Name Type Argument Description aObject <nullable>
with one argument (kludge!) acts as a static method and displays the argument converting nested arrays to a string – useful because
console.debugonly reaches 3 levels.statesboolean if true also displays states.
- Overrides:
- Source:
Returns:
- Type
- string
-
error(s)
-
Displays a message and counts it as an error.
Parameters:
Name Type Argument Description sArray.<?object> <repeatable>
message, to be displayed; joined by blanks.
- Inherited From:
- Overrides:
- Source:
Returns:
the message.
- Type
- string
-
lit( [literal] [, used])
-
Factory method to create a unique literal symbol, maintains
.litsand.litsByNameParameters:
Name Type Argument Description literalstring <optional>
literal's representation conforming to
.config.lits. If omitted represents the$eofliteral terminal.usedboolean <optional>
if
truemark literal as used.Returns:
a unique literal.
- Type
- module:BNF~Lit
-
mark(rule, position)
-
Factory method to represent a mark in a rule.
Parameters:
Name Type Description rulemodule:BNF~Rule rule to mark.
positionnumber position in rule, before a symbol or after all.
Returns:
an object representing the marked rule.
- Type
- module:BNF~Mark
-
message(s)
-
Displays a message on the configured
.log.Parameters:
Name Type Argument Description sArray.<?object> <repeatable>
message, to be displayed; joined by blanks.
- Inherited From:
- Overrides:
- Source:
Returns:
the message.
- Type
- string
-
nt( [name])
-
Factory method to create a unique non-terminal symbol, maintains
.ntsand.ntsByName.Parameters:
Name Type Argument Description namestring <optional>
non-terminal's name conforming to
config.nts; error if a token. If omitted represents the$acceptnon-terminal, if not a string creates a unique name (intended for EBNF translation).Returns:
a unique non-terminal.
- Type
- module:BNF~NT
-
parser( [skip])
-
Factory method to create a parser to recognize and process input.
Parameters:
Name Type Argument Description skipRegEx <optional>
a pattern to define ignorable character sequences, by default white space, must not accept empty input, must not use flags, must not be anchored, should use
(:? )rather than( )for grouping.Returns:
the parser.
- Type
- module:BNF~Parser
-
precedence(assoc, terminals)
-
Factory method to represent a list of terminals with equal precedence level and equal associativity. Creates a new
Precedenceobject, adds it to.levels, adds.prec.leveland.prec.assocto all terminals in the list, and checks for duplicates.Parameters:
Name Type Description assocstring associativity:
'%left','%right', or'%nonassoc'.terminalsArray.<?module:Base~T> to add,
nullelements are ignored; no duplicates.- Inherited From:
- Overrides:
- Source:
Returns:
representing the set, or
nullif there are no terminals. -
reduce(t, rule)
-
Factory method to create a
reducemessage.Parameters:
Name Type Description tmodule:Base~T terminal on which to send message.
rulemodule:BNF~Rule rule to reduce.
Returns:
an object representing the message.
- Type
- module:BNF~Message
-
rule(nt [, symbols] [, terminal])
-
Factory method to create a rule representation for BNF. Maintains rule's non-terminal's
.rulesandthis.rules. Maintains rule's non-terminal's.empty. Precedence levels have to be defined prior to using this method.Parameters:
Name Type Argument Default Description ntmodule:BNF~NT left-hand side, non-terminal.
symbolsArray.<module:Base~Symbol> <optional>
right-hand side, list of symbols.
terminalmodule:Base~T <optional>
<nullable>
null can define rule's precedence, by default the precedence of the rightmost terminal, if any.
Returns:
a new rule representation.
- Type
- module:BNF~Rule
-
scanner( [skip] [, terminals])
-
Factory method to create a scanner.
Parameters:
Name Type Argument Description skipRegExp <optional>
a pattern to define ignorable character sequences, by default white space, must not accept empty input, must not use
d,g, oryflag, should not be anchored, should use(:? )rather than( )for grouping.terminalsArray.<T> <optional>
ordered list to create the lexical analysis pattern.
- Inherited From:
- Overrides:
- Source:
Returns:
the scanner.
- Type
- module:Base~Scanner
-
shift_or_goto(symbol, state)
-
Factory method to create a
shiftorgotomessage.Parameters:
Name Type Description symbolmodule:Base~Symbol symbol on which to send message.
statenumber state to shift to.
Returns:
an object representing the message.
- Type
- module:BNF~Message
-
state(core)
-
Factory method to represent a state of the parser automaton. The state is created from the core marks; rules in the closure are added.
Parameters:
Name Type Description coreArray.<module:BNF~Mark> list of marks in the core, closure is added.
Returns:
an object representing the state.
- Type
- module:BNF~State
-
toString( [states])
-
Displays description of grammar and number of errors if any.
Parameters:
Name Type Argument Description statesboolean <optional>
if true, also displays state table.
Returns:
- Type
- string
-
token( [name] [, pat] [, used])
-
Factory method to create a unique token symbol, maintains
.tokensand.tokensByName.Parameters:
Name Type Argument Description namestring <optional>
token's name conforming to
.config.tokens; error if a non-terminal. If omitted represents the$errortoken with an emptyRegExp.patRegExp <optional>
pattern to match values representing the token in input; used only when the token is created, must not accept empty input, must not use
d,g, oryflag, should not be anchored, should use(:? )rather than( )for grouping.usedboolean <optional>
if
truemark token as used.Returns:
a unique token.
- Type
- module:BNF~Token
-
tuple(lineno, t [, value])
-
Factory method to create an element of a tokenized input stream.
Parameters:
Name Type Argument Default Description linenonumber input position.
tmodule:Base~T <nullable>
terminal, i.e., literal or token object;
scan()usesnullfor an illegal character.valuestring <optional>
<nullable>
null terminal's representation in the input.
- Inherited From:
- Overrides:
- Source:
Returns:
an element of a tokenized input stream.
- Type
- module:Base~Tuple