PRACTICAL FILE
OF
COMPILER DESIGN
SUBMITTED TO: SUBMITTED BY:
Dr. Vinit Kumar lohan Himanshu
154/CSE/19
INDEX
[Link]. Program Name Teacher’s
Remarks
1. Program to find whether given string is keyword
or not
2. Program to find whether given string is identifier
or not
3. Program to find whether given string is constant
or not
4. Program to count blank space and count the no.
Of lines
5. Write a program to identify whether a given line
is a comment or not.
6. Write a program to recognize strings under 'a*',
'a*b+', 'abb'
7. Write a program to simulate lexical analyzer for
validating operators
8. Program to generate tokens for the given grammer
9. Wap to calculate trailing of all the non terminals
in given grammer.
10. Write a program for implementing the
functionalities of predictive parser
PROGRAM NO:-1
PROGRAM TO FIND WHETHER GIVEN STRING IS
KEYWORD OR NOT
#include <stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char a[5][10]={"printf","scanf","if","else","break"};
char str[10];
int i, flag;
puts("Enter the string:: ");
gets(str);
for(i=0;i<strlen(str);i++)
{
if(strcmp(str,a[i])==0)
{
flag=1;
break;
}
else
flag =0;
}
if(flag==1)
puts("keyword");
else
puts("String");
return 0;
}
OUTPUT:
PROGRAM NO:-2
PROGRAM TO FIND WHETHER GIVEN STRING IS
IDENTIFIER OR NOT
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int main()
{
int i,j,flag=1,len;
char str[10];
puts("Enter the string :: ");
gets(str);
len=strlen(str);
for(i=0,j=1;i<len;i++)
{
if(isdigit(str[j]))
{
flag=0;
break;
}
else if(isalpha(str[i]))
{
{
flag++;
continue;
}if(isalnum(str[i]))
{
flag++;
continue;
}
else if(!isdigit(str[i]))
{
flag=0;
break;
}
else
flag++;
}
else if(!isalnum(str[i]))
{
flag=0;
break;
}
}
if(flag==0)
puts("Not Identifier");
else
puts("Identifier");
getch();
}
OUTPUT:
PROGRAM:-3
PROGRAM TO FIND WHETHER GIVEN STRING IS
CONSTANT OR NOT
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
int main()
{
int i,flag;
char a[5];
puts("Enter the value :: ");
gets(a);
for(i=0;i<strlen(a);i++)
{
if(isdigit(a[i]))
flag=1;
else
{
flag=0;
break;
}
}
if(flag==1)
puts("Value is constant");
else
puts("Value is a variable");
getch();
}
OUTPUT:
PROGRAM:-4
PROGRAM TO COUNT BLANK SPACE AND COUNT
THE NO. OF LINES
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int flag=1;
char i,j=0,temp[100];
printf("Enter the Sentence (add '$' at the end) :: \n\n");
while((i=getchar())!='$')
{
if(i==' ')
i=';';
else if(i=='\t')
i='"';
else if(i=='\n')
flag++;
temp[j++]=i;
}
temp[j]=NULL;
printf("\n\n\nAltered Sentence :: \n\n");
puts(temp);
printf("\n\nNo. of lines = %d",flag);
getch();
}
OUTPUT:
PROGRAM NO: - 5
Write a program to identify whether a given line is a comment
or not.
#include<stdio.h>
#include<conio.h>
int main() {
char com[30];
int i=2,a=0;
printf("\n Enter comment:");
gets(com);
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");
getch();
}
OUTPUT:
PROGRAM NO:-6
Write a program to recognize strings under 'a*', 'a*b+', 'abb'
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char s[20],c;
int state=0,i=0;
printf("\n Enter a string:");
gets(s);
while(s[i]!='\0')
{
switch(state)
{
case 0: c=s[i++];
if(c=='a')
state=1;
else if(c=='b')
state=2;
else
state=6;
break;
case 1: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=4;
else
state=6;
break;
case 2: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 3: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=2;
else
state=6;
break;
case 4: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=5;
else
state=6;
break;
case 5: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 6: printf("\n %s is not recognised.",s);
exit(0);
}
}
if(state==1)
printf("\n %s is accepted under rule 'a'",s);
else if((state==2)||(state==4))
printf("\n %s is accepted under rule 'a*b+'",s);
else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);
getch();
}
OUTPUT:
PROGRAM NO:-7
*Write a program to simulate lexical analyzer for validating
operators.
#include<stdio.h>
#include<conio.h>
int main()
{
char s[5];
printf("\n Enter any operator:");
gets(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");
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 a operator");
}
getch();
}
OUTPUT:
PROGRAM:-8
PROGRAM TO GENERATE TOKENS FOR THE GIVEN
GRAMMER
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
int main()
{
int i=0;
char str[20];
printf(" \n Input the string ::");
gets(str);
printf("Corresponding Tokens are :: ");
while(str[i]!='\0')
{
if((str[i]=='(')||(str[i]=='{'))
{
printf("4");
}
if((str[i]==')')||(str[i]=='}'))
{
printf("5");
}
if(isdigit(str[i]))
{
while(isdigit(str[i]))
{
i++;
}
i--;
printf("1");}
if(str[i]=='+')
{
printf("2");
}
if(str[i]=='*')
{
printf("3");
}
i++;
}
getch();
}
OUTPUT:
PROGRAM:-9
WAP TO CALCULATE TRAILING OF ALL THE NON
TERMINALS IN GIVEN GRAMMER
#include<conio.h>
#include<stdio.h>
char arr[18][3] =
{
{'E','+','F'},{'E','*','F'},{'E','(','F'},{'E',')','F'},{'E','i','F'},{'E','$','F'},
{'F','+','F'},{'F','*','F'},{'F','(','F'},{'F',')','F'},{'F','i','F'},{'F','$','F'},
{'T','+','F'},{'T','*','F'},{'T','(','F'},{'T',')','F'},{'T','i','F'},{'T','$','F'},
};
char prod[6] = "EETT";
char res[6][3]=
{
{'E','+','T'},{'T','\0'},
{'T','*','F'},{'F','\0'},
{'(','E',')'},{'i','\0'},
};
char stack [5][2];
int top = -1;
void install(char pro,char re)
{
int i;
for(i=0;i<18;++i)
{
if(arr[i][0]==pro && arr[i][1]==re)
{
arr[i][2] = 'T';
break;
}
}
++top;
stack[top][0]=pro;
stack[top][1]=re;}
int main()
{
int i=0,j;
char pro,re,pri=' ';
for(i=0;i<6;++i)
{
for(j=0;j<3 && res[i][j]!='\0';++j)
{
if(res[i][j]=='+'||res[i][j]=='*'||res[i][j]=='('||res[i][j]==')'||res[i][j]=='i'||r
es[i][j]
=='$')
{
install(prod[i],res[i][j]);
break;
}
}
}
while(top>=0)
{
pro = stack[top][0];
re = stack[top][1];
--top;
for(i=0;i<6;++i)
{
if(res[i][0]==pro && res[i][0]!=prod[i])
{
install(prod[i],re);
}
}
}
for(i=0;i<18;++i)
{
printf("\n\t");
for(j=0;j<3;++j)printf("%c\t",arr[i][j]);
}
getch();
printf("\n\n");
for(i=0;i<18;++i)
{
if(pri!=arr[i][0])
{
pri=arr[i][0];
printf("\n\t%c -> ",pri);
}
if(arr[i][2] =='T')
printf("%c ",arr[i][1]);
}
getch();
}
Output:
PROGRAM:-10
Write a program for implementing the functionalities of
predictive parser
#include<stdio.h>
#include<conio.h>
#include<string.h>
char prol[7][10]={"S","A","A","B","B","C","C"};
char pror[7][10]={"A","Bb","Cd","aB","@","Cc","@"};
char prod[7][10]={"S->A","A->Bb","A->Cd","B->aB","B->@","C-
>Cc","C->@"};
char first[7][10]={"abcd","ab","cd","a@","@","c@","@"};
char follow[7][10]={"$","$","$","a$","b$","c$","d$"};
char table[5][6][10];
numr(char c)
{
switch(c)
{
case 'S': return 0;
case 'A': return 1;
case 'B': return 2;
case 'C': return 3;
case 'a': return 0;
case 'b': return 1;
case 'c': return 2;
case 'd': return 3;
case '$': return 4;
}
return(2);
}
int main()
{
int i,j,k;
for(i=0;i<5;i++)
for(j=0;j<6;j++)
strcpy(table[i][j]," ");
printf("\nThe following is the predictive parsing table for the
following grammar:\n");
for(i=0;i<7;i++)
printf("%s\n",prod[i]);
printf("\nPredictive parsing table is\n");
fflush(stdin);
for(i=0;i<7;i++)
{
k=strlen(first[i]);
for(j=0;j<10;j++)
if(first[i][j]!='@')
strcpy(table[numr(prol[i][0])+1][numr(first[i][j])+1],prod[i]);
}
for(i=0;i<7;i++)
{
if(strlen(pror[i])==1)
{
if(pror[i][0]=='@')
{
k=strlen(follow[i]);
for(j=0;j<k;j++)
strcpy(table[numr(prol[i][0])+1][numr(follow[i][j])+1],prod[i]);
}
}
}
strcpy(table[0][0]," ");
strcpy(table[0][1],"a");
strcpy(table[0][2],"b");
strcpy(table[0][3],"c");
strcpy(table[0][4],"d");
strcpy(table[0][5],"$");
strcpy(table[1][0],"S");
strcpy(table[2][0],"A");
strcpy(table[3][0],"B");
strcpy(table[4][0],"C");
printf("\n--------------------------------------------------------\n");
for(i=0;i<5;i++)
for(j=0;j<6;j++)
{
printf("%-10s",table[i][j]);
if(j==5)
printf("\n--------------------------------------------------------\n");
} getch();
}
OUTPUT: