Vous êtes sur la page 1sur 16

LAB MANUAL

B. TECH Fifth SEMESTER


Compiler Design

List of Experiments
1. Write a program of a grammar which accepts the odd no of zero.
2. Write a program to identify that the given string is identifier or
not.
3. Write a program to identify that the given grammar is Left
recursive or not.
4. Write a program to input a grammar.
5. Write a program to count the number of words in a string.
6. Write a program to find the FIRST of a grammar.
7. Write a program to find the FOLLOW of a grammar.

1. WAP for a finite state automata which accepts the odd no of zero.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10];
int s,q0,q1,i;
clrscr();
printf("Enter a binary string:");
gets(a);
s=q0;
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='0')
{
if(s==q0)
s=q1;
elseif(s==q1)
s=q0;
}
}
if(s==q1)
printf("Grammer is accepted.");
else
printf("Grammer is not accepted.");
getch();
}

Output:-
Enter a binary string:01001
Grammer is accepted.
Enter a binary string:1010
Grammer is not accepted.

2. Write a program to identify that the given string is identifier or not.


#include<iostream.h>
#include<graphics.h>
#include<stdlib.h>
#include<string.h>
#include<dos.h>
#include<ctype.h>
#include<conio.h>
void circle(char*s,int x,int y)
{
circle(x,y,10);
outtextxy(x-5,y,s);
line(x,y,x+100,y);
delay(100);
}
void trans(char*s,char r,char*e)
{
int f=10;
if(strcmp(s,"q10")!=0)
{
f=int(s[1])-48;
}
switch(f)
{
case(0):
if(isalpha(r)||r=='_')
{
strcpy(e,"q1");
}
else if(isdigit(r))
{
strcpy(e,"q3");
}
else if(r=='.')
{
strcpy(e,"q4");
}
else if(r=='='||r=='!')
{
strcpy(e,"q7");
}
else if(r=='<'||r=='>')
{
strcpy(e,"q5");
}
else if(isspace(r))
{
strcpy(e,"qf");
}
else
{
strcpy(e,"qr");
}
break;
case(1):
if(isalpha(r)||r=='_'||isdigit(r))
{
strcpy(e,"q2");
}
else
{
strcpy(e,"qr");
}
break;
case(2):
if(isalpha(r)||r=='_'||isdigit(r))
{
strcpy(e,"q2");
}
else if(r=='\0')
{
outtextxy(100,150,"The entered
string is an identifier\n");
strcpy(e,"qf");
}
else
{
strcpy(e,"qr");
}
break;
case(3) :
if(isdigit(r))
{
strcpy(e,"q3");
}
else if(r=='.')
{
strcpy(e,"q4");
}
else
{
strcpy(e,"qr");
}
break;
case(4):
if(isdigit(r))
{
strcpy(e,"q4");
}
else if(r=='\0')
{
outtextxy(100,150,"The entered string is a float
ing point number\n");
strcpy(e,"qf");
}
else
{
strcpy(e,"qr");
}
break;
case(5):
if(r=='\0')
{
outtextxy(100,150,"The
entered string is a relational operator\n");
strcpy(e,"qf");
}
else if(r=='=')
{
strcpy(e,"q6");
}
else
{
strcpy(e,"qr");
}
break;
case(6):
if(r=='\0')
{
outtextxy(100,150,"The entered
string is a relational operator\n");
strcpy(e,"qf");
}
else
{
strcpy(e,"qr");
}
break;
case(7):
if(r=='=')
{
strcpy(e,"q6");
}
else
{
strcpy(e,"qr");
}
break;
case(8):
if(r=='I')
{
strcpy(e,"q9");
}
else if(isdigit(r))
{
strcpy(e,"q3");
}
else if(r=='>'||r=='<')
{
strcpy(e,"q5");
}
else if(r=='='||r=='!')
{
strcpy(e,"q7");
}
else if(isspace(r))
{
strcpy(e,"qf");
}
else if(isalnum(r))
{
strcpy(e,"q1");
}
else
{
strcpy(e,"qr");
}
break;
case(9):
if(r=='F')
{
strcpy(e,"q10");
}
else
{
strcpy(e,"q2");
}
break;
case(10):
if(r=='\0')
{
outtextxy(100,150,"The entered s
tring is a keyword\n");
strcpy(e,"qf");
}
else
{
strcpy(e,"q2");
}
};
}
int determine(char*s)
{
char*state;
state=(char*)calloc(10,sizeof(char));
strcpy(state,"q8");
int x=50;
int y=50;
for(int i=0;s[i]!='\0';i++)
{
circle(state,x,y);
x=x+50;
trans(state,s[i],state);
}
trans(state,'\0',state);
circle(x,y,10);
outtextxy(x-5,y,state);
circle(x,y,15);
setcolor(BLACK);
line(x,y,x+100,y);
if(strcmp(state,"qf")==0)
return(1);
return(0);
}
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "d:\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
cout<<"Graphics error: %s\n"<<grapherrormsg(errorcode);
cout<<"Press any key to halt:";
getch();
exit(1); /* return with error code */
}
setcolor(GREEN);
char*str;
cleardevice();
str=(char*)calloc(100,sizeof(char));
cout<<"Enter the string "<<endl;
cin>>str;
if(determine(str))
{
setcolor(GREEN);
outtextxy(100,200,"The entered string is accepted\n");
}
else
{
setcolor(GREEN);
outtextxy(100,200,"The string is not accepted\n");
}
getch();
}

3. Write a program to identify that the given grammar is Left recursive or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10][10],i=-1,flag=0;
clrscr();
printf("Enter the Productins:\n");
do
{
i++;
gets(a[i]);
}
while(a[i][0]!='\0');
printf("Your Productions:\n");
for(i=0;a[i][0]!='\0';i++)
puts(a[i]);
for(i=0;a[i][0]!='\0';i++)
{
if(a[i][0]==a[i][1])
flag++;
}
if(flag!=0)
printf("Grammar is Left Recursive.");
else
printf("Grammar is not Left Recursive.");
getch();
}
Output:-
Enter the Productions:
E->E+T
E->T
T->T*F
T->F
F->(E)
F->id
Your Productions:
E->E+T
E->T
T->T*F
T->F
F->(E)
F->id
Grammar is Left Recursive.

4. Write a program to input a grammar.


#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
char stack[100];
int top=-1;
void scan(char *prod);
void main()
{
clrscr();
int i=0,j;
char prod[100][100];
cout<<"Enter productions: "<<endl<<endl;
cout<<"S -> ";
cin>>prod[i];
scan(prod[i]);
while(top>-1)
{
i++;
cout<<stack[top]<<" -> ";
top--;
cin>>prod[i];
scan(prod[i]);
}
cout<<endl<<"Grammar Complete!";
getch();
}
void scan(char *prod)
{
int len=strlen(prod),i;
for(i=len-1;i>=0;i--)
{
if(prod[i]>=65&&prod[i]<=90)
{
top++;
stack[top]=prod[i];
}
}
}

Output:-
Enter productions:
S -> E
E -> E+T
E -> T
T -> T*F
T -> F
F -> (E)
F -> id
Grammar Complete!
5. Write a program to count the number of words in a string.
#include<stdio.h>
#include<conio.h>
void main()
{
char s[50];
int i,count=1;
clrscr();
printf("Enter a string:");
gets(s);
for(i=0;s[i]!='\0';i++)
{
if(s[i]==' ')
count++;
}
printf("Number of words in the string:%d",count);
getch();
}

Output:-
Enter a string:My name is Rahul Kansal.
Number of words in the string:5
6. Write a program to find the FIRST of a grammar.
# include<stdio.h>
# include<conio.h>
char pro[5][20];
struct node
{
char first;
struct node* next;
};
typedef struct node* nodeptr;
nodeptr start=NULL;
nodeptr get_node()
{
nodeptr n;
n=(nodeptr) malloc(sizeof(struct node));
return(n);
}
void add_first(char ft)
{
nodeptr new_node,ptr;
new_node=get_node();
new_node->next=NULL;
new_node->first=ft;
if(start==NULL)
start=new_node;
else
{
ptr=start;
while(ptr->next!=NULL)
1 ptr=ptr->next;
ptr->next=new_node;
}
}
void bar_check(int temp)
{
int i=0;
while(pro[temp][i]!='\0')
{
if(pro[temp][i]=='|')
{
add_first(pro[temp][i+1]);
break;
}
i=i+1;
}
}
void first(char nt)
{
int i,flag_found=0,temp,c,ptr,valptr;
for(i=0;i<5;i++)
{
if(nt==pro[i][0])
{
flag_found=1;
temp=i;
break;
}
}
if(flag_found==0)
{
printf("\nSymbol not found.");
getch();
exit(0);
}
else if(flag_found==1)
{
c=1;
if((pro[temp][c]=='-')&&(pro[temp][c+1]=='>'))
{
ptr=c+2;
valptr=pro[temp][ptr];
if(((valptr>=97)&&(valptr<=122))||(pro[temp][ptr]=='(')|
|(pro[temp][ptr]==')')||(pro[temp][ptr]=='*')||(pro[temp][ptr]=='+')||(pro[temp]
[ptr]=='-')||(pro[temp][ptr]=='/')||(pro[temp][ptr]=='&')||(pro[temp][ptr]=='@')
)
{
add_first(pro[temp][ptr]);
bar_check(temp);
}
else if((valptr>=65)&&(valptr<=90))
first(pro[temp][ptr]);
}
}
}
void main()
{
char nt;
nodeptr temp1;
int i,j,count;
clrscr();
printf("Enter a grammar...\n");
for(i=0;i<5;i++)
{
j=0;
do
{
scanf("%c",&pro[i][j]);
j=j+1;
}
while(pro[i][j-1]!=' ');
}
for(count=1;count<=5;count++)
{
start=NULL;
printf("\nEnter the terminal to compute the first...");
scanf("%s",&nt);
first(nt);
printf("\nThe first list is...\n");
temp1=start;
while(temp1!=NULL)
{
printf("%c\t",temp1->first);
temp1=temp1->next;
}
}
getch();
}

7. Write a program to find the FOLLOW of a grammar.


# include<stdio.h>
# include<conio.h>
char pro[5][20];
struct node
{
char follow;
struct node* next;
};
typedef struct node* nodeptr;
nodeptr start=NULL;
nodeptr temp_start=NULL;
nodeptr get_node()
{
nodeptr n;
n=(nodeptr) malloc(sizeof(struct node));
return(n);
}
void add_follow(char ft,int mode)
{
nodeptr new_node,ptr;
if(mode==1)
{
new_node=get_node();
new_node->next=NULL;
new_node->follow=ft;
if(start==NULL)
start=new_node;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=new_node;
}
}
else if(mode==2)
{
new_node=get_node();
new_node->next=NULL;
new_node->follow=ft;
if(temp_start==NULL)
temp_start=new_node;
else
{
ptr=temp_start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=new_node;
}
}
}
void bar_check(int temp,int mode)
{
int i=0;
while(pro[temp][i]!='\0')
{
if(pro[temp][i]=='|')
{
add_follow(pro[temp][i+1],mode);
break;
}
i=i+1;
}
}
void first(char nt,int mode)
{
int i,flag_found=0,temp,c,ptr,valptr,val;
val=nt;
if(((val>=97)&&(val<=122))||(val=='(')||(val==')')||(val=='*')||(val=='+
')||(val=='-')||(val=='/')||(val=='&')||(val=='@'))
add_follow(nt,mode);
else
{
for(i=0;i<5;i++)
{
if(nt==pro[i][0])
{
flag_found=1;
temp=i;
break;
}
}
if(flag_found==0)
{
printf("\nSymbol not found.");
getch();
exit(0);
}
else if(flag_found==1)
{
c=1;
if((pro[temp][c]=='-')&&(pro[temp][c+1]=='>'))
{
ptr=c+2;
valptr=pro[temp][ptr];
if(((valptr>=97)&&(valptr<=122))||(pro[temp][ptr
]=='(')||(pro[temp][ptr]==')')||(pro[temp][ptr]=='*')||(pro[temp][ptr]=='+')||(p
ro[temp][ptr]=='-')||(pro[temp][ptr]=='/')||(pro[temp][ptr]=='&')||(pro[temp][pt
r]=='@'))
{
add_follow(pro[temp][ptr],mode);
bar_check(temp,mode);
}
else if((valptr>=65)&&(valptr<=90))
first(pro[temp][ptr],mode);
}
}
}
}
void follow(char nt,int mode)
{
int i,flag_found=0,temp,tempc,j,ptr,valptr,flag_e_found=0;
nodeptr temp_ptr;
for(i=0;i<5;i++)
{
j=3;
while(pro[i][j]!='\0')
{
if(nt==pro[i][j])
{
flag_found=1;
temp=i;
tempc=j;
break;
}
if(flag_found!=0)
break;
j=j+1;
}
}
if(flag_found==0)
{
printf("\nSymbol not found.");
getch();
}
else if(flag_found==1)
{
if(nt==pro[0][0])
add_follow('$',1);
ptr=j+1;
if(pro[temp][ptr]!='\0')
{
temp_start=NULL;
first(pro[temp][ptr],2);
temp_ptr=temp_start;
while(temp_ptr!=NULL)
{
if(temp_ptr->follow=='@')
{
flag_e_found=1;
break;
}
temp_ptr=temp_ptr->next;
}
if(flag_e_found==1)
follow(pro[temp][0],1);
first(pro[temp][ptr],1);
}
else if(pro[temp][ptr]=='\0')
follow(pro[temp][0],1);
}
}
void main()
{
char nt;
nodeptr temp1;
int i,j,count;
clrscr();
printf("Enter a grammar...\n");
for(i=0;i<5;i++)
{
j=0;
do
{
scanf("%c",&pro[i][j]);
j=j+1;
}
while(pro[i][j-1]!=' ');
}
for(count=1;count<=5;count++)
{
start=NULL;
temp_start=NULL;
printf("\nEnter the terminal to compute the follow...");
scanf("%s",&nt);
follow(nt,1);
printf("\nThe follow list is...\n");
temp1=start;
while(temp1!=NULL)
{
printf("%c\t",temp1->follow);
temp1=temp1->next;
}
}
getch();
}

Vous aimerez peut-être aussi