System Software and Compiler Design Laboratory
System Software and Compiler Design Laboratory
System Software and Compiler Design Laboratory
The idea behind using three files is to differentiate between the developer and user sides. In the developer
side, all the three files could be made visible. For the user side only header file and application files could be
made visible, which means that the object code of the implementation file could be given to the user along
with the interface given in the header file, hiding the source file, if required. Avoid I/O operations
(printf/scanf) and use data input file where ever it is possible.
Installation procedure of the required software must be demonstrated, carried out in groups and
documented in the journal.
1. Write a LEX program to recognize valid arithmetic expression. Identifiers in the expression
could be only integers and operators could be + and *. Count the identifiers &operators present
and print them separately.
2. Write YACC program to evaluate arithmetic expression involving operators: +, -, *, and /
3. Develop, Implement and Execute a program using YACC tool to recognize all strings ending with
b preceded by n a’s using the grammar an b (note: input n value)
4. Write a YACC program to recognize valid identifier, operators and keywords in the given text
file.
5. Design, develop and implement YACC/C program to demonstrate Shift Reduce Parsing
technique for the grammar rules: E →E+T | T, T →T*F | F, F →(E) | id andparse the
sentence: id + id * id.
6. Design, develop and implement a C/Java program to generate the machine code using Triples
for the statement A = -B * (C +D) whose intermediate code in three-address form:
T1 = -B
T2 = C + D
T3 = T1 + T2
A = T3
System Software and Compiler Design Laboratory
7. Write a LEX program to count the number of words, characters, lines and blank in a given
input file.
8. Write a LEX program to eliminate comment lines in a C program and copy the resulting
program into a separate file.
9. Design, develop and implement a C/C++/Java program to implement Banker’s algorithm.
Assume suitable input required to demonstrate the results
10. Design, develop and implement a C/C++/Java program to implement page replacement
algorithms LRU and FIFO. Assume suitable input required to demonstrate the results.
1. Write a LEX program to recognize valid arithmetic expression. Identifiers in the expression
could be only integers and operators could be + and *. Count the identifiers &operators present
System Software and Compiler Design Laboratory
%{
int plus=0,minus=0,mult=0,divi=0;
int opnd=0,op=0;
int n;
%}
%%
[0-9a-zA-Z]+ {opnd++;}
[0-9a-zA-Z]+\+\-\*\/[0-9A-Za-z]+ {n=0;}
[\+]+ {plus++;op++;}
[\-]+ {minus++;op++;}
[\*]+ {mult++;op++;}
[\/]+ {divi++;op++;}
%%
main()
{
printf("enter the expression");
yylex();
printf("\n +::=%d",plus);
printf("\n -::=%d",minus);
printf("\n *::=%d",mult);
printf("\n /::=%d",divi);
printf("\n number of operators =%d",op);
printf("\n number of operands =%d",opnd);
if((n==0)&&(opnd=op+1))
printf("\n valid expresion");
else
printf("\n invalid expression");
}
System Software and Compiler Design Laboratory
System Software and Compiler Design Laboratory
2. Write YACC program to evaluate arithmetic expression involving operators:+, -, *, and /n value).
//LEX PART//
%{
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext); return NUMBER;}
"(" {return OPB;}
")" {return CPB;}
[\+] {return PLUS;}
[\-] {return MINUS;}
[\*] {return MULTI;}
[\/] {return DIVI;}
;
%%
//YACC PART//
%token NUMBER PLUS MINUS MULTI DIVI OPB CPB
%left '+' '-' '*' '/'
%nonassoc UMINUS
%%
st:exp{printf("valid expression\n");};
exp:exp PLUS exp
|exp MINUS exp
|exp MULTI exp
|exp DIVI exp
|OPB exp CPB
|NUMBER;
%%
main()
{
printf("enter the expression");
yyparse();
}
yyerror(char * S)
{
printf("invalid expression\n");
}
System Software and Compiler Design Laboratory
System Software and Compiler Design Laboratory
3. Develop, Implement and Execute a program using YACC tool to recognize all strings ending with b
preceded by n a’s using the grammar an b (note: input n value)
//Lex part//
%{
#i ncl ude" y.t a b.c"
%}
%%
[ a] { r et u r n I DA; }
[ b] { r et u r n I DB; }
. ;
%%
//YACC PART//
%%
stmt:stmt1 IDB
stmt1: IDA stmt1|IDA IDA IDA IDA IDA IDA IDA IDA IDA
%%
main()
yyparse();
if(!(yyerror()))
printf("valid\n");
else
System Software and Compiler Design Laboratory
yyerror()
}
System Software and Compiler Design Laboratory
4. Write a YACC program to recognize valid identifier, operators and keywords in the given text
file.
//LEX PART//
%{
#include<stdio.h>
#include "y.tab.h"
extern yylval;
%}
%%
[\t];
[+|-|*|/|=|<|>] {printf("operator is %s\n",yytext);return OP;}
//YACC PART//
%{
#include<stdio.h>
#include<stdlib.h>
int id=0,dig=0,key=0,op=0;
%}
|KEY input{key++;}
|OP input{op++;}
|DIGIT {dig++;}
|ID {id++;}
|KEY {key++;}
|OP {op++;}
;
%%
#include<stdio.h>
extern int yylex();
extern int yyparse();
extern FILE *yyin;
main()
{
FILE *myfile=fopen("sample.c","r");
if(!myfile)
{
printf("i cant open sample.c!");
return -1;
}
yyin=myfile;
do
{
yyparse();
}
while(!feof(yyin));
printf("number =%d\n Keywords=%d\n Identifier=%d\n Operators=%d\n",dig,key,id,op);
}
void yyerror()
{
printf("EEK,parse error !Message:");
exit(-1);
}
System Software and Compiler Design Laboratory
System Software and Compiler Design Laboratory
System Software and Compiler Design Laboratory
System Software and Compiler Design Laboratory
5. Design, develop and implement YACC/C program to demonstrate Shift Reduce Parsing technique for
the grammar rules: E →E+T | T, T →T*F | F, F →(E) | id andparse the sentence: id + id * id.
#include<stdio.h>
#include<string.h>
int k=0,z=0,i=0,j=0,c=0;
char a[16],ac[20],stk[15],act[10];
void check();
void main()
{
puts("GRAMMER is E->E+E \n E->E*E \n E->(E) \n E->ID");
puts("enter input string ");
gets(a);
c=strlen(a);
strcpy(act,"SHIFT->");
puts("stack \t input \t action");
for(k=0,i=0;j<c;k++,i++,j++)
{
if(a[j]=='i' && a[j+1]=='d')
{
stk[i]=a[j];
stk[i+1]=a[j+1];
stk[i+2]='\0';
a[j]=' ';
a[j+1]=' ';
printf("\n$%s\t%s$\t%ssymbols",stk,a,act);
check();
}
}
}
void check()
{
strcpy(ac,"REDUCE TO E");
for(z=0;z<c;z++)
if(stk[z]=='i' && stk[z+1]=='d')
{
stk[z]='E';
stk[z]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
j++;
}
for(z=0;z<c;z++)
if(stk[z]=='E' && stk[z+1]=='+' && stk[z+2]=='E')
{
stk[z]='E';
stk[z+1]='\0';
System Software and Compiler Design Laboratory
stk[z+2]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
for(z=0;z<c;z++)
if(stk[z]='E' && stk[z+1]=='*' && stk[z+2]=='E')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
for(z=0;z<c;z++)
if(stk[z]=='(' && stk[z+1]=='E' && stk[z+2]==')')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
}
System Software and Compiler Design Laboratory
System Software and Compiler Design Laboratory
6. Design, develop and implement a C/Java program to generate the machine code using Triples for the
statement A = -B * (C +D) whose intermediate code in three-address form:
T1 = -B
T2 = C + D
T3 = T1 + T2
A = T3
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
char op[2],arg1[5],arg2[5],result[5];
main()
{
FILE *fp1,*fp2;
fp1=fopen("input.txt","r");
fp2=fopen("output.txt","w");
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s%s",result,arg1,op,arg2);
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)
{
System Software and Compiler Design Laboratory
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nMOV %s,R0",result);
}
}
fclose(fp1);
fclose(fp2);
}
System Software and Compiler Design Laboratory
System Software and Compiler Design Laboratory
7. Write a LEX program to count the number of words, characters, lines and blank in a given input file.
%{
unsigned cc=0,wc=0,bc=0,lc=0;
%}
word [^ \t \n ]+
blank [ ]+
eol \n
%%
{word} {wc++;cc+=yyleng;}
{blank} {bc++;}
{eol} {cc++;lc++;}
. cc++;
%%
8. Write a LEX program to eliminate comment lines in a C program and copy the resulting program
into a separate file.
%{
int comment;
%}
%%
^[\t]*"/*".*"*/"[\t]*\n {comment++;}
%%
9. Design, develop and implement a C/C++/Java program to implement Banker’s algorithm. Assume
suitable input required to demonstrate the results.
#include<stdio.h>
int curr[5][5],maxclaim[5][5],avl[5];
int alloc[5]={0,0,0,0,0};
int maxres[5],running[5],safe=0;
int count=0,i,j,exec,r,p,k=1;
int main()
{
printf("enter no of processes:");
scanf("%d",&p);
for(i=0;i<p;i++)
{
running[i]=1;
count++;
}
printf("\nenter no resources:");
scanf("%d",&r);
for(i=0;i<r;i++)
{
printf("enter the resources for instances%d:",k++);
scanf("%d",&maxres[i]);
}
printf("enter maximum resources table\n");
for(i=0;i<p;i++)
System Software and Compiler Design Laboratory
{
for(j=0;j<r;j++)
{
scanf("%d",&maxclaim[i][j]);
}
}
printf("enter allocated resources table\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&curr[i][j]);
}
}
printf("\n the resources of instances:");
for(i=0;i<r;i++)
{
printf("\t%d",maxres[i]);
}
printf("\n the allocated resources table:\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
printf("\t%d",curr[i][j]);
}
printf("\n");
}
printf("\nmaximum resources table\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
printf("\t%d",maxclaim[i][j]);
}
printf("\n");
}
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
alloc[j]+=curr[i][j];
}
}
printf("\n allocated resources:");
for(i=0;i<r;i++)
{
printf("\t%d",alloc[i]);
}
for(i=0;i<r;i++)
System Software and Compiler Design Laboratory
{
avl[i]=maxres[i]-alloc[i];
}
printf("\navailable resources");
for(i=0;i<r;i++)
{
printf("\t%d",avl[i]);
}
printf("\n");
while(count!=0)
{
safe=0;
for(i=0;i<p;i++)
{
if(running[i])
{
exec=1;
for(j=0;j<r;j++)
{
if(maxclaim[i][j]-curr[i][j]>avl[j])
{
exec=0;
break;
}
}
if(exec)
{
printf("\n process %d is executing\n",i+1);
running[i]=0;
count--;
safe=1;
for(j=0;j<r;j++)
{
avl[j]+=curr[i][j];
}
break;
}
}
}
if(!safe)
{
printf("\n process are in unsafe state\n");
break;
}
else
{
printf("\n process are in safe state\n");
printf("\nsafe sequences is:\n");
for(i=0;i<r;i++)
{
System Software and Compiler Design Laboratory
printf("\t%d",avl[i]);
}
printf("\n");
}
}
}
System Software and Compiler Design Laboratory
System Software and Compiler Design Laboratory
10. Design, develop and implement a C/C++/Java program to implement page replacement algorithms LRU
and FIFO. Assume suitable input required to demonstrate the results.
#include<stdio.h>
#include<stdlib.h>
void FIFO(char[],char[],int,int);
void lru(char[],char[],int,int);
void opt(char[],char[],int,int);
int main()
{
int ch,YN=1,i,l,f;
char F[10],s[25];
printf("\n\n\tEnter the number of empty frames:");
scanf("%d",&f);
printf("\n\n\tEnter the length of string:");
scanf("%d",&l);
printf("\n\n\tEnter the string:");
scanf("%s",s);
for(i=0;i<f;i++);
F[i]=-1;
do
{
printf("\n\n\t*********MENU********");
printf("\n\n\t 1:FIFO\n\n\t 2:LRU\n\n\t 3:EXIT");
printf("\n\n\tEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
for(i=0;i<f;i++)
{
F[i]=-1;
}
FIFO(s,F,l,f);
break;
case 2:
for(i=0;i<f;i++)
{
F[i]=-1;
}
lru(s,F,l,f);
break;
case 3:
exit(0);
}
System Software and Compiler Design Laboratory
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
{
flag=1;
break;
}
}
printf("\n\t%c\t",s[i]);
if(j!=f&&flag!=1)
{
F[top]=s[i];
j++;
if(j!=f)
top++;
}
else
{
if(flag!=1)
{
for(k=0;k<top;k++)
{
F[k]=F[k+1];
}
F[top]=s[i];
}
if(flag==1)
{
for(m=k;m<top;m++)
{
F[m]=F[m+1];
}
F[top]=s[i];
}
}
for(k=0;k<f;k++)
{
printf("%c",F[k]);
}
if(flag==0)
{
printf("\tPage-fault%d",cnt);
cnt++;
}
else
printf("\tNo page fault");
flag==0;
}
}
System Software and Compiler Design Laboratory
System Software and Compiler Design Laboratory