You are on page 1of 19

1. Write a LEX program to count the number of characters, words, spaces & no.

of lines in a
given input file.
%{
#include<stdio.h>
int wc=0,uc=0,sc=0,lc=0,tc=0,splc=0,dc=0,lnc=0;
%}
%%
[\n] {lnc++;wc++;}
[' '\t] {sc++;wc++;}
[A-Z] {uc++;}
[a-z] {lc++;}
[0-9] {dc++;}
. {splc++;}
%%
main()
{
FILE *fp;
char filenm[20];
printf("\n Enter text File name:");
scanf("%s",filenm);
fp=fopen(filenm,"r");
if(fp==NULL)
printf("\nError in opening !!");
else
{
yyin=fp;
yylex();
printf("\n num of words = %d\n",wc);
printf("\n num of space count=%d\n",sc);
printf("\n num of uppercase = %d\n",uc);
printf("\n num of lowercase = %d\n",lc);
printf("\n num of spl char =%d\n",splc);
printf("\n num of digit count =%d\n",dc);
printf("\n num of line count =%d\n",lnc);
tc=uc+lc+sc+splc+dc;
printf("\n total num of chars =%d\n",tc);
}
}

Output:
wr.txt:
hi how are you
india hassan 573201
I LOVE INDIA
vi 1.l
lex 1.l
cc lex.yy.c -ll
./a.out

Enter text File name:wr.txt


num of words = 10
num of space count=7
num of uppercase = 10
num of lowercase = 22
num of spl char =0
num of digit count =6
num of line count =3
total num of chars =45
2. Write a LEX program to recognize a valid arithmetic expression and to recognize the
identifiers and operators present and to print them separately.
%{
#include<stdio.h>
int j=0,i=0,b=0,op=0,c=0,id=0,a=0;
char ct[10];
%}
%%
[a-zA-Z]+[0-9a-zA-Z]* {id++;printf("%s: is an identifier \n",yytext);}
[\+\-\*\/\=] {op++;
ct[i]=yytext[0];
i++;
printf("%s: is an operator\n",yytext);}
"(" {b++;}
")" {b--;}
if(b<0)
{
printf("invalid expression!!");
/*exit(0);*/
}
}
%%
main()
{
printf("\n Enter the statement =>");
yylex();
for(j=0;j<i;j++)
{
if(ct[j]=='=')
a++;
}
if(a>1)
{
printf("\n Invalid Expression");
return 0;
}
elseif(op+1==id&&b==c)
printf("\n It is valid expression \n");
else
printf("\n It is invalid expression \n");
}
Output:
vi 2.l
lex 2.l
cc lex.yy.c -ll
./a.out

Enter the statement => c=a+b


c : is an identifier
= : is an operator
a : is an identifier
+ : is an operator
b : is an identifier
Ctrl+d
It is valid expression
Enter the statement =>c=c$1
c : is an identifier
= : is an operator
c : is an identifer
$1 : is an identifier
Ctrl+d
It is invalid expression

3. a) Write a LEX program to find the number of i) Positive and negative integers ii) Positive
and negative fractions.
b) Write a LEX program to count the number of comment lines in a given C program. Also
eliminate them and copy that program into separate file.

3a)
%{
int pi=0,ni=0,pf=0,nf=0;
%}
%%
[+]?]0-9]+ pi++;
[-][0-9]+ ni++;
[+]?[0-9]+\.[0-9]+ pf++;
[-][0-9]+\.[0-9]+ nf++;
.;
%%
main()
{
printf("enter the number\n");
yylex();
printf("num of +int=%d\n num of -int=%d\n num of +frac=%d\n num of -frac=%d\n",pi,ni,pf,nf);
}
Output:
vi 3a.l
lex 3a.l
cc lex.yy..c -ll
./a.out

Enter the numbers


4 -6 7.7 -5.4
Num of +int = 1
Num of -int = 1
Num of +frac = 1
Num of -frac = 1

