// cyk_main.cpp read in grammar // call chomsky to convert grammar // read string // call cyk to determine if accepted #include #include #include #include #include #include "cyk.h" using namespace std; int main(int argc, char * argv[]) { FILE * input_grammar; // file handle char line[100]; // input line from file int n; // input line length int i, j, k; // temporary variables char P[100][100]; // raw productions int np = 0; // number of productions char new_var = 'Z'; // count down for new variable // data for CYK char x[100]; Prod1 P1[100]; int np1 = 0; Prod2 P2[100]; int np2 = 0; if (argc < 2) // check command line { cout << "Provide the file name of a grammar" << endl; exit(1); } input_grammar = fopen(argv[1], "r"); if (input_grammar == NULL) // check if file opened { cout << "could not open " << argv[1] << endl; exit(1); } // clear P array for (i=0; i<100; i++) for (j=0; j<100; j++) P[i][j] = 0; while(1) // read file { i=0; j=0; fgets(line, 99, input_grammar); if (feof(input_grammar)) break; // exit while loop on eof cout << line; // no endl, has new line in it if (line[0] == '#') continue; // skip comments // find first variable n = strlen(line); n--; // ignore new line character while(line[i] == ' ') i++; // skip spaces if (i>=n) continue; P[np][j] = line[i]; i++; j++; // find -> while ((line[i] != '>') && (i<=n)) i++; // find '>' i++; if (i>=n) continue; while ((line[i] == ' ')&&(i<=n)) i++; // skip spaces // find rest of variables while (i1) np++; } cout << "Productions in P matrix" << endl; for (i=0; i1; j--) { if ((P[i][j]!=0)&&(P[i][j+1]!=0)) { P[np][0] = new_var; P[np][1] = P[i][j]; P[np][2] = P[i][j+1]; np++; P[i][j+1] = 0; P[i][j] = new_var; new_var--; if (new_var=='S') new_var--; } } } } cout << "Productions in P matrix after Chomsky" << endl; for (i=0; i> x ; cout << "string read |" << x << "|" << endl; n = strlen(x); if (n<2) break; // input finished cyk(x, n, P1, np1, P2, np2); } cout << "finished cyk_main" << endl; return 0; }