introduction.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta content=
  5. "HTML Tidy for Windows (vers 1st February 2003), see www.w3.org"
  6. name="generator">
  7. <title>
  8. Introduction
  9. </title>
  10. <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
  11. <link rel="stylesheet" href="theme/style.css" type="text/css">
  12. </head>
  13. <body>
  14. <table width="100%" border="0" background="theme/bkd2.gif" cellspacing="2">
  15. <tr>
  16. <td width="10" height="49"></td>
  17. <td width="85%" height="49">
  18. <font size="6" face=
  19. "Verdana, Arial, Helvetica, sans-serif"><b>Introduction</b></font>
  20. </td>
  21. <td width="112" height="49">
  22. <a href="http://spirit.sf.net"><img src="theme/spirit.gif"
  23. width="112" height="48" align="right" border="0"></a>
  24. </td>
  25. </tr>
  26. </table><br>
  27. <table border="0">
  28. <tr>
  29. <td width="10"></td>
  30. <td width="30">
  31. <a href="../index.html"><img src="theme/u_arr.gif" border="0"></a>
  32. </td>
  33. <td width="30">
  34. <a href="preface.html"><img src="theme/l_arr.gif" width="20"
  35. height="19" border="0"></a>
  36. </td>
  37. <td width="30">
  38. <a href="quick_start.html"><img src="theme/r_arr.gif" border="0"></a>
  39. </td>
  40. </tr>
  41. </table>
  42. <p>
  43. Spirit is an object-oriented recursive-descent parser generator framework
  44. implemented using template meta-programming techniques. Expression
  45. templates allow us to approximate the syntax of Extended Backus-Normal
  46. Form (EBNF) completely in C++.
  47. </p>
  48. <p>
  49. The Spirit framework enables a target grammar to be written exclusively
  50. in C++. Inline EBNF grammar specifications can mix freely with other C++
  51. code and, thanks to the generative power of C++ templates, are
  52. immediately executable. In retrospect, conventional compiler-compilers or
  53. parser-generators have to perform an additional translation step from the
  54. source EBNF code to C or C++ code.
  55. </p>
  56. <p>
  57. A simple EBNF grammar snippet:
  58. </p>
  59. <pre><code><font color="#000000"> </font></code><code><font color="#000000"><span class="identifier">group </span> <span class="special">::=</span> <span class="literal">'('</span> <span class="identifier">expression</span> <span class="literal">')'
  60. </span> <span class="identifier">factor </span> <span class=
  61. "special">::=</span> <span class="identifier">integer</span> <span class=
  62. "special">|</span> <span class="identifier">group
  63. </span> <span class="identifier">term </span> <span class=
  64. "special">::=</span> <span class="identifier">factor</span> <span class=
  65. "special">((</span><span class="literal">'*'</span> <span class=
  66. "identifier">factor</span><span class="special">)</span> <span class=
  67. "special">|</span> <span class="special">(</span><span class=
  68. "literal">'/'</span> <span class="identifier">factor</span><span class=
  69. "special">))*
  70. </span> <span class="identifier">expression </span> <span class=
  71. "special">::=</span> <span class="identifier">term</span> <span class=
  72. "special">((</span><span class="literal">'+'</span> <span class=
  73. "identifier">term</span><span class="special">)</span> <span class=
  74. "special">|</span> <span class="special">(</span><span class=
  75. "literal">'-'</span> <span class="identifier">term</span><span class=
  76. "special">))*</span></font></code></pre>
  77. <p>
  78. is approximated using Spirit's facilities as seen in this code snippet:
  79. </p>
  80. <pre><code><font color="#000000"> </font></code><code><font color="#000000"><span class=
  81. "identifier">group </span> <span class=
  82. "special">=</span> <span class="literal">'('</span> <span class=
  83. "special">&gt;&gt;</span> <span class=
  84. "identifier">expression</span> <span class=
  85. "special">&gt;&gt;</span> <span class="literal">')'</span><span class=
  86. "special">;
  87. </span> <span class="identifier">factor </span> <span class=
  88. "special">=</span> <span class="identifier">integer</span> <span class=
  89. "special">|</span> <span class="identifier">group</span><span class="special">;
  90. </span> <span class="identifier">term </span> <span class=
  91. "special">=</span> <span class="identifier">factor</span> <span class=
  92. "special">&gt;&gt;</span> <span class="special">*((</span><span class=
  93. "literal">'*'</span> <span class="special">&gt;&gt;</span> <span class=
  94. "identifier">factor</span><span class="special">)</span> <span class=
  95. "special">|</span> <span class="special">(</span><span class=
  96. "literal">'/'</span> <span class="special">&gt;&gt;</span> <span class=
  97. "identifier">factor</span><span class="special">));
  98. </span> <span class="identifier">expression </span> <span class=
  99. "special">=</span> <span class="identifier">term</span> <span class=
  100. "special">&gt;&gt;</span> <span class="special">*((</span><span class=
  101. "literal">'+'</span> <span class="special">&gt;&gt;</span> <span class=
  102. "identifier">term</span><span class="special">)</span> <span class=
  103. "special">|</span> <span class="special">(</span><span class=
  104. "literal">'-'</span> <span class="special">&gt;&gt;</span> <span class=
  105. "identifier">term</span><span class="special">));</span></font></code>
  106. </pre>
  107. <p>
  108. Through the magic of expression templates, this is perfectly valid and
  109. executable C++ code. The production rule <tt>expression</tt> is in fact
  110. an object that has a member function parse that does the work given a
  111. source code written in the grammar that we have just declared. Yes, it's
  112. a calculator. We shall simplify for now by skipping the type declarations
  113. and the definition of the rule <tt>integer</tt> invoked by
  114. <tt>factor</tt>. The production rule <tt>expression</tt> in our grammar
  115. specification, traditionally called the start symbol, can recognize
  116. inputs such as:
  117. </p>
  118. <pre><code><font color="#000000"> </font></code><span class="number">12345
  119. </span><code><font color="#000000"> </font></code><span class="special">-</span><span class="number">12345
  120. </span><code><font color="#000000"> </font></code><span class="special">+</span><span class="number">12345
  121. </span><code><font color="#000000"> </font></code><span class="number">1</span> <span class=
  122. "special">+</span> <span class="number">2
  123. </span><code><font color="#000000"> </font></code><span class="number">1</span> <span class=
  124. "special">*</span> <span class="number">2
  125. </span><code><font color="#000000"> </font></code><span class="number">1</span><span class=
  126. "special">/</span><span class="number">2</span> <span class=
  127. "special">+</span> <span class="number">3</span><span class=
  128. "special">/</span><span class="number">4
  129. </span><code><font color="#000000"> </font></code><span class="number">1</span> <span class=
  130. "special">+</span> <span class="number">2</span> <span class=
  131. "special">+</span> <span class="number">3</span> <span class=
  132. "special">+</span> <span class="number">4
  133. </span><code><font color="#000000"> </font></code><span class="number">1</span> <span class=
  134. "special">*</span> <span class="number">2</span> <span class=
  135. "special">*</span> <span class="number">3</span> <span class=
  136. "special">*</span> <span class="number">4
  137. </span><code><font color="#000000"> </font></code><span class="special">(</span><span class=
  138. "number">1</span> <span class="special">+</span> <span class=
  139. "number">2</span><span class="special">)</span> <span class=
  140. "special">*</span> <span class="special">(</span><span class=
  141. "number">3</span> <span class="special">+</span> <span class=
  142. "number">4</span><span class="special">)
  143. </span><code><font color="#000000"> </font></code><span class="special">(-</span><span class=
  144. "number">1</span> <span class="special">+</span> <span class=
  145. "number">2</span><span class="special">)</span> <span class=
  146. "special">*</span> <span class="special">(</span><span class=
  147. "number">3</span> <span class="special">+</span> <span class=
  148. "special">-</span><span class="number">4</span><span class="special">)
  149. </span><code><font color="#000000"> </font></code><span class="number">1</span> <span class=
  150. "special">+</span> <span class="special">((</span><span class=
  151. "number">6</span> <span class="special">*</span> <span class=
  152. "number">200</span><span class="special">)</span> <span class=
  153. "special">-</span> <span class="number">20</span><span class=
  154. "special">)</span> <span class="special">/</span> <span class="number">6
  155. </span><code><font color="#000000"> </font></code><span class="special">(</span><span class=
  156. "number">1</span> <span class="special">+</span> <span class=
  157. "special">(</span><span class="number">2</span> <span class=
  158. "special">+</span> <span class="special">(</span><span class=
  159. "number">3</span> <span class="special">+</span> <span class=
  160. "special">(</span><span class="number">4</span> <span class=
  161. "special">+</span> <span class="number">5</span><span class=
  162. "special">))))</span>
  163. </pre>
  164. <p>
  165. Certainly we have done some modifications to the original EBNF syntax.
  166. This is done to conform to C++ syntax rules. Most notably we see the
  167. abundance of shift <tt>&gt;&gt;</tt> operators. Since there are no
  168. 'empty' operators in C++, it is simply not possible to write something
  169. like:
  170. </p>
  171. <pre><code><font color="#000000"> </font></code><span class=
  172. "identifier">a</span> <span class="identifier">b</span>
  173. </pre>
  174. <p>
  175. as seen in math syntax, for example, to mean multiplication or, in our
  176. case, as seen in EBNF syntax to mean sequencing (b should follow a). The
  177. framework uses the shift <tt class="operators">&gt;&gt;</tt> operator
  178. instead for this purpose. We take the <tt class="operators">&gt;&gt;</tt>
  179. operator, with arrows pointing to the right, to mean "is followed by".
  180. Thus we write:
  181. </p>
  182. <pre><code><font color="#000000"> </font></code><span class=
  183. "identifier">a</span> <span class="special">&gt;&gt;</span> <span class=
  184. "identifier">b</span>
  185. </pre>
  186. <p>
  187. The alternative operator <tt class="operators">|</tt> and the parentheses
  188. <tt class="operators">()</tt> remain as is. The assignment operator
  189. <tt class="operators">=</tt> is used in place of EBNF's <tt class=
  190. "operators">::=</tt>. Last but not least, the Kleene star <tt class=
  191. "operators">*</tt> which used to be a postfix operator in EBNF becomes a
  192. prefix. Instead of:
  193. </p>
  194. <pre><code><font color="#000000"> </font></code><span class="identifier">a</span><span class=
  195. "special">*</span> <span class="comment">//... in EBNF syntax,</span>
  196. </pre>
  197. <p>
  198. we write:
  199. </p>
  200. <pre><code><font color="#000000"> </font></code><span class="special">*</span><span class=
  201. "identifier">a</span> <span class="comment">//... in Spirit.</span>
  202. </pre>
  203. <p>
  204. since there are no postfix stars, "<tt class="operators">*</tt>", in
  205. C/C++. Finally, we terminate each rule with the ubiquitous semi-colon,
  206. "<tt>;</tt>".
  207. </p>
  208. <table border="0">
  209. <tr>
  210. <td width="10"></td>
  211. <td width="30">
  212. <a href="../index.html"><img src="theme/u_arr.gif" border="0"></a>
  213. </td>
  214. <td width="30">
  215. <a href="preface.html"><img src="theme/l_arr.gif" width="20"
  216. height="19" border="0"></a>
  217. </td>
  218. <td width="30">
  219. <a href="quick_start.html"><img src="theme/r_arr.gif" border="0"></a>
  220. </td>
  221. </tr>
  222. </table><br>
  223. <hr size="1">
  224. <p class="copyright">
  225. Copyright &copy; 1998-2003 Joel de Guzman<br>
  226. <br>
  227. <font size="2">Use, modification and distribution is subject to the
  228. Boost Software License, Version 1.0. (See accompanying file
  229. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)</font>
  230. </p>
  231. <p>&nbsp;
  232. </p>
  233. </body>
  234. </html>