3b)
%{
#include<stdio.h>
int fl=0,c=0,fla=0;
%}
%%
\/\* {if(!fl) {fl=1;c++;} else REJECT;}
\*\/ {if(fl) fl=0; else REJECT;}
\/\/ {if(!fla) {fla=1;c++;} else REJECT;}
[\n] {if(fla) fla=0; else REJECT;}
. {if(!fl&&!fla) REJECT;}
%%
main(int argc,char *argv[])
{
if(argc!=3)
{
printf("Improper arguments.\nUsage is $./a.out <srcfilename> <destfilename>\n");
exit(0);
}
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
yylex();
printf("Number of comment statemensts is %d\n",c);
}

vi 3b.c
#include<stdio.h> /*header file*/
void main()
{
int a,b,c; /* variable declaration*/
c=a+b;
printf("enter 2 Numbers");
scanf("%d%d",&a,&b);
c=a+b; /*calculating*/
printf("sum = %d",c); /*output*/
}

Output:
vi 3b.l
vi 3b.c
lex 3b.l
cc lex.yy.c -ll
./a.out 3b.c aa
Num of comment lines = 4

4. a)Write a LEX program to find & replace String.


b) Write a LEX program to check the syntax of for loop.

4a)
%{
#include<stdio.h>
#include<string.h>
FILE *ff,*fr;
char p[20],q[20],r[20],fname[20];
%}
word [a-zA-Z]+
eol \n
%%
{word} {
if(strcmp(p,yytext)==0)
fprintf(fr,q);
else
fprintf(fr,yytext);
}
{eol} {fprintf(fr,yytext);}
. {fprintf(fr,yytext);}
%%
int main(int argc,char *argv[])
{
strcpy(fname,argv[1]);
strcpy(p,argv[2]);
strcpy(q,argv[3]);
ff=fopen(fname,"r+");
fr=fopen("rep.txt","w+");
yyin=ff;
yylex();
return(0);
}

Output:
vi 4a.l
lex 4a.l
cc lex.yy.c -ll
./a.out 4.txt computer cs

4.txt
malnad college of engineering
computer & Engg

rep.txt
malnad college of engineering
cs & Engg

4b)
%{
#include<stdio.h>
#include<ctype.h>
int flag=1;
%}
op "++"|"--"
rop "<"|">"|"<="|">="|"!="|"=="
id [a-zA-Z][a-zA-Z0-9]*
no [0-9]*
pp [\n]
%%
for\(({id}=({no}|{id}))?\;{id}{rop}({id}|{no})\;{id}{op}\){pp}? {flag=0;}
. ;
%%
main()
{
FILE *fp;
char filename[20];
printf("enter filename\n");
scanf("%s",filename);
fp=fopen(filename,"r");
if(fp==NULL)
printf("error opening file\n");
else
yyin=fp;
yylex();
if(flag)
printf("incorrect\n");
else
printf("correct\n");
}
vi k
void main()
{
int i;
for(i=0;i<10;i++)
}
vi 4b.l
lex 4b.l
cc lex.yy.c -ll
./a.out

enter filename
k

correct

5) Write YACC program to recognize a valid arithmetic expression that uses operators +, -,*
& /.

vi 5.l

%{
# include "y.tab.h"
%}

%%

[a-zA-Z]+ return ID;


[0-9] return NUM;
[\n] return 0;
. return(yytext[0]);
%%

vi 5.y

%{
#include<stdio.h>
%}

%token ID NUM

%%
assign:ID'='exp
exp:exp'+'term
|exp'-'term
|exp'*'term
|exp'/'term
|term
;
term:ID
|NUM
|'-'ID
|'-'NUM
|'('exp')'
;
%%
main()
{
printf("\nEnter an expression=>");
yyparse();
printf("\n Valid Expression!!");
return;
}

int yyerror(char *s)


{
printf("\n Invalid expression !!");
exit(0);
}

output:
vi 5.l
vi 5.y
lex 5.l
yacc -d 5.y
cc lex.yy.c y.tab.c -ll
./a.out

Enter an expression=>c=a+b

Valid Expression!!
Enter an expression: c-a-

Invalid Expression!!

6) Write YACC program to evaluate an arithmetic expression involving operators +, -, * and /.

vi 6.l
%{
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return NUMBER;}
[ \t] ;
\n return 0;
. return yytext[0];
%%

vi 6.y

