PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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