Compiler Design Practical File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

List of Practicals

 Implementation of Lexical Analyzer to recognize a few patterns


 Implementation of Intermediate Code Generation
 Implementation of Heap storage Allocation
 Implementation of Brute force technique of Top-down parsing
 Implementation of Code Optimization
 Implementation of Recursive Descent Parser
 Implementation of Operator Precedence Parser
 Implementation of Lexical Analyzer using flex
Implementation of Lexical Analyzer to recognize a few patterns

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

void keyword(char str[10])


{

if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||strcmp("int",str)==0||strcm
p("float",str)==0||strcmp("char",str)==0||strcmp("double",str)==0||strcmp("printf",str)==0||strcm
p("switch",str)==0||strcmp("case",str)==0)
printf("\n%s is a keyword",str);

else
printf("\n%s is an identifier",str);
}

void main()
{
FILE *f1,*f2,*f3;
char c,str[10],st1[10];
int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;
f1=fopen("input","r");
f2=fopen("identifier","w");
f3=fopen("specialchar","w");

while((c=getc(f1))!=EOF)
{
if(isdigit(c))
{
tokenvalue=c-'0';
c=getc(f1);
while(isdigit(c))
{
tokenvalue*=10+c-'0';
c=getc(f1);
}
num[i++]=tokenvalue;
ungetc(c,f1);
}
else
if(isalpha(c))
{
putc(c,f2);
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
putc(c,f2);
c=getc(f1);
}
putc(' ',f2);
ungetc(c,f1);
}
else
if(c==' '||c=='\t')
printf(" ");
else
if(c=='\n')
lineno++;
else
putc(c,f3);
}
fclose(f2);
fclose(f3);
fclose(f1);
printf("\n the no's in the program are:");
for(j=0;j<i;j++)
printf("\t%d",num[j]);
printf("\n");
f2=fopen("identifier","r");
k=0;
printf("the keywords and identifier are:");
while((c=getc(f2))!=EOF)
if(c!=' ')
str[k++]=c;
else
{
str[k]='\0';
keyword(str);
k=0;
}
fclose(f2);
f3=fopen("specialchar","r");
printf("\n Special Characters are");
while((c=getc(f3))!=EOF)
printf("\t%c",c);
printf("\n");
fclose(f3);
printf("Total no of lines are:%d",lineno);
}
Output: -
Implementation of Intermediate Code Generation

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

char op[2],arg1[5],arg2[5],result[5];
void main()
{
FILE *fp1,*fp2;
fp1=fopen("input.txt","r");
fp2=fopen("output.txt","w");
while(!feof(fp1))
{

fscanf(fp1,"%s%s%s%s",op,arg1,arg2,result);
if(strcmp(op,"+")==0)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nADD R0,%s",arg2);
fprintf(fp2,"\nMOV %s,R0",result);
}
if(strcmp(op,"*")==0)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nMUL R0,%s",arg2);
fprintf(fp2,"\nMOV %s,R0",result);
}
if(strcmp(op,"-")==0)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nSUB R0,%s",arg2);
fprintf(fp2,"\nMOV %s,R0",result);
}
if(strcmp(op,"/")==0)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nDIV R0,%s",arg2);
fprintf(fp2,"\nMOV %s,R0",result);
}
if(strcmp(op,"=")==0)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nMOV %s,R0",result);
}
}
fclose(fp1);
fclose(fp2);
getch();
}

Input: -

(input.txt)

+ a b t1
* c d t2
- t1 t2 t
=t?x

Output: -

(output.txt)

MOV R0, a
ADD R0, b
MOV t1, R0
MOV R0, c
MUL R0, d
MOV t2, R0
MOV R0, t1
SUB R0, t2
MOV t, R0
MOV R0, t
MOV x, R0
Implementation of Heap storage Allocation

ALGORITHM:-

