/tutorial/backtracking/expand_expression/expand_expression.html

http://github.com/tybor/Liberty · HTML · 156 lines · 139 code · 17 blank · 0 comment · 0 complexity · c06184d4718e836190eb80de9dc72886 MD5 · raw file

  1. <html><head>
  2. <title>The tutorial expand_expression</title>
  3. <style>
  4. body {
  5. background-color: white;
  6. margin: 3px;
  7. }
  8. body * {
  9. /*
  10. margin: 0px 0px 5px 0px;
  11. */
  12. }
  13. h1 {
  14. background-color: #3366FF;
  15. font-size: 300%;
  16. color: white;
  17. text-align: center;
  18. font-weight: normal;
  19. padding: 10px;
  20. }
  21. h2 {
  22. background-color: #3366FF;
  23. font-size: 150%;
  24. color: white;
  25. text-align: left;
  26. font-weight: normal;
  27. padding: 5px;
  28. }
  29. .i {
  30. background-color: yellow;
  31. font-size: 110%;
  32. color: #3366FF;
  33. text-align: center;
  34. font-weight: bolder;
  35. padding: 10px 30px 10px 30px;
  36. }
  37. p {
  38. /*
  39. text-align: justify;
  40. */
  41. }
  42. pre {
  43. margin: 3px 50px 3px 50px;
  44. padding: 3px 5px 3px 5px;
  45. border: 2px dashed #3366FF;
  46. background: #eef;
  47. }
  48. .class {
  49. font-weight: bolder;
  50. color: #3366FF;
  51. }
  52. .sesign {
  53. text-align: center;
  54. font-size: small;
  55. font-style: italic;
  56. }
  57. </style>
  58. </head><body>
  59. <h1>The tutorial expand_expression</h1>
  60. <div class="i">
  61. <p>This tutorial shows basic use of the cluster backtracking.</p>
  62. <p>The same example: expanding syntactic expression, is
  63. written using two differents ways: <span class="class">ABSTRACT</span> and <span class="class">TREE</span>.
  64. It is recommended to start with <span class="class">TREE</span>.</p></div>
  65. <a name="sommaire"/><h2>Sommaire</h2>
  66. <div class="s"><ul class="s">
  67. <li><a href="#chap-1">How to compile?</a></li>
  68. <li><a href="#chap-2">What does it do?</a></li>
  69. <li><a href="#chap-3">How does 'tree' works?</a></li>
  70. <li><a href="#chap-4">How does 'abstract' works?</a></li>
  71. </ul></div>
  72. <a name="chap-1"/><h2>How to compile?</h2>
  73. <div class="c2">
  74. <p>In both case, just type:</p>
  75. <p> se c -clean -o a_nick_name expand_expression</p>
  76. </div>
  77. <a name="chap-2"/><h2>What does it do?</h2>
  78. <div class="c2">
  79. <p>Each of the 2 programs performs syntaxic expansion.
  80. For example, putting the line</p>
  81. <p><pre>
  82. (Hello + Hi) the ( + great) world!
  83. </pre></p>
  84. <p>at input will produce the output:</p>
  85. <p><pre>
  86. (1) Hello the world!
  87. (2) Hello the great world!
  88. (3) Hi the world!
  89. (4) Hi the great world!
  90. </pre></p>
  91. <p>The grammar of the inputs are:</p>
  92. <p><pre>
  93. input ::= alternative;
  94. alternative ::= sequence ['+' sequence]...;
  95. sequence ::= [term]...;
  96. term ::= '(' alternative ')' | "a term";
  97. </pre></p>
  98. <p>Exercice: write a program that does the same syntaxic expansion.
  99. (trick: reuse the parser of the tutorial)</p>
  100. </div>
  101. <a name="chap-3"/><h2>How does 'tree' works?</h2>
  102. <div class="c2">
  103. <p>The tree example uses the most usable class of the
  104. backtracking cluster: the class <span class="class">BACKTRACKING</span>.</p>
  105. <p>The class <span class="class">BACKTRACKING</span> make an exploration of the
  106. and/or graphes made of <span class="class">BACKTRACKING_NODE</span> instances. Many
  107. usefull <span class="class">BACKTRACKING_NODE</span> inheriters are defined and
  108. useables.</p>
  109. <p>The syntaxic expression is transformed to a such
  110. and/or structure. Alternatives does use <span class="class">BACKTRACKING_NODE_OR_PAIR</span>
  111. that make a or between two nodes. Sequences does use
  112. <span class="class">BACKTRACKING_NODE_AND_PAIR</span> that make a and between to nodes.
  113. In both cases, the order of the nodes is meaningful.
  114. Two other predefined nodes are used: the_true_node
  115. (of class <span class="class">BACKTRACKING_NODE_TRUE</span>) for empty terms, and,
  116. the_false_node (of class <span class="class">BACKTRACKING_NODE_FALSE</span>) for
  117. errors of syntax.</p>
  118. <p>To get practical results, the terms are using a local
  119. class <span class="class">STRING_NODE</span> that records what to print in a local
  120. field. When explored these nodes push the string on the
  121. and call the feature continue. Not that covariance is
  122. used.</p>
  123. <p>The use of that class is easy: define the features
  124. 'get_context' and 'restore_context'. These features
  125. will be called during the exploration to save the
  126. context before to explore an alternative and to restore
  127. it during the backtracking. These feature are defined
  128. as returning <span class="class">ANY</span> but please make a covariant redefinition
  129. as in exemples, where the context, an <span class="class">INTEGER</span>, records the
  130. count of items pushed.</p>
  131. </div>
  132. <a name="chap-4"/><h2>How does 'abstract' works?</h2>
  133. <div class="c2">
  134. <p>The abstract example does exactly the same that tree except
  135. that it uses a more abstract class <span class="class">ABSTRACT_BACKTRACKING</span>.</p>
  136. <p>That class is intended to be used when structures to be
  137. explored can not be derived from <span class="class">BACKTRACKING_NODE</span>. </p>
  138. </div>
  139. </body></html>