Sie sind auf Seite 1von 48

Practical No.

:-1
AIM: Write the source code in C/C++/JAVA to convert the given Right Linear Grammar to Left Linear
Grammar form. Perform the following
1. Accept the grammar and print the terminal and non terminals of the light linear grammar
2. Convert the input grammar to its equivalent Left Linear form and pint the new grammar with new set
of productions.
3. Prepare the transition table for both the grammars.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char rhs[4][2]={"b","bU","cR","c"},lhs[4]={'R','S','U','U'},temp;
int i,j;
clrscr();
printf("Initial Productions are:-");
for (i=0;i<4;i++)
{
printf("%c--->%c%c \n",lhs[i],rhs[i][0],rhs[i][1]);
}
printf("Production after Grammar");
for(i=0;i<4;i++)
{
if(rhs[i][0]>=97 && rhs[i][0]<=122)
{
temp=rhs[i][0];
rhs[i][0]=rhs[i][1];
rhs[i][1]=temp;
}
}
for (i=0;i<4;i++)
{
if(rhs[i][0]==0)
printf("%c--->S%c \n",lhs[i],rhs[i][1]);
else
printf("%c--->%c%c\n",lhs[i],rhs[i][0],rhs[i][1]);
}
getch();
}

Output:

Practical No.:-2
AIM: Write a C/C++ /JAVA program to convert the given NDFA to DFA. Perform the following
1. Print the terminal and non terminals of the input NDFA.
2. Show the step by step conversion of to its equivalent DFA in the form of table.
3. Print the new states of the DFA obtained.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char init[3],term[3],final[3],terminal[3]={'a','*','b'},non_term[3]={'S','U','R'},temp,temp1,temp2;
int i,index,j;
clrscr();
for(i=0;i<3;i++)
{
printf("Enter the values for initial symbols\t");
scanf("%s",&init[i]);
if(init[i]==non_term[0]||init[i]==non_term[1]||init[i]==non_term[2])
{}
else
{
printf("Enter the values for initial symbols again\t");
scanf("%s",&init[i]);
}
printf("Enter the values for terminal symbols\t");
scanf("%s",&term[i]);
if(term[i]==terminal[0]||term[i]==terminal[1]||term[i]==terminal[2])
{}
else
{
printf("Enter the values for terminal symbols again\t");
scanf("%s",&term[i]);
}
printf("Enter the values for final symbols\t");
scanf("%s",&final[i]);
if(final[i]==non_term[0]||final[i]==non_term[1]||final[i]==non_term[2])
{}
else
{
printf("Enter the values for final symbols again\t");
printf("\n");
scanf("%s",&final[i]);
}
printf("\n");
}

for(i=0;i<3;i++)
{
if(term[i]=='*')
{
index=i;
temp=final[i];
}
}
for(i=0;i<3;i++)
{
if(init[i]==temp)
{
temp1=term[i];
temp2=final[i];
}
}
term[index]=temp1;
final[index]=temp2;
printf("Terminal Symbols after conversion \n");
for(j=0;j<3;j++)
{
if(term[j]==term[j+1])
{ }
else
printf("%c \n",term[j]);
}
printf("\n Non terminal Symbols after conversion \n");
for(i=0;i<3;i++)
{
printf("%c \n",init[i]);
}
getch();
}

Output:

Practical No:- 03
Aim: Write a C/C++ /JAVAprogram for construction of minimum automaton for the given automaton.
(Given as transition diagram or transition able). Perform the following
1. Accept DFA from user as production rule /transition table.
2. Print the transition table, terminal symbols, non-terminal symbols.
3. Show step by step minimization by using partition method and print each set of partitions
after each step.
#include <stdio.h>
#include <string.h>
int main()
{
int state[10], input1[10],
input2[10],finalstate,nonfinalstates[10],groups[10][10],indexgr1[10],indexgr2[10];
int i=0,count=0,j=0,k=0,m=0,n=0,gr1len=0,gr2len=0,len=0,flag=-99;
clrscr();
for(i=0;i<10;i++) //this just looks ugly now :/
{
indexgr1[i]=-99;
indexgr2[i]=-99;
state[i]=-99;
for(k=0;k<10;k++)
{
groups[i][k]=-99;
}
}
printf("Enter total number of states\n");
scanf("%d",&count);
for(i=0;i<count;i++)
{
printf("\n\nEnter State: %d\n",i+1);
scanf("%d",&state[i]);
printf("\n\nEnter input 1 for state number: %d\n",i+1);
scanf("%d",&input1[i]);
printf("\n\nEnter input 2 for state number: %d\n",i+1);
scanf("%d",&input2[i]);
}
printf("\nYou entered\n");
printf("Num\tState\tinput1\tinput2");
for(i=0;i<count;i++)
{
printf("\n");
printf("%d\t",i+1);
printf("%d",state[i]);
printf("\t");
printf("%d\t",input1[i]);
printf("%d\t",input2[i]);
}
printf("\nEnter the number for the final state from the above table\n");
scanf("%d",&finalstate);

printf("\nFinal state is:\n%d",finalstate);


printf("\nNon final states are\n");
for(i=0;i<count;i++)
{
if(state[i]!=finalstate)
{
nonfinalstates[j]=state[i];
printf("%d\n",nonfinalstates[j]);
j++;
}
}
count=0; //Counter will always have the row count from this point.
for(i=0;i<j;i++)//will give you the group containing the states whose neither input matches the
final state
{
if(input1[i]!=finalstate)
{
if(input2[i]!=finalstate)
{
groups[count][i]=state[i];
printf("Group1: %d %d\n", groups[count][i],i);
}
}
}
count++;
k=0;
for(i=0;i<j;i++)
{
if(input1[i]==finalstate||input2[i]==finalstate)//will give you the group containing the
state whose inputs match the final state :/
{
groups[count][i]=state[i];
printf("Group2: %d %d\n", groups[count][i],i);

}
}
count++;
k=0;
j=0;
for(i=0;i<10;i++)
{
if(groups[0][i]!=-99)
{
gr1len++;
indexgr1[k]=i;
printf("\nindexgr1:%d",indexgr1[k]);
k++;
}
if(groups[1][i]!=-99)
{

gr2len++;
indexgr2[j]=i;
printf("\nindexgr2:%d",indexgr2[j]);
j++;
}
}
printf("\nlen1: %d len2: %d rowcount: %d\n",gr1len,gr2len,count);
k=0;
if(gr1len>1)//check if there's more than one elements, so that their inputs can be compared
{
for(i=0;i<gr1len;i++)
{
if(input1[indexgr1[i]]&&input1[indexgr1[i+1]]!=-99)
{
if(input1[indexgr1[i]]==input1[indexgr1[i+1]])
{
printf("\n\ninput1[%d]=%d\t==\tinput1[%d]=%d",i,input1[indexgr1[i]],i+1,input1[indexgr1[i+1]]);
j=0; flag=-99;
while(j<gr1len)
{
if(i!=j)
{
printf("\n\ninner loop
input2[%d]=%d\t==\tinput2[%d]=%d",i,input2[indexgr1[i]],j,input2[indexgr1[j]]);
if(input2[indexgr1[i]]&&input2[indexgr1[j]]!=99)
{
if(input2[indexgr1[i]]==input2[indexgr1[j]])
{
groups[count][i]=state[indexgr1[i]];
// printf("\nAdded in if:
%d",state[i]);
groups[count][j]=state[indexgr1[j]];
//

printf("\nAdded in if:

%d",state[j]);
count++;
flag=1; //indicates that input2
match at I & J
}
else
{
m=i;//where m holds the
position of the element whose input 2 doesn't match, but input 1 does.
flag=0;
}
}
}

j++;
}
if(flag==0) //add element whose input one matches but input 2
doesn't
{
//

groups[count][indexgr1[m]]=state[indexgr1[m]];
printf("\nAdded in Else part:

%d",groups[count][indexgr1[m]]);
count++;
}
}
}
}
}
else //if there's just one element in a row add it
{
groups[count][indexgr1[0]]=state[indexgr1[0]];
count++;
}
//Rinse and repeat for the next row
k=0;
if(gr2len>1)//check if there's more than one elements, so that their inputs can be compared
{
for(i=0;i<gr2len;i++)
{
if(input1[indexgr2[i]]&&input1[indexgr2[i+1]]!=-99)
{
if(input1[indexgr2[i]]==input1[indexgr2[i+1]])
{
printf("\n\ninput1[%d]=%d\t==\tinput1[%d]=%d",i,input1[indexgr2[i]],i+1,input1[indexgr2[i+1]]
);
j=0; flag=-99;
while(j<gr2len)
{
if(i!=j)
{
printf("\n\ninner loop
input2[%d]=%d\t==\tinput2[%d]=%d",i,input2[indexgr2[i]],j,input2[indexgr2[j]]);
if(input2[indexgr2[i]]&&input2[indexgr2[j]]!=99)
{
if(input2[indexgr2[i]]==input2[indexgr2[j]])
{
groups[count][i]=state[indexgr2[i]];
// printf("\nAdded in if:
%d",state[i]);
groups[count][j]=state[indexgr2[j]];

//

printf("\nAdded in if:

%d",state[j]);
count++;
flag=1; //indicates that input2
match at I & J
}
else
{
m=i;//where m holds the
position of the element whose input 2 doesn't match, but input 1 does.
flag=0;
}
}
}
j++;
}
if(flag==0) //add element whose input one matches but input 2
doesn't
{
//

groups[count][indexgr2[m]]=state[indexgr2[m]];
printf("\nAdded in Else part:

%d",groups[count][indexgr2[m]]);
count++;
}
}
}
}
}
else //if there's just one element in a row add it
{
groups[count][indexgr2[0]]=state[indexgr2[0]];
count++;
}
printf("\nStates are:\n");
for(i=0;i<10;i++)//Print states obtained upto this point
{
for(j=0;j<10;j++)
{
if(state[j]==finalstate)
{
groups[count][j]=finalstate; //Add final state to the last row
}
if(groups[i][j]!=-99)
{
printf("%d\t",groups[i][j]);
}
}
printf("\n");
}
count++;
printf("\nState\tinput1\tinput2");//print minimized states and their respective inputs

for(i=2;i<=count;i++)
{
len=0;
for(j=0;j<10;j++)
{
if(groups[i][j]!=-99)
{
len++; //Find out how many elements there are in a row in the groups
array.
}
}
if(len>1)//if there are more than one states in the groups table choose the 0'th element as
the representative and print it and it's inputs, minimizing(omitting) the rest
{
for(m=1;m<10;m++)//format input2 with the 0'th element which is chosen as a
representative of other elements in the group
{
for(n=0;n<10;n++)
{
if(input2[n]==groups[i][m]&&groups[i][m]!=-99)
{
//
printf("\ninput2[%d]: %d\tgroups[%d][%d]:
%d",n,input2[n],i,m, groups[i][m]);
input2[n]=groups[i][0];
}
}
}
for(j=0;j<10;j++)
{
if(groups[i][j]!=-99)
{
printf("\n");
printf("%d",groups[i][0]);//0'th element chosen as representative
printf("\t");
printf("%d\t",input1[j]);
printf("%d\t",input2[j]);
break;
}
}
}
else //if there's just one element in the row, print it and it's inputs
{
for(j=0;j<10;j++)
{
if(groups[i][j]!=-99)
{
printf("\n");
printf("%d",groups[i][j]);
printf("\t");

printf("%d\t",input1[j]);
printf("%d\t",input2[j]);
break;
}
}
}
}
return 0;
}

Output:

Practical No:- 04
Aim: Write a C/C++/JAVA code to implement the Warshalls Algorithm on the following (the Boolean
matrix is given) matrix B and perform the following in the sequence.
1. Accept the Boolean matrix from the user and store it it in a suitable data structure.
2. Print the input matrix on the screen.
3. Compute B+ the transitive closure (warshall method) on the matrix and print th result on the
screen.
#include <stdio.h>
#include <string.h>
int rows=0,cols=0;
void printMat(int x[10][10]);
void funcWarshall(int y[10][10]);
int main()
{
int i=0,j=0,n;
printf("Enter the value of 'n' for (n x n) matrix you would like to create\n");
scanf("%d",&n);
cols=rows=n;
int matrix_one[10][10];
printf("\nEnter data into your matrix:\n");
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
scanf("\t%d",&matrix_one[i][j]);
}
printf("\n");
}
printf("You Entered: \n");
printMat(matrix_one);
funcWarshall(matrix_one);
printf("\nApplying warshall's algorith we get,\n");
printMat(matrix_one);
return 0;
}
void printMat(int mat[10][10])
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
}
void funcWarshall(int mat[10][10])

{
int i,j,k;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
for(k=0;k<rows;k++)
{
mat[i][j]=mat[i][j]||mat[i][k]&&mat[k][j];
}
}
}
}

Output:

Practical No:- 05
Aim: Write a C/C++/JAVA code to illustrate the generation on SPM for the input grammar. Perform the
following in the sequence.
1. Input the grammar from the user. Print the Terminals and Non-Terminals and Start state.
2. Obtain and print FIRST, FIRST+, LAST and LAST+ matrices and print them on the screen.
3. Compute FIRST* and LAST* and Print them.
4. Calculate () , () and () matrices using suitable formula. Writ the formula separately.
5. Superimpose () , () and () matrices obtain SPM. (Find if It is SPG?)
#include <stdio.h>
#include <string.h>
int indexing(char x);
void printmat(int p[7][7]);
char symbol[7]={'Z','M','L','a','b','(',')'};
int main()
{
char lhs[4]={'Z','M','M','L'}, rhs[4][5]={"bMb\0","a\0","(L\0","Ma)\0"};
int first[7][7],something[7][7],equalto[7][7],last[7][7], firstplus[7][7], lastt[7][7],less[7][7],
greater[7][7],identity[7][7],xyz[7][7],spm[7][7];
int len,i,j,k,b,a,count;
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
equalto[i][j]=0;
}
}
for(count=0;count<=3;count++)
{
j=0;
while(rhs[count][j]!='\0')
{
a=indexing(rhs[count][j]);
j++;
if(j<3)
{
if(rhs[count][j]!='\0')
{
b=indexing(rhs[count][j]);
}
else
{
break;
}
equalto[a][b]=1;
}

}
}

len=strlen(symbol);
for(count=0;count<7;count++)
{
printf("\t%c",symbol[count]);
}
printf("\n");
for(count=0;count<7;count++)
{
printf("%c",symbol[count]);
for(j=0;j<7;j++)
{
if(equalto[count][j]!=1)
{
printf("\t0");
}
else
{
printf("\t%d",equalto[count][j]);
}
}
printf("\n");
}
// printf("\nFirst Matrix\n");
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
first[i][j]=0;
}
}
for(count=0;count<=3;count++)
{
a=indexing(lhs[count]);
b=indexing(rhs[count][0]);
first[a][b]=1;
}
for(count=0;count<7;count++)
{
printf("\t%c",symbol[count]);
}
printf("\n");

