#include #include #define MAX 30 #define TRUE 1 #define FALSE 0 #define ACC 3 #define SHIFT 1 #define RED 2 struct grammar { int tot; //total no. of rules int rules[MAX][MAX];//[i][0]-no of symbols in rule i,[i][[i][0]+1]-whether added rule or not }; struct first_set { int tot; int sym[MAX]; // int tag_sym[MAX]; int set[MAX][MAX]; }; struct closure_set { int set_tot[MAX]; int prod[MAX][MAX]; int dot[MAX][MAX]; int tot; }; struct goto_struc { int tot; int set[MAX]; int sym[MAX]; int dest[MAX]; }; struct { int action[MAX][MAX]; int action_state[MAX][MAX]; int goto_state[MAX][MAX]; }table; struct fol_set { int tot; int sym[MAX]; int set[MAX][MAX]; }; struct goto_struc goto_set; struct closure_set closure; struct fol_set follow; struct first_set first; struct grammar gram; //input grammar int nt[MAX],ntcnt=0;//list of non terminals int t[MAX],tcnt=0; //list of terminals void getinp(); void find_terminals(); void first_calc(); void follow_calc(); void copy1(int,int); void set_const(); int find_closure(int); void find_goto(int,int); void table_const(); void main() { int ch; clrscr(); getinp(); /*augmenting the grammar*/ gram.rules[0][0]=2; gram.rules[0][1]=gram.rules[1][1]; gram.rules[0][2]=gram.rules[1][1]; first_calc(); follow_calc(); set_const(); table_const(); do { if(simulation()) printf("Accepted\n"); else printf("Rejected\n"); printf("Do you want to simulate more y/n? "); fflush(stdin); ch=getchar(); }while(ch=='y'); getch(); } void getinp() { int ch,ch2,i=1,j,ntflg=1; gram.tot=1; printf("Enter the grammar rules..\n"); gram.rules[0][0]=0; while((ch=getchar())!='x') { if(ntflg==1) { if(is_NT(ch)==-1) nt[ntcnt++]=ch; ntflg=0; } if(ch=='\n') { gram.rules[++gram.tot][0]=0; i=1; ntflg=1; } else if(ch=='-') { if((ch2=getchar())=='>'); else { ungetc(ch2,stdin); gram.rules[gram.tot][i++]=ch; gram.rules[gram.tot][0]++; } } else { gram.rules[gram.tot][i]=ch; i++; gram.rules[gram.tot][0]++; } } find_terminals(); /*for(i=1;i=0;i--) { pos=find_sym(gram.rules[i][1]); if((is_NT(gram.rules[i][2])==-1) && (gram.rules[i][2]!=-1)) { if(pos!=-1) { if(!search_first(pos,gram.rules[i][2])) { first.set[pos][0]++; first.set[pos][first.set[pos][0]]=gram.rules[i][2]; } } else { first.set[first.tot][0]=1; first.set[first.tot][1]=gram.rules[i][2]; first.sym[first.tot]=gram.rules[i][1]; first.tot++; } } else if(gram.rules[i][2]!=-1) { k=2; do { epflg=0; pos1=find_sym(gram.rules[i][k]); if(pos!=-1) { for(j=1;j<=first.set[pos1][0];j++) { if(first.set[pos1][j]==-1) epflg=1; if(!search_first(pos,first.set[pos1][j])) { first.set[pos][0]++; first.set[pos][first.set[pos][0]]=first.set[pos1][j]; } } } else if(pos1) { for(j=0;j<=first.set[pos1][0];j++) { if(first.set[pos1][j]==-1) epflg=1; first.set[first.tot][j]=first.set[pos1][j]; } first.sym[first.tot]=gram.rules[i][1]; first.tot++; } k++; }while(epflg==1); } } lp++; } /* printf("FIRST SET..\n"); for(i=0;i",gram.rules[p][1]); else printf("\t%c->",gram.rules[p][1]); for(k=2;k<=gram.rules[p][0];k++) { if(closure.dot[i][j]==(k-2)) printf(" . %c",gram.rules[p][k]); else printf("%c",gram.rules[p][k]); } if(closure.dot[i][j]==gram.rules[p][0]-1) printf("."); } } getch(); } int find_closure(int n) { int i,j,nxt,p,new_set; /*copying prodns from old closure to new closure*/ for(i=0;i=0) { pos=is_T(a); act=table.action[stack[top]][pos]; if(act==0) return 0; else if(act==1) { state=stack[top]; stack[++top]=table.action_state[state][pos]; } else if(act==2) { lhs=gram.rules[table.action_state[stack[top]][pos]][1]; top=top-gram.rules[table.action_state[stack[top]][pos]][0]+1; state=stack[top]; pos=is_NT(lhs); stack[++top]=table.goto_state[state][pos]; } else if(act==3) return 1; } return 0; }