/* $Header: C:\\UTILS\\CVS\\ROOT/programs\\lang\\thue/thue.c,v 1.4 2000/09/02 16:46:29 John Colagioia Exp $ * * Recent Changes: * * $Log: thue.c,v $ * Revision 1.4 2000/09/02 16:46:29 John Colagioia * Trivial bugfix. * * Revision 1.2 2000/02/29 21:51:24 John Colagioia * * Just added some basic header info in the comments. * * */ /* For want of a nail, the shoe was lost. For want of a shoe, the horse was lost. For want of a horse, the knight was lost. For want of a knight, the battle was lost. So it was a kingdom was lost, all for the want of a nail. -- George Herbert, Jacula Prudentum (Colloqual Adaptation) */ #include #include #include #include #include #include #define SEP "::=" char * getline (FILE * infile); struct rule { char lhs[64]; char rhs[64]; } rulebase[128]; int ruleidx = 0, debug = 0; char dataspace[16384], tempspace[16384]; int main (int argc, char *argv[]) { char *line, *c, *tmp, *target[64], tempstr[64]; int state, flagstate, i, j, k, order, temp, rnum[64]; FILE *infile; randomize (); target[0] = dataspace; memset (rulebase, 0, sizeof (rulebase)); memset (dataspace, 0, sizeof (dataspace)); if (argc > 1) infile = fopen (argv[1], "r"); else infile = stdin; if (infile == NULL) return (-1); order = 0; if (argc > 2) switch (argv[2][0]) { case 'r': /* Right-to-Left Processing */ order = 2; break; case 'l': /* Left-to-Right */ order = 1; break; case 'd': debug = 1; break; } /* Get input file */ state = 0; while (!feof (infile)) { line = getline (infile); if (state == 0) { if (line != NULL && !strlen (line)) continue; c = strstr (line, SEP); if (c == NULL) fprintf (stderr, "Malformed production: \"%s\"!\n", line); else if (c == line) state = 1; else { flagstate = 0; for (tmp=line;tmp!=c;tmp++) if (!isspace (*tmp)) flagstate = 1; if (flagstate) { *c = '\000'; c += strlen (SEP); strcpy (rulebase[ruleidx].lhs, line); strcpy (rulebase[ruleidx].rhs, c); ++ruleidx; } else state = 1; } } else if (line != NULL) strcat (dataspace, line); } if (debug) printf ("Initial: \"%s\"\n", dataspace); /* Apply rules */ state = 1; while (state) { /* Get all valid LHSs */ j = 1; k = 0; c = dataspace; for (i=0;i