for(count=0;count<7;count++)
{
printf("%c",symbol[count]);
for(j=0;j<7;j++)
{
printf("\t%d",first[count][j]);
}
printf("\n");
}
// printf("\nLast Matrix\n");
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
last[i][j]=0;
}
}
len=0;
for(count=0;count<=3;count++)
{
len=0;
for(i=0;i<=2;i++)
{
if(rhs[count][i]!='\0')
{
len++;
}
}
a=indexing(lhs[count]);
printf("%d",len);
b=indexing(rhs[count][len-1]);
last[a][b]=1;
}
for(count=0;count<7;count++)
{
printf("\t%c",symbol[count]);
}
printf("\n");

for(count=0;count<7;count++)
{

printf("%c",symbol[count]);
for(j=0;j<7;j++)
{
printf("\t%d",last[count][j]);
}
printf("\n");
}
// printf("\n First+ Matrix: \n");
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
firstplus[i][j]=0;
}
}
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
for(k=0;k<7;k++)
{
first[i][j]=first[i][j]||first[i][k]&&first[k][j];
}
}
}
// printf("\n Last+ Matrix: \n");
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
firstplus[i][j]=0;
}
}
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
for(k=0;k<7;k++)
{
last[i][j]=last[i][j]||last[i][k]&&last[k][j];
}
}
}
// printf("\n Less than\n");
for(i=0;i<7;i++)
{

for(j=0;j<7;j++)
{
less[i][j]=0;
}
}
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
for(k=0;k<7;k++)
{
less[i][j]+=equalto[i][k]*first[k][j];
}
}
}
// printf("Greater than matrix\n");
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
greater[i][j]=0;
lastt[i][j]=last[j][i];
identity[i][j]=0;
xyz[i][j]=0;
something[i][j]=0;
spm[i][j]=0;
}
}
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if(i==j)
{
identity[i][j]=1;
}
}
}
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
for(k=0;k<7;k++)
{
xyz[i][j]+=lastt[i][k]*equalto[k][j];
}
something[i][j]=identity[i][j]+first[i][j];
}

}
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
for(k=0;k<7;k++)
{
greater[i][j]=greater[i][j]||xyz[i][k]&&something[k][j];
}
}
}
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if(equalto[i][j]==1)
{
spm[i][j]=1;
}
else if(less[i][j]==1)
{
spm[i][j]=2;
}
else if(greater[i][j]==1)
{
spm[i][j]=3;
}
}
}
printf("\nSPM\n");
printmat(spm);
printf("\n1 denotes equal to entries\n2 denotes less than entries \n3 denotes greater than entries");
return 0;

