#include "xref.h" #define FIRST 'F' #define INDENT 2 #define MAXINDENT 80 #define EXP_YES '1' #define EXP_NO '0' #define BLANK ' ' #define ALTERNAT '|' #define INHERIT 'i' #define OPTIONAL 'o' #define REPETIT 'r' #define LABEL 'l' #define PAREN 'p' #define PAREN_A 'A' #define PAREN_B 'B' #define MAX_LINES 300 char line[BUFSIZ]; char *LeftSide[MAX_LINES], *RightSide[MAX_LINES]; int Sides_Index = 0; int curr_col = -1, max_col = -1, max_lhs = 0; DemIdent *iIdent = new DemIdent("HEAD"); Vertex *iVertex = new Vertex(iIdent); Vertex_List *p_list = new Vertex_List(iVertex); char PS ; void add_to_both_sides() { int i; LeftSide[Sides_Index] = (char *)malloc( ( i = strlen(line) ) + 1); if (LeftSide[Sides_Index] == NULL) { fprintf(stderr, "Out of memory\n"); exit(0); } strcpy(LeftSide[Sides_Index], line); if ( max_lhs < i ) max_lhs = i; RightSide[Sides_Index] = (char *)malloc(max_col+2); if (RightSide[Sides_Index] == NULL) { fprintf(stderr, "Out of memory\n"); exit(0); } for(i=0; i<= max_col; i++){ if ( i == curr_col ) RightSide[Sides_Index][i] = PS; else RightSide[Sides_Index][i] = '|'; } RightSide[Sides_Index][i] = '\0'; if (++Sides_Index >= MAX_LINES) { fprintf(stderr,"Maximum number of lines allowed is %d.\n", MAX_LINES); exit(0); } } void clean_up_right() { int row, col; for(col=strlen(RightSide[Sides_Index - 1]) - 1; col >=0; col--){ for(row=Sides_Index - 1; RightSide[row][col] != '-' && RightSide[row][col] != '\0'; row--) RightSide[row][col] = ' '; if (RightSide[row][col] == '-') { if (col >= strlen(RightSide[row - 1])) RightSide[row][col] = ' '; } } } void add_classname_to_rhs(Vertex_List *v_list) { Vertex_list_iterator next_arg(*v_list); Vertex_ each_arg; int position = 0, row = 0, col; char *temp; each_arg = next_arg(); while(each_arg = next_arg()){ if( row >=Sides_Index-1) return; while ((col=strlen(RightSide[row])-1) < position) {if (row < Sides_Index-2)row++; else return;} if (RightSide[row][col] != ' ') { temp = RightSide[row]; RightSide[row] = (char *)malloc(strlen(temp)+strlen(each_arg->get_vertex_name()->get_val())+1); if (RightSide[row] == NULL) { fprintf(stderr, "Out of memory\n"); exit(0); } strcpy(RightSide[row],temp); strcat(RightSide[row],each_arg->get_vertex_name()->get_val()); free(temp); } row++; position++; } } void collapse_rhs() { int row, col, i; for(row=0; row < Sides_Index; row ++){ col = strlen(RightSide[row]) - 1; if( RightSide[row][col] == ' '){ for(i=row; i < Sides_Index; i++) strcpy(RightSide[i] + col, RightSide[i] + col + 1); } } } void Demeter_in::print() { input->print(); } void Input::print() { } void Cd_graph::print() { int i, j; DemIdent *iIdent = new DemIdent("HEAD"); Vertex *iVertex = new Vertex(iIdent); Vertex_List *v_list = new Vertex_List(iVertex); adjacencies->print(v_list, this, FIRST,0); add_to_both_sides(); clean_up_right(); add_classname_to_rhs(v_list); collapse_rhs(); for (i=0; i < Sides_Index; i++) { cout << LeftSide[i]; for (j=strlen(LeftSide[i]); j < max_lhs+3; j++) cout << " "; for (j=0; RightSide[i][j]; j++) { if (RightSide[i][j] == '-') cout << "-|"; else if (RightSide[i][j] == '|') cout << " |"; else if (RightSide[i][j] == ' ') cout << " "; else { cout << RightSide[i]+j; break; } } cout << "\n"; } } void Adjacency_Nlist::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format) { Adjacency_list_iterator next_arg(*this); Adjacency_ each_arg; while(each_arg = next_arg()){ if (v_list->occurs_in(((each_arg->get_source())->get_vertex_name())->get_val()) == -1 ) each_arg->print_first(v_list, Cd, type, format); if(each_arg->get_parameters()) each_arg->print_first(v_list, Cd, PAREN_B, format); } } int Vertex_List::occurs_in(char *key) { Vertex_list_iterator next_arg(*this); Vertex_ each_arg; int position = -1; while(each_arg = next_arg()) { if (each_arg->occurs_in(key)) return(position); else ++position; } return(-1); } int Vertex::occurs_in(char* key) { if(!strcmp(key, vertex_name->get_val()) ) return(1); return(0); } void Adjacency::print_first(Vertex_List *v_list, Cd_graph *Cd, char type, int format) { source->print(v_list, Cd, type, format,0); } void Adjacency::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format) { int i; if ( (i=v_list->occurs_in((source->get_vertex_name())->get_val())) == -1){ v_list->append(source); i=v_list->occurs_in((source->get_vertex_name())->get_val()); curr_col = i; if (max_col < curr_col) max_col = curr_col; if (parameters){ parameters->print(v_list, Cd, PAREN, ++format,0); strcat(line, ")"); } strcat(line, "(" ); ns->print(v_list, Cd, type , ++format); strcat(line, ")");} else { curr_col = i; if (max_col < curr_col) max_col = curr_col; if (type==PAREN_B) { if (parameters){ parameters->print(v_list, Cd, PAREN, ++format,0); strcat(line, ")"); } strcat(line, "(" ); ns->print(v_list, Cd, type , ++format); strcat(line, ")");} } } void Vertex::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format,DemIdent *label) { Adjacency *adj; int i; char indstr[INDENT * 256]; //indstr = (char *)realloc(NULL, INDENT * format); //indstr = new char[INDENT * format]; for (i=0; i < (INDENT * format); i++) indstr[i] = ' '; indstr[i] = '\0'; add_to_both_sides(); *line = '\0'; if (p_list->occurs_in((this->get_vertex_name())->get_val()) == -1 ) PS = '-'; else PS ='|'; strcat(line, indstr); switch(type){ case PAREN : strcat(line, "("); break; case ALTERNAT: strcat(line, "|"); break; case INHERIT: strcat(line, "*i*"); break; case REPETIT: strcat(line, "{"); break; case OPTIONAL : strcat(line, "["); break; default: break; } if (label){ strcat(line, "<"); strcat(line, label->get_val()); strcat(line, ">"); }; strcat(line, vertex_name->get_val() ); switch(type){ case REPETIT: strcat(line, "}"); break; case OPTIONAL : strcat(line, "]"); break; } if (type == PAREN_B) {if ((adj = this->expand(Cd)) != NULL){ if ((adj = this->expand(Cd)) != NULL){ adj->print(v_list, Cd, type,format) ; return; }} } else if (type != PAREN_A) if ((adj = this->expand(Cd)) != NULL){ adj->print(v_list, Cd, FIRST,format) ; return; } else{ if (type == PAREN) p_list->append(this); if ((i =v_list->occurs_in(vertex_name->get_val()))==-1) { v_list->append(this); i=v_list->occurs_in(vertex_name->get_val()); } curr_col = i; if (max_col < curr_col) max_col = curr_col; } else { if ((i =v_list->occurs_in(vertex_name->get_val()))==-1) { v_list->append(this); i=v_list->occurs_in(vertex_name->get_val()); } curr_col = i; if (max_col < curr_col) max_col = curr_col; } /* Terminal Sets */ // add_to_both_sides(); // *line = '\0'; } Adjacency * Vertex::expand(Cd_graph *Cd) { return( this->find_defn(vertex_name,Cd->get_adjacencies())); } Adjacency * Vertex::find_defn(DemIdent *key, Adjacency_Nlist *a_list) { Adjacency_list_iterator next_arg(*a_list); Adjacency_ each_arg; while(each_arg = next_arg()) if(!strcmp(key->get_val(), ((each_arg->get_source())->get_vertex_name())->get_val())) return((Adjacency *)each_arg); return((Adjacency *)NULL); } void Neighbors::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format) { } void Neighbors_wc::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format) { construct_ns->print(v_list, Cd, type, format ); } void Any_vertex_List::print(Vertex_List *v_list, Cd_graph *Cd, char type,int format) { Any_vertex_list_iterator next_arg(*this); Any_vertex_ each_arg; while(each_arg = next_arg()) { each_arg->print(v_list, Cd, type, format ,0); } } void Any_vertex::print(Vertex_List *v_list, Cd_graph *Cd, char type,int format,DemIdent *label) {} void Opt_labeled_term::print(Vertex_List *v_list, Cd_graph *Cd,char type,int format,DemIdent *label) { vertex->print(v_list, Cd, type, format,label); } void Term::print(Vertex_List *v_list, Cd_graph *Cd, char type,int format,DemIdent *label) { if ( actual_parameters ){ vertex->print(v_list, Cd, PAREN_A, format,label); actual_parameters->print(v_list, Cd, PAREN, ++format, label); strcat(line, ")" ); } else vertex->print(v_list, Cd, type, format,label); } void Labeled::print(Vertex_List *v_list, Cd_graph *Cd, char type,int format,DemIdent *label) { Opt_labeled_term::print(v_list, Cd, type, format,label_name); } void Normal::print(Vertex_List *v_list, Cd_graph *Cd, char type,int format,DemIdent *label) { Term::print(v_list, Cd, type, format,label); } void Regular::print(Vertex_List *v_list, Cd_graph *Cd, char type,int format,DemIdent * label) { Opt_labeled_term::print(v_list, Cd, type, format,label); } void Optional_term::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format,DemIdent *label) { opt->print(v_list, Cd, type, format,label ); } void Opt_labeled_term_Sandwich::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format,DemIdent*label) { //first->print(v_list, Cd, type, format,0 ); inner->print(v_list, Cd, OPTIONAL, format,0 ); //second->print(v_list, Cd, type, format ,0); } void Inherit_term::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format, DemIdent *label) { inherited->print(v_list, Cd, INHERIT, format,label ); } void Term_Comma_list::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format, DemIdent *label) { Term_list_iterator next_arg(*this); Term_ each_arg; while(each_arg = next_arg()) each_arg->print(v_list, Cd, type, format,0); } void Vertex_Comma_list::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format,DemIdent *label) { Vertex_list_iterator next_arg(*this); Vertex_ each_arg; while(each_arg = next_arg()) each_arg->print(v_list, Cd, type, format,0); } void Syntax_vertex_List::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format,DemIdent *label) { return; } void Syntax_vertex::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format,DemIdent *label) { return; } void Construct_ns::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format) { Neighbors_wc::print(v_list, Cd, type, format ); } void Alternat_ns::print(Vertex_List *v_list, Cd_graph *Cd, char type, int format) { alternat_ns->print(v_list, Cd, ALTERNAT, format); Neighbors_wc::print(v_list, Cd, type, format); } void Term_Bar_list::print(Vertex_List *v_list, Cd_graph *Cd,char type, int format) { Term_list_iterator next_arg(*this); Term_ each_arg; while(each_arg = next_arg()) each_arg->print(v_list, Cd, type, format,0); } void Repetit_n::print(Vertex_List *v_list, Cd_graph *Cd,char type, int format) { sandwiched->print(v_list, Cd, type, format); } void Kernel_Sandwich::print(Vertex_List *v_list, Cd_graph *Cd,char type, int format) { //first->print(v_list, Cd, type, format,0); inner->print(v_list, Cd, type, format); //second->print(v_list, Cd, type, format,0); } void Kernel::print(Vertex_List *v_list, Cd_graph *Cd,char type, int format) { if (nonempty) nonempty->print(v_list, Cd, type, format,0); repeated->print(v_list, Cd, REPETIT, format); } void Term_Sandwich::print(Vertex_List *v_list, Cd_graph *Cd,char type, int format) { //first->print(v_list, Cd, type, format,0); inner->print(v_list, Cd, type, format,0); //second->print(v_list, Cd, type, format,0); }