%{
#include<stdio.h>
%}
%token NUMBER
%left '-' '+'
%left '*' '/'
%nonassoc UMINUS
%%
statement:expression {printf("value= %d\n",$1);}
;
expression:expression '+' expression {$$=$1+$3;}
|expression '-' expression {$$=$1-$3;}
|expression '*' expression {$$=$1*$3;}
|expression '/' expression {if($3==0)
{
yyerror("\nDivide by Zero Error\n");
exit(0);
}
else
$$=$1/$3;
}
|'-' expression %prec UMINUS {$$=-$2;}
|'(' expression ')' {$$=$2;}
|NUMBER
;
%%
main()
{
printf("enter expression\n");
yyparse();
}
void yyerror(char *s)
{
printf("%s\n",s);
}

vi 6.l
vi 6.y
lex 6.l
yacc -d 6.y
cc lex.yy.c y.tab.c -ll
./a.out
enter expression
5+6
value= 11
enter expression
6/0

Divide by Zero Error

7) Write a YACC program to recognize nested IF control statements and display the
levels of nesting.

vi 7.l

%{
#include"y.tab.h"
%}
%%
if return IF;
\( return OPEN;
"<" return OP;
">" return OP;
"<=" return OP;
">=" return OP;
"==" return OP;
"!=" return OP;
"&&" return OP;
"||" return OP;
"!" return UOP;
\) return CLOSE;
{ return BOPEN;
} return BCLOSE;
[0-9a-zA-Z]+ return ID;
. return OTHER;
\n return OTHER;
%%
vi 7.y

%{
#include<stdio.h>
int ifc=0;
%}
%token IF ID OPEN CLOSE BOPEN BCLOSE OP UOP OTHER
%%
ifcond:IF OPEN condition CLOSE block {ifc++;}
;
condition:ID
|ID OP ID
|UOP ID
;
block:BOPEN statement BCLOSE
|statement
;
statement:ifcond
|OTHER statement
|ID statement
|OTHER
|ID
;
%%
main()
{
printf("\n enter a valid if control statement=>");
yyparse();
printf("number of levels of nesting of \"if\" statements=%d\n",ifc);
return;
}
int yyerror(char *s)
{
printf("\n Invalid!!");
exit(0);
}

output:

vi 7.l
vi 7.y
lex 7.l
yacc -d 7.y
cc lex.yy.c y.tab.c -ll
./a.out

enter a valid if control statement+> if(a){if(b)}}}


Invalid!!

enter a valid if control statement+> if(a){if(b){if(c)}}


number of levels of nesting of "if" statements=3
8 a) Write YACC program to recognize the grammar (anb, n >= 10).
b) Write a YACC Program to recognize the grammar (anbmck, m,n,k >=0 and m=n+k).

8a)
vi 8a.l

%{
#include"y.tab.h"
%}
%%
[aA] {return A;}
[bB] {return B;}
. {return yytext[0];}
%%

vi 8a.y

%{
#include<stdio.h>
%}
%token A B;
%%
str:A A A A A A A A A A S
;
S:A S
|B
;
%%
main()
{
printf("\nenter a string\n");
yyparse();
printf("\nvalid string\n");
return;
}
yyerror()
{
printf("\ninvalid string\n");
exit(0);
}

Output:
vi 8a.l
vi 8a.y
lex 8a.l
yacc -d 8a.y
cc lex.yy.c y.tab.c -ll
./a.out
Enter a string
aaaaaaaaaab
valid string
Enter a string
aaaaab
invalid string

8b)
vi 8b.l
%{
#include"y.tab.h"
%}
%%
[aA] {return A;}
[bB] {return B;}
[cC] {return C;}
. {return yytext[0];}
%%

vi 8b.y
%{
#include<stdio.h>
%}
%token A B C;
%%
str:S Z
|S
|Z
;
S:A S B
|A B
Z:B Z C
|B C
;
%%
main()
{
printf("\nenter a string of the form (a^n)(b^m)(c^k) and m=n+k\n");
yyparse();
printf("\nstring recognized\n");
return;
}
yyerror()
{
printf("\nstring not recognized\n");
exit(0);
}

Output:
vi 8b.l
vi 8b.y
lex 8b.l
yacc -d 8b.y
cc lex.yy.c y.tab.c -ll
./a.out
enter the string of the form(a^n)(b^m)(c^k) and m=n+k
abbc
string recognized
enter the string of the form(a^n)(b^m)(c^k) and m=n+k
abc
string not recognized

