Ssman 2
Ssman 2
5. 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
6.
a. Write a LEX program to eliminate comment lines in a C program and copy the resulting
program into a separate file.
b. Write YACC program to recognize valid identifier, operators and keywords in the given text
(C program) file.
7. Design, develop and implement a C/C++/Java program to simulate the working of Shortest
remaining time and Round Robin (RR) scheduling algorithms. Experiment with different
quantum sizes for RR algorithm.
Execution Steps:
Lex <lexfilename.l>
cc lex.yy.c –ll
. /a.out
Output:
Lex Part
%{
#include "y.tab.h"
extern yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext); return num;}
[\+\-\*\/] {return yytext[0];}
[)] {return yytext[0];}
[(] {return yytext[0];}
. {;}
\n {return 0;}
%%
YACC Part
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token num
%left '+' '-'
%left '*' '/'
%%
S:exp { printf("%d\n",$$); exit(0); }
exp:exp'+'exp {$$=$1+$3;}
|exp'-'exp {$$=$1-$3;}
|exp'*'exp {$$=$1*$3;}
|exp'/'exp { if($3==0)
{ printf("Divide by Zero\n");exit(0); }
else
$$=$1/$3; }
|'('exp')' {$$=$2;}
|num {$$=$1;};
%%
int yyerror()
{
printf("error");
exit(0);
}
int main()
{
printf("Enter an expression:\n");
yyparse();
}
Execution Steps:
YACC –d <yaccfilename.y>
lex <lexfilename.l>
cc y.tab.c lex.yy.c –ll
./a.out
Output:
2. Develop, Implement and execute a program using YACC tool to recognize all strings
ending with b preceded by n a’s using the grammar a n b (note: input n value).
Lex Part
%{
#include "y.tab.h"
%}
%%
a { return A; }
b { return B; }
[\n] return '\n';
%%
YACC Part
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token A B
%%
input :s'\n' { printf("Successful Grammar\n"); exit(0); }
s: A s1 B| B
s1: ; | A s1
%%
main()
{
printf("Enter A String\n");
yyparse();
}
int yyerror()
{
printf("Error \n");
exit(0);
}
Execution Steps:
YACC –d <yaccfilename.y>
lex <lexfilename.l>
cc y.tab.c lex.yy.c –ll
/a.out
Output:
3. Design, develop and implement YACC/C program to construct Predictive / LL(1) Parsing
Table for the grammar rules: A →aBa , B →bB | ε. Use this table to parse the sentence:
abba$.
#include<stdio.h>
#include<string.h>
char prod[3][15]={"A->aBa","B->bB","B->@"};
char table[2][3][3]={{"aBa"," "," "}, {"@","bB"," "}};
int size[2][3]={3,0,0,1,2,0},n;
char s[20],stack[20];
void display(int i,int j)
{
int k;
for(k=0;k<=i;k++)
printf("%c",stack[k]);
printf("\t");
for(k=j;k<n;k++)
printf("%c",s[k]);
printf("\n");
}
int main()
{
int i,j,k,row,col,flag=0;
printf("\n the grammar is:\n");
for(i=0;i<3;i++)
printf("%s\n",prod[i]);
printf("\n predicting parsing table is\n\n");
printf("\ta\tb\t$\n");
printf("----------------\n");
for(i=0;i<2;i++)
{
if(i==0)printf("A");
else printf("\nB");
for(j=0;j<3;j++)
{
printf("\t%s",table[i][j]);
}
}
printf("\n enter the input string:");
scanf("%s",s);
printf("\n stack input");
printf("\n------------------------\n");
printf("$A\t%s$\n",s);
strcat(s,"$");
n=strlen(s);
stack[0]='$';
stack[1]='A';
i=1;
j=0;
while(1)
{
if(stack[i]==s[j])
{
i--;
j++;
if(stack[i]=='$' && s[j]=='$')
{
printf("$ $\n SUCCESS\n");
break;
}
else
if(stack[i]=='$' && s[j]!='$')
{
printf("ERROR\n");
break;
}
display(i,j);
}
switch(stack[i])
{
case 'A':row=0;
break;
case'B':row=1;
break;
}
switch(s[j])
{
case 'a':col=0;
break;
case 'b':col=1;
break;
case '$':col=2;
break;
}
if(table[row][col][0]=='\0')
{
printf("\n ERROR\n");
break;
}
else if(table[row][col][0]=='@')
{
i--;
display(i,j);
}
else
{
for(k=size[row][col]-1;k>=0;k--)
{
stack[i]=table[row][col][k];
i++;
}
i--;
display(i,j);
}
}
}
Execution Steps:
cc filename.c
./a.out
Output:
4. 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 and parse the
sentence: id + id * id.
#include<string.h>
#include<stdio.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()
{
printf("GRAMMAR is E->E+T|T \n T->T*F|F \n F->(E)|id \n");
printf("enter input string ");
scanf("%s",a);
c=strlen(a);
strcpy(act,"SHIFT->");
printf("stack \t input \t action\n");
printf(“---------------------------------------------“);
printf("\n$\t%s\t",a);
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%sid",stk,a,act);
check();
}
else
{
stk[i]=a[j];
stk[i+1]='\0';
a[j]=' ';
printf("\n$%s\t%s$\t%ssymbols",stk,a,act);
check();
}
}
}
void check()
{
strcpy(ac,"REDUCE TO ");
for(z=0; z<c; z++)
if(stk[z]=='i' && stk[z+1]=='d')
{
stk[z]='F';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%sF",stk,a,ac);
j++;
}
5. 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>
char op[2],arg1[5],arg2[5],result[5];
int count;
void main()
{
FILE *fp1,*fp2;
fp1=fopen("input.txt","r");
fp2=fopen("output.txt","w");
while(!feof(fp1)&&count<4)
{
count++;
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)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nMOV %s,R0",result);
}
}
fclose(fp1);
fclose(fp2);
}
Execution Steps:
cc filename.c
./a.out
Output:
6. a) Write a LEX program to eliminate comment lines in a C program and copy the resulting
program into a separate file.
%{
#include<stdio.h>
int c_count=0;
%}
%%
"/*"[^*/].*"*/" { c_count++; }
"//".* { c_count++; }
%%
int main( int argc, char **argv)
{
if(argc>1)
{
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
yylex();
printf("Number of Comment Lines: %d\n",c_count);
}
return 0;
}
Execution Steps:
Lex <lexfilename.l>
cc lex.yy.c –ll
. /a.out a.c b.c
Output:
b) Write YACC program to recognize valid identifier, operators and keywords in the given text
(C program) file.
Lex File
%{
#include <stdio.h>
#include "y.tab.h"
extern yylval;
%}
%%
[ \t] ;
[+|-|*|/|=|<|>] {printf("operator is %s\n",yytext); return OP;}
[0-9]+ {yylval = atoi(yytext); printf("numbers is %d\n",yylval); return DIGIT;}
int|char|bool|float|void|for|do|while|if|else|return|void {printf("keyword
is %s\n",yytext);return KEY;}
[a-zA-Z][a-zA-Z0-9]+ {printf("identifier is %s\n",yytext);return ID;}
. ;
%%
Yacc File
%{
#include <stdio.h>
#include <stdlib.h>
int id=0, dig=0, key=0, op=0;
%}
%token DIGIT ID KEY OP
%%
input: DIGIT input { dig++; }
| ID input { id++; }
| KEY input { key++; }
| OP input {op++;}
| DIGIT { dig++; }
| ID { id++; }
| KEY { key++; }
| OP { op++;}
;
%%
main()
{
yyin = fopen("input.c", "r");
do {
yyparse();
} while (!feof(yyin));
printf("numbers = %d\nKeywords = %d\nIdentifiers = %d\noperators =
%d\n", dig, key,id, op);
}
int yyerror() {
printf("EEK, parse error! Message: ");
exit(-1);
}
Execution Steps:
YACC –d <yaccfilename.y>
lex <lexfilename.l>
cc y.tab.c lex.yy.c –ll
./a.out
Output:
7. Design, develop and implement a C/C++/Java program to simulate the working of Shortest
remaining time and Round Robin (RR) scheduling algorithms. Experiment with different
quantum sizes for RR algorithm.
#include<stdio.h>
int main()
{
int count,j,n,time,flag=0,tq,ch=0;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
int endTime,i,smallest;
int remain=0,sum_wait=0,sum_turnaround=0;
printf("1.Round Robin\n2.SRTF\n");
scanf("%d",&ch);
printf("Enter number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter arrival time for process p %d:",i+1);
scanf("%d",&at[i]);
printf("enter burst time for process p %d:",i+1);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
switch(ch)
{
case 1:printf("Enter Time Quantum:\t");
scanf("%d",&tq);
remain=n;
printf("\n Process time|turnaround Time|waiting Time\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=tq && rt[count]>0)
{
time +=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=tq;
time +=tq;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("p[%d]\t|\t%d\t|\t%d\n",count+1,time-
at[count],time-at[count]-bt[count]);
wait_time +=time-at[count]-bt[count];
Dept. of CSE, RIT, Hassan 20
SS AND OS LAB (18CSL66)
turnaround_time +=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else count=0;
}
printf("\n Average waiting time=%0.2f\n",wait_time*1.0/n);
printf("Avg Turnaround time=%0.2f\n",turnaround_time*1.0/n);
break;
case 2: remain=0;
printf("\n process|Turnaround Time|waiting time\n");
rt[9]=9999;
for(time=0;remain!=n;time++)
{
smallest=9;
for(i=0;i<n;i++)
if(at[i]<=time && rt[i]<rt[smallest] &&
rt[i]>0)
smallest=i;
rt[smallest]--;
if(rt[smallest]==0)
{
remain++;
endTime=time+1;
printf("\np[%d]\t|\t%d\t|\t%d", smallest+1,
endTime-at[smallest],endTime-bt[smallest]-
at[smallest]);
printf("\n");
sum_wait +=endTime-bt[smallest]-at[smallest];
sum_turnaround +=endTime-at[smallest];
}
}
printf("\n Average waiting time=%f\n",sum_wait*1.0/n);
printf("Average Turnaround time=%f\n",sum_turnaround*1.0/n);
break;
}
return 0;
}
Execution Steps:
cc filename.c
./a.out
Output:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int max[10][10], need[10][10], alloc[10][10], avail[10],
completed[10], safesequence[10];
int p,r,i,j,process,count;
count=0;
printf("enter number of process:");
scanf("%d",&p);
for(i=0;i<p;i++)
completed[i]=0;
printf("\n\nEnter the number of resources:");
scanf("%d",&r);
printf("\n\nEnter max matrix for each process:");
for(i=0;i<p;i++)
{
printf("\nFor process%d:",i+1);
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\n\n Enter the allocation for each process:");
for(i=0;i<p;i++)
{
printf("\n For process %d:",i+1);
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
}
printf("\n\n Enter the available resources:");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
do
{
printf("\n MaxMatrix:\tAllocation Matrix:\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
printf("%d",max[i][j]);
printf("\t\t");
for(j=0;j<r;j++)
printf("%d",alloc[i][j]);
printf("\n");
}
process=-1;
for(i=0;i<p;i++)
{
if(completed[i]==0)
{
process=i;
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
{
process=-1;
break;
}
}
}
if(process!=-1)
break;
}
if(process!=-1)
{
printf("\n process %d runs to completion!",
process+1);
safesequence[count]=process+1;
count++;
for(j=0;j<r;j++)
{
avail[j]+=alloc[process][j];
alloc[process][j]=0;
max[process][j]=0;
completed[process]=1;
}
}
}while(count!=p && process!=-1);
if(count==p)
{
printf("\n The system is in safe state!\n");
printf("Safe Sequence:<");
for(i=0;i<p;i++)
printf("%d",safesequence[i]);
printf(">\n");
}
else
printf("\n The system is in an unsafe state!");
return 0;
}
Execution Steps:
cc filename.c
./a.out
Output:
#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 no of empty frames: ");
scanf("%d",&f);
printf("\n\n\tEnter the length of the 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\t1:FIFO\n\n\t2:LRU\n\n\t3: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);
}
printf("\n\n\tDo u want to continue IF YES PRESS 1\n\n\t IF
NO PRESS 0 :");
scanf("%d",&YN);
}while(YN==1);
return(0);
}
Dept. of CSE, RIT, Hassan 27
SS AND OS LAB (18CSL66)
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;
}
}
Execution Steps:
cc filename.c
./a.out
Output: