Practical File OF Compiler Design

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 21

PRACTICAL FILE OF COMPILER DESIGN

SUBMITTED TO: BY: Mr. HIMANSHU SHARMA DEVE

SUBMITTED

08/CSE/07

GOLD FIELD INSTITUTE OF TECHNOLOGY & MANAGEMENT

VILL: CHHAINSA, BALLABGARH (FARIDABAD)

INDEX
s. no
1. 2.

Title
Design Automata to accept the String ending with 011 Design an Automata to accept the String having exactly one 0 over alphabet {0,1}

date

t. sign

3.

Design Automata to accept the String which does not contain 000 Design Automata to accept the String which starts with 01 or end with 01

4.

5.

Design Automata to accept the String 101

6.

Design Automata to accept the String which has equal no. of 0s &1s

7.

Design Automata to accept the String which has even no. of 0s &1s Design Automata to reverse of the String

8.

9.

Design Automata to the String has multiple three of 0s

Program -- 1 Design Automata to accept the String ending with 011 #include<stdio.h> #include<conio.h> void main() { int i,n,a[1]; clrscr(); printf(Enter The Value of n : ); scanf(%d,&n); printf(Enter String); for(i=1;i<=n;i++) { scanf(%d,&a[i]); } If((a[n-2]==0)&&(a[n-1]==1)&&(a[n]==1)) { Printf(String Accepted); } else { printf(String Not Accepted); } getch(); }

Output of 1st program Enter The Value of n : 4 Enter the String 0 0 1 1 String Accepted Enter the String 0 1 1 1 String Not Accepted

Program -- 2 Design an Automata to accept the String having exactly one 0 over alphabet {0,1}

#include<stdio.h> #include<conio.h> void main() { int i, len,count=0; int a[20]; clrscr(); printf("Enter the length of the string : "); scanf("%d",&len); printf("Enter the string : \n"); for(i=0;i<len;i++) scanf("%d",&a[i]); { for(i=0;i<len;i++) if(a[i]==0) count=count+1; } if (count==1) printf("String Accepted"); else printf("Not Accepted"); getch(); }

Output of 2nd program

Enter the length of the string : 5 Enter the string 0 1 1 1 1 String Accepted

Enter the length of the string : 4 Enter the string 1 0 0 1 String not Accepted

Program -- 3
Design Automata to accept the String which does not contain 000

#include<stdio.h> #include<conio.h> void main() { clrscr(); int a[10],i,n,count=0; printf("Enter the value of n : "); scanf("%d",&n); printf("Enter the array : \n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) { if((a[i]==0)&&(a[i+1]==0)&&(a[i+2]==0)) count++; } if(count>0) printf("String is not accepted"); else printf("String is accepted"); getch(); }

Output of 3rd program


Enter the value of n : 5 Enter the array 1 1 1 0 0 String is accepted

Enter the value of n : 5 Enter the array 0 0 0 1 1 String is not accepted

Program -- 4 Design Automata to accept the String which starts with 01 or end with 01 #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[1],i,n; printf("Enter the value of n : "); scanf("%d",&n); printf("Enter the array : \n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); if(((a[0]==0)&&(a[1]==1))||((a[n-1]==0)&&(a[n]==1))) printf("String is accepted"); else printf("String is not accepted"); getch(); }

Output of 4th program


Enter the value of n : 5 Enter the array 0 1 1 1 1 String is not accepted

Enter the value of n : 5 Enter the array 1 1 1 1 1 String is not accepted

Program -- 5 Design Automata to accept the String 101 #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[1],i,n; printf("Enter the value of n : "); scanf("%d",&n); if(n==3) { printf("Enter the string : \n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); if((a[1]==1)&&(a[n-1]==0)&&(a[n]==1)) printf("String is accepted"); else printf("String is not accepted"); } else printf(The value of n must be 3); getch(); }

Output of 5th program


Enter the value of n : 3 Enter the string 1 0 1 String is accepted

Enter the value of n : 5 The value of n must be 3

Program -- 6
Design Automata to accept the String which has equal no. of 0s &1s

#include<stdio.h> #include<conio.h> void main() { int a[3],i,n,c1=0,c2=0; clrscr(); printf("Enter the value of n: "); scanf("%d",&n); printf("Enter the string:\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) { if(a[i]==0) c1++; else if(a[i]==1) c2++; } if(c1==c2) printf("String is accepted"); else printf("String is not accepted"); getch(); }

Output of 6th program


Enter the value of n: 6 Enter the string: 1 1 1 0 0 0 String is accepted Enter the value of n: 7 Enter the string: 1 1 1 0 0 0 0 String is not accepted

Program -- 7
Design Automata to accept the String which has even no. of 0s &1s

#include<stdio.h> #include<conio.h> void main() { int a[3],i,n,c1=0,c2=0; clrscr(); { printf("Enter the value of n: "); scanf("%d",&n); printf("Enter the string:\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) { if(a[i]==0) c1++; else if(a[i]==1) c2++; } if((c1%2==0)&&(c2%2==0)) printf("String is accepted"); else printf("String is not accepted"); } getch(); }

Output of 7th program


Enter the value of n: 6 Enter the string: 1 1 1 1 0 0 String is accepted Enter the value of n: 6 Enter the string: 1 1 1 0 0 0 String is not accepted

Program 8 Design Automata to reverse of the String #include<stdio.h> #include<conio.h> void main() { int a[3],i,n; clrscr(); { printf("Enter the value of n: "); scanf("%d",&n); printf("Enter the string:\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("\nThe reverse of the string:"); for(i=n;i>=1;i--) { printf("\n%d",a[i]); } } getch(); }

Output of 8th program


Enter the value of n: 5 Enter the string: 1 0 0 1 1 The reverse of the string: 1 1 0 0 1

Program -- 9 Design Automata to the String has multiple three of 0s #include<stdio.h> #include<conio.h> void main() { int a[3],i,n,c=0; clrscr(); { printf("Enter the value of n: "); scanf("%d",&n); printf("\nEnter the string:\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) { if(a[i]==0) c++; } if(c%3==0) printf("String is accepted"); else printf("String is not accepted"); } getch(); }

Output of 9th program


Enter the value of n: 5 Enter the string: 1 1 0 0 0 String is accepted Enter the value of n: 5 Enter the string: 1 1 1 0 0 String is not accepted

You might also like