Os and CD All Lab Exp
Os and CD All Lab Exp
%{
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* {printf("\n%s is a preprocessor directive",yytext);}
int |
float |
char |
double |
while |
for |
struct |
typedef |
do |
if |
break |
continue |
void |
switch |
return |
else |
goto {printf("\n\t%s is a keyword",yytext);}
"/*" {COMMENT=1;}{printf("\n\t %s is a COMMENT",yytext);}
{identifier}\( {if(!COMMENT)printf("\nFUNCTION \n\t%s",yytext);}
\{ {if(!COMMENT)printf("\n BLOCK BEGINS");}
\} {if(!COMMENT)printf("BLOCK ENDS ");}
{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}
\".*\" {if(!COMMENT)printf("\n\t %s is a STRING",yytext);}
[0-9]+ {if(!COMMENT) printf("\n %s is a NUMBER ",yytext);}
\)(\:)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");}
\( ECHO;
= {if(!COMMENT)printf("\n\t %s is an ASSIGNMENT OPERATOR",yytext);}
\<= |
\>= |
\< |
== |
\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
%%
{
printf("could not open the file");
exit(0);
}
yyin=file;
yylex();
printf("\n");
return(0);
}
int yywrap()
{
return(1);
}
INPUT:
//var.c
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
a=1;
b=2;
c=a+b;
printf("Sum:%d",c);
}
Output:
EX-2
%%
[a-zA-Z]+ {printf("No character other than alphabets");}
// code section
int yywrap() { }
int main()
{
yylex();
return 0;
}
Ex-3
#include<stdio.h>
//#include<conio.h>
#include<string.h>
char input[100];
int i,l;
int main()
printf("\n E->TEP|\nEP->+TEP|@|\nT->FTP|\nTP->*FTP|@|\nF->(E)|ID\n");
scanf("%s",input);
if(E()){
if(input[i]=='$')
else
E(){
if(T()){
if(EP())
return(1);
else
return(0);
else
return(0);
EP(){
if(input[i]=='+'){
i++;
if(T()){
if(EP())
return(1);
else
return(0);
else
return(1); }}
T(){
if(F()){
if(TP())
return(1);
else
return(0); }
else
return(0);
TP(){
if(input[i]=='*'){
i++;
if(F()){
if(TP())
return(1)
else
return(0); }
else
return(0);
else
return(1); }
F(){
if(input[i]=='('){
i++;
if(E()){
if(input[i]==')'){
i++;
return(1); }
else
return(0);
}
else
return(0);
else
if(input[i]>='a'&&input[i]<='z'||input[i]>='A'&&input[i]<='Z')
i++;
return(1);
else
return(0);
}
OUTPUT:
$ cc rdp.c
$ ./a.out
E->TEP|
EP->+TEP|@|
T->FTP|
TP->*FTP|@|
F->(E)|ID
string is accepted
EX-4
Write a C Program to compute the First and Follow sets for the given Grammar.
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int count, n = 0;
char calc_first[10][100];
char calc_follow[10][100];
int m = 0;
// Stores the production rules
char production[10][10];
int k;
char ck;
int e;
int jm = 0;
int km = 0;
int i, choice;
char c, ch;
count = 8;
strcpy(production[0], "E=TR");
strcpy(production[1], "R=+TR");
strcpy(production[2], "R=#");
strcpy(production[3], "T=FY");
strcpy(production[4], "Y=*FY");
strcpy(production[5], "Y=#");
strcpy(production[6], "F=(E)");
strcpy(production[7], "F=i");
int kay;
char done[count];
calc_first[k][kay] = '!';
c = production[k][0];
point2 = 0;
xxx = 0;
xxx = 1;
if (xxx == 1)
continue;
// Function call
findfirst(c, 0, 0);
ptr += 1;
done[ptr] = c;
calc_first[point1][point2++] = c;
if (first[i] == calc_first[point1][lark])
chk = 1;
break;
}
if(chk == 0)
calc_first[point1][point2++] = first[i];
printf("}\n");
jm = n;
point1++;
printf("\n");
printf("-----------------------------------------------\n\n");
char donee[count];
ptr = -1;
point1 = 0;
int land = 0;
ck = production[e][0];
point2 = 0;
xxx = 0;
// Checking if Follow of ck
if(ck == donee[kay])
xxx = 1;
if (xxx == 1)
continue;
land += 1;
// Function call
follow(ck);
ptr += 1;
donee[ptr] = ck;
calc_follow[point1][point2++] = ck;
if (f[i] == calc_follow[point1][lark])
chk = 1;
break;
if(chk == 0)
printf(" }\n\n");
km = m;
point1++;
void follow(char c)
int i, j;
if(production[0][0] == c) {
f[m++] = '$';
{
if(production[i][j] == c)
if(production[i][j+1] != '\0')
followfirst(production[i][j+1], i, (j+2));
follow(production[i][0]);
{
int j;
// encounter a Terminal
if(!(isupper(c))) {
first[n++] = c;
if(production[j][0] == c)
if(production[j][2] == '#')
if(production[q1][q2] == '\0')
first[n++] = '#';
else
first[n++] = '#';
else if(!isupper(production[j][2]))
first[n++] = production[j][2];
else
// at the beginning
findfirst(production[j][2], j, 3);
{
int k;
// a Terminal
if(!(isupper(c)))
f[m++] = c;
else
int i = 0, j = 1;
if(calc_first[i][0] == c)
break;
while(calc_first[i][j] != '!')
if(calc_first[i][j] != '#')
{
f[m++] = calc_first[i][j];
else
if(production[c1][c2] == '\0')
// end of a production
follow(production[c1][0]);
else
j++;
}
}
Output :
First(E)= { (, i, }
First(R)= { +, #, }
First(T)= { (, i, }
First(Y)= { *, #, }
First(F)= { (, i, }
-----------------------------------------------
Follow(E) = { $, ), }
Follow(R) = { $, ), }
Follow(T) = { +, $, ), }
Follow(Y) = { +, $, ), }
Follow(F) = { *, +, $, ), }
Ex-5
Write a C Program to check the validity of input string using predictive parser.
#include<stdio.h>
#include<string.h>
int spt=0,ipt=0;
char s[20],ip[15];
char
*m[5][6]={{"TG","\0","\0","TG","\0","\0"},{"\0","+TG","\0","\0","e","e"},{"FH","\0","\0","
FH","\0","\0"},{"\0","e","FH","\0","e","e"},{"i","\0","\0","(E)","\0","\0"}};
char nt[5]={'E','G','T','H','F'};
char t[6]={'i','+','*','(',')','$'};
int nti(char c) {
int i;
for(i=0;i<5;i++){
if(nt[i]==c)
return(i); }
return(6); }
int ti(char c) {
int i;
for(i=0;i<6;i++) {
if(t[i]==c)
return(i); }
return(7); }
int main()
{
char prod[4],temp[4];
int l,k,j;
printf(
scanf("%s",ip);
strcat(ip,"$");
s[0]='$';
s[1]='E';
s[2]='\0';
spt=1;
while(1) {
if(ip[ipt]=='$'&&s[spt]=='$')
break;
if(ti(s[spt])<5||s[spt]=='$') {
if(s[spt]==ip[ipt]) {
spt--
ipt++; }
else
error();
else if(nti(s[spt]<6))
py(prod,m[nti(s[spt])][ti(ip[ipt])]);
if(prod=='\0')
perror();
l=strlen(prod);
for(k=l
-1,j=0;k>=0&&j<=l;k--,j++)
temp[j]=prod[k];
for(k=0;k<l;k++)
prod[k]=temp[k];
s[spt--]='\0';
strcat(s,prod);
spt=spt+l;
if(s[spt]=='e')
s[spt--]='\0';
else
error(); }
if(s[spt]=='$'&&ip[ipt]=='$')
else
error();
return 0; }
error()
exit(1);
return 0;
$ cc preparsing.c
$ ./a.out
input is parsed
OS 1(A)
#include <stdio.h>
int main() {
scanf("%d", &n);
remain = n;
printf("Enter Arrival Time & Burst Time for process number %d: ", count + 1);
scanf("%d", &at[count]);
scanf("%d", &bt[count]);
scanf("%d", &time_quantum);
flag = 1;
remain--;
} else {
}
OS 1(A)
if (flag == 0) {
time++;
return 0;
OS 1 (B)
#include <stdio.h>
int main() {
scanf("%d", &n);
scanf("%d", &bt[i]);
pos = i;
pos = j;
bt[i] = bt[pos];
bt[pos] = temp;
temp = P[i];
P[i] = P[pos];
P[pos] = temp;
wt[0] = 0;
wt[i] = 0;
wt[i] += bt[j];
total += wt[i];
avg_wt = (float)total / n;
total = 0;
total += tat[i];
}
OS 1(B)
avg_tat = (float)total / n;
return 0;
OS 1(C)
#include <stdio.h>
int main() {
scanf("%d", &n);
scanf("%d", &bt[i]);
printf("Priority: ");
scanf("%d", &pr[i]);
pos = i;
pos = j;
temp = pr[i];
pr[i] = pr[pos];
pr[pos] = temp;
temp = bt[i];
bt[i] = bt[pos];
bt[pos] = temp;
temp = p[i];
p[i] = p[pos];
p[pos] = temp;
wt[0] = 0;
wt[i] = 0;
wt[i] += bt[j];
total += wt[i];
avg_wt = (float)total / n;
total = 0;
total += tat[i];
avg_tat = (float)total / n;
return 0;
}
OS 2-FORK()
#include <stdio.h>
#include <unistd.h>
void fork_example() {
if (fork() == 0) {
} else {
int main() {
fork_example();
return 0;
OS 2 WAIT()
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
pid_t cpid;
if (fork() == 0) {
exit(0);
} else {
return 0;
}
OS 2-EXEC()
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
if (pid < 0) {
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
perror("exec failed");
exit(EXIT_FAILURE);
} else {
int status;
if (WIFEXITED(status)) {
} else {
return 0;
}
OS 2-EXIT()
#include <stdio.h>
#include <stdlib.h>
int main(void)
Printf(“Start”);
Exit(0);
Printf(“End of program”);
OS 3(A)
#include <stdio.h>
#include <conio.h>
int main() {
int allocated_processes = 0
clrscr();
scanf("%d", &ms);
scanf("%d", &bs);
scanf("%d", &nob);
scanf("%d", &mp[i]);
} else {
allocated_processes++;
getch();
return 0;
OS 3 (B)
#include <stdio.h>
#include <conio.h>
OS 3 (B)
int main() {
char ch = 'y';
clrscr();
scanf("%d", &ms);
scanf("%d", &mp[i]);
} else {
printf("Memory is full\n");
getch();
return 0;
}
OS 4
#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 the number of processes: ");
scanf("%d", &p);
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; // Break if we found a process that can
run
}
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!!");
}
return 0;
}
OS 5
#include <stdio.h>
void main() {
int max[10][10], a1[10][10], av[10], i, j, k, m, n, ne[10][10], finish[10],
flag = 0, safe = 0;
int work[10];
printf("\nMaximum matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("\t%d", max[i][j]);
}
printf("\n");
}
printf("\nAllocated matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("\t%d", a1[i][j]);
}
printf("\n");
}
printf("\nAvailable matrix:\n");
for (i = 0; i < n; i++) {
printf("%d\t", av[i]);
}
printf("\n");
printf("\nSafe state\n");
}
OS 6
Simulate the following page replacement algorithms.
a) First In First Out
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
b) Indexed
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk
: \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}