9a) a) Non-recursive shell script that accepts any number of arguments and prints
them in the Reverse order, (For example, if the script is named rargs, then executing
rargs A B C should produce C B A on the standard output).
b) C program that creates a child process to read commands from the standard input
and execute them (a minimal implementation of a shell – like program). You can assume
that no arguments will be passed to the commands to be executed.

9a)
vi 9a.sh

if[$# -eq 0]
then
echo"No argument"
exit
fi
echo "Num of arguments:$#"
echo "Arguments in reverse order"
for x in $*
do
y="$x $y"
done
echo $y

Output :
vi 9a.sh
sh 9a.sh a b c
Num of arguments : 3
Arguments in reverse order
cba

9b)
vi 9b.c

#include<stdio.h>
#include<sys/types.h>
#include<stdlib>
main()
{
pid_t p;
char cmd[20];
p=fork;
if(p<0)
{
printf("fork error\n");
exit(0);
}
elseif(p>0)
{
printf("\n Enter the command\n");
scanf("%s",cmd);
system(cmd);
}
}

Output:

vi 9b.c
cc 9b.c
./a.out

Enter the command


date
Tue Nov 17 16:05:34 EST 2015

Enter the command


pwd
/home/student

10 a) Shell script that accepts file names specified as arguments and creates a shell script that
contains this file as well as the code to recreate these files. Thus if the script generated by
your script is executed, it would recreate the original files.
b) C program to do the following: Using fork( ) create a child process. The child process
prints its own process-id and id of its parent and then exits. The parent process waits for its
child to finish (by executing the wait( )) and prints its own process-id and the id of its child
process and then exits.

10a)

vi 10a.sh
echo "#to unbundle this file sh"
for i
do
echo "echo $i"
ec ho "cat>$i<<'end of $i'";
cat $i
echo "end of $i"
done

output:
vi 10a.sh
ls (to see files and select anyn 1 file name)
sh 10a.sh ada.sh
#to unbundle this file sh
echo ada.sh
cat>ada.sh<<'end of ada.sh'
echo "enter the 2 nos for add"
read n1
read n2
add=$(($n1+$n2))
echo "the sum is :"$add
end of ada.sh
10b)
vi 10b.c

#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int pid=0;
pid=fork();
if(pid==0)
{
printf("\n CHILD PROCESS\n PID this process(child) is %d\n",getpid());
printf("PARENT PID is %d\n",getppid());
}
else
{
wait(0);
printf("\n PARENT PROCESS \n PID of this process(child) is %d\n",getpid());
printf("CHILD PID is %d\n\n",pid);
}
return 0;
}

output:
vi 10b.c
cc 10b.c
./a.out

CHILD PROCESS
PID this process(child) is 21806
PARENT PID is 21805

PARENT PROCESS
PID of this process(child) is 21805
CHILD PID is 21806

11) Design, develop and execute a program in C / C++ to simulate the working of
First come first Served (FCFS) Algorithm. Determine the average turn-around time and
average waiting time.

vi 11.c

#include<stdio.h>
void display();
void getdata();
void waitTime();
void turnAroundTime();
int n,a[100],b[100];

int main()
{
printf("*** FCFS(FIRST COME FIRST SERVICE)SCHEDULING***\n");
getdata();
display ();
waitTime();
turnAroundTime();
}

void getdata();
{
char c;
int i;
printf("Enter the number of process");
scanf("%d",&n);
printf("\n Do you need to enter the arrival time of process [y/n] or [Y/N]\n");
scanf("%s",&c);
for(i=0;i<n;i++)
{
printf("Enter the %d process burst time : ",i+1);
scanf("%d",&b[i]);
if(c=='y'||c=='Y')
{
printf("Enter the %d process arrival time : ",i+1);
scanf("%d",&a[i]);
}
else
{
a[i]=0;
}
}
}

void display()
{
int i;
printf("\n Process\t Burst time\t Arrival time\n");
for(i=0;i<n;i++)
{
printf("%d\t %d\t %d\t",i+1,b[i],a[i]);
}
}

