#include <string.h> #include <stdio.h> #include <stdlib.h>int main () {char p[30][30]; Char q[30][30]; int line=0; int n; int i,j; int count=0; int k,t=0; int flag=0; int l,m=0; Char vn[30]={' "}; Char vt[30]={' "}; printf ("Number of rules:"); scanf ("%d", &n); Line=n; for (i=0;i<30;i++) for (j=0;j<30;j++) {p[i][j]= ' + '; q[i][j]= ' + '; } printf ("Please enter grammar: \ n"); for (i=0;i<line;i++) {scanf ("%s", P[i]); } l=0; m=0; for (i=0;i<line;i++) {for (j=0;j<30&& (p[i][j]!= '); j + +) { if (p[i][j]<= ' z ' &&p[i][j]>= ' a ' | | (p[i][j]<= ' 9 ' &&p[i][j]>= ' 0 ')) {flag=0; for (t=0; vn[t]!= ' + '; t++) {if (Vn[t]==p[i][j]) { Flag=1;break; }} if (flag==0) {vn[l]=p[i][j]; l++; }} if (p[i][j]<= ' Z ' &&p[i][j]>= ' A ') { flag=0; For (t=0;t<30&& (vt[t]!= '); t++) {if (Vt[t]==p[i][j]) {flag=1; Break }} if (flag==0) {vt[m]=p[i][j]; m++; }}}} count=0; k=0; for (i=0;i<line;i++) {for (j=4;j<30&& (p[i][j]!= '); j + +) {if (P I [j]<= ' z ' &&p[i][j]>= ' a ') | | (p[i][j]<= ' Z ' &&p[i][j]>= ' A ') | | (p[i][j]<= ' 9 ' &&p[i][j]>= ' 0 ')) {Q[COUNT][K]=P[I][J]; k++; } else {count++; k=0; }} count++; k=0; } flag=0; for (i=0;i<count;i++) {for (j=i+1;j<count;j++) {if (strcmp (Q[i],q[j]) ==0) {flag=1; Break }}} if (Flag==1) {printf ("non-deterministic with a poor state automaton, i.e. nfa\n\n"); printf ("Constructed with a poor state automaton: \ n"); printf ("NFA n= (k,e (meaning of sum), m,{s},{z}) \ n"); } else {printf ("is determined to have a poor state automaton, ie dfa\n\n\n"); printf ("Constructed with a poor state automaton: \ n"); printf ("DFA n= (k,e (meaning of sum), m,{s},{z}) \ n"); } printf ("Where, \nk={s"); For (i=0;i<30&& (vt!= ')) (i++) {printf (",%c", Vt[i]); } printf ("}\n"); printf ("e={"); For (i=0;i<30&& (vn[i]!= '); i++) {printf ("%c", Vn[i]); } printf ("}\n"); k=0; count=0; for (i=0;i<line;i++) {j=4; while (p[i][j]!= ') {if (k<4) {q[count][k]=p[i][k]; k++; } else {if ((p[i][j]<= ' z ' &&p[i][j]>= ' a ') | | (p[i][j]<= ' Z ' &&p[i][j]>= ' A ') | | (p[i][j]<= ' 9 ' &&p[i][j]>= ' 0 ')) {Q[COUNT][K]=P[I][J]; k++; j + +; } if (p[i][j]== ' l ') {count++; k=0; j + +; } } } count++; k=0; } printf ("\ n"); printf ("m:\n"); l=0; while (vn[l]!= ') {printf ("M (s,%c) ={", Vn[l]); for (i=0;i<30;i++) {for (j=4;j<30&& (q[i][j]!= '); j + +) { if (vn[l]==q[i][j]&& (q[i][j+1]== ') && (q[i][j-1]== ' = ')) printf ("%c", q[i][0 ]); }} printf ("}\t"); l++; } printf ("\ n"); l=0;k=0; while (vt[k]!= ') {l=0; while (vn[l]!= ') {printf ("M (%c,%c) ={", Vt[k],vn[l]); for (i=0;i<30;i++) {for (j=4;j<30&& (q[i][j]!= '); j + +) {if (vt[k]==q[i][j]&&vn[l]==q[i][j+1]) printf ("%c", q[i][0]); } } printf ("}\t"); l++; } k++; printf ("\ n"); } system ("Pause");}
1211 Construction and identification of a poor automaton