J.N.T.U.H. College of Engineering: Certificate
J.N.T.U.H. College of Engineering: Certificate
J.N.T.U.H. College of Engineering: Certificate
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.
Date of Examination_________________________
Internal Examiner External Examiner
JNTUHCEH
Page |2
JNTUHCEH
Page |3
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
JNTUHCEH
Page |5
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:
JNTUHCEH
Page |6
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
JNTUHCEH
Page |8
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
JNTUHCEH
Page |9
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
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
PROGRAM:
/*Lex Part stored in new.l*/
%{
#include"y.tab.h"
%}
%%
[a] {return a1;}
[b] {return 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];}
%%
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
PROGRAM:
%{
#include "y.tab.h"
%}
%%
[a-z0-9]+ {return num;}
[+/] {return op;}
[*-] {return op1;}
%%
%{
#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