You are on page 1of 47

Sr.

Page
No. Practical Title Date No. Sign
1. Write a Program for Lexical analyzer which
takes any text file as input and find
identifier, constant, operator from input
file.
2. Write a program to remove the left recursion
from any given grammar.
3. Write a program to remove the left factor from
any given grammar.
4. Write a program to find first and follow set
from any given grammar.
5. Write a program using C (or C++) for LL(1)
Parsing.
6. Write a program using C (or C++) for Operator
Precedence parsing.
7. Write a program using C (or C++) for SLR
parsing.
8. Write a program using C (or C++) for LALR
parsing.
9. write a program using C(or C++) for LALR
parsing.

a) Write a Lex program which takes input from


text file and count no of characters. no.of lines
& no. of words.

b) Write a Lex program to implement


calculator.
10. Write a LEX program to count the number of
comment lines in a given C program.Also
eliminate them and copy that program into
separate file.

INDEX

Compiler Design (2170701)


(150180107002)
Practical 1
Aim : Write a Program for Lexical analyzer which takes any text file as input
and find identifier, constant, operator from input file.
Program :
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
using namespace std;

int isKeyword(char buffer[]){


char keywords[32][10] =
{"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"};
int i, flag = 0;

for(i = 0; i < 32; ++i){


if(strcmp(keywords[i], buffer) == 0){
flag = 1;
break;
} }

Compiler Design (2170701)


(150180107002)
return flag;
}

int main(){
char ch, buffer[15], operators[] = "+-*/%=";
ifstream fin("program.txt");
int i,j=0;

if(!fin.is_open()){
cout<<"error while opening the file\n";
exit(0);
}

while(!fin.eof()){
ch = fin.get();

for(i = 0; i < 6; ++i){


if(ch == operators[i])
cout<<ch<<" is operator\n";
}

if(isalnum(ch)){
buffer[j++] = ch;
}

Compiler Design (2170701)


(150180107002)
else if((ch == ' ' || ch == '\n') && (j != 0)){
buffer[j] = '\0';
j = 0;

if(isKeyword(buffer) == 1)
cout<<buffer<<" is keyword\n";
else
cout<<buffer<<" is indentifier\n";
}

fin.close();
return 0;
}

Compiler Design (2170701)


(150180107002)
Practical 2
Aim : Write a program to remove the left recursion from any given
grammar.
Program :
#include<stdio.h>
#include<string.h>
#define MAX 10

int main()
{
char nonterminal;
char beta,alpha;
int n;
char production[10][MAX];
int index=3; //Starting of String
int i;

printf("Enter no of production:");
scanf("%d",&n);

printf("Enter the grammer E->E-A :\n");

for(i=0;i<n;i++)
{

Compiler Design (2170701)


(150180107002)
scanf("%s",production[i]);
}

for(i=0;i<n;i++)
{
printf("\nGrammer : : %s",production[i]);
nonterminal=production[i][0];
if(nonterminal==production[i][index])
{
alpha=production[i][index+1];
printf("\nIt's left recursive...");
while(production[i][index]!=0 &&
production[i][index]!='|')
index++;

if(production[i][index]!=0)
{
beta=production[i][index+1];
printf("\nGrammer without left recursion ..\n");
printf("\n%c-
>%c%c\'",nonterminal,beta,nonterminal);
printf("\n%c\'-
>%c%c\'|E\n",nonterminal,alpha,nonterminal);
}
else

Compiler Design (2170701)


(150180107002)
printf("can't be reduced..");
}
else
printf("It's not left recursive..");
index=3;
}
}

Compiler Design (2170701)


(150180107002)
Practical 3
Aim : Write a program to remove the left factor from any given grammar.
Program :
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],a1[10],a2[10],a3[10],a4[10],a5[10];
int i,j=0,k,l;
clrscr();
printf("enter any productions A->");
gets(a);

for(i=0;a[i]!='/';i++,j++)
a1[j]=a[i];
a1[j]='\0';
for(j=++i,i=0;a[j]!='\0';j++,i++)
a2[i]=a[j];
a2[i]='\0';
k=0;
l=0;

Compiler Design (2170701)


(150180107002)
for(i=0;i<strlen(a1)||i<strlen(a2);i++)
{
if(a1[i]==a2[i])
{
a3[k]=a1[i];
k++;
}
else {
a4[l]=a1[i];
a5[l]=a2[i];
l++;
}
}

a3[k]='X';
a3[++k]='\0';
a4[l]='/';
a5[l]='\0';
a4[++l]='\0';
strcat(a4,a5);
printf("\n A->%s",a3);
printf("\n X->%s",a4);
getch();
}

Compiler Design (2170701)


(150180107002)
Practical 4
Aim : Write a program to find first and follow set from any given grammar.
Program :
#include<stdio.h>
#include<string.h>

int i,j,l,m,n=0,o,p,nv,z=0,x=0;
char str[10],temp,temp2[10],temp3[20],*ptr;

struct prod
{
char lhs[10],rhs[10][10],ft[10],fol[10];
int n;
}pro[10];

void findter()
{
int k,t;
for(k=0;k<n;k++)
{
if(temp==pro[k].lhs[0])
{
for(t=0;t<pro[k].n;t++)
{

Compiler Design (2170701)


(150180107002)
if( pro[k].rhs[t][0]<65 || pro[k].rhs[t][0]>90 )
pro[i].ft[strlen(pro[i].ft)]=pro[k].rhs[t][0];
else if( pro[k].rhs[t][0]>=65 && pro[k].rhs[t][0]<=90 )
{
temp=pro[k].rhs[t][0];
if(temp=='S')
pro[i].ft[strlen(pro[i].ft)]='#';
findter();
}
}
break;
}
}
}

void findfol()
{
int k,t,p1,o1,chk;
char *ptr1;
for(k=0;k<n;k++)
{
chk=0;
for(t=0;t<pro[k].n;t++)
{

Compiler Design (2170701)


(150180107002)
ptr1=strchr(pro[k].rhs[t],temp);
if( ptr1 )
{
p1=ptr1-pro[k].rhs[t];
if(pro[k].rhs[t][p1+1]>=65 && pro[k].rhs[t][p1+1]<=90)
{
for(o1=0;o1<n;o1++)
if(pro[o1].lhs[0]==pro[k].rhs[t][p1+1])
{
strcat(pro[i].fol,pro[o1].ft);
chk++;
}
}
else if(pro[k].rhs[t][p1+1]=='\0')
{
temp=pro[k].lhs[0];
if(pro[l].rhs[j][p]==temp)
continue;
if(temp=='S')
strcat(pro[i].fol,"$");
findfol();
chk++;
}
else

Compiler Design (2170701)


(150180107002)
{
pro[i].fol[strlen(pro[i].fol)]=pro[k].rhs[t][p1+1];
chk++;
}
}
}
if(chk>0)
break;
}
}

int main()
{
FILE *f;
//clrscr();

for(i=0;i<10;i++)
pro[i].n=0;

f=fopen("tab5.txt","r");
while(!feof(f))
{
fscanf(f,"%s",pro[n].lhs);
if(n>0)

Compiler Design (2170701)


(150180107002)
{
if( strcmp(pro[n].lhs,pro[n-1].lhs) == 0 )
{
pro[n].lhs[0]='\0';
fscanf(f,"%s",pro[n-1].rhs[pro[n-1].n]);
pro[n-1].n++;
continue;
}
}
fscanf(f,"%s",pro[n].rhs[pro[n].n]);
pro[n].n++;
n++;
}

printf("\n\nTHE GRAMMAR IS AS FOLLOWS\n\n");


for(i=0;i<n;i++)
for(j=0;j<pro[i].n;j++)
printf("%s -> %s\n",pro[i].lhs,pro[i].rhs[j]);

pro[0].ft[0]='#';
for(i=0;i<n;i++)
{
for(j=0;j<pro[i].n;j++)
{

Compiler Design (2170701)


(150180107002)
if( pro[i].rhs[j][0]<65 || pro[i].rhs[j][0]>90 )
{
pro[i].ft[strlen(pro[i].ft)]=pro[i].rhs[j][0];
}
else if( pro[i].rhs[j][0]>=65 && pro[i].rhs[j][0]<=90 )
{
temp=pro[i].rhs[j][0];
if(temp=='S')
pro[i].ft[strlen(pro[i].ft)]='#';
findter();
}
}
}

printf("\n\nFIRST\n");
for(i=0;i<n;i++)
{
printf("\n%s -> ",pro[i].lhs);
for(j=0;j<strlen(pro[i].ft);j++)
{
for(l=j-1;l>=0;l--)
if(pro[i].ft[l]==pro[i].ft[j])
break;
if(l==-1)

Compiler Design (2170701)


(150180107002)
printf("%c",pro[i].ft[j]);
}
}

for(i=0;i<n;i++)
temp2[i]=pro[i].lhs[0];
pro[0].fol[0]='$';
for(i=0;i<n;i++)
{
for(l=0;l<n;l++)
{
for(j=0;j<pro[i].n;j++)
{
ptr=strchr(pro[l].rhs[j],temp2[i]);
if( ptr )
{
p=ptr-pro[l].rhs[j];
if(pro[l].rhs[j][p+1]>=65 && pro[l].rhs[j][p+1]<=90)
{
for(o=0;o<n;o++)
if(pro[o].lhs[0]==pro[l].rhs[j][p+1])
strcat(pro[i].fol,pro[o].ft);
}
else if(pro[l].rhs[j][p+1]=='\0')

Compiler Design (2170701)


(150180107002)
{
temp=pro[l].lhs[0];
if(pro[l].rhs[j][p]==temp)
continue;
if(temp=='S')
strcat(pro[i].fol,"$");
findfol();
}
else
pro[i].fol[strlen(pro[i].fol)]=pro[l].rhs[j][p+1];
}
}
}
}

printf("\n\nFOLLOW\n");
for(i=0;i<n;i++)
{
printf("\n%s -> ",pro[i].lhs);
for(j=0;j<strlen(pro[i].fol);j++)
{
for(l=j-1;l>=0;l--)
if(pro[i].fol[l]==pro[i].fol[j])
break;

Compiler Design (2170701)


(150180107002)
if(l==-1)
printf("%c",pro[i].fol[j]);
}
}
printf("\n");
//getch();
}

Compiler Design (2170701)


(150180107002)
Practical 5
Aim : Write a program using C (or C++) for LL(1) Parsing.
Program :
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void main()
{
clrscr();
int i=0,j=0,k=0,m=0,n=0,o=0,o1=0,var=0,l=0,f=0,c=0,f1=0;
char str[30],str1[40]="E",temp[20],temp1[20],temp2[20],tt[20],t3[20];
strcpy(temp1,'\0');
strcpy(temp2,'\0');
char t[10];
char array[6][5][10] = {
"NT", "<id>","+","*",";",
"E", "Te","Error","Error","Error",
"e", "Error","+Te","Error","\0",
"T", "Vt","Error","Error","Error",
"t", "Error","\0","*Vt","\0",
"V", "<id>","Error","Error","Error"
};

Compiler Design (2170701)


(150180107002)
cout << "\n\tLL(1) PARSER TABLE \n";
for(i=0;i<6;i++)
{
for(j=0;j<5;j++)
{
cout.setf(ios::right);
cout.width(10);
cout<<array[i][j];
}
cout<<endl;
}
cout << endl;
cout << "\n\tENTER THE STRING :";
gets(str);
if(str[strlen(str)-1] != ';')
{
cout << "END OF STRING MARKER SHOULD BE ';'";
getch();
exit(1);
}
cout << "\n\tCHECKING VALIDATION OF THE STRING ";
cout <<"\n\t" << str1;
i=0;

Compiler Design (2170701)


(150180107002)
while(i<strlen(str))
{
again:
if(str[i] == ' ' && i<strlen(str))
{
cout << "\n\tSPACES IS NOT ALLOWED IN SOURSE STRING ";
getch();
exit(1);
}
temp[k]=str[i];
temp[k+1]='\0';
f1=0;
again1:
if(i>=strlen(str))
{
getch();
exit(1);
}
for(int l=1;l<=4;l++)
{
if(strcmp(temp,array[0][l])==0)
{
f1=1;
m=0,o=0,var=0,o1=0;

Compiler Design (2170701)


(150180107002)
strcpy(temp1,'\0');
strcpy(temp2,'\0');
int len=strlen(str1);
while(m<strlen(str1) && m<strlen(str))
{
if(str1[m]==str[m])
{
var=m+1;
temp2[o1]=str1[m];
m++;
o1++;
}
else
{
if((m+1)<strlen(str1))
{
m++;
temp1[o]=str1[m];
o++;
}
else
m++;
}

Compiler Design (2170701)


(150180107002)
}
temp2[o1] = '\0';
temp1[o] = '\0';
t[0] = str1[var];
t[1] = '\0';
for(n=1;n<=5;n++)
{
if(strcmp(array[n][0],t)==0)
break;
}
strcpy(str1,temp2);
strcat(str1,array[n][l]);
strcat(str1,temp1);
cout << "\n\t" <<str1;
getch();

if(strcmp(array[n][l],'\0')==0)
{
if(i==(strlen(str)-1))
{
int len=strlen(str1);
str1[len-1]='\0';
cout << "\n\t"<<str1;
cout << "\n\n\tENTERED STRING IS VALID";

Compiler Design (2170701)


(150180107002)
getch();
exit(1);
}
strcpy(temp1,'\0');
strcpy(temp2,'\0');
strcpy(t,'\0');
goto again1;
}
if(strcmp(array[n][l],"Error")==0)
{
cout << "\n\tERROR IN YOUR SOURCE STRING";
getch();
exit(1);
}
strcpy(tt,'\0');
strcpy(tt,array[n][l]);
strcpy(t3,'\0');
f=0;
for(c=0;c<strlen(tt);c++)
{
t3[c]=tt[c];
t3[c+1]='\0';
if(strcmp(t3,temp)==0)
{

Compiler Design (2170701)


(150180107002)
f=0;
break;
}
else
f=1;
}

if(f==0)
{
strcpy(temp,'\0');
strcpy(temp1,'\0');
strcpy(temp2,'\0');
strcpy(t,'\0');
i++;
k=0;
goto again;
}
else
{
strcpy(temp1,'\0');
strcpy(temp2,'\0');
strcpy(t,'\0');
goto again1;
}

Compiler Design (2170701)


(150180107002)
}
}
i++;
k++;
}
if(f1==0)
cout << "\nENTERED STRING IS INVALID";
else
cout << "\n\n\tENTERED STRING IS VALID";
getch();
}

Compiler Design (2170701)


(150180107002)
Practical 6
Aim : Write a program using C (or C++) for Operator Precedence parsing.
Program :
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,k,n,top=0,col,row;
clrscr();
for(i=0;i<10;i++)
{
stack[i]=NULL;
ip[i]=NULL;
for(j=0;j<10;j++)
{
opt[i][j][1]=NULL;
}
}
printf("Enter the no.of terminals :\n");
scanf("%d",&n);
printf("\nEnter the terminals :\n");
scanf("%s",&ter);
printf("\nEnter the table values :\n");
for(i=0;i<n;i++)
{

Compiler Design (2170701)


(150180107002)
for(j=0;j<n;j++)
{
printf("Enter the value for %c %c:",ter[i],ter[j]);
scanf("%s",opt[i][j]);
}
}
printf("\n**** OPERATOR PRECEDENCE TABLE ****\n");
for(i=0;i<n;i++)
{
printf("\t%c",ter[i]);
}
printf("\n");
for(i=0;i<n;i++){printf("\n%c",ter[i]);
for(j=0;j<n;j++){printf("\t%c",opt[i][j][0]);}}
stack[top]='$';
printf("\nEnter the input string:");
scanf("%s",ip);
i=0;
printf("\nSTACK\t\t\tINPUT STRING\t\t\tACTION\n");
printf("\n%s\t\t\t%s\t\t\t",stack,ip);
while(i<=strlen(ip))
{
for(k=0;k<n;k++)
{

Compiler Design (2170701)


(150180107002)
if(stack[top]==ter[k])
col=k;
if(ip[i]==ter[k])
row=k;
}
if((stack[top]=='$')&&(ip[i]=='$')){
printf("String is accepted\n");
break;}
else if((opt[col][row][0]=='<') ||(opt[col][row][0]=='='))
{ stack[++top]=opt[col][row][0];
stack[++top]=ip[i];
printf("Shift %c",ip[i]);
i++;
}
else{
if(opt[col][row][0]=='>')
{
while(stack[top]!='<'){--top;}
top=top-1;
printf("Reduce");
}
else
{
printf("\nString is not accepted");

Compiler Design (2170701)


(150180107002)
break;
}
}
printf("\n");
for(k=0;k<=top;k++)
{
printf("%c",stack[k]);
}
printf("\t\t\t");
for(k=i;k<strlen(ip);k++){
printf("%c",ip[k]);
}
printf("\t\t\t");
}
getch();
}

