| 1 |
tt(%type) directives is used to associate (non-)terminals with semantic |
| 2 |
value types. E.g., after: |
| 3 |
verb( |
| 4 |
%polymorphic INT: int; TEXT: std::string |
| 5 |
%type <INT> expr |
| 6 |
) |
| 7 |
the tt(expr) nonterminal returns tt(int) semantic values. In a |
| 8 |
rule like: |
| 9 |
verb( |
| 10 |
expr: |
| 11 |
expr '+' expr |
| 12 |
{ |
| 13 |
// Action block: C++ statements here. |
| 14 |
} |
| 15 |
) |
| 16 |
Inside action blocks $-notations can be used to retrieve and assign values |
| 17 |
from/to the elements of production rules. Type directives are used to |
| 18 |
associates $-notations with polymorphic semantic types. In the example $$, $1 |
| 19 |
and $3 are automatically associated with tt(int) values. $$ is associated with |
| 20 |
the rule (here:, `tt(expr:)'; it is the semantic value that becomes available |
| 21 |
once the production rule has been recognized), while $1 and $3 are tt(int) |
| 22 |
values which are associated with, resp., the first and third element (the |
| 23 |
tt(expr) nonterminals) in the production rule tt(expr '+' expr). |
| 24 |
|
| 25 |
Negative dollar indices can also be used (like $-1), but then pre-defined |
| 26 |
associations between nonterminals and semantic types are not available, and |
| 27 |
must explicitly be specified to access their polymorphic value types. Blanks |
| 28 |
cannot be used for type-specifications (so $<INT>-1 is OK, $< INT >-1 is not). |
| 29 |
|
| 30 |
Various $-constructions are available to indicate values of rule-elements. $$ |
| 31 |
constructions refer to values of nonterminals, $1 constructions refer to |
| 32 |
values of numbered elements of production rules (the examples use $1, but in |
| 33 |
general the positive number must be less than the element number of the action |
| 34 |
block in which it is used. Likewise, $-1 constructions refer to values of |
| 35 |
elements of production rules that are specified before the current rule's |
| 36 |
nonterminal, and in theory any negative number could be used. |
| 37 |
|
| 38 |
The term `value of' indicates the generic tt(STYPE_) value of an |
| 39 |
element. When a rule element is associated with a polymorphic type, then the |
| 40 |
term `semantic value of' is used. |
| 41 |
|
| 42 |
Here is an overview of the various possibilities: |
| 43 |
|
| 44 |
itemization( |
| 45 |
itt($$ = ) |
| 46 |
|
| 47 |
A semantic value is assigned to the rule. The right-hand side (rhs) of |
| 48 |
the assignment expression must be an expression of the type that is associated |
| 49 |
with $$. This assignment operation assumes that the type of the rhs-expression |
| 50 |
equals $$'s semantic value type. If the types don't match the compiler issues |
| 51 |
a compilation error when compiling tt(parse.cc). Casting the rhs to the |
| 52 |
correct value type is possible, but in that case the function call operator is |
| 53 |
preferred, which does not require casting. If no semantic value type was |
| 54 |
associated with $$ then the assignment tt($$ = STYPE_{}) can be used. |
| 55 |
|
| 56 |
itt($$(expr)) |
| 57 |
|
| 58 |
A semantic value is assigned to the rule. tt(Expr) must be of a type that |
| 59 |
can statically be cast to $$'s semantic value type. The required |
| 60 |
tt(static_cast) is generated by bic() and doesn't have to be specified for |
| 61 |
tt(expr). |
| 62 |
|
| 63 |
itt(_$$) |
| 64 |
|
| 65 |
This refers to the rule's return value (i.e., tt(d_val_)). No allowance |
| 66 |
is made for associated types. |
| 67 |
|
| 68 |
itt($$) |
| 69 |
|
| 70 |
If no semantic value was associated with $$ then this is shorthand for a |
| 71 |
reference to the rule's value. If a semantic value type was associated with $$ |
| 72 |
then a reference to the rule's semantic value is returned. |
| 73 |
|
| 74 |
itt($$.) |
| 75 |
|
| 76 |
If no semantic value was associated with $$ then this is shorthand for the |
| 77 |
member selector operator, applied to a reference to the rule's return |
| 78 |
value. If a semantic value type was associated with $$ then the member |
| 79 |
selector operator is applied to the rule's semantic value. |
| 80 |
|
| 81 |
itt($$->) |
| 82 |
|
| 83 |
If no semantic value was associated with $$ then this is shorthand for the |
| 84 |
pointer to member operator, applied to the rule's return value. If a semantic |
| 85 |
value type was associated with $$ then the pointer to member operator is |
| 86 |
applied to the rule's semantic value. |
| 87 |
|
| 88 |
|
| 89 |
itt(_$1) |
| 90 |
|
| 91 |
This refers to the variable containing the semantic value of the current |
| 92 |
production rule's first element. No allowance is made for associated types. |
| 93 |
|
| 94 |
itt($1) |
| 95 |
|
| 96 |
This construction refers to the semantic value of the production rule's |
| 97 |
first element. If no semantic value was associated with $1 then this is |
| 98 |
shorthand for the tt(STYPE_) value that's available for the production rule's |
| 99 |
element 1. If a semantic value was associated with $1 then $1 refers to the |
| 100 |
semantic value associated with the production rule's element 1. |
| 101 |
|
| 102 |
itt($1.) |
| 103 |
|
| 104 |
If no semantic value was associated with $1 then this is shorthand for the |
| 105 |
member selector operator, applied to the semantic value of the production |
| 106 |
rule's element 1. If a semantic value was associated with $1 then the member |
| 107 |
selector operator is applied to the semantic value associated with the |
| 108 |
production rule's element 1. |
| 109 |
|
| 110 |
itt($1->) |
| 111 |
|
| 112 |
If no semantic value was associated with $1 then this is shorthand for the |
| 113 |
pointer to member operator, applied to the semantic value of the production |
| 114 |
rule's element 1. If a semantic value was associated with $1 then the pointer |
| 115 |
to member operator is applied to the semantic value associated with the |
| 116 |
production rule's element 1. |
| 117 |
|
| 118 |
itt(_$-1) |
| 119 |
|
| 120 |
This refers to the value of some production rule element, 1 element before |
| 121 |
the current rule's nonterminal. No allowance is made for associated semantic |
| 122 |
types. |
| 123 |
|
| 124 |
itt($-1) |
| 125 |
|
| 126 |
Same: this refers to the value of some production rule element, 1 element |
| 127 |
before the current rule's nonterminal. No allowance is made for associated |
| 128 |
semantic types. |
| 129 |
|
| 130 |
itt($-1.) |
| 131 |
|
| 132 |
This is shorthand for the member selector operator applied to to the value |
| 133 |
of some production rule element, 1 element before the current rule's |
| 134 |
nonterminal. No allowance is made for associated semantic types. |
| 135 |
|
| 136 |
itt($1->) |
| 137 |
|
| 138 |
This is shorthand for the pointer to member operator applied to to the |
| 139 |
value of some production rule element, 1 element before the current rule's |
| 140 |
nonterminal. No allowance is made for associated semantic types. |
| 141 |
|
| 142 |
itt($<tag>-1) |
| 143 |
|
| 144 |
This refers to the semantic value of a polymorphic type (associated with |
| 145 |
tt(tag)) of some production rule element, 1 element before the current rule's |
| 146 |
nonterminal. |
| 147 |
|
| 148 |
If the semantic value type of that element doesn't match the type that is |
| 149 |
associated with tt(tag) then a run-time fatal error results. If that happens, |
| 150 |
and the tt(debug) option/directive was specified when bic() wrote the |
| 151 |
program's tt(parse) member, then the program can be rerun after specifying |
| 152 |
tt(parser.setDebug(Parser::ACTIONCASES)) to locate the tt(parse) function's |
| 153 |
action block where the fatal error was encountered. |
| 154 |
|
| 155 |
itt($<tag>-1.) |
| 156 |
|
| 157 |
This refers to the member selector operator applied to the semantic value |
| 158 |
of a polymorphic type (associated with tt(tag)) of some production rule |
| 159 |
element, 1 element before the current rule's nonterminal. |
| 160 |
|
| 161 |
If the semantic value type of that element doesn't match the type that is |
| 162 |
associated with tt(tag) then a run-time fatal error results. The procedure |
| 163 |
suggested at the previous tt($<tag>-1) item for solving such errors can be |
| 164 |
applied here as well. |
| 165 |
|
| 166 |
|
| 167 |
itt($<tag>-1->) |
| 168 |
|
| 169 |
This refers to the pointer to member operator applied to the semantic |
| 170 |
value of a polymorphic type (associated with tt(tag)) of some production rule |
| 171 |
element, 1 element before the current rule's nonterminal. |
| 172 |
|
| 173 |
If the semantic value type of that element doesn't match the type that is |
| 174 |
associated with tt(tag) then a run-time fatal error results. The procedure |
| 175 |
suggested at the previous tt($<tag>-1) item for solving such errors can be |
| 176 |
applied here as well. |
| 177 |
) |
| 178 |
|
| 179 |
COMMENT( |
| 180 |
whenhtml( |
| 181 |
center( |
| 182 |
table(1)(l)( |
| 183 |
rowline() |
| 184 |
row(cell(center(includefile(polytable)))) |
| 185 |
)) |
| 186 |
includefile(polytablenotes) |
| 187 |
rowline() |
| 188 |
) |
| 189 |
|
| 190 |
whenman( |
| 191 |
bf(%type<TAG> and $$ or $1 specifications:) |
| 192 |
includefile(polytable) |
| 193 |
includefile(polytablenotes) |
| 194 |
) |
| 195 |
|
| 196 |
|
| 197 |
bf(Direct semantic value assignments) (`$$(args)'): |
| 198 |
|
| 199 |
Production rules associated with polymorphic tags and production rules |
| 200 |
associated with tt(%type <STYPE_>) should assign values of matching types to |
| 201 |
the rules' semantic values. If a matching value type is available from a |
| 202 |
production rule's component it can directly be used to assign the semantic |
| 203 |
value of the rule's left-hand side nonterminal. E.g., $$ = $1 returns the |
| 204 |
semantic value of the production rule's first component. |
| 205 |
|
| 206 |
However, often production rules receiving tokens from the lexical scanner |
| 207 |
cannot refer to semantic values of components of their production rules, as |
| 208 |
the scanner merely returns the token and matching text, as in the following |
| 209 |
production rule: |
| 210 |
verb( |
| 211 |
number: // assume %type<NUMBER> number |
| 212 |
NUMBER |
| 213 |
{ |
| 214 |
// convert text to a numeric value |
| 215 |
} |
| 216 |
; |
| 217 |
) |
| 218 |
In such cases the appropriate semantic value must be initialized. |
| 219 |
|
| 220 |
Bic() translates phrases like tt($$ = xyz) into tt(d_val_.get<tag>() = |
| 221 |
xyz) (where tt(tag) is the production rule's left-hand side nonterminal's |
| 222 |
polymorphic tag, and tt(d_val_) (see below) is the parser's data member |
| 223 |
containing the action block's semantic value). The function tt(get) returns a |
| 224 |
reference to a value of the type matching tt(tag), but that value isn't |
| 225 |
available yet: in this case it is the action block's duty to initialize it. |
| 226 |
Unless there's comment at either side of the assignment operator, bic() |
| 227 |
recognizes such situations, and issues a warning, and such warnings should be |
| 228 |
prevented lest the generated tt(parse) function produce a segmentation |
| 229 |
fault. |
| 230 |
|
| 231 |
Fortunately, prevention is easy. To initialize a rule's semantic value a |
| 232 |
tt($$(args)) expression should be used (no blanks are allowed between |
| 233 |
tt($$) and the open-parenthesis). tt(Args) are at least one, comma-separated |
| 234 |
arguments. For tagged nonterminals bic() translates a tt($$(args)) |
| 235 |
expression into |
| 236 |
verb( |
| 237 |
d_val_.assign<tag>(args). |
| 238 |
) |
| 239 |
where `tt(tag)' is the tag associated with the production rule's left-hand |
| 240 |
side nonterminal. For nonterminals that are associated with a |
| 241 |
tt(%type<STYPE_>) specification only one argument can be passed to |
| 242 |
tt($$(arg)), which translates to |
| 243 |
verb( |
| 244 |
d_val_ = (arg); |
| 245 |
) |
| 246 |
The tt($$(args)) syntax cannot be used for untagged production rules. |
| 247 |
|
| 248 |
If a nonterminal is associated with tt(%type<STYPE_>) and its production |
| 249 |
rule calls a function returning a value of a known tagged type, then an |
| 250 |
explicit tt(assign) call can be used. E.g., |
| 251 |
verb( |
| 252 |
Symbol useSymbol(); // available function |
| 253 |
|
| 254 |
%polymorphic SYMBOL: Symbol; // directives |
| 255 |
%type<STYPE_> rule |
| 256 |
|
| 257 |
rule: // grammar rule |
| 258 |
IDENTIFIER |
| 259 |
{ |
| 260 |
$$.assign<Tag_::SYMBOL>(useSymbol()); |
| 261 |
} |
| 262 |
; |
| 263 |
|
| 264 |
bf(Member calls) (`$$.', `$1.', `($$)', `($1)', etc.):) |
| 265 |
|
| 266 |
When `$$.' or `$1.' is used default tags are ignored. A warning is issued |
| 267 |
that the default tag is ignored. This syntax allows members of the semantic |
| 268 |
value type (tt(STYPE_)) to be called explicitly. The default tag is only |
| 269 |
ignored if there are no additional characters (e.g., blanks, closing |
| 270 |
parentheses) between the dollar-expressions and the member selector operator |
| 271 |
(e.g., no tags are used with $1.member(), but tags are used with |
| 272 |
tt(($1).member())). In fact, notations like tt(($$), ($1)), etc. are synonym |
| 273 |
to using tt($$.get<Tag_::TYPE>(), $1.get<Tag_::TYPE>()) |
| 274 |
|
| 275 |
The opposite, overriding default tag associations, is |
| 276 |
accomplished using constructions like $<STYPE_>$ and $<STYPE_>1. |
| 277 |
|
| 278 |
When negative dollar indices are used, the appropriate tag must explicitly be |
| 279 |
specified. The next example shows how this is realized in the grammar |
| 280 |
specification file itself: |
| 281 |
verb( |
| 282 |
%polymorphic INT: int |
| 283 |
%type <INT> ident |
| 284 |
%% |
| 285 |
|
| 286 |
type: |
| 287 |
ident arg |
| 288 |
; |
| 289 |
|
| 290 |
arg: |
| 291 |
{ |
| 292 |
call($-1.get<Tag_::INT>()); |
| 293 |
} |
| 294 |
; |
| 295 |
) |
| 296 |
In this example tt(call) may define an tt(int) or tt(int &) parameter. |
| 297 |
|
| 298 |
It is also possible to delegate specification of the semantic value to the |
| 299 |
function tt(call) itself, as shown next: |
| 300 |
verb( |
| 301 |
%polymorphic INT: int |
| 302 |
%type <INT> ident |
| 303 |
%% |
| 304 |
|
| 305 |
type: |
| 306 |
ident arg |
| 307 |
; |
| 308 |
|
| 309 |
arg: |
| 310 |
{ |
| 311 |
call($-1); |
| 312 |
} |
| 313 |
; |
| 314 |
) |
| 315 |
Here, the function tt(call) could be implemented like this: |
| 316 |
verb( |
| 317 |
void call(STYPE_ &st) |
| 318 |
{ |
| 319 |
st.get<Tag_::INT>() = 5; |
| 320 |
} |
| 321 |
) |
| 322 |
|
| 323 |
Semantic values may also directly be associated with terminal tokens. In that |
| 324 |
case it is the lexical scanner's responsibility to assign a properly typed |
| 325 |
value to the parser's tt(STYPE_ d_val_) data member. When the lexical |
| 326 |
scanner receives a pointer to the parser's tt(d_val_) data member (using, |
| 327 |
e.g., a member tt(setSval(STYPE_ *dval))) |
| 328 |
IFDEF(manual)((cf. section ref(PRIVDATA)))(), |
| 329 |
then the lecical scanner must use em(tagged assignment) as shown in the above |
| 330 |
example to reach the different polymorphic types. The lexical scanner, having |
| 331 |
defined a tt(Parser::STYPE_ *d_val) data member could then use statements |
| 332 |
like |
| 333 |
verb( |
| 334 |
d_val.assign<Tag_::INT>(stoi(matched())); |
| 335 |
) |
| 336 |
to assign an tt(int) value to the parser's semantic value, which is then |
| 337 |
available to the parser when the lexical scanner's tt(lex) function returns as |
| 338 |
the semantic value (e.g., $1) of the production rule's element specifying the |
| 339 |
terminal token. Note, however that this requires the scanner to harbor some |
| 340 |
intelligence about the meaning of a tt(Parser::INT) token. It can be argued |
| 341 |
that this intelligence should only be available to the parser, and that the |
| 342 |
scanner should merely recognize regular expressions and return tokens and |
| 343 |
their corresponding matched text. |
| 344 |
|
| 345 |
END) |
| 346 |
|
| 347 |
|
| 348 |
|