void waitTime()
{
int w[100],i;
float totalwait=0;
w[0]=0;
for(i=1;i<n;i++)
{
w[i]=b[i-1]-a[i]+w[i-1];
totalwait=w[i]+totalwait;
}
printf("\n Total waiting time =%f",totalwait);
printf("\n Average waiting time = %f",totalwait);
}

void turnAroundTime()
{
int tat[100],i;
float totaltat=0;
tat[-1]=0;
for(i=0;i<n;i++)
{
tat[i]=b[i]-a[i]+tat[i-1];
totaltat=totaltat+tat[i];
}
printf("\n\n Total turnAroundTime(TAT)=%f",totaltat);
printf("\n Average turnAroundTime avg (TAT) =%f",totaltat\n);
}

Output:

cc 11.c
./a.out

***FCFS(FIRST COME FIRST SERVICE)SCHEDULING***


Enter the number of process : 4
Do you need to enter the arrival time of process [y/n] or [Y/N]
Y
Enter the 1 process burst time :7
Enter the 1 process arrival time :0
Enter the 2 process burst time :4
Enter the 2 process arrival time :2
Enter the 3 process burst time :1
Enter the 3 process arrival time :4
Enter the 4 process burst time :4
Enter the 4 process arrival time :5

Process Burst time Arrival time


1 7 0
2 4 2
3 1 4
4 4 5

Total waiting time= 11.0000


Average waiting time= 2.75000
Total turnAroundTime(TAT)=27.000
Average turnAroundTime avg(TAT)=6.75000

12) Design, develop and execute a program in C/C++ to simulate FIFO Page
Replacement Algorithm.

vi 12.c

#include<stdio.h>
main()
{
int a[10],b[20],n,p=0,q=0,m=0,h,k,i,ql=1,pf;
char f='F';
printf("enter the number of pages: ");
scanf("%d",&n);
printf("enter the number of frames : ");
scanf("%d",&pf);
printf("enter %d page number:",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
if(p==0)
{
if(q>=pf)
q=0;
a[q]=b[i];
q++;
if(ql<pf)
{
ql=q;
}
}
printf("\n %d",b[i]);
printf("\t");
for(h=0;h<ql;h++)
printf("%d",a[h]);
if((p==0)&&(q<=pf))
{
printf("-->%c",f);
m++;
}
p=0;
for(k=0;k<ql;k++)
{
if(b[i+1]==a[k])
p=1;
}
}
printf("\n no of faults = %d",m);
}

output:
vi 12.c
cc 12.c
./a.out
enter the number of pages: 12
enter the number of frames : 3
enter 12 page number:1 2 3 4 1 2 5 1 2 3 4 5

1 1-->F
2 12-->F
3 123-->F
4 423-->F
1 413-->F
2 412-->F
5 512-->F
1 512
2 512
3 532-->F
4 534-->F
5 534
no of faults = 9
13) Develop and execute a program in C/C++ to simulate the working of First fit
memory management Algorithm.

Vi 13.c

#include<stdio.h>
main()
{
int p,m,j,i;
int parr[10],marr[10],flag[10];
printf("Enter number of processes:");
scanf("%d",&p);
printf("Enter number of Memory blocks:");
scanf("%d",&m);
for(i=0;i<p;i++)
{
printf("Enter size of process %d:",i+1);
scanf("%d",&parr[i]);
}
for(i=0;i<m;i++)
{
printf("Enter size of memory %d:",i+1);
scanf("%d",&marr[i]);
}
for(i=0;i<p;i++)
{
for(j=0;j<m;j++)
{
if(marr[j]>=parr[i])
{
marr[j]-=parr[i];
printf("Allocating process %d to memory %d\n Size remaining in it after allocation
%d\n\n",i+1,j+1,marr[j]);
flag[i]=1;
break;
}
else
flag[i]=0;
}
}
for(i=0;i<p;i++)
{
if(flag[i]==0)
{
printf("Not enough memory for process %d",i);
}
}

Output:
cc 13.c
./a.out
Enter number of processes:3
Enter number of Memory blocks:3
Enter size of process 1:400
Enter size of process 2:200
Enter size of process 3:150
Enter size of memory 1:200
Enter size of memory 2:300
Enter size of memory 3:350
Allocating process 2 to memory 1
Size remaining in it after allocation 0

Allocating process 3 to memory 2


Size remaining in it after allocation 150

Not enough memory for process 0

You might also like