You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`const SemanticValues& sv` contains semantic values. `SemanticValues` structure is defined as follows.
89
+
`const SemanticValues& sv` contains the following information:
90
+
91
+
- Semantic values
92
+
- Matched string information
93
+
- Token information if the rule is literal or uses a token boundary operator
94
+
- Choice number when the rule is 'prioritized choise'
95
+
96
+
`any& dt` is a 'read-write' context data which can be used for whatever purposes. The initial context data is set in `peg::parser::parse` method.
97
+
98
+
`peg::any` is a simpler implementatin of [boost::any](http://www.boost.org/doc/libs/1_57_0/doc/html/any.html). It can wrap arbitrary data type.
99
+
100
+
A semantic action can return a value of arbitrary data type, which will be wrapped by `peg::any`. If a user returns nothing in a semantic action, the first semantic value in the `const SemanticValues& sv` argument will be returned. (Yacc parser has the same behavior.)
`peg::any` class is very similar to [boost::any](http://www.boost.org/doc/libs/1_57_0/doc/html/any.html). You can obtain a value by castning it to the actual type. In order to determine the actual type, you have to check the return value type of the child action for the semantic value.
117
-
118
-
`any& dt` is a data object which can be used by the user for whatever purposes.
119
-
120
-
The following example uses `<` ... ` >` operators. They are the *token boundary* operators.
129
+
The following example uses `<` ... ` >` operator, which is *token boundary* operator.
121
130
122
131
```cpp
123
132
auto syntax = R"(
@@ -128,7 +137,7 @@ auto syntax = R"(
128
137
129
138
peg pg(syntax);
130
139
131
-
pg["TOKEN"] = [](const SemanticValues& sv) {
140
+
pg["TOKEN"] = [](const auto& sv) {
132
141
// 'token' doesn't include trailing whitespaces
133
142
auto token = sv.token();
134
143
};
@@ -145,7 +154,7 @@ peg::pegparser parser(
145
154
" ~_ <- [ \t]* "
146
155
);
147
156
148
-
parser["ROOT"] = [&](constSemanticValues& sv) {
157
+
parser["ROOT"] = [&](constauto& sv) {
149
158
assert(sv.size() == 2); // should be 2 instead of 5.
*cpp-peglib* is able to generate an AST (Abstract Syntax Tree) when parsing. `enable_ast` method on `peg::parser` class enables the feature.
246
+
247
+
```
248
+
peg::parser parser("...");
249
+
250
+
parser.enable_ast();
251
+
252
+
shared_ptr<peg::Ast> ast;
253
+
if (parser.parse("...", ast)) {
254
+
cout << peg::ast_to_s(ast);
255
+
256
+
ast = peg::AstOptimizer(true).optimize(ast);
257
+
cout << peg::ast_to_s(ast);
258
+
}
259
+
```
260
+
261
+
`peg::AstOptimizer` removes redundant nodes to make a AST simpler. You can make your own AST optimizers to fit your needs.
262
+
263
+
See actual usages in the [AST calculator example](https://github.com/yhirose/cpp-peglib/blob/master/example/calc3.cc) and [PL/0 Interpreter example](https://github.com/yhirose/cpp-peglib/blob/master/language/pl0/pl0.cc).
0 commit comments