NFA FROM RE AIM:
To write a C program to convert the regular expression to NFA. ALGORITHM:
1. Star Startt the the prog progra ram. m. 2. Declare Declare all necessary necessary header header files files.. 3. Defi Define ne the the main main func functio tion. n. 4. Declare Declare the varia variables bles and initial initialize ize variable variabless r & c to ‘0’. 5. Use a for for loop within another another for loop to initialize the the matrix matrix for NFA NFA states. 6. Get a regula regularr expressio expression n from the the user & store store it in ‘m’. ‘m’. 7.
Obtain the length of the expression using strlen() function and store it in ‘n’.
8. Use for for loop upto upto the string string length length and follo follow w steps 8 to 12. 12. 9. Use switch switch case to check check each each character character of the the expressi expression on 10.If case is ‘*’, set the links as ‘E’ or suitable inputs as per rules. 11.If case is ‘+’, set the links as ‘E’ or suitable inputs as per rules. 12.Check the default default case, i.e.,for single single alphabet or 2 consecutive consecutive alphabets and set the links to respective alphabet. 13.End the switch case. 14.Use for loop to print the states along the matrix. 15.Use a for loop within another for lop and print the value of respective links. 16.Print the states start state as ‘0’ and final state. 17.End the program. program.
PROGRAM:
#include
#include void main() { char m[20],t[10][10]; int n,i,j,r=0,c=0; clrscr(); printf("\n\t\t\t\tSIMULATI printf("\n\t\t\t\tSIMULATION ON OF NFA"); printf("\n\t\t\t\t********* printf("\n\t\t\t\t*****************"); ********"); for(i=0;i<10;i++) { for(j=0;j<10;j++) { t[i][j]=' t[i][j]=' '; } } printf("\n\nEnter printf("\n\nEnter a regular expression:"); expression:"); scanf("%s",m); n=strlen(m); for(i=0;i
t[r][r+3]='E'; t[r+1][r+2]=m[i-1]; t[r+2][r+1]='E'; t[r+2][r+3]='E'; r=r+3; break; } case '+': { t[r][r+1]=m[i-1]; t[r+1][r]='E'; r=r+1; break; } default: { if(c==0) { if((isalpha(m[i]))&&(isalpha(m[i+1]))) { t[r][r+1]=m[i]; t[r+1][r+2]=m[i+1]; r=r+2; c=1; } c=1; } else if(c==1) { if(isalpha(m[i+1])) { t[r][r+1]=m[i+1]; r=r+1; c=2; } } else { if(isalpha(m[i+1])) {
t[r][r+1]=m[i+1]; r=r+1; c=3; } } } break; } } printf("\n"); printf("\n"); for(j=0;j<=r;j++) printf(" %d",j); printf("\n_______________ printf("\n______________________________ ____________________\n"); _____\n"); printf("\n"); printf("\n"); for(i=0;i<=r;i++) { for(j=0;j<=r;j++) { printf(" %c",t[i][j]); %c",t[i][j]); } printf(" | %d",i); printf("\n"); printf("\n"); } printf("\nStart printf("\nStart state: 0\nFinal state: state: %d",i-1); getch(); }
OUTPUT:
Enter a regular Expression: a|b
SIMULATION SIMULATION OF NFA ***************** Enter a regular expression:a|b 0 1 2 3 4 5 ________________________ ___________________________________ ___________ E
E
|0 |1 E
a b
E |5
|2 |3 |4
Start state: 0 Final state: 5
RESULT:
Thus the C program to convert regular expression to NFA has been executed and the output has been verified successfully.
SIMULATION OF DFA AIM:
To write a C program to convert the regular expression to DFA. ALGORITHM:
1. Star Startt the the prog progra ram. m. 2. A structur structuree is created created and the varia variables bles are declar declared. ed. 3. Defi Define ne the the main main func functio tion. n. 4. Declare Declare the the array array for for states states and and inpstr inpstr.. 5.
Define the readtt function to initialize the number of states ,number of inputs and transition from one state to other.
6. Get the the no. of state statess and no. of of inputs inputs from from the user. user. 7. Start Start the for loop loop and continue continue till it satisfie satisfiess the condition. condition. 8. Print Print the states states accor according ding to to the the condition condition.. 9. Generate Generate the the initial initial and and final final states states of the string string.. 10. Define
the dfa simul function to simulate it step 10-16.
11.Initialize the variable start to ‘0’ and begin for loop. 12.Check whether start is equal equal to ‘-1’, then print print “DFA “DFA Halted”. Halted”. 13.Get the string from the user and save in ‘inpstr’. 14.Read the string and until the end-of-string end-of-string reached do step 15. 15.Go from one state state to other other based on the transition entered entered and update update start by the final state. state. 16.Print “string is accepted” if a state is equal to nstates-1. 17.Else print “String is not accepted”. 18.Display the output. 19.End the program. program.
PROGRAM:
#include #include typedef struct { int input[2]; }state; void readtt(state *states,int nstates,int ninps) { int state,inp; printf("Enter printf("Enter the state transition:\n"); transition:\n"); for(state=0;state
void main() { state states[100]; int nstates,ninps; char inpstr[20]; clrscr(); printf("\n\t\t\t printf("\n\t\t\t SIMULATION SIMULATION OF DFA"); printf("\n\t\t\t printf("\n\t\t\t *****************"); *****************"); printf("\nEnter printf("\nEnter the no. of states: states: "); scanf("%d",&nstates); printf("Enter printf("Enter the no.of inputs: "); "); scanf("%d",&ninps); readtt(states,nstates,ninps); printf("\nInitial printf("\nInitial state: 0\nFinal state: state: %d",nstates-1);
printf("\n\nEnter printf("\n\nEnter the string: "); scanf("%s",inpstr); dfasimul(states,nstates,inpstr); getch(); }
OUTPUT :
SIMULATION OF DFA ***************** Enter the no. of states: 2 Enter the no.of inputs: 2 Enter the state transition: (state:0,0):0 (state:0,1):1 (state:1,0):0 (state:1,1):1 Initial state: 0 Final state: 1 Enter the string: 1001 String is accepted Enter the no. of states: 2 Enter the no.of inputs: 2 Enter the state transition: (state:0,0):0 (state:0,1):1 (state:1,0):0 (state:1,1):1 Initial state: 0 Final state: 1 Enter the string: 0110 String is not accepted
RESULT:
Thus the C program to convert the regular expression to DFA has been executed and the output has been verified successfully.
SHIFT REDUCE PARSING AIM:
To write a C program to perform the shift reduce parsing. ALGORITHM:
1. Star Startt the the prog progra ram. m. 2. Defi Define ne the the main main func functio tion. n. 3. Declare Declare array array for string string and stack and other other necessar necessary y variables. variables. 4. Get the the expressio expression n from the the user user and store store it as as string. string. 5. Appen Append d $ to the the end end of the the string string.. 6. Store Store $ into into the the stack stack.. 7. Print three three columns columns as ‘Stack’, ‘Stack’, ‘String’ ‘String’ and ‘Action’ for the respective actions. 8. Use for for loop from from i as 0 till string string length length and and check the the string. string. 9. If string string has has some operat operator or or id, id, push it it to the stack stack.. 10.Mark this action as ‘Shift’. ‘Shift’. 11.Print the stack, string and action values. 12.If stack contains some production on shifting, reduce it. 13.Mark this action as ‘Reduce’. 14.Print the stack, string and action values. 15.Repeat steps 9 to 14 again and again till the for loop is valid. 16.Now check the string and the stack. 17.If the string contains only $ and the stack has only $E within it, then print that the ‘given string is valid’. 18.Else print that the ‘given string is invalid’. 19.End the program. program.
PROGRAM:
#include #include #include void main() { char str[25],stk[25]; int i,j,t=0,l,r; i,j,t=0,l,r; clrscr(); printf("Enter printf("Enter the String : "); "); scanf("%s",&str); l=strlen(str); str[l]='$'; stk[t]='$'; printf("Stack\t\tString\t\tA printf("Stack\t\tString\t\tAction\n--------ction\n---------------------------------------------------\n --------\n "); for(i=0;i
for(j=0;j<=t;j++) printf("%c",stk[j]); printf("%c",stk[j]); printf("\t\t "); for(j=i+1;j<=l;j++) printf("%c",str[j]); printf("%c",str[j]); if(stk[t]=='+' if(stk[t]=='+' || stk[t]=='*') stk[t]=='*') printf("\t\tShift"); else printf("\t\tReduce"); printf("\n "); } while(t>1) { if(stk[t]=='E' && (stk[t-1]=='+' || stk[t-1]=='*') && stk[t-2]=='E') { t-=2; for(j=0;j<=t;j++) printf("%c",stk[j]); printf("\t\t"); printf(" %c",str[l]); printf("\t\tReduce\n "); } else t-=2; } if(t==1 && stk[t]!='+' && stk[t]!='*') printf("\nThe printf("\nThe Given String is Valid\n\n"); Valid\n\n"); else printf("\nThe printf("\nThe Given String is Invalid\n\n"); Invalid\n\n"); getch(); }
OUTPUT :
Enter the String : id+id*id Stack String ----------------------------------$id +id*id$ $E +id*id$ $E+ id*id$ $E+id *id$ $E+E *id$ $E+E* id$ $E+E*id $ $E+E*E $ $E+E $ $E $
Action Shift Reduce Shift Shift Reduce Shift Shift Reduce Reduce Reduce
The Given String is Valid
RESULT:
Thus the C program to perform the shift reduce parsing has been executed and the output has been verified successfully.
OPERATOR PRECEDENCE PARSING
AIM:
To write a C program program for implementing implementing operator precedence precedence parsing for given grammar.
ALGORITHM:
1. Star Startt the the prog progra ram. m. 2. Declare Declare all all necessary necessary header header files files and and variabl variables es 3. Assign Assign all the preced precedence ence relatio relation n for given given grammer grammer in a 2 d array array 4. Get Get input input from from user user in in an arra array y 5. Append Append $ symb symbol ol at the end end of of the input input 6. Create Create stack stack using array array and and put $ as first first element element 7. Display Display the the contents contents of of stack stack and input input array array 8.
If last element of stack and input is $ then the parsing is completed
9. Compare Compare top top of stack and and top of input input use find find function function to get get element element 10.
If the symbol is space print parser error
11.
If the symbol is < then shift top element of input to stack.
12.
Display the contents of stack and input array
13.
If the symbol is > then remove all elements from stack
14.
Display the contents of stack and input array
15.
End of the program.
PROGRAM:
#include #include int find(char a) { switch(a) { case 'a': return 0; case '+': return 1; case '*': return 2; case '$': return 3; } } void display(char a[],int top1,char b[],int top2) { int i; for(i=0;i<=top1;i++) printf("%c",a[i]); printf("%c",a[i]); printf("\t"); printf("\t"); for(i=top2;i','>','>','<','< ','>','>','>','<','<','<','>','< ','<','>','<','>','<','> ','>','<','>','<','<','< ','<','<','<',' ',' '}; char input[10]; char stack[10]={'$'}; int top_stack=0,top_input=0,i,j top_stack=0,top_input=0,i,j;; clrscr(); printf("operator printf("operator precedence parsing parsing for E->E+E/E*E/a\n"); E->E+E/E*E/a\n"); printf("enter printf("enter the string\n"); string\n"); scanf("%s",input); strcat(input,"$");
printf("stack\tinput\n"); printf("stack\tinput\n"); display(stack,top_stack,input,top_input); while(1) { if((stack[top_stack]=='$') if((stack[top_stack]=='$')&&(input[top &&(input[top_input]=='$') _input]=='$'))) { printf("string printf("string accepted"); break; } if(table[find(stack[top_stack if(table[find(stack[top_stack])][find(input[ ])][find(input[top_input])] top_input])]==' ==' ') { printf("parse printf("parse error"); getch(); exit(0); } if(table[find(stack[top_stack if(table[find(stack[top_stack])][find(input[ ])][find(input[top_input])] top_input])]=='<') =='<') { stack[++top_stack]='<'; stack[++top_stack]=input[ stack[++top_stack]=input[top_input]; top_input]; top_input++; display(stack,top_stack,input,top_input); continue; } if(table[find(stack[top_stack if(table[find(stack[top_stack])][find(input[ ])][find(input[top_input])] top_input])]=='>') =='>') { stack[++top_stack]='>'; display(stack,top_stack,input,top_input); top_stack-=3; display(stack,top_stack,input,top_input); } } getch(); }
OUTPUT :
Operator precedence parsing for E->E+E/E*E/a Enter the string a+a*a stack input $ a+a*a$ $ +a*a$ $ +a*a$ $<+ a*a$ $<+ *a$ $<+ *a$ $<+<* a$ $<+<* $ $<+<* $ $<+<*> $ $<+ $ $<+> $ $ $ string accepted
RESULT:
Thus the C program for implementing implementing operator precedence parsing has been executed and the output has been verified successfully.
SLR PARSING TABLE CONSTRUCTIO CONSTRUCTION N AIM:
To write a C++ program to generate a parsing table for SLR Parsing. ALGORITHM:
1. Star Startt the the prog progra ram. m. 2. Declare Declare all all necessary necessary header header files files and and variabl variables. es. 3.
construct SLR parsing table for all the states and fill the Action and Goto first.
4. Insert Insert the init initial ial state state as 0 in in the stack. stack. 5. Compare Compare with with the the first first input input symbol. symbol. 6. Action Action is taken taken accordin according g to general general parsi parsing ng table. table. 7. If actio action=Sh n=Shift, ift, goto goto step step 8 else step 9 8. push push the the symb symbol ol in in stack stack.. 9. If action= action= Reduce, Reduce, pop the symbol symbol from from the the stack.rep stack.replace lace by the terminal symbol, else goto step 10. 10.if action=goto, push the state number in the stack. 11.If action= accept, it implies the parsing is successful. 12.end the program.
non
PROGRAM:
#include #include #include #include #include void main() { clrscr(); g.getdfg(); int h[100],count=0,rt=0,found=0; set_of_item cc[20]; str t=putdot (g.p[0],2); cc[0] = closure(t.ele); closure(t.ele); int tab[20][20]; for(int i=0; i<20; i++) for(int j=0; j<20;j++) tab [i][j] =0; int m=0; count++; char a[20]; int ll=0; for (i=0; i
set_of_item t=go_to(cc[i],g.v[j]); t=go_to(cc[i],g.v[j]); if(t.c !=0) { for(int k =0; k
fpr(int ml=0; ml
} } found=1; break; } if(poped==tab[0][j]) break; } } else { cout<<”not cout<<”not accepted”; getch(); exit(0); } } cout<<”accepted”; getch(); }
OUTPUT :
IN.DAT 3 5 7 E T F + * ( ) I S=E E=E+T E=T T=T*F T= F F=(E) F=i
SLR-TABLE
0 1 2 3 4 5 6 7 8 9 10 11
E 1
8
T 2
2
F 3
+
*
S6 r2 r4
S7 r4
3
)
3 10
r6 S4
r2 r4
r6
S7 r3 r5
$
S5 r6 S5 S5 S11
S4 S6 r1 r3 r5
i S5
r2 r4 S4
r6 9
( S4
r1 r3 r5
r1 r3 r5
RESULT:
Thus the C++ program to generate a parsing table for SLR Parsing has been executed and the the output has been verified verified successfully. successfully.