#include #include #include #include #include #include #include #define N 300 using namespace std; typedef struct { int from; int to; string label; } trans; int main(int argv, char **argc) { if (argv!=2) { fprintf(stderr,"usage: stree2dot stree_file\n"); exit(EXIT_FAILURE); } FILE *in=fopen(argc[1],"r"); char buffer[N]; map node; // true if leaf, false else vector transition; char c; char *token; trans t; // reading //fgets(buffer,N,in); while (fgets(buffer,N,in)) { if (buffer[0]=='[') { int current=atoi(&buffer[1]); t.from=-current-1; node[-current-1]=false; //printf("new node: %i\n",current); } else { token=strtok(buffer,"\'"); token=strtok(NULL,"\'"); string label(token); if (label[0]=='<') label=""; t.label=label; //printf("\"%s\"\n",label.c_str()); token=strtok(NULL,"\'"); if (token[1]=='[') { // leaf int leaf=atoi(&token[2]); //printf("leaf=%i\n",leaf); node[leaf]=true; t.to=leaf; } else { int next=atoi(&token[1]); //printf("next=%i\n",next); t.to=-next-1; } transition.push_back(t); } } // end while printf("digraph \"suffix tree\" {\n"); printf("nodesep=0.5;center=true;labelfloat=false\n"); //pritnf("root [shape=plaintext,label=\"racine\"];\n"); // output int i=0; for (map::iterator it=node.begin(); it!=node.end(); it++) { //printf("node %i is ",it->first); if (it->second) printf("%i [color=black,shape=circle,peripheries=1];\n",it->first); else printf("%i [style=filled,color=black,shape=circle,height=0.15,width=0.15,label=\"\",peripheries=1];\n",it->first); //printf("%i [shape=circle,label=\"%i\",peripheries=1];\n",it->first,i++); } for (vector::iterator it=transition.begin(); it!=transition.end(); it++) { printf("%i -> %i [label=\"%s\"];\n",it->from,it->to,it->label.c_str()); } printf("}\n"); return 0; };