1. Start
2. Assume memory size as 50 bytes
3. Allocation:---
 Read variable name and size.
 If continuous available free space is greater than or equal to variable size then
allocate. And make those bits in allocation array as 1.
 If space not sufficient then allocation is not possible.
4. Deal location:---
 Read variable name
 Compare variable name with variable name presented in heap allocated array. If
match is found free the memory space allocated for that variable.
 Make valid bit as zero in heap array for that variable.
 Allocation bits in array make as 0 for that particular variable memory.
5. Deal located memory space will be utilized when new variable requires
Memory….

6. Stop.

//program

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

struct heaps
{
char var[10];
int no_byte;
int st_byte;
int valid;
}heaparr[20];

int alloc[50],harpos=0;
void main()
{

int ch,i;
/* if memory allocated then alloc[pos]=1 else 0*/
for(i=0;i<50;i++)
alloc[i]=0;
while(1)
{
clrscr();
printf("\n enter u r choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:allocat();
break;
case 2:deallocat();
break;
case 3: disp();
break;
case 4: exit(0);
getch();
}/*Closing of switch*/
}/* Closing of while*/
}/* Closing of main*/

allocat()
{

char vname[10],s;
int vsize,i,count,st,k;
printf("\n enter variable name:");
scanf("%s",&vname);
printf("\n enter how many bytes required for %s",vname);
scanf("%d",&vsize);
for(i=0;i<50;i++)
{ count=0;
while((alloc[i]==1)&&(i<50))
i++;
st=i;
while(count<vsize)
{
if(alloc[i]==0)
count++;
else
break;
i++;
}
if(count==vsize)
{
strcpy(heaparr[harpos].var,vname);
heaparr[harpos].valid=1;
heaparr[harpos].no_byte=vsize;
heaparr[harpos].st_byte=st;
for(k=st;k<st+vsize;k++)
alloc[k]=1;
harpos++;
break;
}
}/*Closing of for loop*/

if(i>50)
printf("\n Allocation is not possible\n");
else
printf("\n Allocated succesfully\n");

}/*Closing of allocation*/

disp()
{

int i;
for(i=0;i<harpos;i++)
{
if(heaparr[i].valid==1)
{
printf("\n Name of variable %s",heaparr[i].var);
printf("\n starting byte %d",heaparr[i].st_byte);
printf("\n number of bytes%d",heaparr[i].no_byte);
}
}

}/* Closing of display*/

deallocat()
{
int i;
char vnam[10];
int sta_byte,len_byte,end_byte;
printf("variables presented are...\t");
for(i=0;i<harpos;i++)
printf("%s",heaparr[i].var);
printf("ENTER VARIABLE NAME WHICH IS TO BE DEALLOCATED\t");
scanf("%s",vnam);
for(i=0;i<harpos;i++)
{
if(heaparr[i].valid==1)
{
if(strcmp(vnam,heaparr[i].var)==0)
break;
}
}
if(i<harpos)
{
heaparr[i].valid=0;
sta_byte=heaparr[i].st_byte;
len_byte=heaparr[i].no_byte;
end_byte=sta_byte+len_byte;
for(i=sta_byte;i<end_byte;i++)
alloc[i]=0;
}
}/* Closing of deal location*/

OUTPUT :-

enter u r choice 1
enter variable name: a
enter how many bytes required for a 10
Allocated successfully

Enter u r choice 1
Enter variable name: b
Enter how many bytes required for b 20
Allocated successfully

Enter u r choice 1
Enter variable name:c
Enter how many bytes required for c 30
Allocation is not possible

Enter u r choice 3

Name of variable a
starting byte 0
number of bytes10
bytes allocated are 0..9
Name of variable b
starting byte 10
number of bytes20
bytes allocated are 10..29

Enter u r choice 1

Enter variable name:c


Enter how many bytes required for c 10
Allocated successfully

Enter u r choice 3

Name of variable a
Starting byte 0
Number of bytes10
Bytes allocated are 0..9

