Professional Documents
Culture Documents
Lex Program 2.
Yacc Program 2.
printf("PARSING TABLE:\n");
for(p=0;p<2;p++)
{
for(q=0;q<3;q++)
{
for(r=0;r<3;r++)
printf("%c",m[p][q][r]);
printf("\t");
}
printf("\n");
}
printf("\nStack\t\tInput\n");
printf("________\t_________\n");
for(k=0;k<=i;k++)
printf("%c",stack[k]); /* Initial stack*/
printf("\t\t");
for(k=j;k<=n;k++)
printf("%c",ip[k]); /* input*/
printf("\n");
#include<stdio.h>
#include<string.h>
int z=0,i=0,j=0,c=0; /*c for length of string, i for stack, j for input and z for check function*/
char a[16],ac[20],stk[15],act[10]; /*act[ ] for shift and ac[ ] for reduce*/
void check(); // to check top of stack
void main()
{
printf("GRAMMER is E->E+T|T\nT->T*F|F\nF->(E)|id\n");
printf("enter input string\n");
scanf("%s",a);
c=strlen(a);
strcpy(act,"SHIFT "); //initial configuration
printf("stack \t input \t action");
printf("\n------------------------\n");
printf("$\t%s$",a);
for(i=0; j<c; i++,j++)
{
if(a[j]=='i'&&a[j+1]=='d')//check input for "id"
{
stk[i]=a[j];
stk[i+1]=a[j+1]; //shift id to stack
stk[i+2]='\0';
a[j]=' '; //delete "id" from input array
a[j+1]=' ';
printf("\n$%s\t%s$\t%sid",stk,a,act);
//displays the first line SHIFT->id
check();
}
else
{
stk[i]=a[j];
stk[i+1]='\0';
a[j]=' ';
printf("\n$%s\t%s$\t%s%c",stk,a,act,stk[i]);
//print SHIFT + etc. for inputs like +,*,(,)
check();
}
}
if((strcmp(stk,"E")==0)) //at the end of input if stack holds "E"
printf("\n--\n SUCCESS!!!!!!!!!!\n");
else
printf("\n---\nERROR!!!!!!\n");
}
void check()
{
for(z=0;z<c;z++)
if(stk[z]=='(' && stk[z+1]=='E' && stk[z+2]==')')
{ //if stack holds (E)
stk[z]='F'; //reduce to F
stk[z+1]='\0';
strcpy(ac,"REDUCE F->(E)"); //dispaly REDUCE
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2; //top of stack holds F
}
for(z=0;z<c;z++) // if stack holds id
if(stk[z]=='i' && stk[z+1]=='d')
{
stk[z]='F';
stk[z+1]='\0';
strcpy(ac,"REDUCE F->id");
printf("\n$%s\t%s$\t%s",stk,a,ac);
j++; //move input pointer
}
for(z=0;z<c;z++) // if stack holds T*F
{
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
char tset[4][3][3]= { {"-","B","?"},
{"+","C","D"},
{"*","0","1"},
{"=","A","2"}
};
int main()
{
int row,col;
printf("Input statement is:A = -B * (C+D)\n");
printf("Intermediate code in three address form is:\nT1 = -B\nT2 = C + D\nT3 = T1 * T2\nA
=T3\n");
printf("Triples are:\n");
for(row=0;row<4;row++)
{
for(col=0;col<3;col++)
printf("%c\t",tset[row][col][0]);
printf("\n");
/*Write appropriate statement to dispaly the table*/
}
printf("Machine code generated is:\n");
for(row=0;row<4;row++)
{
col=2;
if (tset[row][col][0]=='?')
{
printf("\nLD R0,%c%c",tset[0][0][0],tset[0][1][0]);
}
else
{
if(tset[row][0][0]=='+')
{
printf("\nLD R1,%c",tset[1][1][0]);
printf("\nLD R2,%c",tset[1][2][0]);
printf("\nADD R1,R1,R2");
}
else
{
if(tset[row][0][0]=='*')
{
printf("\nMUL R0,R0,R1");
}
else
{
printf("\nST %c,R0",tset[3][1][0]);/*write appropriate data
to be printed from the table*/
}
}
}
}
printf("\n");
return 0;
Lex Program 6
/*Program 6a*/
%{
#include<stdlib.h>
int comm=0;
%}
BC [ \t]*[\/][\*]
EC "\*""\/"
SLC "//"
QT "\""
%x CMT
%X QUOT
%X SLCMT
%%
{BC} {BEGIN CMT;}
<CMT>{EC} {BEGIN 0;}
<CMT>. |
<CMT>\n ;
{QT} {ECHO; BEGIN QUOT;}
<QUOT>{QT} {ECHO;BEGIN 0;}
<QUOT>.|\n {ECHO;}
{SLC} {BEGIN SLCMT;}
<SLCMT>\n {BEGIN 0;}
<SLCMT>. ;
%%
int main(int argc, char *argv[])
{
FILE *fin, *fout;
if( argc!=3)
{
printf("\nUsage:<exec><input file><output file>\n");
exit(1);
}
fin=fopen(argv[1], "r");
if(!fin)
{
printf("\nInput file not found\n");
exit(1);
}
yyin=fin;
fout=fopen(argv[2], "w");
yyout=fout;
yylex();
}
Sample file(in.c)
(output)Out.c
int main()
{
int a,b,c=0;
a=3;
b=5;
c=a+b;
printf("\n/*printing sum*/\nThe sum is %d",c);
}
Lex program 6b
Yacc Program 6b
%{
#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 FILE *yyin;
int main(int argc, char *argv[])
{
if( argc!=2)
{
printf("\nUsage:<exec><input file>\n");
exit(1);
}
FILE *myfile = fopen(argv[1], "r");
if (!myfile)
{
printf("can't open input C file");
return -1;
}
yyin = myfile;
do
{
yyparse();
}
while(!feof(yyin));
printf("numbers = %d\n Keywords = %d\n Identifiers = %d\n operators = %d\n",dig,
key,id, op);
}
int yyerror(char *s)
{
printf("%s\n",s);
exit(0);
}
Sample input(input.c)
int main()
{
int a,b,c=0;
a=3;
b=5;
c=a+b;
printf("\nThe sum is %d",c);
}
#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 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");
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("P%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
Sample Output 1:
Sample Output2:
Enter the no of processes: 4
For process 2: 2 5 6
For process 3: 9 0 4
For process 4: 2 3 4
For process 2: 0 0 2
For process 3: 2 0 1
For process 4: 0 0 1
#include<stdio.h>
int n,nf,in[100],p[50],hit=0,i,j,k,pgfaultcnt=0;
void getData()
{
printf("\nEnter length of page reference sequence:");
scanf("%d",&n);
printf("\nEnter the page reference sequence:");
for(i=0; i<n; i++)
scanf("%d",&in[i]);
printf("\nEnter no of frames:");
scanf("%d",&nf);
}
void initialize()
{
pgfaultcnt=0;
for(i=0; i<nf; i++)
p[i]=9999;
}
int isHit(int data)
{
hit=0;
for(j=0; j<nf; j++)
{
if(p[j]==data)
{
hit=1;
break;
}
}
return hit;
}
void dispPages()
{
for(k=0; k<nf; k++)
{
if(p[k]!=9999)
printf(" %d",p[k]);
}
}
void fifo()
{
initialize();
int m=0;
for(i=0; i<n; i++)
{
printf("\nFor %d :",in[i]);
if(isHit(in[i])==0)
{
p[m]=in[i];
m=(m+1)%nf;
pgfaultcnt++;
dispPages();
}
else
printf("No page fault");
}
printf("\nTotal no of page faults:%d",pgfaultcnt);
}
void lru()
{
initialize();
int least[50];
for(i=0; i<n; i++)
{
printf("\nFor %d :",in[i]);
if(isHit(in[i])==0)
{
for(j=0; j<nf; j++)
{
int pg=p[j];
int found=0;
for(k=i-1; k>=0; k--)
{
if(pg==in[k])
{
least[j]=k;
found=1;
break;
}
else
found=0;
}
if(!found)
least[j]=-9999;
}
int min=9999;
int repindex;
for(j=0; j<nf; j++)
{
if(least[j]<min)
{
min=least[j];
repindex=j;
}
}
p[repindex]=in[i];
pgfaultcnt++;
dispPages();
}
else
printf("No page fault!");
}
printf("\nTotal no of page faults:%d",pgfaultcnt);
}
int main()
{
int choice;
while(1)
{
printf("\nPage Replacement Algorithms\n1.Enter data\n 2.FIFO\n
3.LRU\n 4.Exit\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:getData();
break;
case 2:fifo();
break;
case 3:lru();
break;
default:return 0;
break;
}
}
}