number_list_attribute___one_more__with_style.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Number List Attribute - one more, with style</title>
  5. <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../../index.html" title="Spirit X3 3.0.4">
  8. <link rel="up" href="../tutorials.html" title="Tutorials">
  9. <link rel="prev" href="number_list_redux___list_syntax.html" title="Number List Redux - list syntax">
  10. <link rel="next" href="roman.html" title="Roman Numerals">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="number_list_redux___list_syntax.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorials.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="roman.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h3 class="title">
  27. <a name="spirit_x3.tutorials.number_list_attribute___one_more__with_style"></a><a class="link" href="number_list_attribute___one_more__with_style.html" title="Number List Attribute - one more, with style">Number
  28. List Attribute - one more, with style</a>
  29. </h3></div></div></div>
  30. <p>
  31. You've seen that the <code class="computeroutput"><span class="identifier">double_</span></code>
  32. parser has a <code class="computeroutput"><span class="keyword">double</span></code> attribute.
  33. All parsers have an attribute, even complex parsers. Those that are composed
  34. from primitives using operators, like the list parser, also have an attribute.
  35. It so happens that the attribute of a list parser:
  36. </p>
  37. <pre class="programlisting"><span class="identifier">p</span> <span class="special">%</span> <span class="identifier">d</span>
  38. </pre>
  39. <p>
  40. is a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span></code> of the attribute of <code class="computeroutput"><span class="identifier">p</span></code>. So, for our parser:
  41. </p>
  42. <pre class="programlisting"><span class="identifier">double_</span> <span class="special">%</span> <span class="char">','</span>
  43. </pre>
  44. <p>
  45. we'll have an attribute of:
  46. </p>
  47. <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span>
  48. </pre>
  49. <p>
  50. So, what does this give us? Well, we can simply pass in a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span></code> to our number list parser and it will
  51. happily churn out our result in our vector. For that to happen, we'll use
  52. a variation of the <code class="computeroutput"><span class="identifier">phrase_parse</span></code>
  53. with an additional argument: the parser's attribute. With the following arguments
  54. passed to <code class="computeroutput"><span class="identifier">phrase_parse</span></code>
  55. </p>
  56. <div class="orderedlist"><ol class="orderedlist" type="1">
  57. <li class="listitem">
  58. An iterator pointing to the start of the input
  59. </li>
  60. <li class="listitem">
  61. An iterator pointing to one past the end of the input
  62. </li>
  63. <li class="listitem">
  64. The parser object
  65. </li>
  66. <li class="listitem">
  67. Another parser called the skip parser
  68. </li>
  69. <li class="listitem">
  70. The parser's attribute
  71. </li>
  72. </ol></div>
  73. <p>
  74. Our parser now is further simplified to:
  75. </p>
  76. <pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Iterator</span><span class="special">&gt;</span>
  77. <span class="keyword">bool</span> <span class="identifier">parse_numbers</span><span class="special">(</span><span class="identifier">Iterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">Iterator</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;&amp;</span> <span class="identifier">v</span><span class="special">)</span>
  78. <span class="special">{</span>
  79. <span class="keyword">using</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">double_</span><span class="special">;</span>
  80. <span class="keyword">using</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">phrase_parse</span><span class="special">;</span>
  81. <span class="keyword">using</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">_attr</span><span class="special">;</span>
  82. <span class="keyword">using</span> <span class="identifier">ascii</span><span class="special">::</span><span class="identifier">space</span><span class="special">;</span>
  83. <span class="keyword">bool</span> <span class="identifier">r</span> <span class="special">=</span> <span class="identifier">phrase_parse</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span>
  84. <span class="comment">// Begin grammar</span>
  85. <span class="special">(</span>
  86. <span class="identifier">double_</span> <span class="special">%</span> <span class="char">','</span>
  87. <span class="special">)</span>
  88. <span class="special">,</span>
  89. <span class="comment">// End grammar</span>
  90. <span class="identifier">space</span><span class="special">,</span> <span class="identifier">v</span><span class="special">);</span>
  91. <span class="keyword">if</span> <span class="special">(</span><span class="identifier">first</span> <span class="special">!=</span> <span class="identifier">last</span><span class="special">)</span> <span class="comment">// fail if we did not get a full match</span>
  92. <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
  93. <span class="keyword">return</span> <span class="identifier">r</span><span class="special">;</span>
  94. <span class="special">}</span>
  95. </pre>
  96. <p>
  97. The full cpp file for this example can be found here: <a href="../../../../../example/x3/num_list/num_list4.cpp" target="_top">num_list4.cpp</a>
  98. </p>
  99. <p>
  100. <span class="bold"><strong>Hey, no more actions!!!</strong></span> Now we're entering
  101. the realm of attribute grammars. Cool eh?
  102. </p>
  103. </div>
  104. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  105. <td align="left"></td>
  106. <td align="right"><div class="copyright-footer">Copyright &#169; 2001-2018 Joel de Guzman,
  107. Hartmut Kaiser<p>
  108. Distributed under the Boost Software License, Version 1.0. (See accompanying
  109. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  110. </p>
  111. </div></td>
  112. </tr></table>
  113. <hr>
  114. <div class="spirit-nav">
  115. <a accesskey="p" href="number_list_redux___list_syntax.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorials.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="roman.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
  116. </div>
  117. </body>
  118. </html>