• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[/==============================================================================
2    Copyright (C) 2001-2011 Joel de Guzman
3    Copyright (C) 2001-2011 Hartmut Kaiser
4
5    Distributed under the Boost Software License, Version 1.0. (See accompanying
6    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7===============================================================================/]
8
9[section Mini XML - ASTs!]
10
11Stop and think about it... We've come very close to generating an AST
12(abstract syntax tree) in our last example. We parsed a single structure and
13generated an in-memory representation of it in the form of a struct: the
14`struct employee`. If we changed the implementation to parse one or more
15employees, the result would be a `std::vector<employee>`. We can go on and add
16more hierarchy: teams, departments, corporations. Then we'll have an AST
17representation of it all.
18
19In this example (actually two examples), we'll now explore how to create
20ASTs. We will parse a minimalistic XML-like language and compile the results
21into our data structures in the form of a tree.
22
23Along the way, we'll see new features:
24
25* Inherited attributes
26* Variant attributes
27* Local Variables
28* Not Predicate
29* Lazy Lit
30
31The full cpp files for these examples can be found here:
32[@../../example/qi/mini_xml1.cpp] and here: [@../../example/qi/mini_xml2.cpp]
33
34There are a couple of sample toy-xml files in the mini_xml_samples subdirectory:
35[@../../example/qi/mini_xml_samples/1.toyxml],
36[@../../example/qi/mini_xml_samples/2.toyxml], and
37[@../../example/qi/mini_xml_samples/3.toyxml] for testing purposes.
38The example [@../../example/qi/mini_xml_samples/4.toyxml] has an error in it.
39
40[import ../../example/qi/mini_xml1.cpp]
41[import ../../example/qi/mini_xml2.cpp]
42
43[heading First Cut]
44
45Without further delay, here's the first version of the XML grammar:
46
47[tutorial_xml1_grammar]
48
49Going bottom up, let's examine the `text` rule:
50
51    rule<Iterator, std::string(), space_type> text;
52
53and its definition:
54
55    text = lexeme[+(char_ - '<')        [_val += _1]];
56
57The semantic action collects the chars and appends them (via +=) to the
58`std::string` attribute of the rule (represented by the placeholder `_val`).
59
60[heading Alternates]
61
62    rule<Iterator, mini_xml_node(), space_type> node;
63
64and its definition:
65
66    node = (xml | text)                 [_val = _1];
67
68We'll see a `mini_xml_node` structure later. Looking at the rule
69definition, we see some alternation going on here. An xml `node` is
70either an `xml` OR `text`. Hmmm... hold on to that thought...
71
72    rule<Iterator, std::string(), space_type> start_tag;
73
74Again, with an attribute of `std::string`. Then, it's definition:
75
76    start_tag =
77            '<'
78        >>  !char_('/')
79        >>  lexeme[+(char_ - '>')       [_val += _1]]
80        >>  '>'
81    ;
82
83[heading Not Predicate]
84
85`start_tag` is similar to the `text` rule apart from the added `'<'` and `'>'`.
86But wait, to make sure that the `start_tag` does not parse `end_tag`s too, we
87add: `!char_('/')`. This is a "Not Predicate":
88
89    !p
90
91It will try the parser, `p`. If it is successful, fail; otherwise, pass. In
92other words, it negates the result of `p`. Like the `eps`, it does not consume
93any input though. It will always rewind the iterator position to where it
94was upon entry. So, the expression:
95
96    !char_('/')
97
98basically says: we should not have a `'/'` at this point.
99
100[heading Inherited Attribute]
101
102The `end_tag`:
103
104    rule<Iterator, void(std::string), space_type> end_tag;
105
106Ohh! Now we see an inherited attribute there: `std::string`. The `end_tag` does
107not have a synthesized attribute. Let's see its definition:
108
109    end_tag =
110            "</"
111        >>  lit(_r1)
112        >>  '>'
113    ;
114
115`_r1` is yet another __phoenix__ placeholder for the first inherited attribute
116(we have only one, use `_r2`, `_r3`, etc. if you have more).
117
118[heading A Lazy Lit]
119
120Check out how we used `lit` here, this time, not with a literal string, but with
121the value of the first inherited attribute, which is specified as `std::string` in
122our rule declaration.
123
124Finally, our `xml` rule:
125
126    rule<Iterator, mini_xml(), space_type> xml;
127
128`mini_xml` is our attribute here. We'll see later what it is. Let's see its
129definition:
130
131    xml =
132            start_tag                   [at_c<0>(_val) = _1]
133        >>  *node                       [push_back(at_c<1>(_val), _1)]
134        >>  end_tag(at_c<0>(_val))
135    ;
136
137Those who know __fusion__ now will notice `at_c<0>` and `at_c<1>`. This gives us
138a hint that `mini_xml` is a sort of a tuple - a fusion sequence. `at_c<N>` here
139is a lazy version of the tuple accessors, provided by __phoenix__.
140
141[heading How it all works]
142
143So, what's happening?
144
145# Upon parsing `start_tag`, the parsed start-tag string is placed in
146  `at_c<0>(_val)`.
147
148# Then we parse zero or more `node`s. At each step, we `push_back` the result
149  into `at_c<1>(_val)`.
150
151# Finally, we parse the `end_tag` giving it an inherited attribute: `at_c<0>(_val)`.
152  This is the string we obtained from the `start_tag`. Investigate `end_tag` above.
153  It will fail to parse if it gets something different from what we got from the
154  `start_tag`. This ensures that our tags are balanced.
155
156To give the last item some more light, what happens is this:
157
158    end_tag(at_c<0>(_val))
159
160calls:
161
162    end_tag =
163            "</"
164        >>  lit(_r1)
165        >>  '>'
166    ;
167
168passing in `at_c<0>(_val)`, the string from start tag. This is referred to in the
169`end_tag` body as `_r1`.
170
171[heading The Structures]
172
173Let's see our structures. It will definitely be hierarchical: xml is
174hierarchical. It will also be recursive: xml is recursive.
175
176[tutorial_xml1_structures]
177
178[heading Of Alternates and Variants]
179
180So that's what a `mini_xml_node` looks like. We had a hint that it is either
181a `string` or a `mini_xml`. For this, we use __boost_variant__. `boost::recursive_wrapper`
182wraps `mini_xml`, making it a recursive data structure.
183
184Yep, you got that right: the attribute of an alternate:
185
186    a | b
187
188is a
189
190  boost::variant<A, B>
191
192where `A` is the attribute of `a` and `B` is the attribute of `b`.
193
194[heading Adapting structs again]
195
196`mini_xml` is no brainier. It is a plain ol' struct. But as we've seen in our
197employee example, we can adapt that to be a __fusion__ sequence:
198
199[tutorial_xml1_adapt_structures]
200
201[heading One More Take]
202
203Here's another version. The AST structure remains the same, but this time,
204you'll see that we make use of auto-rules making the grammar
205semantic-action-less. Here it is:
206
207[tutorial_xml2_grammar]
208
209This one shouldn't be any more difficult to understand after going through the
210first xml parser example. The rules are almost the same, except that, we got rid
211of semantic actions and used auto-rules (see the employee example if you missed
212that). There is some new stuff though. It's all in the `xml` rule:
213
214[heading Local Variables]
215
216    rule<Iterator, mini_xml(), locals<std::string>, space_type> xml;
217
218Wow, we have four template parameters now. What's that `locals` guy doing there?
219Well, it declares that the rule `xml` will have one local variable: a `string`.
220Let's see how this is used in action:
221
222    xml %=
223            start_tag[_a = _1]
224        >>  *node
225        >>  end_tag(_a)
226    ;
227
228# Upon parsing `start_tag`, the parsed start-tag string is placed in
229  the local variable specified by (yet another) __phoenix__ placeholder:
230  `_a`. We have only one local variable. If we had more, these are designated
231  by `_b`..`_z`.
232
233# Then we parse zero or more `node`s.
234
235# Finally, we parse the `end_tag` giving it an inherited attribute: `_a`, our
236  local variable.
237
238There are no actions involved in stuffing data into our `xml` attribute. It's
239all taken care of thanks to the auto-rule.
240
241[endsect]
242