Call Girls Delhi {Jodhpur} 9711199012 high profile service
Compiler Design Lab File
1. Index
S. No. Name of Program Page
No.
Remarks/
Signature
1. Write a C program for simulation of a finite
state automata to recognize the tokens of
various control statements.
1
2. Write a C program to copy the contentof
one file to another and count the number of
comment lines in a C file.
4
3. Write a C program to count the number of
lines, number of words, number of blank
spaces andnumber of characterin a C file.
6
4. Write a C program to find the no of
identifiers in a C file.
8
5. Write a C program to evaluate an
arithmetic expressioninvolving operators +,
-, *, /.
11
6. Write a C program to recognize strings
‘aaab’, ‘abbb’, ‘ab’, ‘a’, aabb using the
grammer (an bn, n>=0).
14
7. Write a C program to find FIRST of NON
TERMINALS of the given grammar.
16
2. - 1 -
Program 1
Write a C program for simulation of a finite state automata to recognize
the tokens ofvarious control statements.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char a[8];
int s,i,flag=0,loc1=0,loc2=0,j;
printf("Enter any stringn");
gets(a);
for(i=0;i<8;i++)
{
int ch=a[i];
if(ch!=32)
{
loc2=i;
flag++;
}
if(ch==32)
{
s=flag;
flag=0;
switch(s)
{
case 2:
if(a[loc1]=='i' && a[loc2]=='f')
{
printf("STRING HAS if AS A KEYWORD");
}
break;
case 3:
if(a[loc1]=='f' && a[loc1+1]=='o' && a[loc2]=='r')
{
printf("STRING HAS for AS A KEYWORD");
}
break;
default:
5. - 4 -
Program 2
Write a C program to copy the contentof one file to another and count the
number of comment lines in a C file.
%{
#include<stdio.h>
#include<conio.h>
int com=0;
%}
%%
/*[^*]**(*|([^*/][^*]**))*/ com++;//[^n]*[n] com++;
% fprintf(yyout,"%%");
.|[n] fprintf(yyout, yytext);
%%
main(int argc, char **argv)
{
clrscr();
if(argc!=3)
{
printf("n Arguments passed in wrong mannern");
return 1;
}
yyin=fopen(*(argv+1),"r");
yyout=fopen(*(argv+2),"w");
if(!(yyin&&yyout))
{
printf("n Specified file cannot be opened!");
return 1;
}
yylex();
printf("n Total number of comment lines=%d n", com);
}
int yywrap()
{
return 1;
7. - 6 -
Program 3
Write a C program to count the number of lines, number of words, number
of blank spacesand number of characterin a C file.
%{
#include<stdio.h>
#include<conio.h>
int cc=0,bc=0,wc=0,lc=0;
%}
%%
[^ tn]+ { wc++;
cc=cc+yyleng;}
n lc++;
" " bc++;
t bc=bc+5;
%%
main(int argc, char *Count[])
{
clrscr();
if (argc!=2)
{
printf("n usage:./a.out filenamen");
return(0);
}
yyin=fopen(Counr[1],"r");
yylex();
printf("n no of lines are %dn", lc);
printf("n no of words are %dn", wc);
printf("n no of blanks are %dn", bc);
printf("n no of character are %dn", cc);
}
int yywrap()
{
return 1;
}
17. - 16 -
Program 7
Write a C program to find FIRST of NON TERMINALS of the
given grammar.
#include<stdio.h>
#include<conio.h>
char array[10][20],temp[10];
int c,n;
void fun(int,int[]);
int fun2(int i,int j,int p[],int );
void main()
{
clrscr();
int p[2],i,j;
printf("Enter the no. of productions :");
scanf("%d",&n);
printf("Enter the productions :n");
for(i=0;i<n;i++)
scanf("%s",array[i]);
for(i=0;i<n;i++)
{
c=-1, p[0]=-1, p[1]=-1;
fun(i,p);
printf("First(%c) : [ ",array[i][0]);
for(j=0;j<=c;j++)
printf("%c,", temp[j]);
printf("b ].n");
getch();
}
}
int fun2(int i, int j, int p[],int key)
{
int k;
if(!key)
{
for(k=0;k<n;k++)
if(array[i][j]==array[k][0])
break;
p[0]=i;p[1]=j+1;
fun(k,p);
return 0;
}