J.N.T.U.H. College of Engineering: Certificate

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

Page |1

J.N.T.U.H. COLLEGE OF ENGINEERING

KUKATPALLY, HYDERABAD – 500 085

CERTIFICATE
This is to certify that ______________ of B.Tech III year I Semester
bearing the Hall-Ticket number ___________ has fulfilled his/her
COMPILER DESIGN LAB record for the academic year 2018-2019.

Signature of the Head of the Department Signature of the staff member

Date of Examination_________________________
Internal Examiner External Examiner

JNTUHCEH
Page |2

Sno. Name of the experiment Date Page no. Sign

1. Design a DFA to accept all 29/06/18 3


strings containing substring
(01)

2. Write a LEX program to scan 06/07/18 5


keywords and identifiers of C

3. Write a LEX program to scan 06/07/18 6


integers and float numbers of C

4. LEX Program to count number 13/07/18 7


of lines, words and characters
in a given string:

5. Write a LEX program to count 20/07/18 8


number of consonants and
vowels

6. Write a C Program to 03/08/18 9


implement predictive parsing

(First and follow functions)

7. Write a C program to 31/08/18 14


implement RD parser for the
grammar
S → AB, A → a/є, B→b/є

8. YACC Program to recognise the 28/09/18 16


grammar anb

9. YACC Program a valid variable 05/10/18 18


that starts with a letter
followed by any number of
digits or alphabets

10. YACC Program to parse a string 12/10/18 20


involving operators i.e. +,/,*,-

JNTUHCEH
Page |3

1. C program to implement DFA to accept all strings having a substring 01

PROGRAM:

#include<stdio.h>
void A(char a[],int i);
void B(char a[],int i);
void C(char a[],int i);
void A(char a[],int i)
{
printf("%c",a[i]);
if(a[i]=='0')
{
i++;
B(a,i);
}
else if(a[i]=='1')
{
i++;
A(a,i);
}
else
{
printf("Error");
}
}
void B(char a[],int i)
{
if(a[i]=='0')
{
i++;
B(a,i);
}
else if(a[i]=='1')
{
i++;
C(a,i);
}
else
{
printf("Error");

JNTUHCEH
Page |4

}
}
void C(char a[],int i)
{
if(a[i]=='0')
{
i++;
C(a,i);
}
else if(a[i]=='1')
{
i++;
C(a,i);
}
else if(a[i]=='\0')
{
printf("String Accepted");
}
else
{
printf("Error");
}
}
void main()
{
char a[20];
printf("Enter a string containing 0's and 1's: ");
scanf("%s",a);
A(a,0);
}

OUTPUT:
Enter a string containing 0's and 1's: 11111
Error

Enter a string containing 0's and 1's: 100011


String Accepted

Enter a string containing 0's and 1's: 10110101


String Accepted

JNTUHCEH
Page |5

2. Lex Program to Scan All Reserved Words and Identifiers of C Language.

PROGRAM:
%{
char c='a';
%}
%%
int|if|char|else|float|double|while|for|goto|break|continue|auto|const|short
|struct|unsigned|switch|unsigned|switch|void|long|case|default|enum|register
|sizeof|typedef|volatile|do|extern|static|union|return c='k';
[_|a-z]([A-Z]|[a-z]|[0-9]|_)* c='i';
.* c='v';
%%

main()
{
printf("Enter string: ");
yylex();
if(c=='k')
printf("Keyword\n");
else if(c=='i')
printf("Identifier\n");
else
printf("Invalid\n");
}

OUTPUT:

Enter String: _ab


Identifier
Enter String : struct
Keyword
Enter String: struc.t
Invalid

JNTUHCEH
Page |6

3. LEX Program to scan float and int numbers in C

PROGRAM:
%{
int i=0;
}%

%%
[0-9]+ i=1;
[0-9]* "." [0-9]+ i=2;
%%

main()
{
yylex();
if(i==1)
printf("Integer");
else if(i==2)
printf("Float");
else
printf("Error: Neither Integer Not Float");
}

OUTPUT:
Lex int.l
cc lex.yy.c-ll
./a.out
32.6
Float
24
Integer

JNTUHCEH
Page |7

4. LEX Program to count number of lines, words and characters in a given string:

PROGRAM:
%{
int cc=0;
int lc=0;
int wc=0;
int bc=0;
%}
word [^ \t\n]+
col[\n]
blank[ ]
%%
{blank} bc++;
{word} {wc++;cc+=yyleng;}
{col} lc++;
%%

main()
{
printf("Enter a string: ");
yylex();
printf("\nWords: %d, Characters: %d, Lines: %d, Blanks: %d”,wc,cc,lc,bc);
}

OUTPUT:
lex new.l
cc lex.yy.c
./a.out

Enter a string: We all study in JNTUH

Words: 5, Characters: 17, Lines: 1. Blanks: 4

JNTUHCEH
Page |8

5. LEX Program to count number of consonants and vowels in a given string:

PROGRAM:
%{
int v=0;
int c=0;
%}

%%
[ \t\n]+
[aeiouAEIOU] v++;
[a-zA-Z] c++;
%%

main()
{
printf("Enter a string: ");
yylex();
printf("\nNumber of vowels=%d",v);
printf("Number of consonants=%d",c);
}

OUTPUT:
lex new.l
cc lex.yy.c
./a.out

Enter a string: We all study in JNTUH


Number of vowels=5
Number of consonants=12

JNTUHCEH
Page |9

6. C Program to implement predictive parsing.

PROGRAM:

#include<stdio.h>
#include<string.h>

char*t,*f,*a,*b,*e,*ps;

void printSet(char*c)
{
int i;
printf(" {%c",c[0]);
for(i=1;i<strlen(c);i++)
printf(", %c",c[i]);
printf("}");
}

int isTerminal(char c)
{
if((c>32 && c<65) || (c>90 && c<126))
return 1;
return 0;
}

void initProds(char*T,char*E,char*F,char*A,char*B)
{
strcpy(t,T);
strcpy(e,E);
strcpy(a,A);
strcpy(b,B);
strcpy(f,F);
strcpy(ps,"FABTE");
}

JNTUHCEH
P a g e | 10

char*findProdOf(char c)
{
switch(c)
{
case 'T':
return t;
case 'A':
return a;
case 'B':
return b;
case 'E':
return e;
case 'F':
return f;
}
return "";
}

char*First(char*NT)
{
int i,j=0;
char*a;
strcpy(a,"");
if(isTerminal(NT[0])==1)
{
a[j]=NT[0];
a[j+1]='\0';
j++;
}
else
strcat(a,First(findProdOf(NT[0])));
for(i=1;i<strlen(NT);i++)
{
if(NT[i]=='/')
{
if(isTerminal(NT[i+1])==1)
{
a[j]=NT[i+1];
a[j+1]='\0';
j++;
}

JNTUHCEH
P a g e | 11

else
strcat(a,First(findProdOf(NT[i+1])));
}
}
return a;
}

char*Follow(char t)
{
int i,j,k=0,l;
char *currProd,*ans;
strcpy(ans,"");
if(t=='T')
{
strcat(ans,'$');
k++;
}
for(i=0;i<strlen();i++)
{
if(ps[i]!=t)
{
strcpy(currProd,prodOf(ps[i]));
for(j=0;j<strlen(currProd);j++)
{
if(currProd[j]==t && currProd[j+1]!=ps[i])
{
if(currProd[j+1]=='\0' || currProd[j+1]=='/')
{
strcat(ans,Follow(ps[i]));
k=strlen(ans);
}
else if(isTerminal(currProd[j+1]==1))
{
ans[k]=currProd[j+1];
ans[k+1]='\0';
k++;
}
else if(isTerminal(currProd[j+1]==0))
{
strcat(ans,First(prodOf(currProd[j+1])));
strcat(ans,Follow(ps[i]));

JNTUHCEH
P a g e | 12

k=strlen(ans);
}
}
}
}
}
k=0;
for(i=0;i<strlen(ans);i++)
{
if(ans[i]!='@')
{
fol[k]=ans[i];
k++;
}
}
fol[k]='\0';
return fol;
}

void main()
{
char *T,*E,*A,*B,*F;
clrscr();
strcpy(E,"TA");
strcpy(T,"FB");
strcpy(A,"+TA/@");
strcpy(B,"*FB/@");
strcpy(F,"(E)/i");
initProds(T,E,F,A,B);
printf("\nE->%s",E);
printf("\nFirst(E)=");printSet(First(E));
printf("\nFollow(E)=");printSet(Follow(E));
printf("\nA->%s",A);
printf("\nFirst(A)=");printSet(First(A));
printf("\nFollow(A)=");printSet(Follow(A));
printf("\nT->%s",T);
printf("\nFirst(T)=");printSet(First(T));
printf("\nFollow(T)=");printSet(Follow(T));
printf("\nB->%s",B);
printf("\nFirst(B)=");printSet(First(B));
printf("\nFollow(B)=");printSet(Follow(B));

JNTUHCEH
P a g e | 13

printf("\nF->%s",F);
printf("\nFirst(F)=");printSet(First(F));
printf("\nFollow(F)=");printSet(Follow(F));
getch();
}

OUTPUT:

E->TA
First(E)= { c, i}
Follow(E)= { ), $}

