CD (KCS - 552) Lab File
CD (KCS - 552) Lab File
CD (KCS - 552) Lab File
Session: 2023-24
1 Write a program in C/C++ to show the transition table from a given transition diagram.
3 Write a program in C/C++ to find the FIRST set for a given set of
production rule of a grammar.
4 Write a program in C/C++ to find a FOLLOW set from a given set of production rule.
5 Write a program in C/C++ to generate intermediate code from a given syntax tree
statement.
6 Write a program in C/C++ to generate Intermediate Code (Postfix Expression) from given
syntax tree
Algorithm:
Start
Enter the number of states.
Enter the number of input variables.
Enter the state and its information.
Enter the input variables.
Enter the transition function information i.e. transition value from a state with a input variable.
Show the Transition Table.
Stop
Program (tt.c):
// write a program to display transition table on the screen
#include<stdio.h>
#include<stdlib.h>
struct setStates
{
int state;
int final; // 0 - NO 1 - YES
};
typedef struct setStates sstate;
void main()
{
int s,v,i,j;
int **sv,*var;
sstate *states;
printf("\nInput the number of finite set of states : ");
scanf("%d",&s);
scanf("%d",&v);
for(i=0;i<s;i++)
{
sv[i]=(int *)malloc(sizeof(int));
}
/*printf("\n2 sucess\n");
for(i=0;i<s;i++)
{
for(j=0;j<v;j++)
{
printf("%d\t",sv[i][j]);
}
printf("\n");
}*/
for(i=0;i<s;i++)
{
scanf("%d%d%d",&states[i].state,&states[i].start,&states[i].final);
}
for(i=0;i<v;i++)
{
scanf("%d",&var[i]);
}
for(i=0;i<s;i++)
{
for(j=0;j<v;j++)
{
printf("\nThe sates %c with input veribale %c move to state : ",states[i].state,var[j]);
scanf("%d",&sv[i][j]);
}
}
printf("\t");
for(i=0;i<v;i++)
{
printf("%c\t",var[i]);
}
printf("\n ");
for(i=0;i<s;i++)
{
printf("\n%c %c %c\t",states[i].state,(states[i].start==0)?' ':'$',(states[i].final==0)?' ':'*'); for(j=0;j<v;j++)
{
printf("%c\t",sv[i][j]);
}
printf("\n");
}
}
Output:
98 0 0
99 0 0
100 0 0
move to state : 97
The sates c with input variable 1 move to state : 100 The
a $ *b c
d a
a d
d b
Algorithm:
1. Start
2. Get the input expression from the user.
3. Store the keywords and operators.
4. Perform analysis of the tokens based on the ASCII values.
5.
ASCII Range TOKEN TYPE
Program (lexi.c):
/* Lexical Analyzer */
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
char key[11][10]={"for","while","do","then","else","break","switch","case","if","continue"};
char oper[13]={'+','-','*','/','%','&','<','>','=',';',':','!'};
char a[20],b[20],c[20];
int i,j,l,m,k,flag;
clrscr();
i=0;
while(a[i])
flag=0;
j=0;
l=0;
b[0]='\0';
if((toascii(a[i]>=97))&&(toascii(a[i]<=122)))
if((toascii(a[i+1]>=97))&&(toascii(a[i+1]<=122)))
while((toascii(a[i]>=97))&&(toascii(a[i]<=122)))
b[j]=a[i];
j++; i++;
} b[j]='\
0';
else
b[j]=a[i]; i+
+; b[j+1]='\
0';
for(k=0;k<=9;k++)
if(strcmpi(b,key[k])==0)
flag=1;
break;
if(flag==1)
else
else if((toascii(a[i]>=48))&&(toascii(a[i]<=57)))
if((toascii(a[i+1]>=48))&&(toascii(a[i+1]<=57)))
while((toascii(a[i]>=48))&&(toascii(a[i]<=57)))
c[l]=a[i];
l++; i++;
else
c[l]=a[i];
i++;l++;
} c[l]='\
0';
}//second ifelse
else
for(m=0;m<13;m++)
{
if(a[i]==oper[m])
break;
if(m>=13)
i++;
}//last else
} //while
getch();
Output:
( is the symbol
i is the identifier
5 is the constant
) is the symbol
if is the keyword
( is the symbol
b is the identifier
20 is the constant
) is the symbol
Algorithm:
Procedure First
Program:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void searchFirst(int n, int i, char pl[], char r[], char result[], int k)
int j,flag;
for(j=i+1;j<n;j++)
if(r[i]==pl[j])
if(isupper(r[j]))
searchFirst(n,j,pl,r,result,k);
result[k++]=r[j];
result[k++]=','; flag=0;
if(flag==0)
for(j=0;j<k-1;j++)cout<<result[j];
void main()
clrscr();
char pr[10][10],pl[10],r[10],prev,result[10];
int i,n,k,j;
cin>>n;
if(n==0) exit(0);
for(i=0;i<n;i++)
cin>>pl[i];
gets(pr[i]);
r[i]=pr[i][0];
for(i=0;i<n;i++)
cout<<pl[i]<<"->"<<pr[i]<<"\n";//<<";"<<r[i]<<"\n";
cout<<"\n----O U T P U T---\n\n";
prev=pl[0];k=0;
for(i=0;i<n;i++)
if(prev!=pl[i])
cout<<"\nFIRST("<<prev<<")={";
for(j=0;j<k-1;j++)cout<<result[j];
cout<<"}";
k=0;prev=pl[i];
//cout<<"\n3";
if(prev==pl[i])
{
result[k++]=r[i];
result[k++]=',';
if(isupper(r[i]))
cout<<"\nFIRST("<<prev<<")={";
searchFirst(n,i,pl,r,result,k);
cout<<"}";
k=0;prev=pl[i+1];
if(i==n)
cout<<"\nFIRST("<<prev<<")={";
for(j=0;j<k-1;j++)cout<<result[j];
cout<<"}";
k=0;prev=pl[i];
getch();
Output:
E->TX
X- >+TX
X->e
T->FY
Y->*FY
Y->e
F->(E)
F->i
----O U T P U T---
FIRST(E)={(,i}
FIRST(X)={+,e}
FIRST(T)={(,i}
FIRST(Y)={*,e}
FIRST(F)={(,i}
E->aTX
E->TX
T->FY
F->(E)
F->i
----O U T P U T---
FIRST(E)={a,(,i}
FIRST(T)={(,i}
FIRST(F)={(,i}
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
void main()
{clrscr();
int i,z;
char c,ch;
14
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
do
{ m=
0;
scanf("%c",&c);
follow(c);
printf("FOLLOW(%c) = { ",c);
for(i=0;i<m;i++)
printf("%c ",f[i]);
printf(" }\n");
scanf("%d%c",&z,&ch);
while(z==1);
void follow(char c)
{ if(a[0][0]==c)f[m+
+]='$';
for(i=0;i<n;i++)
15
for(j=2;j<strlen(a[i]);j++)
if(a[i][j]==c)
if(a[i][j+1]!='\0')first(a[i][j+1]);
if(a[i][j+1]=='\0'&&c!=a[i][0])
follow(a[i][0]);
}
}void first(char c)
int k; if(!
(isupper(c)))f[m++]=c;
for(k=0;k<n;k++)
if(a[k][0]==c)
if(a[k][2]=='$') follow(a[i][0]);
else if(islower(a[k][2]))f[m++]=a[k][2];
else first(a[k][2]);
Output:
E=E+T
T=T*F
F=a
;E FOLLOW(E)={$ +}
FOLLOW(T)={$ +}
FOLLOW(F)={$ +}
Do you want to continue (0/1)?0
Algorithm:
i. pop twice the STACK and result add to the newID(identifier) and
PRINT.
ii. TOP:=TOP-2. Save newID to STACK[TOP]
iii. FLAG:=0
[EndIF]
7.
s
s
(i
):
// only
contain
digits at
the end
#includ
e<iostre
am.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
char g,exp[20],stack[20];
int m=0,i,top=-1,flag=0,len,j;
gets(exp);
len=strlen(exp);
if(isdigit(exp[len-1]))
cout<<"T = inttoreal(";
i=len-1;
while(isdigit(exp[i]))
i--;
for(j=i+1;j<len;j++)
cout<<exp[j];
cout<<".0)\n";
exp[i+1]='T';len=i+2;
}
else //If expression having no digit
cout<<"T = "<<exp[len-1]<<"\n";
exp[len-1]='T';
for(i=len-1;i>=0;i--)
if(exp[i]=='=')
if((i-1)==0)
if(isalpha(stack[top]))
else
break;
else
break;
if(exp[i]=='+'||exp[i]=='/'||exp[i]=='*'||exp[i]=='-'||exp[i]=='%')
{
if(flag==0)
flag=1;top=top+1;
stack[top]=exp[i];
else
g=char('A' + m);m++;
cout<<g<<" = "<<stack[top]<<stack[top-1]<<"\n";
stack[top-1]=g;
stack[top]=exp[i];
flag=0;
else
top=top+1;
stack[top]=exp[i];
if(top>1)
g=char('A' + m);m++;
cout<<g<<" = "<<stack[top]<<stack[top-1]<<stack[top-2]<<"\n";
top=top-2;
stack[top]=g;flag=0;
}
Output:
#include<stdio.h>
#include<conio.h>
char stack[20];
int top=-1;
void push(char x)
stack[++top]=x;
char pop()
if(top==-1){
return -1;
Else
return stack[top--];
int priority(char x)
if(x == ‘(‘)
return 0;
return 1;
return 2;
}main()
char exp[20];
char *e , x;
clrscr();
scanf(“%s”,&exp);
e = exp ;
while(*e != ‘\0’)
if(isalnum(*e))
printf(“%c”,*e);
push(*e);
while(( x =pop() ) != ‘( ‘ )
printf(“%c”,x);
else
printf(“%c”, pop());
push(*e);
} e+
+;
while(top != -1)
{printf(“%c”,pop());
Output:
Algorithm:
Program (srp.cpp):
#include<stdio.h>
#include<conio.h>
#include<string.h>
void check();
void check1();
void copy();
char stack[20];
char temp[10];
char result[10];
int i,j;
void main()
clrscr();
scanf("%s",&stack);
check();
getch();
void check()
for(;i<strlen(stack)+1;i++)
temp[j]='E';
j++;
temp[j]=stack[i];
j++;
check1();
void check1()
l: for(j=0,i=0;i<strlen(temp);)
%c",temp[i]); i++;
print(i);
printf("\n\t %c",temp[i]);
i++;
print(i);
i--;
copy();
goto l;
else
printf("\n\t %c",temp[i]);
i++;
print(i);
void copy()
j=0;
while(temp[i]!='\0')
temp[j]=temp[i];
j++;
i++;
temp[j]='\0';
}void print(int val)
printf("\t\t");
for(;val<strlen(temp);val++)
printf("%c",temp[val]);
Output:
E +E*E-E
+ E*E-E
E *E-E
E *E-E
* E-E
E -E
E -E
- E
Expressions Output: E
#include<iostream>
#include<string>
int main()
string exp;
cin>>exp;
int j=0,k=0;
char q;
for(int i=exp.length()-1;i>1;i--)
cout<<j<<"->"<<exp[i]<<endl; j+
+;
for(int i=exp.length()-1;i>1;i--)
cout<<j<<"->"<<exp[i]<<k<<k+1<<endl;
j++;
k+=2;
}
}cout<<j<<"->"<<exp[0]<<endl;
j++;
cout<<j<<"->"<<exp[1]<<j-1<<j-2<<endl;
return 0;