1)
SOURCE CODE:
#include<stdio.h>
#define MAX_SIZE 80
void main()
{
char com[MAX_SIZE];
int i=2,a=0;
printf("\n Enter comment:");
fgets(com,MAX_SIZE,stdin);
if(com[0]=='/')
{
if(com[1]=='/')
printf("\n It is a comment");
else if(com[1]=='*')
{
for(i=2;i<=30;i++)
{
if(com[i]=='*'&&com[i+1]=='/')
{
printf("\n It is a comment");
a=1;
break;
}
else continue;
}
if(a==0)
printf("\n It is not a comment");
}
else
printf("\n It is not a comment");
}else
printf("\n It is not a comment");
OUTPUT:
Enter a comment:
/*message*/
It is a comment
Enter a comment:
//Hello
It is a comment
2)
SOURCE CODE:
#include<stdio.h>
#define MAX 200
int main()
{
char s[MAX],c,ca=0;
int state=0,i=0;
printf("\n Enter a string:");
scanf("%s", s);
while(s[i]!='\0')
{
switch(state)
{
case 0:
c=s[i++];
if(c=='a')
{
state=0;
ca++;
}
else if(c=='b')
state=1;
else
state=4;
break;
case 1:
c=s[i++];
if(c=='b')
state=2;
else
state=4;
break;
case 2:
c=s[i++];
if(c=='b')
state=3;
else
state=4;
break;
case 3:
c=s[i++];
if(c=='b')
state=3;
else
state=4;
break;
case 4:
printf("%s is not a recognised string.\n",s);
return 0;
}
}
if(state==0)
printf("%s is accepted under rule 'a*'.\n",s);
else if(state==1||state==3||(state==2&&(ca>1||ca==0)))
printf("%s is accepted under rule 'a*b+'.\n",s);
else if(state==2)
printf("%s is accepted under rule 'abb'.\n",s);
else if(state==4)
printf("%s is not recognised string.\n”,s);
}
OUTPUT:
Enter a string:’a’
‘a’ is a recognized string
Enter a string:’abb’
‘abb’ is a recognized string
3)
SOURCE CODE:
#include<stdio.h>
#include<ctype.h>
int main()
{
char a[10];
int flag, i=1;
printf("Enter an identifier:");
scanf("%s",a);
if(isalpha(a[0]))
flag=1;
else
flag=0;
while(a[i]!='\0')
{
if(!isdigit(a[i])&&!isalpha(a[i]))
{
flag=0;
break;
}
i++;
}
if(flag==1)
printf("It is a valid identifier.\n");
else
printf("It is not a valid identifier.\n");
}
OUTPUT:
Enter an identifier: TECH
It is a valid identifier.
4)
SOURCE CODE:
#include<stdio.h>
int main()
{
char s[5];
printf("\n Enter any operator:");
scanf("%s",s);
switch(s[0])
{
case'>': if(s[1]=='=')
printf("\n Greater than or equal");
else
printf("\n Greater than");
break;
case'<': if(s[1]=='=')
printf("\n Less than or equal");
else
printf("\nLess than");
break;
case'=': if(s[1]=='=')
printf("\nEqual to");
else
printf("\nAssignment");
break;
case'!': if(s[1]=='=')
printf("\nNot Equal");
else
printf("\n Bit Not");
break;
case'&': if(s[1]=='&')
printf("\nLogical AND");
else
printf("\n Bitwise AND");
break;
case'|': if(s[1]=='|')
printf("\nLogical OR");
else
printf("\nBitwise OR");
break;
case'+': printf("\n Addition");
break;
case'-': printf("\nSubstraction");
break;
case'*': printf("\nMultiplication");
break;
case'/': printf("\nDivision");
break;
case'%': printf("Modulus");
break;
default: printf("\n Not above mentioned operator");
}
}
OUTPUT:
Enter any operator:+
Addition
5)
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
char stack[20],ip[20],opt[10][10][1],ter[10];
inti,j,k,n,top=0,row,col;
clrscr();
for(i=0;i<10;i++)
{
stack[i]=NULL;
ip[i]=NULL;
for(j=0;j<10;j++)
{
opt[i][j][1]=NULL;
}
}
printf("Enter the no.of terminals:");
scanf("%d",&n);
printf("\nEnter the terminals:");
scanf("%s",ter);
printf("\nEnter the table values:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the value for %c %c:",ter[i],ter[j]);
scanf("%s",opt[i][j]);
}
}
printf("\nOPERATOR PRECEDENCE TABLE:\n");
for(i=0;i<n;i++)
{
printf("\t%c",ter[i]);
}
printf("\n ");
printf("\n");
for(i=0;i<n;i++)
{
printf("\n%c |",ter[i]);
for(j=0;j<n;j++)
{
printf("\t%c",opt[i][j][0]);
}
}
stack[top]='$';
printf("\n\nEnter the input string(append with $):");
scanf("%s",ip);
i=0;
printf("\nSTACK\t\t\tINPUT STRING\t\t\tACTION\n");
printf("\n%s\t\t\t%s\t\t\t",stack,ip);
while(i<=strlen(ip))
{
for(k=0;k<n;k++)
{
if(stack[top]==ter[k])
row=k;
if(ip[i]==ter[k])
col=k;
}
if((stack[top]=='$')&&(ip[i]=='$'))
{
printf("String is ACCEPTED");
break;
}
else if((opt[row][col][0]=='<') ||(opt[row][col][0]=='='))
{
stack[++top]=opt[row][col][0];
stack[++top]=ip[i];
ip[i]=' ';
printf("Shift %c",ip[i]);
i++;
}
else
{
if(opt[row][col][0]=='>')
{
while(stack[top]!='<')
{
--top;
}
top=top-1;
printf("Reduce");
}
else
{
printf("\nString is not accepted");
break;
}
}
printf("\n");
printf("%s\t\t\t%s\t\t\t",stack,ip);
}
getch(); }
OUTPUT:
Enter the no.of terminals:4
Enter the terminals:i+*$
Enter the table values:
Enter the value for i i:
-
Enter the value for i +:>
Enter the value for i *:>
Enter the value for i $:>
Enter the value for + i:<
Enter the value for + +:>
Enter the value for + *:<
Enter the value for + $:>
Enter the value for * i:<
Enter the value for * +:>
Enter the value for * *:>
Enter the value for * $:>
Enter the value for $ i:<
Enter the value for $ +:<
Enter the value for $ *:<
Enter the value for $ $:-
OPERATOR PRECEDENCE TABLE:
i+*$
i|->>>
+|<><>
*|<>>>
$|<<<-
Enter the input string(append with $):i+i*i$
STACK INPUT STRING ACTION
$ i+i*i$ Shift
$<i +i*i$ Reduce
$<i +i*i$ Shift
$<+ i*i$ Shift
$<+<i *i$ Reduce
$<+<i *i$ Shift
$<+<* i$ Shift
$<+<*<i $ Reduce
$<+<*<i $ Reduce
$<+<*<i $ Reduce
$<+<*<i $ String is ACCEPTED
6.a) FIRST
SOURCE CODE:
#include<stdio.h>
#include<ctype.h>
void FIRST(char[],char );
void result(char[],char);
intnop;
char prod[10][10];
void main()
{
int i;
char choice;
char c;
char res1[20];
clrscr();
printf("How many number of productions ? :");
scanf(" %d",&nop);
printf("enter the production string like E=E+T\n");
for(i=0;i<nop;i++)
{
printf("Enter productions Number %d : ",i+1);
scanf(" %s",prod[i]);
}
do
{
printf("\n Find the FIRST of :");
scanf(" %c",&c);
memset(res1,’0’,sizeof(res));
FIRST(res1,c);
printf("\n FIRST(%c)= { ",c);
for(i=0;res1[i]!='\0';i++)
printf(" %c ",res1[i]);
printf("}\n");
printf("press 'y' to continue : ");
scanf(" %c",&choice);
}
while(choice=='y'||choice =='Y');
}
void FIRST(char res[],char c)
{
inti,j,k;
char subres[5];
int eps;
subres[0]='\0';
res[0]='\0';
memset(res,’0’,sizeof(res));
memset(subres,’0’,sizeof(res));
if(!(isupper(c)))
{
result(res,c);
return ;
}
for(i=0;i<nop;i++)
{
if(prod[i][0]==c)
{
if(prod[i][2]=='$')
result(res,'$');
else
{
j=2;
while(prod[i][j]!='\0')
{
eps=0;
FIRST(subres,prod[i][j]);
for(k=0;subres[k]!='\0';k++)
result(res,subres[k]);
for(k=0;subres[k]!='\0';k++)
if(subres[k]=='$')
{
eps=1;
break;
}
if(!eps)
break;
j++;
}
}
}
}
return ;
}
void result(char res[],char val)
{
int k;
for(k=0 ;res[k]!='\0';k++)
if(res[k]==val)
return;
res[k]=val;
res[k+1]='\0';
}
OUTPUT :
How many number of productions ?:8
enter the production string like E=E+T
Enter productions Number 1 : E=TX
Enter productions Number 2 : X=+TX
Enter productions Number 3 : X=$
Enter productions Number 4 : T=FY
Enter productions Number 5 : Y=*FY
Enter productions Number 6 : Y=$
Enter productions Number 7 : F=(E)
Enter productions Number 8 : F=i
Find the FIRST of :X
FIRST(X)= { + $ }
press 'y' to continue : Y
Find the FIRST of :F
FIRST(F)= { ( i }
press 'y' to continue : Y
Find the FIRST of :Y
FIRST(Y)= { * $ }
press 'y' to continue : Y
Find the FIRST of :E
FIRST(E)= { ( i }
press 'y' to continue : Y
Find the FIRST of :T
FIRST(T)= { ( i }
press 'y' to continue : N
6.b) FOLLOW
SOURCE CODE:
#include<stdio.h>
#include<string.h>
intnop,m=0,p,i=0,j=0;
char prod[10][10],res[10];
void FOLLOW(char c);
void first(char c);
void result(char);
void main()
{
int i;
int choice;
charc,ch;
printf("Enter the no.of productions: ");
scanf("%d", &nop);
printf("enter the production string like E=E+T\n");
for(i=0;i<nop;i++)
{
printf("Enter productions Number %d : ",i+1);
scanf(" %s",prod[i]);
}
do
{
m=0;
memset(res,’0’,sizeof(res));
printf("Find FOLLOW of -->");
scanf(" %c",&c);
FOLLOW(c);
printf("FOLLOW(%c) = { ",c);
for(i=0;i<m;i++)
printf("%c ",res[i]);
printf(" }\n");
printf("Do you want to continue(Press 1 to continue ... )?");
scanf("%d%c",&choice,&ch);
}
while(choice==1);
}
void FOLLOW(char c)
{
if(prod[0][0]==c)
result('$');
for(i=0;i<nop;i++)
{
for(j=2;j<strlen(prod[i]);j++)
{
if(prod[i][j]==c)
{
if(prod[i][j+1]!='\0')
first(prod[i][j+1]);
if(prod[i][j+1]=='\0'&&c!=prod[i][0])
FOLLOW(prod[i][0]);
}
}
}
}
void first(char c)
{
int k;
if(!(isupper(c)))
result(c);
for(k=0;k<nop;k++)
{
if(prod[k][0]==c)
{
if(prod[k][2]=='$')
FOLLOW(prod[i][0]);
else if(islower(prod[k][2]))
result(prod[k][2]);
else
first(prod[k][2]);
}
}
}
void result(char c)
{
int i;
for( i=0;i<=m;i++)
if(res[i]==c)
return;
res[m++]=c;
}
OUTPUT:
Enter the no.of productions: 8
enter the production string like E=E+T
Enter productions Number 1 : E=TX
Enter productions Number 2 : X=+TX
Enter productions Number 3 : X=$
Enter productions Number 4 : T=FY
Enter productions Number 5 : Y=*FY
Enter productions Number 6 : Y=$
Enter productions Number 7 : F=(E)
Enter productions Number 8 : F=i
Find FOLLOW of -->X
FOLLOW(X) = { $ ) }
Do you want to continue(Press 1 to continue ....)?1
Find FOLLOW of -->E
FOLLOW(E) = {$ ) }
Do you want to continue(Press 1 to continue ....)?1
Find FOLLOW of -->Y
FOLLOW(Y) = { + $ ) }
Do you want to continue(Press 1 to continue ....)?1
Find FOLLOW of -->T
FOLLOW(T) = { +$ ) }
Do you want to continue(Press 1 to continue ....)?1
Find FOLLOW of -->F
FOLLOW(F) = { * + $ ) }
Do you want to continue(Press 1 to continue. ... )?2
7)
SOURCE CODE:
#include<stdio.h>
#include<string.h>
char input[10];
int i=0,error=0;
void E();
void T();
voidEprime();
voidTprime();
void F();
void main()
{
clrscr();
printf("Enter an arithmetic expression :\n");
gets(input);
E();
if(strlen(input)==i&&error==0)
printf("\nAccepted..!!!");
else
printf("\nRejected..!!!");
getch();
}
void E()
{
T();
Eprime();
}
voidEprime()
{
if(input[i]=='+')
{
i++;
T();
Eprime();
}
}
void T()
{
F();
Tprime();
}
voidTprime()
{
if(input[i]=='*')
{
i++;
F();
Tprime();
}
}
void F()
{
if(input[i]=='(')
{
i++;
E();
if(input[i]==')')
i++;
}
else if(isalpha(input[i]))
{
i++;
while(isalnum(input[i])||input[i]=='_')
i++;
}
else
error=1;
}
OUTPUT :
1)
Enter an arithmetic expression :
sum+month*interest
Accepted..!!!
2)
Enter an arithmetic expression :
sum+avg*+interest
Rejected..!!!
8)
SOURCE CODE:
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
char ip_sym[15],stack[15];
int ip_ptr=0,st_ptr=0,len,i;
char temp[2],temp2[2];
char act[15];
void check();
void main()
{
printf("\n\t\t SHIFT REDUCE PARSER\n");
printf("\n GRAMMER\n");
printf("\n E->E+E\n E->E/E");
printf("\n E->E*E\n E->a/b");
printf("\n enter the input symbol:\t");
gets(ip_sym);
printf("\n\t stack implementation table");
printf("\n stack\t\t input symbol\t\t action");
printf("\n \t\t \t\t \n");
printf("\n $\t\t%s$\t\t\t--",ip_sym);
strcpy(act,"shift ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
len=strlen(ip_sym);
for(i=0;i<=len-1;i++)
{
stack[st_ptr]=ip_sym[ip_ptr];
stack[st_ptr+1]='\0';
ip_sym[ip_ptr]=' ';
ip_ptr++;
printf("\n $%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
strcpy(act,"shift ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
check();
st_ptr++;
}
check();
}
void check()
{
int flag=0;
temp2[0]=stack[st_ptr];
temp2[1]='\0';
if((isalpha(temp2[0])))
{
stack[st_ptr]='E';
printf("\n $%s\t\t%s$\t\t\tE->%s",stack,ip_sym,temp2);
flag=1;
}
if((!strcmp(temp2,"+"))||(!strcmp(temp2,"*"))||(!strcmp(temp2,"/")))
{
flag=1;
}
if((!strcmp(stack,"E+E"))||(!strcmp(stack,"E/E"))||(!strcmp(stack,"E*E")))
{
if(!strcmp(stack,"E+E"))
{
strcpy(stack,"E");
printf("\n $%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
}
else if(!strcmp(stack,"E/E"))
{
strcpy(stack,"E");
printf("\n $%s\t\t %s$\t\t\tE->E/E",stack,ip_sym);
}
else
{
strcpy(stack,"E");
printf("\n $%s\t\t%s$\t\t\tE->E*E",stack,ip_sym);
}
flag=1;
st_ptr=0;
}
if(!strcmp(stack,"E")&&ip_ptr==len)
{
printf("\n $%s\t\t%s$\t\t\tACCEPT",stack,ip_sym);
exit(0);
}
if(flag==0)
{
printf("\n $%s\t\t%s$\t\t\tReject",stack,ip_sym);
exit(0);
}
return;
}
OUTPUT:
1)
SHIFT REDUCE PARSER GRAMMER
E->E+E
E->E/E
E->E*E
E->E-E
E->id
enter the input symbol: a+b*c
stack implementation table
stack input symbol action
$ a+b*c$ --
$a +b*c$ shift a
$E +b*c$ E->a
$E+ b*c$ shift +
$E+b *c$ shift b
$E+E *c$ E->b
$E *c$ E->E+E
$E* c$ shift *
$E*c $ shift c
$E*E $ E->c
$E $ E->E*E
$E $ ACCEPT
2) SHIFT REDUCE PARSER GRAMMER
E->E+E
E->E/E
E->E*E
E->E-E
E->id
enter the input symbol: a+b*+c
stack implementation table
stack input symbol action
$ a+b*+c$ --
$a +b*+c$ shift a
$E +b*+c$ E->a
$E+ b*+c$ shift +
$E+b *+c$ shift b
$E+E *+c$ E->b
$E *+c$ E->E+E
$E* +c$ shift *
$E*+ c$ shift +
$E*+c $ shift c
$E*+E $ E->c
$E*+E reject
/
9)
SOURCE CODE:
#include<stdio.h>
int main()
{
int i;
for(i=0;i<10;i+=2)
{
printf("fun(%d)\n",i+1);
printf("fun(%d)\n",i+2);
}
}
OUTPUT :
fun(1)
fun(2)
fun(3)
fun(4)
fun(5)
fun(6)
fun(7)
fun(8)
fun(9)
fun(10)
10)
SOURCE CODE:
Write a program for constant propagation.
#include<stdio.h>
int main()
{
int x, y, z;
x = 10;
y = x + 45;
z = y + 4;
printf("The value of z = %d", z);
return 0;
}
OUTPUT:
$ vi test.c
$ cc –c –S test.c
$ vi test.s //before optimization assembly code
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
movl $10, 20(%esp)
movl 20(%esp), %eax
addl $45, %eax
movl %eax, 24(%esp)
movl 24(%esp), %eax
addl $4, %eax
movl %eax, 28(%esp)
movl $.LC0, %eax
movl 28(%esp), %edx
movl %edx, 4(%esp)
movl %eax, (%esp)
call printf
movl $0, %eax
leave
ret
$ cc –c –S -O2 test.c
$ vi test.s //after optimization assembly code
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
movl $59, 4(%esp)
movl $.LC0, (%esp)
call printf
xorl %eax, %eax
leave
ret