Name of variable b
Starting byte 10
Number of bytes20
Bytes allocated are 10..29

Name of variable c
Starting byte 30
Number of bytes10
Bytes allocated are 30..39

Enter u r choice 2
variables presented are... a b c
ENTER VARIABLE NAME, WHICH IS TO BE DEALLOCATED b
Deal located successfully

Enter u r choice 3

Name of variable a
Starting byte 0
Number of bytes10
Bytes allocated are 0..9
Implementation of Brute force technique of Top-down parsing
#include<stdio.h>
#include<conio.h>

char *inpt; /* pointer to the input*/

main()
{

char ipst[20];
int len;
clrscr();
printf("The productions are.....\n s->aBc \n B->b/cd/ce\n");
printf("Enter the string to be parsed \t ");
gets(ipst);
inpt=ipst;
if(S())
printf("****String is Parsed***\n");
else
printf("****Invalid String******\n");
getch();
} /* closing of main*/

S()
{
if(*inpt=='a')
{
inpt++;
if(B())
{
if(*inpt=='c')
{
inpt++;
if(*inpt=='\0')
return(1);
else
return(0);
}
else
return(0);
}
else
return(0);
}
}/* closing of s procedure*/
B()
{
char *binpt,*binpt1;
binpt=inpt;
if(*inpt=='c')
{
inpt++;
binpt1=inpt;
if(*inpt=='d')
{
inpt++;
return(1);
}
else
{
inpt=binpt1;
if(*inpt=='e')
{
inpt++;
return(1);
}
else
return(0);
}
}
else
{
inpt=binpt;
if(*inpt=='b')
{
inpt++;
return(1);
}
else
return(0);
}
}/* Closing of B procedure */
OUTPUT :-

1 ) The productions are.....

s->aBc
B->b/cd/ce

Enter the string to be parsed abc

****string is parsed***

2 ) The productions are.....

s->aBc
B->b/cd/ce

Enter the string to be parsed acdc

****string is parsed***

3 ) The productions are.....

s->aBc
B->b/cd/ce

Enter the string to be parsed acec

****string is parsed***

4 ) The productions are.....

s->aBc
B->b/cd/ce

Enter the string to be parsed acecd

****Invaid string ***


Implementation of Code Optimization
#include<stdio.h>
#include<conio.h>
#include<string.h>
char s[20],o[20];
void main()
{
int i=0,j=0,k,f1=1,f=1,k1=0;
void part();
clrscr();
printf("\n Enter the input string\n");
scanf("%s",o);
strlen(o);
while(o[k1]!='\0')
{
if((o[k1]=='=')==1)

{
break;
}
k1++;
}
for(j=k1+1;j<strlen(o);j++)
{
s[i]=o[j];
i++;
}
s[i]='\0';
i=strlen(s);
j=0;
printf("\n Three address code is\n");
if(i>3)
{
while(s[j]!='\0')
{
if((s[j]=='*')==1||(s[j]=='/')==1)
{
k=j;
if(f1!=0)
{
printf("t1=%c\n",s[k+1]);
printf("t2=%c%ct1",s[k-1],s[k]);
}
else
{
if(k>3)
{
printf("t2=t1%c%c\n",s[k],s[k+1]);
}
else
{
printf("\t2=t1%c%c\n",s[k],s[k-1]);
}
}
f=0;
break;
}
j++;
}
j=0;
while(s[j]!='\0')
{
if((s[j]=='+')==1||(s[j]=='-')==1)
{
k=j;
if(f==0)
{

if(k<3)
{
printf("\nt3=t2%c%c\n",s[k],s[k-1]);
}
else
{
printf("\nt3=t2%c%c\n",s[k],s[k+1])
}
}
else
{
printf("t1=%c%c%c\n",s[k-1],s[k],s[k+1]);
}
f1=0;
}
j++;
}
printf("%c=t3",o[0]);
}
else
{
printf("t1=%s\n",s);
printf("%c=t1",o[0]);
}
part();
getch();
}
void part()
{
int i=0,j=0,k,f1=1,f=1,k1=0;
while(o[k1]!='\0')
{
if((o[k1]=='=')==1)
{
break;
}
k1++;
}
for(j=k1+1;j<strlen(o);j++)
{
s[i]=o[j];
i++;
}
s[i]='\0';
i=strlen(s);
j=0;
printf("\n OPTIMIZED CODE\n");
if(i>3)
{
while(s[j]!='\0')

{
if((s[j]=='*')==1||(s[j]=='/')==1)
{
k=j;
if(f1!=0)
{
printf("t1=%c%c%c\n",s[k-1],s[k],s[k+1]);
}
else
{
if(k>3)
{
printf("t2=t1%c%c\n",s[k],s[k+1]);
}
else
{
printf("\t2=t1%c%c\n",s[k],s[k-1]);
}
}
f=0;
break;
}
j++;
}
j=0;
while(s[j]!='\0')
{
if((s[j]=='+')==1||(s[j]=='-')==1)
{
k=j;
if(f==0)
{
if(k<3)
{
printf("t2=t1%c%c\n",s[k],s[k-1]);
}
else
{
printf("t2=t1%c%c\n",s[k],s[k+1]);
}
}
else
{
printf("t1=%c%c%c\n",s[k-1],s[k],s[k+1]);
}
f1=0;
}
j++;
}
printf("%c=t2",o[0]);

}
else
{
printf("t1=%s\n",s);
printf("%c=t1",o[0]);
}
}
Implementation of Recursive Descent Parser