A->+TA/@
First(A)= { +, @}
Follow(A)= { $, )}

T->FB
First(T)= { (, i}
Follow(T)= { ), $, +}

B->*FB/@
First(B)= { *, @}
Follow(B)= { ), $, +}

F-> (E)/I
First(F)= { (, i}
Follow(F)= { ), $, +, *}

JNTUHCEH
P a g e | 14

7. C Program to implement RD parser for a Grammar.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int i=0,a=0;
char str[30];
void S();
void A();
void B();
void main()
{
clrscr();
printf("Enter the string:");
gets(str);
S();
if((i==99)&&(str[a]=='\0'))
printf("\n string is accepted");
else if((i==3)||(str[a]!='\0'))
printf("\n string is not accepted");
getch();
}
void S()
{
A();
B();
}
void A()
{
if(str[0]=='a')
{ i++; a++; }
else
i=0;
}
void B()
{
if((str[i]=='b')||(str[i]=='\0'))
{
i=99;

JNTUHCEH
P a g e | 15

a++;
}
else i=3;

OUTPUT:
Enter a string: b
String Accepted

Enter a string: ab
String Accepted

Enter a string: ca
String not Accepted

JNTUHCEH
P a g e | 16

8. YACC Program to recognise the grammar anb:

PROGRAM:
/*Lex Part stored in new.l*/

%{
#include"y.tab.h"
%}

%%
[a] {return a1;}
[b] {return b1;}
%%

/*Yacc part stored in new.y*/


%{
#include<stdio.h>
int count=10;
%}
%token a1 b1

%%
st:s{printf("valid\n");}
;
s:a1 rab b1
;
rab:rab a1
|
;
%%

main()
{
yyparse();
}
yyerror()
{
printf("Invalid");
}

JNTUHCEH
P a g e | 17

OUTPUT:
Lex new.l
Yacc –d new.y
cc lex.yy.c y.tab.h –ll
./a.out

aaab
valid

abba
Invalid

JNTUHCEH
P a g e | 18

9. Yacc Program a valid variable that starts with a letter followed by any
number of digits or alphabets:

PROGRAM:
/* Lex Part stored in new.l*/
%{
#include"y.tab.h"
%}
%%
[a-z] {return alp;}
[0-9] {return dig;}
. {return yytext[0];}
%%

/*Yacc Part stored in new.y*/


%{
#include<stdio.h>
extern int yyparse();
extern int yylex();
%}
%token dig alp
%%
st:id {printf("Valid\n"); exit(0);}
;
id:id dig
|id alp
|alp
;
%%

main()
{
yyparse();
}
yyerror()
{
printf("Invalid\n");
}

JNTUHCEH
P a g e | 19

OUTPUT:

Lex new.l
Yacc –d new.y
cc lex.yy.c y.tab.h –ll
./a.out

a2432434
Valid

df232334
Valid

44556dd
Invalid

JNTUHCEH
P a g e | 20

10. Yacc Program to parse a string involving operators i.e. +,/,*,-

PROGRAM:

/* Lex Part stored in new.lex*/

%{
#include "y.tab.h"
%}

%%
[a-z0-9]+ {return num;}
[+/] {return op;}
[*-] {return op1;}
%%

/*Yacc part stored in new.y*/

%{
#include<stdio.h>
extern int yyparse();
extern int yylex();
%}
%token num op op1

%%
st:id {printf("Valid\n"); exit(0);}
;
id:id op num
|id op1 num
|num
;
%%
main()
{
yyparse();
}
yyerror()
{
printf("Invalid\n");
}

JNTUHCEH
P a g e | 21

OUTPUT:
Lex new.l
Yacc –d new.y
cc lex.yy.c y.tab.h –ll
./a.out

a+b
valid

ab*
Invalid

A$b
Invalid

A+b-c*8
valid

JNTUHCEH

You might also like