Compiler Design (2170701)


(150180107002)
Practical 7
Aim : Write a program using C (or C++) for SLR parsing.
Program :
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
void main(){
char table[20][20][20],ter[20],stack[20],ip[20],st1[20],pro[20][20],num;
int i,j,t,k,top=0,st,col,row,pop,np,no,len;
for(i=0;i<20;i++){
ter[i]=NULL;
stack[i]=NULL;
ip[i]=NULL;
st1[i]=NULL;
for(j=0;j<20;j++){
pro[i][j]=NULL;
for(k=0;k<20;k++)
{
table[i][j][k]=NULL;
}
}
}

Compiler Design (2170701)


(150180107002)
printf("Enter the no of productions:");
scanf("%d",&np);
printf("Enter the productions:");
for(i=0;i<np;i++)
{
scanf("%s",pro[i]);
}
printf("Enter the no.of states:");
scanf("%d",&st);
printf("Enter the states:");
scanf("%s",st1);
printf("Enter the no of terminals:");
scanf("%d",&t);
printf("Enter the terminals:");
scanf("%s",ter);
for(i=0;i<st;i++)
{
for(j=0;j<t;j++)
{
printf("\nEnter the value for %c %c:",st1[i],ter[j]);
scanf("%s",table[i][j]);
}
}
printf("\nSLR TABLE:\n");

Compiler Design (2170701)


(150180107002)
for(i=0;i<t;i++)
{
printf("\t%c",ter[i]);
}
for(i=0;i<st;i++)
{
printf("\n\n%c",st1[i]);
for(j=0;j<t;j++)
{
printf("\t%s",table[i][j]);
}
}

stack[top]='a';
printf("\nEnter the input string:");
scanf("%s",ip);
i=0;
printf("\n\nSTACK\t\tINPUT STRING\t\tACTION\n");
printf("\n%s\t\t%s\t\t",stack,ip);
while(i<=strlen(ip) )
{
for(j=0;j<st;j++)
{
if(stack[top]==st1[j])

Compiler Design (2170701)


(150180107002)
col=j;
}
for(j=0;j<t;j++)
{
if(ip[i]==ter[j])
{
row=j;
}
}
if((stack[top]=='b')&&(ip[i]=='$')){printf("\nString accepted");break;}
else if(table[col][row][0]=='s')
{
top++;
stack[top]=ter[row];
top++;
stack[top]=table[col][row][1];
i++;
printf("Shift %c %d\n",ter[row],table[col][row][1]);
}
else if(table[col][row][0]=='r')
{
no=(int)table[col][row][1];
no=no-48;
len=strlen(pro[no]);

Compiler Design (2170701)


(150180107002)
len=len-3;
pop=2*len;
printf("POP %d",pop);
for(j=0;j<pop;j++)
{
top=top-1;
}
top++;
stack[top]=pro[no][0];
k=top;
k=k-1;
printf(" Push [%c,",pro[no][0]);
for(j=0;j<st;j++)
{
if(stack[k]==st1[j])
{
col=j;
}
}
k++;
for(j=0;j<t;j++)
{
if(stack[k]==ter[j])
{

Compiler Design (2170701)


(150180107002)
row=j;
}
}
top++;
stack[top]=table[col][row][0];
printf("%c]\n",table[col][row][0]);
}
else{printf("\nError\nThe string not accepted.");
break;
}
printf("\n");
for(j=0;j<=top;j++)
{
printf("%c",stack[j]);
}
printf("\t\t");
for(j=i;j<strlen(ip);j++)
{
printf("%c",ip[j]);
}
printf("\t\t");
}
}

Compiler Design (2170701)


(150180107002)
Practical 8
Aim :Write a program using C (or C++) for LALR parsing.
Program :
#include<stdio.h>
#include<string.h>
void main()
{
char table[20][20][20],ter[20],stack[20],ip[20],st1[20],pro[20][20],num;
int i,j,t,k,top=0,st,col,row,pop,np,no,len;
printf("Enter the no of productions:");
scanf("%d",&np);
printf("Enter the productions:");
for(i=0;i<np;i++)
{
scanf("%s",pro[i]);
}
printf("Enter the no.of states:");
scanf("%d",&st);
printf("Enter the states:");
scanf("%s",st1);
printf("Enter the no of terminals:");
scanf("%d",&t);
printf("Enter the terminals:");

Compiler Design (2170701)


(150180107002)
scanf("%s",ter);
for(i=0;i<st;i++)
{
for(j=0;j<t;j++)
{
printf("\nEnter the value for %c %c:",st1[i],ter[j]);
scanf("%s",table[i][j]);
}
}
printf("\nLALR TABLE:\n");
for(i=0;i<t;i++)
{
printf("\t%c",ter[i]);
}
for(i=0;i<st;i++)
{
printf("\n\n%c",st1[i]);
for(j=0;j<t;j++)
{
printf("\t%s",table[i][j]);
}
}
stack[top]='$'; top++;
stack[top]='0';

Compiler Design (2170701)


(150180107002)
printf("\nEnter the input string:");
scanf("%s",ip);
i=0;
printf("\n\nSTACK\t\tINPUT STRING\t\tACTION\n");
printf("\n%s\t\t%s\t\t",stack,ip);
while(i<=strlen(ip) )
{
for(j=0;j<st;j++)
{
if(stack[top]==st1[j])
col=j;
}
for(j=0;j<t;j++)
{
if(ip[i]==ter[j])
{
row=j;
}
}
if((stack[top]=='1')&&(ip[i]=='$'))
{
printf("\nString accepted");
break;
}

Compiler Design (2170701)


(150180107002)
else if(table[col][row][0]=='s')
{
top++;
stack[top]=ter[row];
top++;
stack[top]=table[col][row][1];
i++;
printf("Shift %c %c\n",ter[row],table[col][row][1]);
}
else if(table[col][row][0]=='r')
{
no=(int)table[col][row][1];
no=no-49;
len=strlen(pro[no]);
len=len-3;
pop=2*len;
printf("POP %d",pop);
for(j=0;j<pop;j++)
{
top=top-1;
}
top++;
stack[top]=pro[no][0];
k=top;

Compiler Design (2170701)


(150180107002)
k=k-1;
printf(" Push [%c,",pro[no][0]);
for(j=0;j<st;j++)
{
if(stack[k]==st1[j])
{
col=j;
}
}
k++;
for(j=0;j<t;j++)
{
if(stack[k]==ter[j])
{
row=j;
}
}
top++;
stack[top]=table[col][row][0];
printf("%c]\n",table[col][row][0]);
}
else{printf("\nError\nThe string not accepted.");break;
}
printf("\n");

Compiler Design (2170701)


(150180107002)
for(j=0;j<=top;j++)
{
printf("%c",stack[j]);
}
printf("\t\t");
for(j=i;j<strlen(ip);j++)
{
printf("%c",ip[j]);
}
printf("\t\t");
}
}