#include<stdio.h>
char *a;

main()
{
char s[10];
clrscr();
printf("ENTER THE STRING");
gets(s);
a=s;
if(E())

printf("STRING IS PARSED");
else
printf(invalid string");
getch();
}

int E()
{
T();
EPRIME();
}
int EPRIME()
{
if(*a=='+'' )
{
a++;;
T();
EPRIME();
}
}
int T()
{
F();
TPRIME();
}
int TPRIME()
{
if(*a=='*')
{
a++;;
F();
TPRIME();
}
}
int F()
{
if(*a=='i')
a++ ;
else if(*a==')')
{
a++;;
E();
if(*a==')')
a++ ;
else
error();
}
else
error();
}
int error()
{
return(0);
}

Input:-
ENTER THE STRING :(i+i)

Output:-
STRING IS PARSED
Implementation of Operator Precedence Parser

#include<stdio.h>
#include<conio.h>
void main(){

/*OPERATOR PRECEDENCE PARSER*/


char stack[20],ip[20],opt[10][10][1],ter[10];
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++)
{
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++)
{
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");
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();
}
Output:
Enter the value for * *:>
Enter the value for * $:>
Enter the value for $ i:<
Enter the value for $ +:<
Enter the value for $ *:<
Enter the value for $ $:accept

**** OPERATOR PRECEDENCE TABLE ****


i + * $

i e > > >


+ < > < >
* < > > >
$ < < < a
*/
Enter the input string:
i*i

STACK INPUT STRING ACTION

$ i*i Shift i
$<i *i Reduce
$ *i Shift *
$<* i Shift i
$<*<i
String is not accepted
Implementation of Lexical Analyzer using flex

//Decalring two counters one for number of lines other for number of characters
%{
int no_of_lines = 0;
int no_of_chars = 0;
%}
/***rule 1 counts the number of lines,
rule 2 counts the number of characters
and rule 3 specifies when to stop
taking input***/
%%
\n ++no_of_lines;
. ++no_of_chars;
end return 0;
%%
/*** User code section***/
int yywrap(){}
int main(int argc, char **argv)
{
yylex();
printf("number of lines = %d, number of chars = %d\n",
no_of_lines, no_of_chars );
return 0;
}

Output: -

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy