mini_xml.qbk 7.6 KB

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