Friday, May 3, 2019
SS 1.A - LEX - RECOGNIZE VALID ARITHMETIC EXPRESSION
1 -A) 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.
lab1.l - PROGRAM
%{
#include<stdio.h>
int v=0,op=0,id=0,flag=0;
%}
%%
[a-zA-Z]+[0-9A-Za-z]* {id++;}
[0-9]+ {id++;}
[\+\-\*/\=] {op++;}
"(" {v++;}
")" {v--;}
";" {flag=1;}
.|\n {return 0;}
%%
int main()
{
printf("Enter the expression:");
yylex();
if((op+1)==id && v==0 && flag==0)
{
printf("\n Expression is Valid\n");
printf("No of identifier = %d \n No of Operators = %d \n",id,op);
}
else
printf("\n Expression is Invalid\n");
return 0;
}
lab1.l - PROGRAM
%{
#include<stdio.h>
int v=0,op=0,id=0,flag=0;
%}
%%
[a-zA-Z]+[0-9A-Za-z]* {id++;}
[0-9]+ {id++;}
[\+\-\*/\=] {op++;}
"(" {v++;}
")" {v--;}
";" {flag=1;}
.|\n {return 0;}
%%
int main()
{
printf("Enter the expression:");
yylex();
if((op+1)==id && v==0 && flag==0)
{
printf("\n Expression is Valid\n");
printf("No of identifier = %d \n No of Operators = %d \n",id,op);
}
else
printf("\n Expression is Invalid\n");
return 0;
}
OUTPUT :( click on image to zoom )
SS 1.B - YACC - EVALUATE ARITHMETIC EXPRESSION
1 - B) Write YACC program to evaluate arithmetic expression involving operators: +, -, *, and /
lab1b.l - PROGRAM
%{
#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;}
%%
lab1b.y - PROGRAM
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token num
%left '+' '-'
%left '*' '/'
%%
input: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 error\n");exit(0);}
else $$=$1/$3;}
|'('exp')'{$$=$2;}
|num{$$=$1;};
%%
int yyerror()
{
printf("error");
exit(0);
}
int main()
{
printf("Enter an expression:\n");
yyparse();
}
STEPS & OUTPUT :( click on image to zoom )
lab1b.l - PROGRAM
%{
#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;}
%%
lab1b.y - PROGRAM
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token num
%left '+' '-'
%left '*' '/'
%%
input: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 error\n");exit(0);}
else $$=$1/$3;}
|'('exp')'{$$=$2;}
|num{$$=$1;};
%%
int yyerror()
{
printf("error");
exit(0);
}
int main()
{
printf("Enter an expression:\n");
yyparse();
}
STEPS & OUTPUT :( click on image to zoom )
SS 2 - YACC - RECOGNIZE ALL STRINGS
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 an b (note: input n value)
lab2.l - PROGRAM
%{
#include "y.tab.h"
%}
%%
a {return A;}
b {return B;}
[\n] return '\n';
%%
lab2.y - PROGRAM
%{
#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);
}
OUTPUT :( click on image to zoom )
lab2.l - PROGRAM
%{
#include "y.tab.h"
%}
%%
a {return A;}
b {return B;}
[\n] return '\n';
%%
lab2.y - PROGRAM
%{
#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);
}
OUTPUT :( click on image to zoom )
SS 3 - C - PREDICTIVE/LL(1) PARSING TABLE
3 - Design, develop and implement YACC/C program to construct Predictive / LL(1) Parsing Table for the grammar rules: A ®aBa , B ®bB | e. Use this table to parse the sentence: abba$
lab3.c - PROGRAM
/*GRAMMER RULES ---- A ->aBa , B ->bB | @*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char prod [3][10]={"A->aBa","B->bB","B->@"};
char first[3][10]={"a","b","@"};
char follow[3][10]={"$","a","a"};
char table[3][4][10];
char input[10];
int top=-1;
char stack[25];
char curp[20];
void push(char item)
{
stack[++top]=item;
}
void pop()
{
top=top-1;
}
void display()
{
int i;
for(i=top;i>=0;i--)
printf("%c",stack[i]);
}
int numr(char c)
{
switch(c)
{
case'A':return 1;
case'B':return 2;
case'a':return 1;
case'b':return 2;
case'@':return 3;
}
return 1;
}
int main()
{
char c;
int i,j,k,n;
for(i=0;i<3;i++){
for(j=0;j<4;j++){
strcpy(table[i][j],"EMPTY");
}
}
printf("\nGrammar\n");
for(i=0;i<3;i++)
printf("%s\n",prod[i]);
printf("\nfirst={%s,%s,%s}",first[0],first[1],first[2]);
printf("\nfollow={%s,%s}\n",follow[0],follow[1]);
printf("\nPredictive parsing table for the given grammar :\n");
strcpy(table[0][0],"");
strcpy(table[0][1],"a");
strcpy(table[0][2],"b");
strcpy(table[0][3],"$");
strcpy(table[1][0],"A");
strcpy(table[2][0],"B");
for(i=0;i<3;i++)
{
if(first[i][0]!='@')
strcpy(table[numr(prod[i][0])][numr(first[i][0])],prod[i]);
else
strcpy(table[numr(prod[i][0])][numr(follow[i][0])],prod[i]);
}
printf("\n-------------------------------------------------------------------\n");
for(i=0;i<3;i++){
for(j=0;j<4;j++)
{
printf("%-30s",table[i][j]);
if(j==3) printf("\n-------------------------------------------------------------------\n");
}
}
printf("Enter the input string terminated with $ to parse:-");
scanf("%s",input);
for(i=0;input[i]!='\0';i++){
if((input[i]!='a')&&(input[i]!='b')&&(input[i]!='$'))
{
printf("Invalid String");
exit(0);
}
}
if(input[i-1]!='$')
{
printf("\n\nInput String Entered Without End Marker $");
exit(0);
}
push('$');
push('A');
i=0;
printf("\n\n");
printf("Stack\t Input\tAction");
printf("\n-------------------------------------------------------------------\n");
while(input[i]!='$'&&stack[top]!='$')
{
display();
printf("\t\t%s\t",(input+i));
if(stack[top]==input[i])
{
printf("\tMatched %c\n", input[i]);
pop();
i++;
}
else
{
if(stack[top]>=65&&stack[top]<92)
{
strcpy(curp,table[numr(stack[top])][numr(input[i])]);
if(!(strcmp(curp,"e")))
{
printf("\nInvalid String - Rejected\n");
exit(0);
}
else
{
printf("\tApply production %s\n",curp);
if(curp[3]=='@')
pop();
else
{
pop();
n=strlen(curp);
for(j=n-1;j>=3;j--)
push(curp[j]);
}
}
}
}
}
display();
printf("\t\t%s\t",(input+i));
printf("\n-------------------------------------------------------------------\n");
if(stack[top]=='$'&&input[i]=='$')
{
printf("\nValid String - Accepted\n");
}
else
{
printf("Invalid String - Rejected\n");
}
}
OUTPUT :( click on image to zoom )
SS 4 - C - SHIFT REDUCE PARSING TECHNIQUE
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.
lab4.c - PROGRAM
#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();
int main()
{
puts("GRAMMAR is E->E+E \n E->E*E \n E->(E) \n E->id");
puts("\nEnter 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%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 E");
for(z=0; z<c; z++)
if(stk[z]=='i' && stk[z+1]=='d')
{
stk[z]='E';
stk[z+1]='\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';
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+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
}
lab4.c - PROGRAM
#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();
int main()
{
puts("GRAMMAR is E->E+E \n E->E*E \n E->(E) \n E->id");
puts("\nEnter 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%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 E");
for(z=0; z<c; z++)
if(stk[z]=='i' && stk[z+1]=='d')
{
stk[z]='E';
stk[z+1]='\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';
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+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
}
OUTPUT :( click on image to zoom )
SS 5 - C - GENERATE MACHINE CODE USING TRIPLES
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
lab5.c - PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
char op[2],arg1[5],arg2[5],result[5];
int 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)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nMOV %s,R0",result);
}
}
fclose(fp1);
fclose(fp2);
}
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
lab5.c - PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
char op[2],arg1[5],arg2[5],result[5];
int 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)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nMOV %s,R0",result);
}
}
fclose(fp1);
fclose(fp2);
}
input.txt
T1 -B = ?
T2 C + D
T3 T1 * T2
A T3 = ?
OUTPUT :( click on image to zoom )
SS 6.A - LEX - ELIMINATE COMMENT LINES IN A C PROGRAM
6 - a) Write a LEX program to eliminate comment lines in a C program and copy the resulting program into a separate file.
lab6.l - PROGRAM
%{
#include<stdio.h>
int sl=0;
int ml=0;
%}
%%
"/*"[a-zA-Z0-9' '\t\n]+"*/" ml++;
"//".* sl++;
%%
main()
{
yyin=fopen("f1.c","r");
yyout=fopen("f2.c","w");
yylex();
fclose(yyin);
fclose(yyout);
printf("\n Number of single line comments are = %d\n",sl);
printf("\nNumber of multiline comments are =%d\n",ml);
}
f1.c FILE
#include<stido.h>
int main()
{
// this is a comment
printf("hello");
/* this is another comment */
}
OUTPUT :( click on image to zoom )
lab6.l - PROGRAM
%{
#include<stdio.h>
int sl=0;
int ml=0;
%}
%%
"/*"[a-zA-Z0-9' '\t\n]+"*/" ml++;
"//".* sl++;
%%
main()
{
yyin=fopen("f1.c","r");
yyout=fopen("f2.c","w");
yylex();
fclose(yyin);
fclose(yyout);
printf("\n Number of single line comments are = %d\n",sl);
printf("\nNumber of multiline comments are =%d\n",ml);
}
f1.c FILE
#include<stido.h>
int main()
{
// this is a comment
printf("hello");
/* this is another comment */
}
OUTPUT :( click on image to zoom )
SS 6.B - YACC - RECOGNIZE VALID IDENTIFIER, OPERATORS AND KEYWORDS
6 - b) Write YACC program to recognize valid identifier, operators and keywords in the given text (C program) file.
lab6.l PROGRAM
%{
#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-Z0-9]+ {printf("identifier is %s\n",yytext);return ID;}
. ;
%%
lab6.y PROGRAM
%{
#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++;}
;
%%
#include <stdio.h>
extern int yylex();
extern int yyparse();
extern FILE *yyin;
main()
{
FILE *myfile = fopen("inputfile.c", "r");
if (!myfile)
{
printf("I can't open inputfile.c!");
return -1;
}
yyin = myfile;
do{
yyparse();
}while (!feof(yyin));
printf("numbers = %d\nKeywords = %d\nIdentifiers = %d\noperators = %d\n",dig, key,id, op);
}
void yyerror()
{
printf("EEK, parse error! Message: ");
exit(-1);
}
inputfile.txt FILE
#include<stdio.h>
int main()
{
int a ;
int b ;
a = 1 ;
b = 2 ;
a = a+b;
return 0 ;
}
OUTPUT :( click on image to zoom )
lab6.l PROGRAM
%{
#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-Z0-9]+ {printf("identifier is %s\n",yytext);return ID;}
. ;
%%
%{
#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++;}
;
%%
#include <stdio.h>
extern int yylex();
extern int yyparse();
extern FILE *yyin;
main()
{
FILE *myfile = fopen("inputfile.c", "r");
if (!myfile)
{
printf("I can't open inputfile.c!");
return -1;
}
yyin = myfile;
do{
yyparse();
}while (!feof(yyin));
printf("numbers = %d\nKeywords = %d\nIdentifiers = %d\noperators = %d\n",dig, key,id, op);
}
void yyerror()
{
printf("EEK, parse error! Message: ");
exit(-1);
}
inputfile.txt FILE
#include<stdio.h>
int main()
{
int a ;
int b ;
a = 1 ;
b = 2 ;
a = a+b;
return 0 ;
}
OUTPUT :( click on image to zoom )
SS 7 - C - ROUND ROBIN (RR) SCHEDULING ALGORITHM
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.
lab7.c PROGRAM
// CPU Scheduling -Round Robin
#include<stdio.h>
struct process
{
char name;
int at,bt,wt,tt,rt;
int completed;
}p[10];
int n;
int q[10]; //queue
int front=-1,rear=-1;
void enqueue(int i)
{
if(rear==10)
printf("overflow");
rear++;
q[rear]=i;
if(front==-1)
front=0;
}
int dequeue()
{
if(front==-1)
printf("underflow");
int temp=q[front];
if(front==rear)
front=rear=-1;
else
front++;
return temp;
}
int isInQueue(int i)
{
int k;
for(k=front;k<=rear;k++)
{
if(q[k]==i)
return 1;
}
return 0;
}
void sortByArrival()
{
struct process temp;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i].at>p[j].at)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
}
int main()
{
int i,j,time=0,sum_bt=0,tq;
char c;
float avgwt=0;
printf("Enter Number of Processes:\n");
scanf("%d",&n);
for(i=0,c='A';i<n;i++,c++)
{
p[i].name=c;
printf("\nProcess %c\n",c);
printf("\tArrival Time :");
scanf("%d",&p[i].at);
printf("\tBurst Time :");
scanf("%d",&p[i].bt);
p[i].rt=p[i].bt;
p[i].completed=0;
sum_bt+=p[i].bt;
}
printf("\nEnter the time quantum:");
scanf("%d",&tq);
sortByArrival();
enqueue(0);
printf("\nProcess execution order: ");
for(time=p[0].at;time<sum_bt;)
{
i=dequeue();
if(p[i].rt<=tq)
{
time+=p[i].rt;
p[i].rt=0;
p[i].completed=1;
printf(" %c ",p[i].name);
p[i].wt=time-p[i].at-p[i].bt;
p[i].tt=time-p[i].at;
for(j=0;j<n;j++)
{
if(p[j].at<=time && p[j].completed!=1&& isInQueue(j)!=1)
{
enqueue(j);
}
}
}
else
{
time+=tq;
p[i].rt-=tq;
printf(" %c ",p[i].name);
for(j=0;j<n;j++)
{
if(p[j].at<=time && p[j].completed!=1&&i!=j&& isInQueue(j)!=1)
{
enqueue(j);
}
}
enqueue(i);
}
}
printf("\n\nName\tArrival Time\tBurst Time\tResponse Time\tTurnAround Time");
for(i=0;i<n;i++)
{
avgwt+=p[i].wt;
printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d",p[i].name,p[i].at,p[i].bt,p[i].wt,p[i].tt);
}
printf("\n\nAverage waiting time:%f",avgwt/n);
}
lab7.c PROGRAM
// CPU Scheduling -Round Robin
#include<stdio.h>
struct process
{
char name;
int at,bt,wt,tt,rt;
int completed;
}p[10];
int n;
int q[10]; //queue
int front=-1,rear=-1;
void enqueue(int i)
{
if(rear==10)
printf("overflow");
rear++;
q[rear]=i;
if(front==-1)
front=0;
}
int dequeue()
{
if(front==-1)
printf("underflow");
int temp=q[front];
if(front==rear)
front=rear=-1;
else
front++;
return temp;
}
int isInQueue(int i)
{
int k;
for(k=front;k<=rear;k++)
{
if(q[k]==i)
return 1;
}
return 0;
}
void sortByArrival()
{
struct process temp;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i].at>p[j].at)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
}
int main()
{
int i,j,time=0,sum_bt=0,tq;
char c;
float avgwt=0;
printf("Enter Number of Processes:\n");
scanf("%d",&n);
for(i=0,c='A';i<n;i++,c++)
{
p[i].name=c;
printf("\nProcess %c\n",c);
printf("\tArrival Time :");
scanf("%d",&p[i].at);
printf("\tBurst Time :");
scanf("%d",&p[i].bt);
p[i].rt=p[i].bt;
p[i].completed=0;
sum_bt+=p[i].bt;
}
printf("\nEnter the time quantum:");
scanf("%d",&tq);
sortByArrival();
enqueue(0);
printf("\nProcess execution order: ");
for(time=p[0].at;time<sum_bt;)
{
i=dequeue();
if(p[i].rt<=tq)
{
time+=p[i].rt;
p[i].rt=0;
p[i].completed=1;
printf(" %c ",p[i].name);
p[i].wt=time-p[i].at-p[i].bt;
p[i].tt=time-p[i].at;
for(j=0;j<n;j++)
{
if(p[j].at<=time && p[j].completed!=1&& isInQueue(j)!=1)
{
enqueue(j);
}
}
}
else
{
time+=tq;
p[i].rt-=tq;
printf(" %c ",p[i].name);
for(j=0;j<n;j++)
{
if(p[j].at<=time && p[j].completed!=1&&i!=j&& isInQueue(j)!=1)
{
enqueue(j);
}
}
enqueue(i);
}
}
printf("\n\nName\tArrival Time\tBurst Time\tResponse Time\tTurnAround Time");
for(i=0;i<n;i++)
{
avgwt+=p[i].wt;
printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d",p[i].name,p[i].at,p[i].bt,p[i].wt,p[i].tt);
}
printf("\n\nAverage waiting time:%f",avgwt/n);
}
OUTPUT :( click on image to zoom )
SS 8 - C - BANKER’S ALGORITHM
8 - Design, develop and implement a C/C++/Java program to implement Banker’s algorithm. Assume suitable input required to demonstrate the results.
lab8.c PROGRAM
#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 = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the 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\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter 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 Max matrix:\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)//if not completed
{
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("\nProcess %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("\nThe system is in a safe state!!\n"); printf("Safe Sequence : < "); for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
OUTPUT :( click on image to zoom )
lab8.c PROGRAM
#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 = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the 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\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter 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 Max matrix:\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)//if not completed
{
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("\nProcess %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("\nThe system is in a safe state!!\n"); printf("Safe Sequence : < "); for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
OUTPUT :( click on image to zoom )
SS 9 - C - PAGE REPLACEMENT ALGORITHMS LRU AND FIFO
9 - 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.
lab9.c PROGRAM
#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("\nEnter the no of empty frames: ");
scanf("%d",&f);
printf("\nEnter the length of the string: ");
scanf("%d",&l);
printf("\nEnter the string: ");
scanf("%s",s);
for(i=0;i<f;i++)
F[i]=-1;
do
{
printf("\n*********** MENU ***********");
printf("\n1:FIFO\n2:LRU \n3:EXIT");
printf("\nEnter 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\nDo u want to continue IF YES PRESS 1\nIF NO PRESS 0 : ");
scanf("%d",&YN);
} while(YN==1);
return(0);
}
void FIFO(char s[],char F[],int l,int f)
{
int i,j=0,k,flag=0,cnt=0;
printf("\n\tPAGE\t FRAMES\t\t\t FAULTS");
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
flag=1;
}
if(flag==0)
{
printf("\n\t%c\t",s[i]);
F[j]=s[i];
j++;
for(k=0;k<f;k++)
printf(" %c",F[k]);
printf("\tPage-fault%d",cnt);
cnt++;
}
else
{
flag=0;
printf("\n\t%c\t",s[i]);
for(k=0;k<f;k++)
printf(" %c",F[k]);
printf("\tNo page-fault");
}
if(j==f)
j=0;
}
}
void lru(char s[],char F[],int l,int f)
{
int i,j=0,k,m,flag=0,cnt=0,top=0;
printf("\n\tPAGE\t FRAMES\t\t\t FAULTS");
for(i=0;i<l;i++)
{
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;
}
}
lab9.c PROGRAM
#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("\nEnter the no of empty frames: ");
scanf("%d",&f);
printf("\nEnter the length of the string: ");
scanf("%d",&l);
printf("\nEnter the string: ");
scanf("%s",s);
for(i=0;i<f;i++)
F[i]=-1;
do
{
printf("\n*********** MENU ***********");
printf("\n1:FIFO\n2:LRU \n3:EXIT");
printf("\nEnter 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\nDo u want to continue IF YES PRESS 1\nIF NO PRESS 0 : ");
scanf("%d",&YN);
} while(YN==1);
return(0);
}
void FIFO(char s[],char F[],int l,int f)
{
int i,j=0,k,flag=0,cnt=0;
printf("\n\tPAGE\t FRAMES\t\t\t FAULTS");
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
flag=1;
}
if(flag==0)
{
printf("\n\t%c\t",s[i]);
F[j]=s[i];
j++;
for(k=0;k<f;k++)
printf(" %c",F[k]);
printf("\tPage-fault%d",cnt);
cnt++;
}
else
{
flag=0;
printf("\n\t%c\t",s[i]);
for(k=0;k<f;k++)
printf(" %c",F[k]);
printf("\tNo page-fault");
}
if(j==f)
j=0;
}
}
void lru(char s[],char F[],int l,int f)
{
int i,j=0,k,m,flag=0,cnt=0,top=0;
printf("\n\tPAGE\t FRAMES\t\t\t FAULTS");
for(i=0;i<l;i++)
{
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;
}
}
OUTPUT :( click on image to zoom )
SS 10.A - C - A NUMERICAL CALCULATOR
10 - a). Design, develop and implement a C/C++/Java program to simulate a numerical calculator.
lab10a.c PROGRAM
#include <stdio.h>
int main()
{
char operator;
float num1, num2, result;
printf("Simulation of a Simple Calculator\n\n");
printf("Enter two numbers \n");
scanf("%f %f", &num1,&num2);
fflush(stdin);
printf("\nEnter the operator [+,-,*,/] \n");
scanf("%s", &operator);
switch(operator)
{
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num1 / num2; break;
default : printf("Error in operation"); break;
}
printf("\n%.2f %c %.2f = %.2f\n", num1, operator, num2, result);
}
lab10a.c PROGRAM
#include <stdio.h>
int main()
{
char operator;
float num1, num2, result;
printf("Simulation of a Simple Calculator\n\n");
printf("Enter two numbers \n");
scanf("%f %f", &num1,&num2);
fflush(stdin);
printf("\nEnter the operator [+,-,*,/] \n");
scanf("%s", &operator);
switch(operator)
{
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num1 / num2; break;
default : printf("Error in operation"); break;
}
printf("\n%.2f %c %.2f = %.2f\n", num1, operator, num2, result);
}
OUTPUT :( click on image to zoom )
SS 10.B - C - PAGE REPLACEMENT TECHNIQUE
10 - b). Design, develop and implement a C/C++/Java program to simulate page replacement technique.
lab10b.c PROGRAM
#include<stdio.h>
int main()
{
int reference_string[10], page_faults = 0, m, n, s, pages, frames;
printf("\nEnter Total Number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter values of Reference String:\n");
for(m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &reference_string[m]);
}
printf("\nEnter Total Number of Frames:\t");
scanf("%d", &frames);
int temp[frames];
for(m = 0; m < frames; m++)
temp[m] = -1;
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(reference_string[m] == temp[n])
{
s++;
page_faults--;
}
}
page_faults++;
if((page_faults <= frames) && (s == 0))
temp[m] = reference_string[m];
else if(s == 0)
temp[(page_faults - 1) % frames] = reference_string[m];
printf("\n");
for(n = 0; n < frames; n++)
printf("%d\t", temp[n]);
}
printf("\nTotal Page Faults:\t%d\n", page_faults);
return 0;
}
lab10b.c PROGRAM
#include<stdio.h>
int main()
{
int reference_string[10], page_faults = 0, m, n, s, pages, frames;
printf("\nEnter Total Number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter values of Reference String:\n");
for(m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &reference_string[m]);
}
printf("\nEnter Total Number of Frames:\t");
scanf("%d", &frames);
int temp[frames];
for(m = 0; m < frames; m++)
temp[m] = -1;
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(reference_string[m] == temp[n])
{
s++;
page_faults--;
}
}
page_faults++;
if((page_faults <= frames) && (s == 0))
temp[m] = reference_string[m];
else if(s == 0)
temp[(page_faults - 1) % frames] = reference_string[m];
printf("\n");
for(n = 0; n < frames; n++)
printf("%d\t", temp[n]);
}
printf("\nTotal Page Faults:\t%d\n", page_faults);
return 0;
}
OUTPUT :( click on image to zoom )
SS 11 - LEX - SENTENCE IS SIMPLE/COMPOUND
11. Program to recognize whether a given sentence is simple or compound.
vi lab11.l
vi lab11.l
%{
int flag=0;
%}
%%
" and " |
" or " |
" but " |
" because " |
" than "|
" nevertheless " {flag=1;}
%%
int main()
{
printf("Enter the sentence:\n");
yylex();
if(flag==1)
printf("compound statement");
else
printf("simple statements\n");
}
OUTPUT :( click on image to zoom )
SS 12 - C++ EMULATE UNIX ln COMMAND
12. Write a C++ program to emulate the Unix ln command
lab12.cpp PROGRAM
lab12.cpp PROGRAM
#include<iostream>
#inculde<unistd.h>
int main(int argc, char* argv[])
{
using namespace std;
if(argc!=3)
{
cout<<"Usage ./a.out sourcefile destination file\n";
return 0;
}
if(link(argv[1],argv[2])==-1)
{
cout<<"cant link\n";
return 1;
}
else
{
cout<<"Files have been Linked\n";
}
return 0;
}
OUTPUT :( click on image to zoom )
Subscribe to:
Posts (Atom)