sem3
sem4
sem5
sem6
sem7
MADL
SSL
CGL
%{ #include
int v=0,op=0,id=0,flag=0; %} %% [0-9][0-9]* {id++;printf("\nIdentifier:");ECHO;} [\+\-\*\/\=] {op++;printf("\nOperartor:");ECHO;} "(" {v++;} ")" {v--;} ";" {flag=1;} .|\n {return 0;} %% int main() { printf("Enter the expression:\n"); yylex(); if((op+1) ==id && v==0 && flag==0) { printf("\n\nIdentifiers are:%d\nOperators are:%d\n",id,op); printf("\nExpression is Valid\n"); } else printf("\nExpression is Invalid\n"); return 1; } int yywrap() { return 1; } Lex Part %{ #include "y.tab.h" extern int yylval; %} %% [0-9]+ {yylval=atoi(yytext);return num;} [\+\-\*\/] {return yytext[0];} [)] {return yytext[0];} [(] {return yytext[0];} . {;} \n {return 0;} %% yacc part %{ #include
#include
int yylex(); int yylval; int yyerror(); %} %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. Invalid expression.\n"); exit(0); } else {$$=$1/$3;}} |'('exp')' {$$=$2;} |num {$$=$1;}; %% int yyerror() { printf("Error. Invalid Expression.\n"); exit(0); } int main() { printf("Enter an expression:\n"); yyparse(); }
Copy
PROGRAM 1
%{ #include "y.tab.h" %} %% a {return A;} b {return B;} [\n] return '\n'; %% %{ #include
#include
int yylex(); int yyerror(); %} %token A B %% input:s'\n' {printf("Successful Grammar\n");exit(0);} s: A s1 B| B s1: ; | A s1; %% int main() { printf("\nEnter A String\n"); yyparse(); } int yyerror() { printf("\ninvalid string \n"); exit(0); } lex 2.l yacc -d 2.y cc lex.yy.c y.tab.c -ll ./a.out Enter A String ab Successful Grammar Enter A String ac c invalid string Enter A String azlanb zlnSuccessful Grammar
Copy
PROGRAM 2
#include
#include
#include
char prod[3][10] = {"A->aBa", "B->bB", "B->@"}; char input[10], stack[25]; int top = -1; int j = 0, k, l; void push(char item) { stack[++top] = item; } void pop() { top = top - 1; } void display() { int j; for (j = top; j >= 0; j--) { printf("%c", stack[j]); } } void stackpush(char p) { if (p == 'A') { pop(); for (j = strlen(prod[0]) - 1; j >= 3; j--) push(prod[0][j]); } else { pop(); for (j = strlen(prod[1]) - 1; j >= 3; j--) push(prod[1][j]); } } int main() { char c; int i; printf("first(A)={a}\t"); printf("follow(A)={$}\n"); printf("first(B)={b,@}\t"); printf("follow(B)={a}\n\n"); printf("\t a \t b \t $ \n"); printf("A \t%s\n", prod[0]); printf("B\t%s\t%s\n", prod[2], prod[1]); 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] != '$') && (input[i] != 'b')) { printf("Invalid string\n"); exit(0); } } if (input[i - 1] != '$') { printf("\nInput string entered without end marker $\n"); exit(0); } push('$'); push('A'); printf("\n\nstack\tInput\ttransaction"); printf("\n---------------------------\n"); i = 0; // Resetting the input string index to 0 while (i <= strlen(input) && stack[top] != '$') { printf("\n"); display(); printf("\t"); for (l = i; l < strlen(input); l++) printf("%c", input[l]); printf("\t"); if (stack[top] == 'A') { printf("A->aBa"); stackpush('A'); } else if (stack[top] == 'B') { if (input[i] != 'b') { printf("B->@"); printf("\t matched @"); pop(); } else { printf("B->bB"); stackpush('B'); } } else { if (stack[top] == input[i]) { printf("pop %c", input[i]); printf("\tmatched %c", input[i]); pop(); i++; } else break; } } if (stack[top] == '$' && input[i] == '$') { printf("\n$\t$"); printf("\nValid String Accepted\n"); } else printf("\nInvalid string rejected \n"); return 0; }
Copy
PROGRAM 3
#include
#include
#include
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("GRAMMAR 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
Copy
PROGRAM 4
#include
#include
#include
#include
char op[2],arg1[5],arg2[5],result[5]; void 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 = ?
Copy
PROGRAM 5
%{ #include
int sl=0; int ml=0; %} %% "/*"[a-zA-Z0-9' '\t\n]+"*/" ml++; "//".* sl++; %% int 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); return 0; } //test progrqm #include
int main() { /*abcd sbbnd bbsg*/ printf("hello"); printf("world"); return 0; } lex 6a.l gcc lex.yy.c -ll ./a.out Number of single line comments are =1 Number of multiline comments are=1 %{ #include
#include "y.tab.h" extern int 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
#include
int yyerror(); int yylex(); 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
int yyparse(); extern FILE *yyin; int main() { FILE *myfile = fopen("a.c","r"); if (!myfile) { printf("I can't open sam_input.c!"); return -1; } yyin = myfile; do { yyparse(); }while (!feof(yyin)); printf("numbers = %d\nKeywords = %d\nIdentifiers = %d\noperators =%d\n",dig, key,id, op); return 1; } int yyerror() { printf("EEK, parse error! Message: "); exit(-1); } void main() { int a; float bc; char c; char ch; if(a==80) printf("good"); else printf("bad"); } lex 6b.l yacc -d 6b.y gcc lex.yy.c y.tab.c -ll ./a.out keyword is void identifier is main keyword is int identifier is a keyword is float identifier is bc keyword is char identifier is c keyword is char identifier is ch keyword is if identifier is a operator is = operator is = numbers is 80 identifier is printf identifier is good keyword is else identifier is printf identifier is bad numbers = 1 Keywords = 7 Identifiers = 10 operators =2
Copy
PROGRAM 6
#include
#include
struct proc { int id; int arrival; int burst; int rem; int wait; int finish; int turnaround; float ratio; } process[10]; struct proc temp; int no; int chkprocess(int); int nextprocess(); void roundrobin(int, int, int[], int[]); void srtf(int); int main() { int n,tq,choice; int bt[10],st[10],i,j,k; for(; ;) { printf("Enter the choice \n"); printf(" 1. Round Robin\n 2. SRT\n 3. Exit \n"); scanf("%d",&choice); switch(choice) { case 1: printf("Round Robin scheduling algorithm\n"); printf("Enter number of processes:\n"); scanf("%d",&n); printf("Enter burst time for sequences:"); for(i=0;i
tq) st[i]=st[i]-tq; else if(st[i]>=0) { temp1=st[i]; st[i]=0; } sq=sq+temp1; tat[i]=sq; } if(n==count) break; } for(i=0;i
process[j].arrival) { temp = process[i]; process[i] = process[j]; process[j] = temp; } } } no = 0; j = 1; while(chkprocess(n) == 1) { if(process[no + 1].arrival == time) { while(process[no+1].arrival==time) no++; if(process[j].rem==0) process[j].finish=time; j = nextprocess(); } if(process[j].rem != 0) { process[j].rem--; for(i = 1; i <= no; i++) { if(i != j && process[i].rem != 0) process[i].wait++; } } else { process[j].finish = time; j=nextprocess(); time--; k=j; } time++; } process[k].finish = time; printf("\n\n\t\t\t---SHORTEST REMAINING TIME FIRST---"); printf("\n\n Process Arrival Burst Waiting Finishing turnaround Tr/Tb\n"); printf("%5s %9s %7s %10s %8s %9s\n\n", "id", "time", "time", "time", "time", "time"); for(i = 1; i <= n; i++) { process[i].turnaround = process[i].wait + process[i].burst; printf("%5d %8d %7d %8d %10d %9d %10.1f ", process[i].id, process[i].arrival, process[i].burst, process[i].wait, process[i].finish, process[i].turnaround, process[i].ratio); tavg=tavg+ process[i].turnaround; wavg=wavg+process[i].wait; printf("\n\n"); } tavg=tavg/n; wavg=wavg/n; printf("tavg=%f\t wavg=%f\n",tavg,wavg); }
Copy
PROGRAM 7
#include
#include
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("Enter the no of resources : "); scanf("%d", &r); printf("Enter 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("Enter 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("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("Max matrix:\t\nAllocation 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("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("The system is in a safe state!!\n"); printf("Safe Sequence : < "); for( i = 0; i < p; i++) printf("%d ", safeSequence[i]); printf(">\n"); } else printf("The system is in an unsafe state!!"); }
Copy
PROGRAM 8
#include
#include
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
Copy
PROGRAM 9
Copy
PROGRAM 10
Would you like to upload Programs?
no
yes