SSC Lab Programs
SSC Lab Programs
Lex program
%{
#include<stdio.h>
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;}
%%
int main()
yylex();
{
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;
Output:
Steps to compile and Run the Lex program
After the successful compilation of the lex program the lex.yy.c file is
generated automatically
After the successful Run of the lex program the a.exe file is generated
automatically
To see the output of Lex program type a.exe in the command prompt
To Execute:a.exe
Note: you have to go location where u have safed the file(i.e Lex Program) and
open the command prompt there only. And then compile, run and execute the
program. All steps are shown in the above output picture.
1 b. Write YACC program to evaluate arithmetic expression involving
operators: +, -,*, and /
Lex program
%option noyywrap
%{
#include "y.tab.h"
extern yylval;
%}
%%
. {;}
\n {return 0;}
%%
Yacc program
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token num
%%
input:exp {printf("%d\n",$$);exit(0);}
|exp'-'exp{$$=$1-$3;}
|exp'*'exp{$$=$1*$3;}
else $$=$1/$3;}
|'('exp')'{$$=$2;}
|num{$$=$1;};
%%
int yyerror()
exit(0);
int main()
printf("Enter an expression:\n");
yyparse();
Output:
After the successful compilation of the lex program the lex.yy.c file is
generated automatically
1 file_name.tab.c and
2 file name.tab.h
After the successful Run of the lex and Yacc program the a.exe file is
generated automatically
To see the output of Lex and Yacc program type a.exe in the command prompt
To Execute:a.exe
Note: you have to go location where u have safed the files(i.e Lex and Yacc
Program) and open the command prompt there only. And then compile, run and
execute the program.
Lex program
%option noyywrap
%{
#include "y.tab.h"
%}
%%
a {return A;}
b {return B;}
%%
Yacc program
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token A B
%%
s1: ; | A s1
%%
main()
printf("\nEnter A String\n");
yyparse();
int yyerror()
printf("\nError \n");
exit(0);
}
Output:
After the successful compilation of the lex program the lex.yy.c file is
generated automatically
After the successful compilation of the yacc program the 2 files is generated
automatically namely
1 file_name.tab.c and
2 file name.tab.h
To see the output of Lex and Yacc program type a.exe in the command prompt
To Execute:a.exe
Note: you have to go location where u have safed the files(i.e Lex and Yacc
Program) and open the command prompt there only. And then compile, run and
execute the program.
C program
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
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]);
void 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]);
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]!='$')
exit(0);
push('$');
push('A');
i=0;
printf("\n\n");
printf("stack\tInput\taction");
printf("\n---------\n");
while(i!=strlen(input)&&stack[top]!='$')
printf("\n");
for(l=top;l>=0;l--)
printf("%c",stack[l]);
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$");
else
Output:
Steps to compile and Run the c program
After the successful compilation of the c program the a.exe file is generated
automatically
Note: you have to go location where u have safed the file(i.e c Program) and
open the command prompt there only. And then compile, run and execute the
program.
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();
void main()
gets(a);
c=strlen(a);
strcpy(act,"SHIFT->");
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");
stk[z]='E';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
j++;
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-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;
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:
After the successful compilation of the c program the a.exe file is generated
automatically
Note: you have to go location where u have safed the file(i.e c Program) and
open the command prompt there only. And then compile, run and execute the
program.
C program
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
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 file
T1 -B = ?
T2 C + D
T3 T1 * T2
A T3 = ?
Output:
First u have to create input.txt file where u are executing your c program file
After the successful compilation of the c program the a.exe and output.txt
file is generated automatically
Note: you have to go location where u have safed the file(i.e c Program) and
open the command prompt there only. And then compile, run and execute the
program.
Lex program
%option noyywrap
%{
#include<stdio.h>
int sl=0;
int ml=0;
%}
%%
"//".* sl++;
%%
main()
yyin=fopen("f1.c","r");
yyout=fopen("f2.c","w");
yylex();
fclose(yyin);
fclose(yyout);
F1.c program
#include<stdio.h>
int main()
// this is a comment
printf("Hello Imam");
Output:
Steps to compile and Run the Lex program
After the successful compilation of the lex program the lex.yy.c file is
generated automatically
After the successful Run of the lex program the a.exe file is generated
automatically
To see the output of Lex program type a.exe in the command prompt
To Execute:a.exe
Note: you have to go location where u have safed the file(i.e Lex Program) and
open the command prompt there only. And then compile, run and execute the
program.
Lex program
%option noyywrap
%{
#include <stdio.h>
#include "y.tab.h"
extern yylval;
%}
%%
[ \t];
int|char|bool|float|void|for|do|while|if|else|return|void {printf("keyword is
%s\n",yytext);return KEY;}
. ;
%%
Yacc program
%{
#include <stdio.h>
#include <stdlib.h>
%}
%%
input:
| ID input { id++; }
| OP input {op++;}
| DIGIT { dig++; }
| ID { id++; }
| KEY { key++; }
| OP { op++;}
%%
#include <stdio.h>
main()
if (!myfile)
return -1;
yyin = myfile;
do{
yyparse();
}while (!feof(yyin));
void yyerror() {
exit(-1);
}
F2.c program
#include<stdio.h>
int main()
printf("Hello Imam");
Output:
Steps to compile and Run the Lex and Yacc program
After the successful compilation of the lex program the lex.yy.c file is
generated automatically
After the successful compilation of the yacc program the 2 files is generated
automatically namely
1 file_name.tab.c and
2 file name.tab.h
After the successful Run of the lex and Yacc program the a.exe file is
generated automatically
To see the output of Lex and Yacc program type a.exe in the command prompt
To Execute:a.exe
Note: you have to go location where u have safed the files(i.e Lex and Yacc
Program) and open the command prompt there only. And then compile, run and
execute the program.
C program
#include<stdio.h>
int main()
int count,j,n,time,flag=0,time_quantum,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;
scanf("%d",&ch);
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&at[i]);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
switch(ch)
case 1:
scanf("%d",&time_quantum);
remain=n;
for(time=0,count=0;remain!=0;)
time+=rt[count];
rt[count]=0;
flag=1;
else if(rt[count]>0)
rt[count]-=time_quantum;
time+=time_quantum;
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];
turnaround_time+=time-at[count];
flag=0;
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
break;
case 2:
remain=0;
rt[9]=9999;
for(time=0;remain!=n;time++)
smallest=9;
for(i=0;i<n;i++)
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];
break;
default:
printf("Invalid\n");
return 0;
Output:
Steps to compile and Run the C program
After the successful compilation of the c program the a.exe file is generated
automatically
Note: you have to go location where u have safed the file(i.e c Program) and
open the command prompt there only. And then compile, run and execute the
program.
C program
#include <stdio.h>
#include <stdlib.h>
int main()
count = 0;
scanf("%d", &p);
completed[i] = 0;
scanf("%d", &r);
scanf("%d", &alloc[i][j]);
scanf("%d", &avail[i]);
do
printf("\n");
process = -1;
process = i ;
process = -1;
break;
if(process != -1)
break;
}
if(process != -1)
safeSequence[count] = process + 1;
count++;
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
if(count == p)
else
Output:
Steps to compile and Run the c program
After the successful compilation of the c program the a.exe file is generated
automatically
Note: you have to go location where u have safed the file(i.e c Program) and
open the command prompt there only. And then compile, run and execute the
program.
C program
#include<stdio.h>
#include<stdlib.h>
int main()
int ch,YN=1,i,l,f;
char F[10],s[25];
scanf("%d",&f);
scanf("%d",&l);
scanf("%s",s);
for(i=0;i<f;i++)
F[i]=-1;
do
printf("\n1:FIFO\n2:LRU \n3:EXIT");
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);
}
scanf("%d",&YN);
} while(YN==1);
return(0);
//FIFO
int i,j=0,k,flag=0,cnt=0;
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;
}
//LRU
int i,j=0,k,m,flag=0,cnt=0,top=0;
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]);
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
flag=0;
Output:
Steps to compile and Run the c program
After the successful compilation of the c program the a.exe file is generated
automatically
Note: you have to go location where u have safed the file(i.e c Program) and open
the command prompt there only. And then compile, run and execute the program.