Compiler Design (2170701)


(150180107002)
Practical 9
Aim : write a program using C(or C++) for LALR parsing.

a) Write a Lex program which takes input from text file and count no of
characters. no.of lines & no. of words.

Program :
PROCEDURE:
Step 1:- right click and open terminal.
Step 2:- write command gedit pr1.l
Step 3:- Now press the CNTR+S to save file.
Step 4:- Now write command lex pr1.l
Step 5:- Now write command cc lex.yy.c –ll for compile the file.
Step 6:- Now write command ./a.out to see the output.

%{
intnum_lines = 0, num_chars = 0 , num_words=0;
%}
%%
\n ++num_lines; ++num_chars ; ++num_words;
[ \t] ++num_words;
. ++num_chars;
%%
main()
{
yyin=fopen("input.txt","r");
yylex();
printf( "# of lines = %d, # of chars = %d ,# of words = %d\n", num_lines,
num_chars,num_words);

Compiler Design (2170701)


(150180107002)
}

b) Write a Lex program to implement calculator.


Program :
%{
#include
int op=0,i;
float a,b;
%}

dig [0-9]+|([0-9]*)"."([0-9]+)
add "+"
sub "-"
mul "*"
div "/"
pow "^"
ln \n
%%
{dig} {digi();} /*** digi() is a user defined function ***/
{add} {op=1;}
{sub} {op=2;}
{mul} {op=3;}
{div} {op=4;}

Compiler Design (2170701)


(150180107002)
{pow} {op=5;}
{ln} {printf("\n the result :%f\n\n",a);}

%%
digi()
{
if(op==0)
a=atof(yytext); /*** atof() is used to convert the ASCII input to float***/
else
{
b=atof(yytext);
switch(op)
{
case 1:a=a+b;
break;
case 2:a=a-b;
break;
case 3:a=a*b;
break;
case 4:a=a/b;
break;
case 5:for(i=a;b>1;b--)
a=a*i;
break;

Compiler Design (2170701)


(150180107002)
}
op=0;
}
}
main(int argv,char *argc[])
{
yylex();
}
yywrap()
{
return 1;
}

Compiler Design (2170701)


(150180107002)
Practical 10
Aim : Write a LEX program to count the number of comment lines in a given
C program.Also eliminate them and copy that program into separate file.
Program :
%{
int com=0;
%}
%%
"/*"[^\n]+"*/" {com++;fprintf(yyout, " ");}
%%
int main()
{
printf("Write a C program\n");
yyout=fopen("output", "w");
yylex();
printf("Comment=%d\n",com);
return 0;
}

Compiler Design (2170701)


(150180107002)

You might also like