Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

print_normal.C

Go to the documentation of this file.
00001 
00006 
00007 #include "node.h"
00008 
00009 #ifndef TOOLBOX
00010 
00011 local char* first_char(char* s)
00012 {
00013     char* s1 = s;
00014     while (*s1 == '(' || *s1 == ')' || *s1 == ',') s1++;
00015 
00016     if (*s1 == '\0') err_exit("first_char: NULL label");
00017 
00018     return s1;
00019 }
00020 
00021 local bool precedes(char*s1, char* s2)
00022 {
00023     // Return TRUE if s1 precedes s2 in our chosen ordering scheme.
00024 
00025     char* c1 = first_char(s1);
00026     char* c2 = first_char(s2);
00027 
00028     while (*c1 == *c2) {
00029 
00030         if (*c1 == '\0') err_exit("precedes: identical names!");
00031 
00032         c1++;
00033         c2++;
00034     }
00035 
00036     // Trick: short strings precede long ones because ")" and "," 
00037     // happen to precede alphanumeric characters in ASCII!
00038 
00039     if (*c1 > *c2) return FALSE;
00040     return TRUE;
00041 }
00042 
00043 local void sort_names(int n, char** list)       // Bubble sort!!
00044 {
00045     for (int i = 0; i < n; i++) {
00046         for (int j = i + 1; j < n; j++) {
00047             if (precedes(list[j], list[i])) {
00048                 char* temp = list[i];
00049                 list[i] = list[j];
00050                 list[j] = temp;
00051             }
00052         }
00053     }
00054 }
00055 
00056 local void construct_node_name(node* b)
00057 {
00058     if (!b->is_leaf()) {
00059         for_all_daughters(node, b, b1)
00060             construct_node_name(b1);
00061 
00062         char** list = new char*[b->n_daughters()];
00063 
00064         int n = 0;
00065         for_all_daughters(node, b, b2) {
00066             if (b2->get_name() == NULL) {
00067 
00068                 // Create name if none exists.
00069 
00070                 char temp2[128];
00071                 sprintf(temp2, "#%d", b2->get_index());
00072                 b2->set_label(temp2);
00073             }
00074             list[n++] = b2->get_name();
00075         }
00076 
00077         sort_names(n, list);
00078 
00079         // Don't worry about running out of space for now...
00080 
00081         char temp[1024];
00082 
00083         temp[0] = '(';
00084         temp[1] = '\0';
00085         int length = 2;
00086 
00087         for (int i = 0; i < n; i++) {
00088 
00089             int sl = strlen(list[i]);
00090             if (length + sl > 1024)
00091                 err_exit("construct_node: buffer overflow.");
00092 
00093             strcat(temp, list[i]);
00094             if (i != n - 1) strcat(temp, ",");
00095 
00096             length += sl + 1;
00097         }
00098         strcat(temp, ")");
00099 
00100         b->set_name(temp);
00101     }
00102 }
00103 
00104 void print_normal_form(node* b, ostream& s)
00105 {
00106     construct_node_name(b);
00107     s << b->get_name() << endl;
00108 }
00109 
00110 char* get_normal_form(node* b)  // Unused...
00111 {
00112     construct_node_name(b);
00113     return b->get_name();
00114 }
00115 
00116 /*===========================================================================*/
00117 
00118 #else
00119 
00120 /*-----------------------------------------------------------------------------
00121  *  main  --  driver to directly print out a tree structure
00122  *-----------------------------------------------------------------------------
00123  */
00124 main(int argc, char ** argv)
00125     {
00126     int i = 0;
00127     node * root;    /* root node */
00128 
00129     check_help();
00130 
00131     while (root = get_node(cin))
00132         {
00133         // cerr << "snapshot #" << ++i << ":  n = "
00134         //      << root->n_leaves() << endl;
00135         print_normal_form(root, cerr);
00136         // rmtree(root);
00137         }
00138     }
00139 
00140 #endif
00141 
00142 /*===========================================================================*/
00143 
00144 /* endof: print_normal.C */
00145 

Generated at Sun Feb 24 09:57:12 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001