0% found this document useful (0 votes)
65 views20 pages

Compilerlab 7CS7

The document describes 10 experiments related to compiler design. Experiment 1 explains the phases of a compiler and storing student records in a file. Experiment 2 implements a program to count whitespace and newline characters in a string. Experiment 3 determines if a given string is an identifier, operator, or constant. Experiment 4 designs a symbol table to store variable names and types. Experiment 5 creates a syntax tree for a given expression. Experiment 6 generates Polish notation from an infix expression. Experiment 7 develops an operator precedence parser. Experiment 8 parses an expression using a brute force top-down technique. Experiment 9 implements a max heap allocation strategy. Experiment 10 simulates different memory allocation strategies like first fit, best fit, and worst fit.

Uploaded by

Harshit Jain
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views20 pages

Compilerlab 7CS7

The document describes 10 experiments related to compiler design. Experiment 1 explains the phases of a compiler and storing student records in a file. Experiment 2 implements a program to count whitespace and newline characters in a string. Experiment 3 determines if a given string is an identifier, operator, or constant. Experiment 4 designs a symbol table to store variable names and types. Experiment 5 creates a syntax tree for a given expression. Experiment 6 generates Polish notation from an infix expression. Experiment 7 develops an operator precedence parser. Experiment 8 parses an expression using a brute force top-down technique. Experiment 9 implements a max heap allocation strategy. Experiment 10 simulates different memory allocation strategies like first fit, best fit, and worst fit.

Uploaded by

Harshit Jain
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 20

EXPERIMENT NO 1

Explain all the phases of Compiler add store the student record in file(c++)
EXPERIMENT NO 2
/* PROGRAM TO FIND THE NUMBER OF WHITESPACES AND
NEWLINES CHARACTERS */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[200],ch;
int a=0,space=0,newline=0;
clrscr();
printf("\n enter a string(press escape to quit entering):");
ch=getche();
while((ch!=27) && (a<199))
{
str[a]=ch;
if(str[a]==' ')
{
space++;
}
if(str[a]==13)
{
newline++;
printf("\n");
}
a++;
ch=getche();
}
printf("\n the number of lines used : %d",newline+1);
printf("\n the number of spaces used is : %d",space);
getch();
}

EXPERIMENT NO 3
/*THIS PROGRAM IS TO FIND OUT WHETHER A GIVEN STRING
IS A IDENTIFIER AND OPERATOR OR NOT */
#include<stdio.h>
#include<conio.h>
void main()
{
char expr[20];
int length=0;
int i=0,j=0,cntid=1,cntop=1,cntcount=1;
char temp[10];
int k=0;
clrscr();
for(j=0;j<10;j++)
temp[j]=NULL;
printf("Enter the Expression :");
gets(expr);
length=strlen(expr);
printf("\n");
for(i=0;i<length;i++)
{
for(j=97;j<=(97+26);j++)
{
if(expr[i]==j)
{
printf("\n %c is id %d\n",expr[i],cntid);
cntid++;
break;
}
}
if(expr[i]==61||expr[i]==42||expr[i]==4317||expr[i]==45||
expr[i]==47)
{
printf("\n %c is operator %d\n",expr[i],cntop);
cntop++;
}
k=0;

for(j=48;j<=57;j++)
{
if(expr[i]==j)
{
k=0;
do
{
temp[k]==expr[i];
k++;
i++;
}
while(expr[i]!=61&&expr[i]!=42&&expr[i]!
=43&&expr[i]!='\0');
printf("\n %s is constant %d \n",temp,cntconst);
cntconst++;
}
}
}
getch();
}

EXPERIMENT NO.4
/* PROGRAM TO DESIGN SYMBOL TABLE */
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
void main()
{
fstream f;
clrscr();
char name[20],ch;
char *ptr;
cout<<"enter file name:";
cin>>name;
f.open(name,ios::in|ios::out);
int i=0,j,x1=0,x2=0,x3=0,y=0;
char s[80],v1[10][20],v2[10][20],v3[10][20];
char d_type[3][10]={"int","char","float"};
cout<<"prog is \n";
while(!f.eof())
{
f.getline(s,80);
cout<<s<<endl;
for(i=0;i<3;i++)
{
ptr=strstr(s,d_type[i]);
if(ptr!=NULL)
{
if(!strstr(s,"(")&&!strstr(s,")"))
{
if(i==0)
{
y=0;
for(j=4;s[j]!=';';j++)
{
if(s[j]==',')

{
v1[x1][y]='\0';
x1++;
y=0;
continue;
}
v1[x1][y++]=s[j];
}
v1[x1][y]='\0';
x1++;
}
if(i==1)
{
y=0;
for(j=5;s[j]!=';';j++)
{
if(s[j]==',')
{
v2[x2][y]='\0';
x2++;
y=0;
continue;
}
v2[x2][y++]=s[j];
}
v2[x2][y]='\0';
x2++;
}
if(i==2)
{
y=0;
for(j=6;s[j]!=';';j++)
{
if(s[j]==',')
{
v3[x3][y]='\0';
x3++;
y=0;
continue;
}

v3[x3][y++]=s[j];
}
v3[x3][y]='\0';
x3++;
}
}
}
}
}
f.close();
cout<<"The symbol table is:"<<endl<<endl;
cout<<"Symbol name\tType\t Address"<<endl<<endl;
for(i=0;i<x1;i++)
{
cout<<"\t"<<v1[i]<<"\tint"<<"\t"<<&v1[i]<<endl;
}
for(i=0;i<x2;i++)
{
cout<<"\t"<<v2[i]<<"\tchar"<<"\t"<<&v2[i]<<endl;
}
for(i=0;i<x3;i++)
{
cout<<"\t"<<v3[i]<<"\tfloat"<<"\t"<<&v3[i]<<endl;
}
getch();
}

