Compiler Design Lab
Compiler Design Lab
TRISHAL, MYMENSINGH
LAB REPORT ON
Compiler Design Lab
Course code: CSE-324
Submitted To
Subrata Kumar Das
Assistant Professor
Department of Computer Science and Engineering
Jatiya Kabi Kazi Nazrul Islam University,
Trishal,Mymensingh. Submitted By
Effat Tazmil Dity
Roll: 14102010
Reg:3389
Session : 2013-14
Dept. of CSE,JKKNIU
0
Index
1 Design a DFA that accepts all and only the string of 0's and 1's that have 2
the sequence 01 somewhere in the string.
2 Design a DFA to accept the language l={w | w has both an even number 3
of 0's and an even number of 1's}
3 Design a NFA that will accept the string only when input is end with 4
abb.
8 Write a program which will count and find all the keywords used in a 13
program.
10 Write a C program that will find out the prefix, suffix, substring and 16
subsequence from a given string.
1
Lab Report No: 01
Experiment Name: Design a DFA that accepts all and only the string of 0's and 1's that
have the sequence 01 somewhere in the string.
Program code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
char str[100];
int tt[3][2]={{1,0},{1,2},{2,2}}; //Transition table
int j=0,state=0,check=1,i;
for(i=0;i<strlen(str);i++){
if(str[i]=='0')
state=tt[state][0];
else if(str[i]=='1')
state=tt[state][1];
else
check=0;
}
Output:
Enter any string: 101000110
The string [ 101000110 ] is accepted.
2
Lab Report No: 02
Experiment Name: Design a DFA to accept the language l={w|w has both an even
numbers of 0's and an even numbers of 1's}
Program code:
#include<stdio.h>
#include<string.h>
int main(){
char str[100];
int tt[4][2]={{1,3},{0,2},{3,1},{2,0}}; //Transition table
int j=0,state=0,check=1,i;
printf("\n Enter any string: ");
gets(str);
for(i=0;i<strlen(str);i++){
if(str[i]=='0')
state=tt[state][0];
else if(str[i]=='1')
state=tt[state][1];
else
check=0;
}
if(state==0 && check==1)
printf("\n The string [ %s ] is accepted.",str);
else
printf("\n The string [ %s ] is not accepted.",str);
return 0;
}
Output:
3
Lab Report No: 03
Experiment Name: Design a NFA that accepts the strings consisting of a and b only and
it will accepts when the input is abb in the end.
Program code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main(){
int i=0;
int state[2],test=1;
char symbol[]={'a','b'},str[30];
char *table[5][3]={
{"-","a","b"},
{"1","12","1"},
{"2","-","3"},
{"3","-","4"},
{"4","-","-"} };
printf("\n Enter input string : ");
scanf("%s",str);
state[0]=1;
state[1]=1;
while(str[i]!='\0'){
if(str[i]==symbol[0]) {
state[0]=table[1][1][0]-'0'; // Getting number(state) from characters // ie, '1'-'0' = 1
state[1]=table[1][1][1]-'0';
}
else if(str[i]==symbol[1]) {
state[0]=atoi(table[state[0]][2]);
state[1]=atoi(table[state[1]][2]);
}
else
test=0;
i++;
}
if((state[0]==4 || state[1]==4) && test==1)
printf("\n The string [ %s ] is accepted.",str);
else
printf("\n The string [ %s ] is not accepted.",str);
return 0;
}
Output:
Enter input string : abaababb
The string [ abaababb ] is accepted.
4
Lab Report No: 04
Experiment Name: Write a program in C which will eliminates white spaces and
comments from a source code.
Program code:
#include<stdio.h>
int main(){
char c,ch[100];
FILE *f;
f=fopen("FILE/sourceCode.c","r");
c=getc(f);
while(c!=EOF){
int i=0;
if(c=='/'){
ch[i]=c;
i++;
c=getc(f);
if(c=='*'){
ch[i]=c;
i++;
sst:
c=getc(f);
while(c!='/'){
ch[i]=c;
i++;
c=getc(f);
}
if(ch[i-1]=='*'){
c=getc(f);
if(c=='\n')
c=getc(f);
}
else goto sst;
}
else if(c=='/'){
c=getc(f);
while(c!='\n'){
ch[i]=c;
i++;
c=getc(f);
}
}
else{
5
printf("%c%c",ch[0],c);
}
}
else if(c=='\n'){
printf("\n");
c=getc(f);
}
else if ( c == ' '){
c=getc(f);
}
else{
printf("%c",c);
c=getc(f);
}
}
fclose(f);
return 0;
}
Output:
#include<stdio.h>
intmain()
{
printf("\nCompilerDesignLabProgram");
printf("WhichCanEliminateWhiteSpaces");
printf("AndCommentsFromASourceCode");
return0;
}
6
Lab Report No: 05
Experiment Name: Write a program in C which will eliminates white spaces and
comments from a source code.
Program code:
#include<stdio.h>
#include <ctype.h>
#define SIZE 50 /* Size of Stack */
char [SIZE];
int top = -1; /* Global declarations */
7
pofx[k++] = pop();
elem = pop(); /* Remove ( */
}
else{ /* Operator */
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
pofx[k++] = pop();
pofx[k] = '\0'; /* Make pofx as valid string */
printf("\n\n Given Infix Expression: %s \n \n Postfix Expression:
%s\n", infx, pofx);
return 0;
}
Output:
8
Lab Report No: 06
Experiment Name: Design an e-NFA that accepts decimal numbers consisting of:
a.An optional + or _ sign
b.A string of digits
c.A decimal point and
d.Another string of digits.
Program code:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int main(){
char str[100];
int ts[6][4]={{1,1,-1,-1},{-1,-1,14,2},{-1,-1,3,-1},{-1,5,3,-1},{-1,-1,-1,3},{-1,-1,-1,-1}};
int state[100],check=1,i,flag=0,k=0,j,m=0,s=0;
for(i=0;i<100;i++)
state[i]=-2;
state[0]=0;
printf("\n Enter a decimal number: ");
gets(str);
i=0;
for(i=0;i<strlen(str);i++){
for(j=0;j<=m;j++){
if(str[i]!='+'&&str[i]!='-'&&!isdigit(str[i])&&str[i]!='.'){ //invalid decimal number
printf("\n The number is not accepted.\n\n");
exit(0);
}
else if((str[i]=='+'||str[i]=='-')){ //having optional + or - sign at the beginning
state[j]=ts[state[j]][0];
}
else if(state[j]==0){ //transition with epsilion input
i--;
state[j]=ts[state[j]][1];
}
else if(isdigit(str[i])&&state[j]==1){ //in state 1 two transition for a digit
state[j]=1;
state[m+1]=4;
m++;
}
else if(isdigit(str[i])){ //rather state 1;one transition for a digit
state[j]=ts[state[j]][2];
}
9
else if(str[i]=='.'){ //on input '.'
state[j]=ts[state[j]][3];
flag=1;
}
else
check=0;
}
}
for(i=0;i<m;i++){
if(state[i]==3){
printf("\n The number is accepted.\n\n");
exit(0);
}
}
printf("\n The number is not accepted.\n\n"); //invalid decimal number
return 0;
}
Output:
10
Lab Report No: 07
Experiment Name: Write a program for recognizing all the keywords of a C language.
Program code:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
//Create user define pointer array for all keywords
char *key[]={"auto","break","case","char","const","continue","default","do","double",
"else","enum","extern","float","for","goto","if","int","long","register",
"return","short","signed","sizeof","static","struct","switch","typedef",
"union","unsigned","void","volatile","while","\0"};
int main(){
char c,buf[100];
FILE *f;
f=fopen("FILE/findKeyword.c","r"); //Take input from file
c=getc(f); //Take only one character
printf("\n Recognized Keywords: \n");
while(c!=EOF){
int i=0;
if(isalpha(c)){ //Check the character is alphabetic
buf[i]=c;i++;
c=getc(f);
while(isalpha(c)){
buf[i]=c;
c=getc(f);
i++;
}
buf[i]='\0';i=0;
while(*(key+i)!='\0'){ //Check the keyword is not null
if(strcmp(*(key+i),buf)==0){ //Compare the keyword with single character
printf("\n %s",buf); //Print the keyword and break
break;
}
i++;
}
}
c=getc(f);
}
fclose(f);//Close file
return 0;
}
11
Output:
Recognized Keywords:
int
int
for
if
break
if
else
return
12
Lab Report No: 08
Experiment Name: Write a program which will count and find all the keywords used in a
program.
Program code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
char key[100];
char
keyword[32][15]={"auto","break","case","char","const","continue","default","do","double","e
lse",
"enum","extern","float","for","goto","if","int","long","register","return","short",
"signed","sizeof","static","struct","switch","typedef","union","unsigned","void",
"volatile","while"};
int is_keyword(){
int i=0;
for(i=0;i<32;i++){
if(strcmp(key,keyword[i])==0){
return 1;
}
}
return 0;
}
int main(){
FILE *fp;
char ch,c,fl[100];
int i=0,j,k=1;
int b;
fp=fopen("FILE/countKeyword.c","r");
printf("\nThe input file is:\n---------------------------\n");
while((ch=getc(fp))!=EOF){
putchar(ch); //Printing the source program
}
fclose(fp);
13
}
else{
key[i]='\0';
b=is_keyword();
if(b==1)
printf("\n (%d) : %s\n",k++,key);
i=0;
}
}
fclose(fp);
//End of recognizing the keywords from the source program
return 0;
}
Output:
The input file is:
#include <stdio.h>
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i) {
if(n%i==0) {
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
The output is:
Keywords:
(1) : int
(2) : int
(3) : for
(4) : if
(5) : break
(6) : if
(7) : else
(8) : return
14
Lab Report No: 09
Experiment Name: Define a DFA that will accept valid identifier.
Program code:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main(){
char str[100];
int ts[3][2]={{1,2},{1,2},{2,2}};
int j=0,state=0,check=1,i;
printf("\n Enter a string: ");
gets(str);
for(i=0;i<strlen(str);i++){
if(state==1){ //checking after entering into final state
if(isalpha(str[i])||isdigit(str[i])||str[i]=='_') /*at final state only character,digit and
underscore is accepted*/
state=ts[state][0];
else
state=ts[state][1];
}
else if(isalpha(str[i])||str[i]=='_')
state=ts[state][0];
else
state=ts[state][1];
}
if(state==1)
printf("\n The string [ %s ] is accepted.",str);
else
printf("\n The string [ %s ] is not accepted.",str);
return 0;
}
Output:
15
Lab Report No: 10
Experiment Name: Write a C program that will find out the prefix, suffix, substring and
subsequence from a given string.
Program code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main(){
char str[100],substr[100];
int i,j,flag=0;
printf("\n Enter a string: ");
gets(str);
printf("\n Enter a part of the main string: ");
gets(substr);
//start of finding prefix
for(i=0;i<strlen(str);i++){
if(str[i]!=substr[i])
break;
}
if(i==strlen(substr)){
printf("\n\"%s\" is a prefix of \"%s\"\n",substr,str);
flag=1;
}
//end of finding prefix
//start of finding suffix
j=strlen(substr)-1;
for(i=strlen(str)-1;i>=0;i--){
if(str[i]!=substr[j]||j==-1)
break;
j--;
}
if(j==-1){
printf("\n\n\"%s\" is a suffix of \"%s\"\n",substr,str);
flag=1;
}
//end of finding sffix
16
else{
j++;
if(j==strlen(substr))
break;
}
}
if(j==strlen(substr)){
printf("\n\"%s\" is a substring of \"%s\"\n",substr,str);
flag=1;
}
//end of finding substring
if(j==strlen(substr)){
printf("\n\"%s\" is a subsequence of \"%s\"\n",substr,str);
flag=1;
}
//end of finding subsequence
return 0;
}
Output:
Enter a string: banana
Enter a part of the main string: ana
17