void printmat(int n[7][7])


{
int count, j;
for(count=0;count<7;count++)
{
printf("\t%c",symbol[count]);
}
printf("\n");

for(count=0;count<7;count++)

{
printf("%c",symbol[count]);
for(j=0;j<7;j++)
{
printf("\t%d",n[count][j]);
}
printf("\n");
}
}
int indexing(char c)
{
int z;
// printf("c=%c",c);
for(z=0;z<7;z++)
{
if(symbol[z]==c)
{
// printf("symb[z]=%c z:%d\t\N",symbol[z],z);
return z;
}
}
return 99;
}

Output:

Practical No.:6
AIM:- Write a C/C++/JAVA code to demonstrate the step by step parsing of input string to a SPG
using
already provided SPM. Perform the following.
1. Input and store the SPM in a suitable data structure and print it on the screen.
2. Print the terminals and non-terminals of the grammar.
3. Input string from the user and show the step by step reduction of the string. (show handle at
each iteration)
4. Print the acceptance or rejection of the string.
#include<iostream.h>
#include<conio.h>
#include <stdio.h>
#include<string.h>
int fi[7]={1,7,8,6,3,8};
int gi[7]={1,4,2,7,7,5,9};
char symbols[7]={'Z','M','L','a','b','(',')'},v;
char lhs[4]={'Z','M','M','L'};
char rhs[4][4]={"bMb\0","a\0","(L\0","Ma)\0"};
char cmp[10];
char result[10];
char newstring[10][10];
int row=0;
char lhsc,str1,str2;
char str[10][10],temp1,temp2,temp3;
void replace(int i,char lhs);
void main()
{
int indexingfi(char);
int indexinggi(char);
int i,j,a,b,len,index,r,cnt=0,k=0;
printf("Enter String to be parsed\n");
scanf("%s",str[0]);
len=strlen(str[0]);
str[0][0]='#';
str[0][len+1]='#';
for(i=0;i<len;i++)
{
str[0][i+1]=str[row][i];
}
i=0;
do
{
str1=str[row][i];
str2=str[row][i+1];
a=indexingfi(str1);

b=indexinggi(str2);
if(a<b)
{
cmp[cnt]='l';
cnt++;
}
if(a>b)
{
cmp[cnt]='g';
cnt++;
if(cmp[cnt-1]=='l')
{
for(j=0;j<4;j++)
{
v=str[row][i];
r=strcmpp(v,rhs[j]);
if(r==0)
{
str[row][i]=lhs[j];
break;
}
}

}
if(cmp[cnt-1]=='e')
{
for(j=0;j<4;j++)
{
r=strcmpp(result[i],rhs[j]);
if(r==0)
{
lhsc=lhs[j];
replace(index,lhsc);
}
}
}
}
if(a==b)
{
cmp[cnt]='e';
cnt++;
if(cmp[cnt-1]=='e')
{
result[k]=str[row][i+1];
k++;
}
else
{
index=i;

result[k]=str[row][i];
result[k+1]=str[row][i+1];
k++;
}
}
i++;
}
while((strlen(str[row]))!=1);
}
void replace(int i,char lhs)
{
int n;
int l;
row++;
for(l=0;l<i;l++)
{
str[row][i]=str[row-1][l];
}
str[row][l]=lhs;
n=strlen(result);
for(l=l+n;l<strlen(str[row]);l++)
{
str[row][l]=str[row-1][l];
}
printf("%s",str[row]);
}

int indexingfi(char c)
{
int i,z;
for(z=0,i=0;z<8,i<10;z++,i++)
{
if(symbols[z]==c)
{
return fi[z];
}
}
return 0;
}
int indexinggi(char b)
{
int z,i;
for(z=0,i=0;z<8,i<10;z++,i++)

{
if(symbols[z]==b)
{
return gi[z];
}
}
return 0;
}

OUTPUT:

Practical No.:7
AIM: Write a C/C++/JAVA code to illustrate the generation on OPM for the input operator grammar.
Perform the following in the sequence.
1. Input the grammar from the user. Print the Terminals (operators) and Non-Terminals and
Start state.
2. Obtain and print FIRST, FIRST+, LAST and LAST+ matrices and print them on the screen.
3. Compute FIRST* and LAST* and Print them.
4. Calculate (=) , (<) and (>) matrices using suitable formula. Writ the formula separately.
5. Superimpose (=), (<) and (>) matrices obtain OPM. (Find if it is OPG?)
#include <stdio.h>
#include <string.h>
char symbol[8]={'E','T','F','i','+','*','(',')'};
int indexing(char x);
void printmat(int p[8][8]);
void transpose(int n[8][8], int z[8][8]);
void assign(int a[8][8]);
int main()
{
char lhs[6]={'E,','E','T','T','F','F'};
char rhs[6][5]={{"E+T\0"},{"T\0"},{"T*F\0"},{"F\0"},{"(E)\0"},{"i\0"}};
int equalto[8][8], first[8][8], last[8][8], identity[8][8], first_star[8][8], first_term[8][8],temp[8][8],
temp1[8][8], last_star[8][8], last_term[8][8], temp2[8][8], temp3[8][8], temp4[8][8];
int i,j,k,a,b,count,len,temporary;
char opm[8][8];
assign(equalto);
assign(first);
assign(last);
assign(identity);
assign(temp2);
assign(temp3);
assign(first_star);
assign(first_term);
assign(temp);
assign(temp1);
assign(last_star);
assign(last_term);
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
opm[i][j]='0';
if(i==j)
{
identity[i][j]=1;
}
}
}
for(count=0;count<=5;count++)

{
j=0;
while(rhs[count][j]!='\0')
{
a=indexing(rhs[count][j]);
j++;
if(j<3)
{
if(rhs[count][j]!='\0')
{
b=indexing(rhs[count][j]);
}
else
{
break;
}
equalto[a][b]=1;
}
}
}
printf("\nThe Equalto matrix is:\n\n");
printmat(equalto);
printf("\nThe First matrix is:\n\n");
for(count=0;count<=5;count++)
{
len=strlen(rhs[count]);
a=indexing(lhs[count]);
b=indexing(rhs[count][0]);
first[a][b]=1;
}
printmat(first);
printf("\nThe first+ matrix is: \n\n");
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
for(k=0;k<8;k++)
{
first[i][j]=first[i][j]||first[i][k]&&first[k][j];
}
}
}
printmat(first);
printf("\nThe First* matrix is: \n\n");
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{

first_star[i][j]=identity[i][j]||first[i][j];
}
}
printmat(first_star);
printf("\nThe First Term matrix is: \n\n");
for(count=0;count<=5;count++)
{
for(i=0;i<=4;i++)
{
if(rhs[count][i]=='i'||rhs[count][i]=='+'||rhs[count][i]=='*'||rhs[count][i]=='('||rhs[count][i]==')')
{
a=indexing(lhs[count]);
b=indexing(rhs[count][i]);
first_term[a][b]=1;
break;
}
}
}
printmat(first_term);
printf("\nThe (First*).(First Term) matrix is: \n\n");
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
for(k=0;k<8;k++)
{
temp[i][j]=temp[i][j]||first_star[i][k]&&first_term[k][j];
}
}
}
printmat(temp);
printf("\nThe (equalto).(First*).(First Term) matrix is: \n\n");
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
for(k=0;k<8;k++)
{
temp1[i][j]=temp1[i][j]||equalto[i][k]&&temp[k][j];
}
}
}
printmat(temp1);
printf("\nThe last matrix is: \n\n");
for(count=0;count<=5;count++)
{
len=strlen(rhs[count]);
a=indexing(lhs[count]);
b=indexing(rhs[count][len-1]);
last[a][b]=1;

}
printmat(last);
printf("\nThe Last+ matrix is: \n\n");
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
for(k=0;k<8;k++)
{
last[i][j]=last[i][j]||last[i][k]&&last[k][j];
}
}
}
printmat(last);
printf("\nThe Last* matrix is: \n\n");
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
last_star[i][j]=identity[i][j]||last[i][j];
}
}
printmat(last_star);
printf("\nThe Last Term matrix is: \n\n");
for(count=0;count<=5;count++)
{
for(i=0;i<strlen(rhs[count]);i++)
{
if(rhs[count][i]=='i'||rhs[count][i]=='+'||rhs[count][i]=='*'||rhs[count][i]=='('||rhs[count][i]==')')
{
temporary=i;
a=indexing(lhs[count]);
b=indexing(rhs[count][temporary]);
printf("\n%d %d",a,b);
}
}
last_term[a][b]=1;
}
printmat(last_term);
printf("\nThe (Last*).(Last Term) matrix is: \n\n");
assign(temp2);
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
for(k=0;k<8;k++)
{
temp2[i][j]=temp2[i][j]||last_star[i][k]&&last_term[k][j];
}

}
}
printmat(temp2);
printf("\nThe {(Last*).(Last Term)}.(equalto) matrix is: \n\n");
assign(temp4);
transpose(temp2,temp4);
assign(temp2);
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
for(k=0;k<8;k++)
{
temp2[i][j]=temp2[i][j]||temp4[i][k]&&equalto[k][j];
}
}
}
printmat(temp1);
assign(temp3);
printf("\n\nOPM matrix is:\n\n");
for(i=0;i<=7;i++)
{
for(j=0;j<=7;j++)
{
if(equalto[i][j]==1)
{
opm[i][j]='=';
}
else if(temp1[i][j]==1)
{
opm[i][j]='<';
}
else if(temp2[i][j]==1)
{
opm[i][j]='>';
}
}
}
for(count=0;count<8;count++)
{
printf("\t%c",symbol[count]);
}
printf("\n");
for(count=0;count<8;count++)
{
printf("%c",symbol[count]);

for(j=0;j<8;j++)
{
printf("\t%c",opm[count][j]);
}
printf("\n");
}
return 0;
}
void assign(int p[8][8])
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
p[i][j]=0;
}
}
}
void printmat(int n[8][8])
{
int count, j;
for(count=0;count<8;count++)
{
printf("\t%c",symbol[count]);
}
printf("\n");
for(count=0;count<8;count++)
{
printf("%c",symbol[count]);
for(j=0;j<8;j++)
{
printf("\t%d",n[count][j]);
}
printf("\n");
}
}
int indexing(char c)
{
int z;
for(z=0;z<8;z++)
{
if(symbol[z]==c)
{

return z;
}
}
return 0;
}
void transpose(int p[8][8], int q[8][8])
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
q[i][j]=p[j][i];
}
}
}
OUTPUT:

Practical No.:-8
AIM: Write a C/C++/JAVA code to demonstrate the step by step parsing of input string to an OPG
using already provided OPM. Perform the following.
1. Input and store the OPM in a suitable data structure and print it on the screen.
2. Print the operators and non-terminals of the grammar.
3. Input string from the user and show the step by step reduction of the string. (show prime phrase at each
iteration)
4. Print the acceptance or rejection of the string.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int
index[5],opm[8][8]={{0,0,0,1,0,0,1,0},{0,0,0,0,1,0,0,0},{0,0,0,0,0,0,0,0},{0,1,0,3,2,2,3,2},{0,0,1,3,3,2,3,
2},{1,0,0,2,2,2,0,2},{0,0,0,3,3,0,3,0},{0,0,0,3,3,0,3,0}};
char lhs[6]={'E','E','T','T','F','F'},prod[][6]={{"E+T"},{"T"},{"T*F"},{"F"},{"(E)"},{"i"}};
char symbs[15]={'E','T','F','+','*','(',')','i'},parsing[1][20],hand[10],handvar[3];
int flag=0,i,ind=0,a,b,j,k=1,cnt=0;
void main()
{
int g,pos=0;
//void temparr();
int func(char c);
void greater(int pos);
clrscr();
printf("OPM parsing");
printf("Enter string to parse:");
scanf("%s",&parsing[0]);
printf("%s",parsing[0]);
printf("\nstep by step:");
//temparr();
for(i=0;strlen(parsing[i])!=1;i++)
{
flag=0,k=1,pos=0;
for(g=0;hand[g]!='\0';g++)
{
hand[g]='\0';
}
hand[0]=parsing[i][0];
for(j=0;flag!=1;j++)

{
printf("i=%d j=%d",i,j);
printf("\tparsingij=%c",parsing[i][j]);
if(parsing[i][j]!='\0')
a=func(parsing[i][j]);
printf("\n a=%d",a);
if(parsing[i][j+1]!='\0')
b=func(parsing[i][j+1]);
printf("\t b=%d",b);
if(parsing[i][j+1]=='\0')
greater(pos);
if(opm[a][b]==1||opm[a][b]==0)
{
if(hand[0]=='\0')
{
hand[0]=parsing[i][j+1];
pos=j+1;
}
else
{
hand[k]=parsing[i][j+1];
//F
k++;
}
}
if(opm[a][b]==0)
{
cnt++;
if(cnt==1)
{
index[ind]=parsing[i][j+1]; //F
}
if(cnt==2)
{
greater(ind);
}
}
if(opm[a][b]==3)
{
greater(pos);
}
if(opm[a][b]==2)
{
hand[0]=parsing[i][j+1];
pos=j+1;

}
}//for
}
getch();
}
void greater(int pos)
{
int x,y,z,w,f=0;
void step(int z,int pos);
for(x=0;x<=5;x++)
{
printf("hand=%c",hand);
w=strcmp(prod[x],hand);
if(w==0)
{
z=x;
f=1;
}
}
if(f!=1)
{
clrscr();
printf("\nEntered string is not parsable");
getch();
exit(0);
}
flag=1;
step(z,pos);
printf("\n call to %d and %d",z,pos);
}
void step(int z,int pos)
{
int k1,x,y;
for(x=0;x<pos;x++)
{
printf("i=%d",i);
printf("i+1=%d",i+1);
parsing[i+1][x]=parsing[i][x];
printf("Later: i+1=%d",i+1);
}
printf("\tlhs=%c",lhs[z]);
parsing[i+1][pos]=lhs[z];

k1=strlen(hand) + (x);
for(y=pos+1;parsing[i][k1]!='\0';y++)
{
printf("\ni=%d",i);
printf("\ti+1=%d",i+1);
parsing[i+1][y]=parsing[i][k1];
printf("Later: i+1=%d",i+1);
k1++;
}
if(parsing[i+1][0]!='\0')
{
printf("\nStep is %d: %s",i+1,parsing[i+1]);
}
}
int func(char c)
{
int i,j,len;
len=strlen(symbs);
for(i=0;i<len;i++)
{
if(c==symbs[i])
{
j=i;
break;
}
}
return j;
}

Output:

Practical No.:9
AIM: Write a C/C++/JAVA code to construct the Simple Precedence Function for the given SPM to
demonstrate the linearization of SPM. Perform ht following
1. Input and store grammar and SPM. Print them on the screen.
2. Compute functions f and g using the suitable technique and print he precedence function on
the screen.
#include <stdio.h>
#include <string.h>
void printmat(int p[7][7]);
char symbol[7]={'Z','M','L','a','b','(',')'};
int main()
{
int
ge[7][7]={{0,0,0,0,0,0,0},{0,0,0,1,1,0,0},{0,0,0,1,1,0,0},{0,0,0,1,1,0,1},{0,1,0,0,0,0,0},{0,0,1,0,0,0,0},{
0,0,0,1,1,0,0}};
int
le[7][7]={{0,0,0,0,0,0,0},{0,0,0,1,1,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,1},{0,1,0,1,0,1,0},{0,1,1,1,0,1,0},{0
,0,0,0,0,0,0}};
int b[14][14],b_plus[14][14],b_star[14][14], temp1[14][14],
quad1[7][7],quad2[7][7],quad3[7][7],quad4[7][7],
plus_quad1[7][7],plus_quad2[7][7],plus_quad3[7][7],plus_quad4[7][7], identity[14][14];
int trans[7][7];
int i,j,k,m,n,count=0;
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
trans[i][j]=0;
quad1[i][j]=0;
quad2[i][j]=0;
quad3[i][j]=0;
quad4[i][j]=0;
plus_quad1[i][j]=0;
plus_quad2[i][j]=0;
plus_quad3[i][j]=0;
plus_quad4[i][j]=0;
}
}
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
trans[i][j]=le[j][i];
}
}
//Printing greater than matrix

printf("\nThe greater than matrix is: \n\n");


printmat(ge);
//printing less than matrix
printf("\nThe Less than matrix is: \n\n");
printmat(le);
//printing the transpose of less than matrix
printf("\nThe transpose matrix of Less than matrix is: \n\n");
printmat(trans);
for(i=0;i<14;i++)
{
for(j=0;j<14;j++)
{
b[i][j]=0;
b_plus[i][j]=0;
b_star[i][j]=0;
temp1[i][j]=0;
identity[i][j]=0;
}
}
for(i=7,m=0;i<14,m<7;i++,m++)
{
for(j=0,n=0;j<7,n<7;j++,n++)
{
b[i][j]=trans[m][n];
quad3[m][n]=trans[m][n];
}
}
for(i=0,m=0;i<7,m<7;i++,m++)
{
for(j=7,n=0;j<14,n<7;j++,n++)
{
b[i][j]=ge[m][n];
quad2[m][n]=ge[m][n];
}
}
printf("\nB matrix is\n\n");
for(i=0;i<14;i++)
{
for(j=0;j<14;j++)
{
printf(" %d",b[i][j]);
}
printf("\n");
}
//applying warshall on quadrants two and three of mat b to get first quadrant of b_plus
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
for(k=0;k<7;k++)
{

plus_quad1[i][j]=plus_quad1[i][j]||quad2[i][k]&&quad3[k][j];
}
}
}
//applying warshall on quadrant 3 of B and quadrant 1 of B+ to get third quadrand of b+
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
for(k=0;k<7;k++)
{
plus_quad3[i][j]=plus_quad3[i][j]||quad3[i][k]&&plus_quad1[k][j];
}
}
}
//applying warshall on quadrant 3 of b+ and quadrand 2 of b to obtain quadrant 4 of b+
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
for(k=0;k<7;k++)
{
plus_quad4[i][j]=plus_quad4[i][j]||plus_quad3[i][k]&&quad2[k][j];
}
}
}
//applying warshall on quadrants 1 and 4 of b+ to obtain quadrant 2 of b+
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
for(k=0;k<7;k++)
{
plus_quad2[i][j]=plus_quad2[i][j]||plus_quad1[i][k]&&plus_quad4[k][j];
}
}
}
//combining all quadrands to form b+ matrix
//first quadrant.
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
b_plus[i][j]=plus_quad1[i][j];
}
}
//second quadrant.
for(i=0,m=0;i<7,m<7;i++,m++)
{
for(j=7,n=0;j<14,n<7;j++,n++)
{

b_plus[i][j]=plus_quad2[m][n];
}
}
//third quadrant.
for(i=7,m=0;i<14,m<7;i++,m++)
{
for(j=0,n=0;j<7,n<7;j++,n++)
{
b_plus[i][j]=plus_quad3[m][n];
}
}
//fourth quadrant.
for(i=7,m=0;i<14,m<7;i++,m++)
{
for(j=7,n=0;j<14,n<7;j++,n++)
{
b_plus[i][j]=plus_quad4[m][n];
}
}
printf("\nB plus matrix is\n\n");
for(i=0;i<14;i++)
{
for(j=0;j<14;j++)
{
printf(" %d",b_plus[i][j]);
}
printf("\n");
}
//creating 14x14 identity matrix.
for(i=0;i<14;i++)
{
for(j=0;j<14;j++)
{
if(i==j)
{
identity[i][j]=1;
}
}
}
//calculating b* as (Identity) + (b+)
for(i=0;i<14;i++)
{
for(j=0;j<14;j++)
{
b_star[i][j]=identity[i][j]||b_plus[i][j];
}
}
printf("\nB star matrix is\n\n");
for(i=0;i<14;i++)
{
for(j=0;j<14;j++)

{
printf(" %d",b_star[i][j]);
}
printf("\n");
}
//calculating function fi and gi
printf("\nFunctions of Fi and Gi are: \n");
printf("\n\tZ
M
L
a
b
printf("\nFi\t");
for(i=0;i<7;i++)
{
count=0;
for(j=0;j<14;j++)
{
if(b_star[i][j]==1)
{
count++;
}
}
printf("%d\t",count);
}
printf("\nGi\t");
for(i=7;i<14;i++)
{
count=0;
for(j=0;j<14;j++)
{
if(b_star[i][j]==1)
{
count++;
}
}
printf("%d\t",count);
}
}
void printmat(int n[7][7])
{
int count, j;
for(count=0;count<7;count++)
{
printf("\t%c",symbol[count]);
}
printf("\n");

for(count=0;count<7;count++)
{

)\n");

printf("%c",symbol[count]);
for(j=0;j<7;j++)
{
printf("\t%d",n[count][j]);
}
printf("\n");
}
}

OUTPUT:

Practical No.:10
AIM: Write a C/C++/JAVA code to demonstrate the step by step parsing of input string using the given
SPF (Precedence function). Perform the following.
1. Input and store the OPM in a suitable data structure and print it on the screen.
2. Print the operators and non-terminals of the grammar.
3. Input string from the user and show the step by step reduction of the string. (show prime
phrase at each iteration)
4. Print the acceptance or rejection of the string.
#include <stdio.h>
#include <string.h>
char symbols_array[7]={'Z','M','L','a','b','(',')'}, rhs[][5]={"bMb\0","a\0","(L\0","Ma)\0"},
lhs[4]={'Z','M','M','L'}, str[10][10];
int fi[7]={1, 7, 8, 9, 4, 2, 8}, gi[7]={1, 4, 2, 7, 7, 5, 9}, rowcount=0, startpos, endpos, str_length;
void printProductions();
void printFunctions();
int getValue(char x, char y);
int replace();
int main()
{
char temp1[10]="\0";
int i,f,g,parsing_result;
printProductions();
printFunctions();
printf("\nEnter the string to be parsed: \n\n");
scanf("%s",temp1);
str_length=strlen(temp1);
str[rowcount][0]='#';
str[rowcount][str_length+1]='#';
for(i=0;i<str_length;i++)
{
str[rowcount][i+1]=temp1[i];
}
printf("\nYou entered: %s",str[rowcount]);
rowcount++;
for(i=0; strlen(str[rowcount-1])!=3; )
{
str_length=strlen(str[rowcount-1]);
f=getValue(str[rowcount-1][i],'f');
g=getValue(str[rowcount-1][i+1],'g');
if(f<g)
{
//printf("\nf(%c)\t<\tg(%c)\n%d\t<\t%d",str[rowcount1][i],str[rowcount-1][i+1],f, g);
i++;
}

else if(f>g)
{
//printf("\nf(%c)\t>\tg(%c)\n%d\t>\t%d",str[rowcount1][i],str[rowcount-1][i+1],f, g);
startpos=endpos=i;
f=getValue(str[rowcount-1][i-1],'f');

//compare previous two

characters.
g=getValue(str[rowcount-1][i],'g');
if(f==g)
{
startpos=i-1;
endpos=i;
}
f=getValue(str[rowcount-1][i+1],'f');
g=getValue(str[rowcount-1][i+2],'g');
if(f>g)
endpos=i+1;
parsing_result=replace();
if(parsing_result==-99)
goto ERROR;
else
i=0;

//compare next two characters.

}
else if(f==g)
{
//printf("\nf(%c)\t=\tg(%c)\n%d\t=\t%d",str[rowcount1][i],str[rowcount-1][i+1],f, g);
startpos=endpos=i;
f=getValue(str[rowcount-1][i-1],'f');//compare previous two characters.
g=getValue(str[rowcount-1][i],'g');
if(f==g)
{
startpos=i-1;
endpos=i;
}
f=getValue(str[rowcount-1][i+1],'f');
//compare next two characters.
g=getValue(str[rowcount-1][i+2],'g');
if(f>g)
{
endpos=i+1;
}
else if(f==g)
{
endpos=i+2;
}
parsing_result=replace();
if(parsing_result==-99)
goto ERROR;

else
i=0;
}
}
if(strlen(str[rowcount-1]) == 3 && str[rowcount-1][1]==symbols_array[0])
printf("\nString parsed.\tSuccess!");
else
ERROR : printf("\nString cannot be parsed.\tError!"); //goto label definition
return 0;
}
int getValue(char symbol, char function_type)
{
int index, value, len=strlen(symbols_array);
if(symbol=='#')
{
return 0;
}
else
{
for(index=0;index<len;index++)
{
if(symbol==symbols_array[index])
{
break;
}
}
if(function_type == 'f')
{
value=fi[index];
}
else if(function_type == 'g')
{
value=gi[index];
}
return value;
}
}
int replace()
{
int i=0,j=0,k=0,parse_flag=0;
char str1[10]="\0";
for(i=startpos,j=0;i<=endpos;i++,j++)
{
str1[j]=str[rowcount-1][i];
//retrieve the string to be checked with the RHS array
}
printf("\nString to be checked with the RHS is: '%s'",str1);
for(i=0;i<=4;i++)//check against all the values in the RHS array
{

if(strcmp(str1,rhs[i]) == 0)
{
for(j=0;j<startpos;j++)//if equal add all characters from zero till the start pos
variable first into a new row
{
str[rowcount][k]=str[rowcount-1][j];
k++;
}
str[rowcount][k]=lhs[i]; //add the LHS side of the production whose RHS
matches the string1
k++;
for(j=endpos+1;j<str_length;j++)
//finally, add the remaining string into
the new row from endpos+1 to the length of the previous row.
{
str[rowcount][k]=str[rowcount-1][j];
k++;
}
printf("\nCase matched with production number: ( %d )\t%c\t--->\t%s\nString
obtained after replacement is: '%s'",i+1,lhs[i],rhs[i],str[rowcount]);
parse_flag = 1;
rowcount++;
break;
}
}
if(parse_flag==1)
return parse_flag;
else
return parse_flag=-99;
}
void printProductions()
{
int count=0,len=strlen(lhs);
printf("\nProductions:\n\nNum\tLHS\t\tRHS\n");
for(count=0;count<len;count++)
{
printf("\n%d.)\t%c\t-->\t%s",count+1,lhs[count],rhs[count]);
}
}
void printFunctions()
{
int count=0,len=strlen(symbols_array);
printf("\n\nFunctions\n\nSymbols:\t");
for(count=0;count<len;count++)
{
printf("%c\t",symbols_array[count]);
}
printf("\nFI:\t\t");
for(count=0;count<len;count++)

{
printf("%d\t",fi[count]);
}
printf("\nGI:\t\t");
for(count=0;count<len;count++)
{
printf("%d\t",gi[count]);
}
}
OUTPUT:

Das könnte Ihnen auch gefallen