EXPERIMENT NO: 5
/* PROGRAM TO DESIGN THE SYNTEX TREE */
//when expression is inputted it shold be taken care of that
// the operators and operands must be separated by a single space
#include<stdio.h>
#include<string.h>
typedef struct node
{
char str[10];
struct node * left;
struct node * right;
}node;
int isoper(char *ch)
{
if(*ch=='='||*ch=='+'||*ch=='-'||*ch=='*'||*ch=='/')
return 1;
else
return 0;
}
int isoper1(char ch)
{
if(ch=='='||ch=='+'||ch=='-'||ch=='*'||ch=='/')
return 1;
else
return 0;
}
int isalphanum(char ch)
{
if(((ch>=65&&ch<=90)||(ch>=97&&ch<=122)||
(ch>=48&&ch<=57)))
return 1;
else
return 0;
}

int gettoken(char *token, char*expr)


{
static int i;
int j=0;
for(;expr[i]!=NULL;i++)
{
if(!isalphanum(expr[i])&&!isoper1(expr[i]))
{
token[j]=NULL;
i++;
break;
}
else
token[j++]=expr[i];
}
if(expr[i]==NULL)
{
token[j]=NULL;
return 0 ;
}
else
return 1;
}
void print(node * root);
void main()
{ int i,flag=0;
char expr[50],token[25];
node *root,*ptr,*prev,*ptr1;
clrscr();
printf("Enter an expression: ");
gets(expr);
while((gettoken(token,expr))==1)
{
// allocate memory for token
ptr=(node*)malloc(sizeof(node));
strcpy(ptr->str,token);
ptr->left=NULL;
ptr->right=NULL;
if(isoper(token)&&!flag) // for root node

{
root=ptr1=ptr;
root->left=prev;
flag=1;
}
else if(isoper(token))
{
ptr1->right=ptr;
ptr->left=prev;
ptr1=ptr1->right;
}
prev=ptr;
}
/* to handle last node*/
ptr=(node*)malloc(sizeof(node));
strcpy(ptr->str,token);
ptr->left=NULL;
ptr->right=NULL;
ptr1->right=ptr;
print(root);
}
void print(node * root)
{
while(root)
{ if(root->left!=NULL) // child of last node should not be printed
{
printf("\nRoot => %s ",root->str);
printf("\nChild => %s & => %s ",root->left->str,root>right->str);
}
root=root->right;
}
}

EXPERIMENT NO 6
WRITE A PROGRAM FOR GENERATING FOR VARIOUS
INTERMIDIATE CODE FORMS : POLISH NOTATION
#include<stdio.h>
#include<conio.h>
#include<string.h>
char stack[20];
int k=-1;
void push(char c)
{
k++;
stack[k]=c;
}
char pop()
{
char c;
c=stack[k];
k--;
return c;
}
int isAlpha(char c)
{
if((c>=65&&c<=90)||(c>=97&&c<=122))
return 1;
else
return 0;
}
void main()
{
char str[50],str1[50],c,temp;
int i,j=0,l;
clrscr();
puts("ENTER EXPRESSION : ");
flushall();

//

gets(str);
strcpy(str,"((A+B)*C-(D-E))^(F+G)");
l=strlen(str);
for(i=0;i<strlen(str);i++)
{
c=str[i];
if(isAlpha(c))
{
str1[j]=c;
j++;
}
else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^'||c=='('||c==')')
{
if(k==-1)
push(c);
else if(c=='+'||c=='-')
{
if(stack[k]!='(')
{
str1[j]=pop();
j++;
push(c);
}
else
{
push(c);
}
}
else if(c=='*'||c=='/')
{
if(stack[k]=='+'||stack[k]=='-'||stack[k]=='(')
push(c);
else
{
str1[j]=pop();
j++;
push(c);
}
}
else if(c=='^')

{
if(stack[k]!='^')
push(c);
else
{
str1[j]=c;
j++;
}
}
else if(c=='(')
push(c);
else if(c==')')
{
while(stack[k]!='(')
{
str1[j]=pop();
j++;
}
pop();
}
}
}
while(k>=0&&stack[k]!='(')
{
str1[j]=pop();
j++;
}
str1[j]=NULL;
printf("PREFIX EXPRESSION IS : ");
puts(str1);
getch();
}

EXPERIMENT NO 7
WRITE A PROGRAM TO DEVELOP AN OPERATER
PRECEDENCE PARSER

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
void main()
{
clrscr();
char a[10];
gets(a);
int v;
v=strlen(a);
for(int i=0;i<v;i++)
{
if(a[i]=='^')
{
cout<<"("<<a[i]<<")";
}
else if(a[i]=='*'||a[i]=='/')
{
cout<<"(("<<a[i]<<"))";
}
else if(a[i]=='+'||a[i]=='-')
{
cout<<"((("<<a[i]<<")))";
}
else
{
cout<<"(((("<<a[i]<<"))))";
}
}
getch();
}

EXPERIMENT NO 8
WRITE A PROGRAM TO PARSE USING BRUTE FORCE
TECHNIQUE OF TOP DOWN PARSING.

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int a[30];
clrscr();
int min=10000,temp=0,i,lev,n,noofc,z;
printf("please enter how many number");
cin>>n;
for(i=0;i<n;i++)
a[i]=0;
cout<<"enter value of root";
cin>>a[0];
for(i=1;i<=n/2;i++)
{
cout<<"please enter no of child of parent with value"<<a[i-1]<<":";
cin>>noofc;
for(int j=1;j<=noofc;j++)
{z=(i)*2+j-2;
cout<<"please enter value of child";
cin>>a[z];
}
}
for(i=n-1;i>=n/2;i--)
{
temp=0;
for(int j=i+1;j>=1;j=j/2)
temp=temp+a[j-1];
if(temp<min)
min=temp;

cout<<"temp min is"<<temp<<"\n";


}
cout<<"min is"<<min;
getch();
}

EXPERIMENT NO 9
WRITE APROGRAM TO DEGINE A MAX HEAP ALLOCATION

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
void main()
{
int n,a[20],i,j,temp;
clrscr();
cout<<"Enter the no. of elements:";
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
for(j=1;j<=i;j++)
{
if(j==2)
{
if(a[1]<a[2])
{
temp=a[1];
a[1]=a[2];
a[2]=temp;
}
}
if(j==3)
{
if(a[1]<a[3])
{
temp=a[1];
a[1]=a[3];
a[3]=temp;

}
}
if(j!=1 && j!=2 && j!=3)
{
if(a[(j-2)/2+1]<a[j])
{
temp=a[(j-2)/2+1];
a[(j-2)/2+1]=a[j];
a[j]=temp;
}
}
}
}
for(i=1;i<=n;i++)
{
if(((2*i)<=(n-1)) && ((2*i+1)<=n))
{
cout<<"parent "<<a[i]<<"\n";
cout<<"left "<<a[2*i]<<"\n";
cout<<"right "<<a[2*i+1]<<"\n";
}
}
}

EXPERIMENT NO 10
WRITE A PROGRAM TO
ALLOCATION STRATEGY

SIMULATE

#include<stdio.h>
#include<conio.h>
#define maxm 100
#define minm 0
struct memory
{
int blknm,blksz,pro,flag;
}mem[10];
void ff();
void bf();
void wf();
void show();
int noblk, nopro, prosz[10];
void main()
{
int i;
clrscr();
Printf("Enter the no.of memory blocks : ");
scanf("%d",&noblk);
for(i=0;i<noblk;i++)
{
printf("Mem. size of block %d : ",i+1);
scanf("%d",&mem[i].blksz);
}
printf("No.of Process : ");
scanf("%d",&nopro);
for(i=0;i<nopro;i++)
{
printf("Mem. size of Pro.%d : ", i+1);
scanf("%d",&prosz[i]);
}

HEAP

STORAGE

printf("ntFirst Fitnt=========n");
ff();
printf("ntBest Fitnt========n");
bf();
getch();
//return 0;
}
void ff()
{
int i, j;
for(i=0;i<10;i++)
mem[i].flag=0;
for(i=0;i<nopro;i++)
{
for(j=0;j<noblk;j++)
{
if(mem[j].flag != 1 && prosz[i]<=mem[j].blksz)
{
mem[j].pro = i+1;
mem[j].flag = 1;
break;
}
}
}
show();
}
void bf()
{
int i,j,k,min;
for(i=0;i<10;i++)
mem[i].flag = 0;

for(i=0;i<nopro;i++)
{
min = maxm;
for(j=0;j<noblk;j++)
if(mem[j].blksz >= prosz[i] && mem[j].flag != 1)
{
if(min>(mem[j].blksz-prosz[i]))
{
min=mem[j].blksz - prosz[i];
k=j;
}
}
mem[k].pro = i+1;
mem[k].flag = 1;
}
show();
}
void show()
{
int i;
printf("\nMemory Block ProcessnBlockSizeName \n");
for(i=0;i<noblk;i++)
printf("%d\t%d\t%d \n",i+1,mem[i].blksz,mem[i].pro);
}

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