尊敬的 微信汇率:1円 ≈ 0.046166 元 支付宝汇率:1円 ≈ 0.046257元 [退出登录]
SlideShare a Scribd company logo
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 choicen");
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 possiblen");
else
printf("n Allocated succesfullyn");
}/*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 DEALLOCATEDt");
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/cen");
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 stringn");
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 isn");
if(i>3)
{
while(s[j]!='0')
{
if((s[j]=='*')==1||(s[j]=='/')==1)
{
k=j;
if(f1!=0)
{
printf("t1=%cn",s[k+1]);
printf("t2=%c%ct1",s[k-1],s[k]);
}
else
{
if(k>3)
{
printf("t2=t1%c%cn",s[k],s[k+1]);
}
else
{
printf("t2=t1%c%cn",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%cn",s[k],s[k-1]);
}
else
{
printf("nt3=t2%c%cn",s[k],s[k+1])
}
}
else
{
printf("t1=%c%c%cn",s[k-1],s[k],s[k+1]);
}
f1=0;
}
j++;
}
printf("%c=t3",o[0]);
}
else
{
printf("t1=%sn",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 CODEn");
if(i>3)
{
while(s[j]!='0')
{
if((s[j]=='*')==1||(s[j]=='/')==1)
{
k=j;
if(f1!=0)
{
printf("t1=%c%c%cn",s[k-1],s[k],s[k+1]);
}
else
{
if(k>3)
{
printf("t2=t1%c%cn",s[k],s[k+1]);
}
else
{
printf("t2=t1%c%cn",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%cn",s[k],s[k-1]);
}
else
{
printf("t2=t1%c%cn",s[k],s[k+1]);
}
}
else
{
printf("t1=%c%c%cn",s[k-1],s[k],s[k+1]);
}
f1=0;
}
j++;
}
printf("%c=t2",o[0]);
}
else
{
printf("t1=%sn",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("nSTACKtttINPUT STRINGtttACTIONn");
printf("n%sttt%sttt",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 acceptedn");
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("ttt");
for(k=i;k<strlen(ip);k++){
printf("%c",ip[k]);
}
printf("ttt");
}
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 = %dn",
no_of_lines, no_of_chars );
return 0;
}
Output: -

More Related Content

Similar to Compiler design.pdf

2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
Warui Maina
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
Muhammad Ulhaque
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
GC University Faisalabad
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
AnkurRajSingh2
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
Vikram Nandini
 
string , pointer
string , pointerstring , pointer
string , pointer
Arafat Bin Reza
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Viji B
 
Some examples of the 64-bit code errors
Some examples of the 64-bit code errorsSome examples of the 64-bit code errors
Some examples of the 64-bit code errors
PVS-Studio
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
C programming Assignments and Questions.pdf
C programming Assignments and  Questions.pdfC programming Assignments and  Questions.pdf
C programming Assignments and Questions.pdf
rajd20284
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Naveen Gupta
 
Core c sharp and .net quick reference
Core c sharp and .net quick referenceCore c sharp and .net quick reference
Core c sharp and .net quick reference
Arduino Aficionado
 
Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick reference
ilesh raval
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
Lec2&3_DataStructure
Lec2&3_DataStructureLec2&3_DataStructure
Lec2&3_DataStructure
Ibrahim El-Torbany
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
Saad Gabr
 
Lec2
Lec2Lec2
Lec2
Saad Gabr
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
mohd_mizan
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
Abed Bukhari
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
yndaravind
 

Similar to Compiler design.pdf (20)

2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
string , pointer
string , pointerstring , pointer
string , pointer
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Some examples of the 64-bit code errors
Some examples of the 64-bit code errorsSome examples of the 64-bit code errors
Some examples of the 64-bit code errors
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
C programming Assignments and Questions.pdf
C programming Assignments and  Questions.pdfC programming Assignments and  Questions.pdf
C programming Assignments and Questions.pdf
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Core c sharp and .net quick reference
Core c sharp and .net quick referenceCore c sharp and .net quick reference
Core c sharp and .net quick reference
 
Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick reference
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Lec2&3_DataStructure
Lec2&3_DataStructureLec2&3_DataStructure
Lec2&3_DataStructure
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Lec2
Lec2Lec2
Lec2
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 

More from Nitesh Dubey

HTML Presentation
HTML  PresentationHTML  Presentation
HTML Presentation
Nitesh Dubey
 
MLApproachToProgramming.ppt
MLApproachToProgramming.pptMLApproachToProgramming.ppt
MLApproachToProgramming.ppt
Nitesh Dubey
 
seminar topic of holography.ppt
seminar topic of holography.pptseminar topic of holography.ppt
seminar topic of holography.ppt
Nitesh Dubey
 
Online shopping ppt
Online shopping pptOnline shopping ppt
Online shopping ppt
Nitesh Dubey
 
Python lab manual all the experiments are available
Python lab manual all the experiments are availablePython lab manual all the experiments are available
Python lab manual all the experiments are available
Nitesh Dubey
 
Web Technology Lab files with practical
Web Technology Lab  files with practicalWeb Technology Lab  files with practical
Web Technology Lab files with practical
Nitesh Dubey
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
Nitesh Dubey
 
Software engineering practical
Software engineering practicalSoftware engineering practical
Software engineering practical
Nitesh Dubey
 
Principal of programming language lab files
Principal of programming language lab files Principal of programming language lab files
Principal of programming language lab files
Nitesh Dubey
 
database management system lab files
database management system lab filesdatabase management system lab files
database management system lab files
Nitesh Dubey
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
Nitesh Dubey
 
Computer Organization And Architecture lab manual
Computer Organization And Architecture lab manualComputer Organization And Architecture lab manual
Computer Organization And Architecture lab manual
Nitesh Dubey
 
industrial training report on Ethical hacking
industrial training report on Ethical hackingindustrial training report on Ethical hacking
industrial training report on Ethical hacking
Nitesh Dubey
 
Project synopsis on face recognition in e attendance
Project synopsis on face recognition in e attendanceProject synopsis on face recognition in e attendance
Project synopsis on face recognition in e attendance
Nitesh Dubey
 
Hrms industrial training report
Hrms industrial training reportHrms industrial training report
Hrms industrial training report
Nitesh Dubey
 
Industrial training report on core java
Industrial training report on core java Industrial training report on core java
Industrial training report on core java
Nitesh Dubey
 
SEWAGE TREATMENT PLANT mini project report
SEWAGE TREATMENT PLANT mini project reportSEWAGE TREATMENT PLANT mini project report
SEWAGE TREATMENT PLANT mini project report
Nitesh Dubey
 
synopsis report on BIOMETRIC ONLINE VOTING SYSTEM
synopsis report on BIOMETRIC ONLINE VOTING SYSTEMsynopsis report on BIOMETRIC ONLINE VOTING SYSTEM
synopsis report on BIOMETRIC ONLINE VOTING SYSTEM
Nitesh Dubey
 
artificial intelligence ppt
artificial intelligence pptartificial intelligence ppt
artificial intelligence ppt
Nitesh Dubey
 
object oriented Programming ppt
object oriented Programming pptobject oriented Programming ppt
object oriented Programming ppt
Nitesh Dubey
 

More from Nitesh Dubey (20)

HTML Presentation
HTML  PresentationHTML  Presentation
HTML Presentation
 
MLApproachToProgramming.ppt
MLApproachToProgramming.pptMLApproachToProgramming.ppt
MLApproachToProgramming.ppt
 
seminar topic of holography.ppt
seminar topic of holography.pptseminar topic of holography.ppt
seminar topic of holography.ppt
 
Online shopping ppt
Online shopping pptOnline shopping ppt
Online shopping ppt
 
Python lab manual all the experiments are available
Python lab manual all the experiments are availablePython lab manual all the experiments are available
Python lab manual all the experiments are available
 
Web Technology Lab files with practical
Web Technology Lab  files with practicalWeb Technology Lab  files with practical
Web Technology Lab files with practical
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
 
Software engineering practical
Software engineering practicalSoftware engineering practical
Software engineering practical
 
Principal of programming language lab files
Principal of programming language lab files Principal of programming language lab files
Principal of programming language lab files
 
database management system lab files
database management system lab filesdatabase management system lab files
database management system lab files
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
Computer Organization And Architecture lab manual
Computer Organization And Architecture lab manualComputer Organization And Architecture lab manual
Computer Organization And Architecture lab manual
 
industrial training report on Ethical hacking
industrial training report on Ethical hackingindustrial training report on Ethical hacking
industrial training report on Ethical hacking
 
Project synopsis on face recognition in e attendance
Project synopsis on face recognition in e attendanceProject synopsis on face recognition in e attendance
Project synopsis on face recognition in e attendance
 
Hrms industrial training report
Hrms industrial training reportHrms industrial training report
Hrms industrial training report
 
Industrial training report on core java
Industrial training report on core java Industrial training report on core java
Industrial training report on core java
 
SEWAGE TREATMENT PLANT mini project report
SEWAGE TREATMENT PLANT mini project reportSEWAGE TREATMENT PLANT mini project report
SEWAGE TREATMENT PLANT mini project report
 
synopsis report on BIOMETRIC ONLINE VOTING SYSTEM
synopsis report on BIOMETRIC ONLINE VOTING SYSTEMsynopsis report on BIOMETRIC ONLINE VOTING SYSTEM
synopsis report on BIOMETRIC ONLINE VOTING SYSTEM
 
artificial intelligence ppt
artificial intelligence pptartificial intelligence ppt
artificial intelligence ppt
 
object oriented Programming ppt
object oriented Programming pptobject oriented Programming ppt
object oriented Programming ppt
 

Recently uploaded

Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...
Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...
Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...
Banerescorts
 
SPICE PARK JUL2024 ( 6,866 SPICE Models )
SPICE PARK JUL2024 ( 6,866 SPICE Models )SPICE PARK JUL2024 ( 6,866 SPICE Models )
SPICE PARK JUL2024 ( 6,866 SPICE Models )
Tsuyoshi Horigome
 
Call Girls Chennai +91-8824825030 Vip Call Girls Chennai
Call Girls Chennai +91-8824825030 Vip Call Girls ChennaiCall Girls Chennai +91-8824825030 Vip Call Girls Chennai
Call Girls Chennai +91-8824825030 Vip Call Girls Chennai
paraasingh12 #V08
 
Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)
Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)
Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)
Tsuyoshi Horigome
 
Better Builder Magazine, Issue 49 / Spring 2024
Better Builder Magazine, Issue 49 / Spring 2024Better Builder Magazine, Issue 49 / Spring 2024
Better Builder Magazine, Issue 49 / Spring 2024
Better Builder Magazine
 
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUESAN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
drshikhapandey2022
 
🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...
🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...
🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...
dulbh kashyap
 
Kandivali Call Girls ☑ +91-9967584737 ☑ Available Hot Girls Aunty Book Now
Kandivali Call Girls ☑ +91-9967584737 ☑ Available Hot Girls Aunty Book NowKandivali Call Girls ☑ +91-9967584737 ☑ Available Hot Girls Aunty Book Now
Kandivali Call Girls ☑ +91-9967584737 ☑ Available Hot Girls Aunty Book Now
SONALI Batra $A12
 
Technological Innovation Management And Entrepreneurship-1.pdf
Technological Innovation Management And Entrepreneurship-1.pdfTechnological Innovation Management And Entrepreneurship-1.pdf
Technological Innovation Management And Entrepreneurship-1.pdf
tanujaharish2
 
The Differences between Schedule 40 PVC Conduit Pipe and Schedule 80 PVC Conduit
The Differences between Schedule 40 PVC Conduit Pipe and Schedule 80 PVC ConduitThe Differences between Schedule 40 PVC Conduit Pipe and Schedule 80 PVC Conduit
The Differences between Schedule 40 PVC Conduit Pipe and Schedule 80 PVC Conduit
Guangdong Ctube Industry Co., Ltd.
 
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdfAsymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
felixwold
 
❣Unsatisfied Bhabhi Call Girls Surat 💯Call Us 🔝 7014168258 🔝💃Independent Sura...
❣Unsatisfied Bhabhi Call Girls Surat 💯Call Us 🔝 7014168258 🔝💃Independent Sura...❣Unsatisfied Bhabhi Call Girls Surat 💯Call Us 🔝 7014168258 🔝💃Independent Sura...
❣Unsatisfied Bhabhi Call Girls Surat 💯Call Us 🔝 7014168258 🔝💃Independent Sura...
hotchicksescort
 
Call Girls Madurai 8824825030 Escort In Madurai service 24X7
Call Girls Madurai 8824825030 Escort In Madurai service 24X7Call Girls Madurai 8824825030 Escort In Madurai service 24X7
Call Girls Madurai 8824825030 Escort In Madurai service 24X7
Poonam Singh
 
Sri Guru Hargobind Ji - Bandi Chor Guru.pdf
Sri Guru Hargobind Ji - Bandi Chor Guru.pdfSri Guru Hargobind Ji - Bandi Chor Guru.pdf
Sri Guru Hargobind Ji - Bandi Chor Guru.pdf
Balvir Singh
 
🔥 Hyderabad Call Girls  👉 9352988975 👫 High Profile Call Girls Whatsapp Numbe...
🔥 Hyderabad Call Girls  👉 9352988975 👫 High Profile Call Girls Whatsapp Numbe...🔥 Hyderabad Call Girls  👉 9352988975 👫 High Profile Call Girls Whatsapp Numbe...
🔥 Hyderabad Call Girls  👉 9352988975 👫 High Profile Call Girls Whatsapp Numbe...
aarusi sexy model
 
An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...
An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...
An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...
DharmaBanothu
 
MODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptx
MODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptxMODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptx
MODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptx
NaveenNaveen726446
 
My Airframe Metallic Design Capability Studies..pdf
My Airframe Metallic Design Capability Studies..pdfMy Airframe Metallic Design Capability Studies..pdf
My Airframe Metallic Design Capability Studies..pdf
Geoffrey Wardle. MSc. MSc. Snr.MAIAA
 
❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...
❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...
❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...
nainakaoornoida
 
SELENIUM CONF -PALLAVI SHARMA - 2024.pdf
SELENIUM CONF -PALLAVI SHARMA - 2024.pdfSELENIUM CONF -PALLAVI SHARMA - 2024.pdf
SELENIUM CONF -PALLAVI SHARMA - 2024.pdf
Pallavi Sharma
 

Recently uploaded (20)

Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...
Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...
Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...
 
SPICE PARK JUL2024 ( 6,866 SPICE Models )
SPICE PARK JUL2024 ( 6,866 SPICE Models )SPICE PARK JUL2024 ( 6,866 SPICE Models )
SPICE PARK JUL2024 ( 6,866 SPICE Models )
 
Call Girls Chennai +91-8824825030 Vip Call Girls Chennai
Call Girls Chennai +91-8824825030 Vip Call Girls ChennaiCall Girls Chennai +91-8824825030 Vip Call Girls Chennai
Call Girls Chennai +91-8824825030 Vip Call Girls Chennai
 
Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)
Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)
Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)
 
Better Builder Magazine, Issue 49 / Spring 2024
Better Builder Magazine, Issue 49 / Spring 2024Better Builder Magazine, Issue 49 / Spring 2024
Better Builder Magazine, Issue 49 / Spring 2024
 
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUESAN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
 
🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...
🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...
🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...
 
Kandivali Call Girls ☑ +91-9967584737 ☑ Available Hot Girls Aunty Book Now
Kandivali Call Girls ☑ +91-9967584737 ☑ Available Hot Girls Aunty Book NowKandivali Call Girls ☑ +91-9967584737 ☑ Available Hot Girls Aunty Book Now
Kandivali Call Girls ☑ +91-9967584737 ☑ Available Hot Girls Aunty Book Now
 
Technological Innovation Management And Entrepreneurship-1.pdf
Technological Innovation Management And Entrepreneurship-1.pdfTechnological Innovation Management And Entrepreneurship-1.pdf
Technological Innovation Management And Entrepreneurship-1.pdf
 
The Differences between Schedule 40 PVC Conduit Pipe and Schedule 80 PVC Conduit
The Differences between Schedule 40 PVC Conduit Pipe and Schedule 80 PVC ConduitThe Differences between Schedule 40 PVC Conduit Pipe and Schedule 80 PVC Conduit
The Differences between Schedule 40 PVC Conduit Pipe and Schedule 80 PVC Conduit
 
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdfAsymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
 
❣Unsatisfied Bhabhi Call Girls Surat 💯Call Us 🔝 7014168258 🔝💃Independent Sura...
❣Unsatisfied Bhabhi Call Girls Surat 💯Call Us 🔝 7014168258 🔝💃Independent Sura...❣Unsatisfied Bhabhi Call Girls Surat 💯Call Us 🔝 7014168258 🔝💃Independent Sura...
❣Unsatisfied Bhabhi Call Girls Surat 💯Call Us 🔝 7014168258 🔝💃Independent Sura...
 
Call Girls Madurai 8824825030 Escort In Madurai service 24X7
Call Girls Madurai 8824825030 Escort In Madurai service 24X7Call Girls Madurai 8824825030 Escort In Madurai service 24X7
Call Girls Madurai 8824825030 Escort In Madurai service 24X7
 
Sri Guru Hargobind Ji - Bandi Chor Guru.pdf
Sri Guru Hargobind Ji - Bandi Chor Guru.pdfSri Guru Hargobind Ji - Bandi Chor Guru.pdf
Sri Guru Hargobind Ji - Bandi Chor Guru.pdf
 
🔥 Hyderabad Call Girls  👉 9352988975 👫 High Profile Call Girls Whatsapp Numbe...
🔥 Hyderabad Call Girls  👉 9352988975 👫 High Profile Call Girls Whatsapp Numbe...🔥 Hyderabad Call Girls  👉 9352988975 👫 High Profile Call Girls Whatsapp Numbe...
🔥 Hyderabad Call Girls  👉 9352988975 👫 High Profile Call Girls Whatsapp Numbe...
 
An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...
An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...
An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...
 
MODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptx
MODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptxMODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptx
MODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptx
 
My Airframe Metallic Design Capability Studies..pdf
My Airframe Metallic Design Capability Studies..pdfMy Airframe Metallic Design Capability Studies..pdf
My Airframe Metallic Design Capability Studies..pdf
 
❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...
❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...
❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...
 
SELENIUM CONF -PALLAVI SHARMA - 2024.pdf
SELENIUM CONF -PALLAVI SHARMA - 2024.pdfSELENIUM CONF -PALLAVI SHARMA - 2024.pdf
SELENIUM CONF -PALLAVI SHARMA - 2024.pdf
 

Compiler design.pdf

  • 1. 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
  • 2. 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);
  • 3. 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); }
  • 5. 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); } }
  • 6. 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
  • 7. 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;
  • 8. 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 choicen"); 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++;
  • 9. } 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 possiblen"); else printf("n Allocated succesfullyn"); }/*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 DEALLOCATEDt");
  • 10. 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
  • 11. 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
  • 12. 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/cen"); 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*/
  • 14. 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 ***
  • 15. 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 stringn"); 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 isn"); if(i>3) { while(s[j]!='0') { if((s[j]=='*')==1||(s[j]=='/')==1) { k=j; if(f1!=0) { printf("t1=%cn",s[k+1]); printf("t2=%c%ct1",s[k-1],s[k]); } else {
  • 17. 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 CODEn"); if(i>3) { while(s[j]!='0') { if((s[j]=='*')==1||(s[j]=='/')==1) { k=j; if(f1!=0) { printf("t1=%c%c%cn",s[k-1],s[k],s[k+1]); } else { if(k>3) { printf("t2=t1%c%cn",s[k],s[k+1]); } else { printf("t2=t1%c%cn",s[k],s[k-1]); } } f=0; break; }
  • 19. 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();
  • 20. } } 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
  • 21. 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("nSTACKtttINPUT STRINGtttACTIONn"); printf("n%sttt%sttt",stack,ip);
  • 22. 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 acceptedn"); 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("ttt"); for(k=i;k<strlen(ip);k++){ printf("%c",ip[k]); } printf("ttt"); } getch(); }
  • 23. 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
  • 24. 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 = %dn", no_of_lines, no_of_chars ); return 0; } Output: -
  翻译: