/tutorial/backtracking/expand_expression/expand_expression.txt

http://github.com/tybor/Liberty · Plain Text · 100 lines · 72 code · 28 blank · 0 comment · 0 complexity · bbeb0e97ee03985d71ce6f088b7f9c7a MD5 · raw file

  1. The tutorial expand_expression
  2. ##############################
  3. This tutorial shows basic use of the cluster backtracking.
  4. The same example: expanding syntactic expression, is
  5. written using two differents ways: ABSTRACT and TREE.
  6. It is recommended to start with TREE.
  7. How to compile?
  8. ===============
  9. In both case, just type:
  10. se c -clean -o a_nick_name expand_expression
  11. What does it do?
  12. ================
  13. Each of the 2 programs performs syntaxic expansion.
  14. For example, putting the line
  15. {{{{
  16. (Hello + Hi) the ( + great) world!
  17. }}}}
  18. at input will produce the output:
  19. {{{{
  20. (1) Hello the world!
  21. (2) Hello the great world!
  22. (3) Hi the world!
  23. (4) Hi the great world!
  24. }}}}
  25. The grammar of the inputs are:
  26. {{{{
  27. input ::= alternative;
  28. alternative ::= sequence ['+' sequence]...;
  29. sequence ::= [term]...;
  30. term ::= '(' alternative ')' | "a term";
  31. }}}}
  32. Exercice: write a program that does the same syntaxic expansion.
  33. (trick: reuse the parser of the tutorial)
  34. How does 'tree' works?
  35. ======================
  36. The tree example uses the most usable class of the
  37. backtracking cluster: the class BACKTRACKING.
  38. The class BACKTRACKING make an exploration of the
  39. and/or graphes made of BACKTRACKING_NODE instances. Many
  40. usefull BACKTRACKING_NODE inheriters are defined and
  41. useables.
  42. The syntaxic expression is transformed to a such
  43. and/or structure. Alternatives does use BACKTRACKING_NODE_OR_PAIR
  44. that make a or between two nodes. Sequences does use
  45. BACKTRACKING_NODE_AND_PAIR that make a and between to nodes.
  46. In both cases, the order of the nodes is meaningful.
  47. Two other predefined nodes are used: the_true_node
  48. (of class BACKTRACKING_NODE_TRUE) for empty terms, and,
  49. the_false_node (of class BACKTRACKING_NODE_FALSE) for
  50. errors of syntax.
  51. To get practical results, the terms are using a local
  52. class STRING_NODE that records what to print in a local
  53. field. When explored these nodes push the string on the
  54. and call the feature continue. Not that covariance is
  55. used.
  56. The use of that class is easy: define the features
  57. 'get_context' and 'restore_context'. These features
  58. will be called during the exploration to save the
  59. context before to explore an alternative and to restore
  60. it during the backtracking. These feature are defined
  61. as returning ANY but please make a covariant redefinition
  62. as in exemples, where the context, an INTEGER, records the
  63. count of items pushed.
  64. How does 'abstract' works?
  65. ==========================
  66. The abstract example does exactly the same that tree except
  67. that it uses a more abstract class ABSTRACT_BACKTRACKING.
  68. That class is intended to be used when structures to be
  69. explored can not be derived from BACKTRACKING_NODE.
  70. .def [[:upper:]_]\{2,\} <span class="class">&</span>
  71. .def {{{{ <pre>
  72. .def }}}} </pre>