...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
A Nonterminal is a symbol in a Parsing Expression Grammar production that represents a grammar fragment. Nonterminals may self reference to specify recursion. This is one of the most important concepts and the reason behind the word "recursive" in recursive descent generation.
Nonterminals can have both consumed and inherited attributes. The Nonterminal's Signature specifies both the consumed and inherited attributes. The specification uses the function declarator syntax:
RT(A0, A1, A2, ..., AN)
where RT
is the Nonterminal's
consumed attribute and A0
... AN
are the Nonterminal's
inherited attributes.
The default value is void()
(no consumed and inherited attributes).
The Nonterminal models a C++ function. The Nonterminal's consumed attribute is analogous to the function return value as it is the type -exposed- by the Nonterminal. Its inherited attributes are analogous to function arguments. The inherited attributes (arguments) can be passed in just like any Lazy Argument, e.g.:
r(expr) // Evaluate expr at parse time and pass the result to the Nonterminal r
_val
The boost::spirit::karma::_val
placeholder can be used in Boost.Phoenix semantic
actions anywhere in the Nonterminal's definition. This Boost.Phoenix
placeholder refers to the Nonterminal's (consumed) attribute. The _val
placeholder acts like an immutable
reference to the Nonterminal's attribute.
Note | |
---|---|
Starting with Spirit V2.5
(distributed with Boost V1.47) the placeholder |
_r1
...r10
The boost::spirit::_r1
...boost::spirit::r10
placeholders can be used in Boost.Phoenix
semantic actions anywhere in the Nonterminal's definition. These Boost.Phoenix placeholders
refer to the Nonterminal's inherited attributes.
Nonterminals can have local variables that will be created on the stack at runtime. A locals descriptor added to the Nonterminal declaration will give the Nonterminal local variables:
template <typename T0, typename T1, typename T2, ..., typename TN> struct locals;
where T0
... TN
are the types of local variables
accessible in your Boost.Phoenix
semantic actions using the placeholders:
boost::spirit::_a
boost::spirit::_b
boost::spirit::_c
boost::spirit::_d
boost::spirit::_e
boost::spirit::_f
boost::spirit::_g
boost::spirit::_h
boost::spirit::_i
boost::spirit::_j
which correspond to the Nonterminal's local variables T0
... T9
.
Notation
x
A Nonterminal
X
A Nonterminal type
arg1
, arg2
, ..., argN
Lazy Arguments that evaluate to each of the Nonterminal's inherited attributes.
In addition to the requirements defined in Generator
, for any Nonterminal
the following must be met:
Expression |
Semantics |
Return type |
---|---|---|
|
In a generator expression, invoke Nonterminal |
|
|
In a generator expression, invoke Nonterminal |
|
|
Set the name of a Nonterminal |
|
|
Get the name of a Nonterminal |
|
Expression |
Description |
---|---|
|
The Signature of |
|
The local variables of |