syntax_diagram.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Syntax Diagram</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="../abstracts.html" title="Abstracts">
  9. <link rel="prev" href="../abstracts.html" title="Abstracts">
  10. <link rel="next" href="parsing_expression_grammar.html" title="Parsing Expression Grammar">
  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="../abstracts.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../abstracts.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="parsing_expression_grammar.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.abstracts.syntax_diagram"></a><a class="link" href="syntax_diagram.html" title="Syntax Diagram">Syntax Diagram</a>
  28. </h3></div></div></div>
  29. <p>
  30. In the next section, we will deal with Parsing Expression Grammars (PEG)
  31. <a href="#ftn.spirit_x3.abstracts.syntax_diagram.f0" class="footnote" name="spirit_x3.abstracts.syntax_diagram.f0"><sup class="footnote">[2]</sup></a>, a variant of Extended Backus-Naur Form (EBNF) <a href="#ftn.spirit_x3.abstracts.syntax_diagram.f1" class="footnote" name="spirit_x3.abstracts.syntax_diagram.f1"><sup class="footnote">[3]</sup></a> with a different interpretation. It is easier to understand PEG
  32. using Syntax Diagrams. Syntax diagrams represent a grammar graphically. It
  33. was used extensively by Niklaus Wirth <a href="#ftn.spirit_x3.abstracts.syntax_diagram.f2" class="footnote" name="spirit_x3.abstracts.syntax_diagram.f2"><sup class="footnote">[4]</sup></a> in the "Pascal User Manual". Syntax Diagrams are easily
  34. understandable by programmers due to their similarity to flow charts. The
  35. isomorphism of the diagrams and functions make them ideal for representing
  36. <a href="https://en.wikipedia.org/wiki/Recursive_descent_parser" target="_top">Recursive
  37. Descent</a> parsers which are essentially mutually recursive functions.
  38. </p>
  39. <h5>
  40. <a name="spirit_x3.abstracts.syntax_diagram.h0"></a>
  41. <span class="phrase"><a name="spirit_x3.abstracts.syntax_diagram.elements"></a></span><a class="link" href="syntax_diagram.html#spirit_x3.abstracts.syntax_diagram.elements">Elements</a>
  42. </h5>
  43. <p>
  44. All diagrams have one entry and one exit point. Arrows connect all possible
  45. paths through the grammar from the entry point to the exit point.
  46. </p>
  47. <div class="blockquote"><blockquote class="blockquote"><p>
  48. <span class="inlinemediaobject"><img src="../.././images/start_stop.png" alt="start_stop"></span>
  49. </p></blockquote></div>
  50. <p>
  51. Terminals are represented by round boxes. Terminals are atomic and usually
  52. represent plain characters, strings or tokens.
  53. </p>
  54. <div class="blockquote"><blockquote class="blockquote"><p>
  55. <span class="inlinemediaobject"><img src="../.././images/terminal.png" alt="terminal"></span>
  56. </p></blockquote></div>
  57. <p>
  58. Non-terminals are represented by boxes. Diagrams are modularized using named
  59. non-terminals. A complex diagram can be broken down into a set of non-terminals.
  60. Non-terminals also allow recursion (i.e. a non-terminal can call itself).
  61. </p>
  62. <div class="blockquote"><blockquote class="blockquote"><p>
  63. <span class="inlinemediaobject"><img src="../.././images/non-terminal.png" alt="non-terminal"></span>
  64. </p></blockquote></div>
  65. <h5>
  66. <a name="spirit_x3.abstracts.syntax_diagram.h1"></a>
  67. <span class="phrase"><a name="spirit_x3.abstracts.syntax_diagram.constructs"></a></span><a class="link" href="syntax_diagram.html#spirit_x3.abstracts.syntax_diagram.constructs">Constructs</a>
  68. </h5>
  69. <p>
  70. The most basic composition is the Sequence. B follows A:
  71. </p>
  72. <div class="blockquote"><blockquote class="blockquote"><p>
  73. <span class="inlinemediaobject"><img src="../.././images/sequence.png" alt="sequence"></span>
  74. </p></blockquote></div>
  75. <p>
  76. The ordered choice henceforth we will call <span class="emphasis"><em>alternatives</em></span>.
  77. In PEG, ordered choice and alternatives are not quite the same. PEG allows
  78. ambiguity of choice where one or more branches can succeed. In PEG, in case
  79. of ambiguity, the first one always wins.
  80. </p>
  81. <div class="blockquote"><blockquote class="blockquote"><p>
  82. <span class="inlinemediaobject"><img src="../.././images/alternative.png" alt="alternative"></span>
  83. </p></blockquote></div>
  84. <p>
  85. The optional (zero-or-one):
  86. </p>
  87. <div class="blockquote"><blockquote class="blockquote"><p>
  88. <span class="inlinemediaobject"><img src="../.././images/optional.png" alt="optional"></span>
  89. </p></blockquote></div>
  90. <p>
  91. Now, the loops. We have the zero-or-more and one-or-more:
  92. </p>
  93. <div class="blockquote"><blockquote class="blockquote"><p>
  94. <span class="inlinemediaobject"><img src="../.././images/kleene.png" alt="kleene"></span>
  95. </p></blockquote></div>
  96. <div class="blockquote"><blockquote class="blockquote"><p>
  97. <span class="inlinemediaobject"><img src="../.././images/plus.png" alt="plus"></span>
  98. </p></blockquote></div>
  99. <p>
  100. Take note that, as in PEG, these loops behave greedily. If there is another
  101. 'A' just before the end-point, it will always fail because the preceding
  102. loop has already exhausted all 'A's and there is nothing more left. This
  103. is a crucial difference between PEG and general Context Free Grammars (CFGs).
  104. This behavior is quite obvious with syntax diagrams as they resemble flow-charts.
  105. </p>
  106. <h5>
  107. <a name="spirit_x3.abstracts.syntax_diagram.h2"></a>
  108. <span class="phrase"><a name="spirit_x3.abstracts.syntax_diagram.predicates"></a></span><a class="link" href="syntax_diagram.html#spirit_x3.abstracts.syntax_diagram.predicates">Predicates</a>
  109. </h5>
  110. <p>
  111. Now, the following are Syntax Diagram versions of PEG predicates. These are
  112. not traditionally found in Syntax Diagrams. These are special extensions
  113. we invented to closely follow PEGs.
  114. </p>
  115. <p>
  116. First, we introduce a new element, the Predicate:
  117. </p>
  118. <div class="blockquote"><blockquote class="blockquote"><p>
  119. <span class="inlinemediaobject"><img src="../.././images/predicate.png" alt="predicate"></span>
  120. </p></blockquote></div>
  121. <p>
  122. This is similar to the conditionals in flow charts where the 'No' branch
  123. is absent and always signals a failed parse.
  124. </p>
  125. <p>
  126. We have two versions of the predicate, the <span class="emphasis"><em>And-Predicate</em></span>
  127. and the <span class="emphasis"><em>Not-Predicate</em></span>:
  128. </p>
  129. <div class="blockquote"><blockquote class="blockquote"><p>
  130. <span class="inlinemediaobject"><img src="../.././images/and_predicate.png" alt="and_predicate"></span>
  131. </p></blockquote></div>
  132. <div class="blockquote"><blockquote class="blockquote"><p>
  133. <span class="inlinemediaobject"><img src="../.././images/not_predicate.png" alt="not_predicate"></span>
  134. </p></blockquote></div>
  135. <p>
  136. The <span class="emphasis"><em>And-Predicate</em></span> tries the predicate, P, and succeeds
  137. if P succeeds, or otherwise fail. The opposite is true with the <span class="emphasis"><em>Not-Predicate</em></span>.
  138. It tries the predicate, P, and fails if P succeeds, or otherwise succeeds.
  139. Both versions do a look-ahead but do not consume any input regardless if
  140. P succeeds or not.
  141. </p>
  142. <div class="footnotes">
  143. <br><hr style="width:100; text-align:left;margin-left: 0">
  144. <div id="ftn.spirit_x3.abstracts.syntax_diagram.f0" class="footnote"><p><a href="#spirit_x3.abstracts.syntax_diagram.f0" class="para"><sup class="para">[2] </sup></a>
  145. Bryan Ford: Parsing Expression Grammars: A Recognition-Based Syntactic
  146. Foundation, <a href="http://pdos.csail.mit.edu/~baford/packrat/popl04/" target="_top">http://pdos.csail.mit.edu/~baford/packrat/popl04/</a>
  147. </p></div>
  148. <div id="ftn.spirit_x3.abstracts.syntax_diagram.f1" class="footnote"><p><a href="#spirit_x3.abstracts.syntax_diagram.f1" class="para"><sup class="para">[3] </sup></a>
  149. Richard E. Pattis: EBNF: A Notation to Describe Syntax, <a href="http://www.cs.cmu.edu/~pattis/misc/ebnf.pdf" target="_top">http://www.cs.cmu.edu/~pattis/misc/ebnf.pdf</a>
  150. </p></div>
  151. <div id="ftn.spirit_x3.abstracts.syntax_diagram.f2" class="footnote"><p><a href="#spirit_x3.abstracts.syntax_diagram.f2" class="para"><sup class="para">[4] </sup></a>
  152. Niklaus Wirth: The Programming Language Pascal. (July 1973)
  153. </p></div>
  154. </div>
  155. </div>
  156. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  157. <td align="left"></td>
  158. <td align="right"><div class="copyright-footer">Copyright &#169; 2001-2018 Joel de Guzman,
  159. Hartmut Kaiser<p>
  160. Distributed under the Boost Software License, Version 1.0. (See accompanying
  161. 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>)
  162. </p>
  163. </div></td>
  164. </tr></table>
  165. <hr>
  166. <div class="spirit-nav">
  167. <a accesskey="p" href="../abstracts.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../abstracts.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="parsing_expression_grammar.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
  168. </div>
  169. </body>
  170. </html>