Download - Unix Lab Manual12
-
7/28/2019 Unix Lab Manual12
1/35
1)Writeashellscriptthatacceptscommand lineargumentsandprintstheminreverseorder.
echo "Checking the number of arguments"
if [ $#then -eq 0 ]
echo "No argumentsexit
supplied"
fin=$#whiledo
[$n -gt 0 ]
eval echo \$$nn=`expr$n - 1`
done
-
7/28/2019 Unix Lab Manual12
2/35
OUTPUT:
[root@localhost unix]$Checking the number ofNo arguments supplied
[root@localhost unix]$Checking the number ofgreatareindians
sh prg01.sharguments
sh prg01.sharguments indiansare great
[root@localhostunix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
3/35
2)Writeashellscript,whichtakestwofilenamesascommand linearguments
andchecks,whethertwofileshave identicalfilepermissionsandprint.Ifnot,
printfilenamesandtheirpermissionsseparately.
if [ $# -eq 0 ]thenechoexit
elif [thenechoexit
elseecho
fi
"No filename given"
$# -eq 1 ]
"Only one filename is given"
"Two filenames given"
if [ !thenechoexit
fi
-e $1-a ! -e $2 ]
"Bothfiles doesnot exist"
if [ !thenechoexit
fiif [ !thenechoexit
fi
-e $1]
"File1 does notexist"
-e $2]
"File2 does notexist"
x=`ls -l $1 | cut -c 2-10`y=`ls -l $2 | cut -c 2-10`
if [ $x = $y ]then
echoelseechoechoecho
fi
"Common permissions"
"Uncommon"File 1:""File 2:"
permissions"$x$y
-
7/28/2019 Unix Lab Manual12
4/35
OUTPUT:
[mca3a6@localhost mca3a6]$sh prg2.shInsufficient number of arguments[root@localhost unix]$sh prg2.sh a1
only one file name is given,Insufficient[root@localhostunix]$[root@localhost unix]$sh prg2.sh f1f2TwoTwo
file names are givenfiles exists
Two file permissions are sameFile permission: rw-rw-r--[root@localhost unix]$chmod u+x f2[root@localhost unix]$sh prg2.sh f1Two file names are givenTwo files existsDifferent file permissionsFile1: rw-rw-r--File2: rwxrw-r--
f2
[root@localhostunix]$_
mailto:mca3a6@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:mca3a6@localhost -
7/28/2019 Unix Lab Manual12
5/35
3)Writeashellscripttocheckwhetherthegivenstringispalindromeornot.
echo "Enter a string:- "
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
i=1while [ $len -ge $i ]do
ch=`echo $str | cut -c $i`
ch1=`echo $str | cut -c $len`
if [ $ch != $ch1 ]thenecho "The given string is not a palindrome"exit
fi
len=`expr $len - 1`i=`expr $i + 1`
done
OUTPUT:
[mca3a6@localhost mca3a6]$ sh prg03.shEnter any stringmadamThe entered string is palindrome[mca3a6@localhost mca3a6]$ sh prg03.shEnter any stringsirThe string is not a palindrome[mca3a6@localhost mca3a6]$
mailto:mca3a6@localhostmailto:mca3a6@localhostmailto:mca3a6@localhostmailto:mca3a6@localhostmailto:mca3a6@localhostmailto:mca3a6@localhost -
7/28/2019 Unix Lab Manual12
6/35
4)Writeashellscripttodothefollowingactions:-
a) Accepttwofilenamesasanargument.b) Sortboththefilestotemporaryfileandmergethesortedfiletogetthe
finaloutput.Finallyremovethetemporaryfile.
if [ $# -ne 2 ]then
echo "Invalid arguments"exit
fi
sort $1>tempfile1sort $2>tempfile2
echo "Sorted Merged file is"
sort -m tempfile1 tempfile2 | sort -n
rm tempfile1 tempfile2
OUTPUT:
[root@localhost unix]$ sh file.shInvalid arguements
[root@localhost unix]$ sh file.sh mat
Invalid arguements122[[root@localhost unix]$ sh file.sh mat mat111 2 3224 5 67 8 9
[root@localhost unix]$ sh file.sh f1 f2hihow[root@localhost unix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
7/35
-
7/28/2019 Unix Lab Manual12
8/35
5)Writeashellscripttoacceptanumberandprintthedigitsofthatnumberinreverseorder.
echo "Enter a number: "
read numrev=0
if [ $num -lt 0 ]then
echo "Positive number has to be entered"
exitfi
while [ $num -gt 0 ]do
rem=`expr $num % 10`rev=`expr $rev \* 10 + $rem`num=`expr $num \/ 10`
done
echo "The reverse of the given number is:"echo $rev
OUTPUT:
[root@localhost unix]$ sh prg04.shEnter a positive number6usage!Input atleast two digit number[root@localhost unix]$ sh prg04.shEnter a positive number56Reverse of the given number 56 is: 65[root@localhost unix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
9/35
6)Writeashellscriptto locktheterminal.
clear
echo "Enter your password"stty -echoread p1
stty echo
echo "Confirm your password: "stty -echoread p2
stty echoif [ "$p1" != "$p2" ]
thenecho "Confirmation mistake"exit
fi
clearecho "Screen locked"echo "To open the lock re-enter the password"stty -echoread p3
stty echo
while [ "$p3" != "$p1" ]do
echo "Wrong password"echo "Enter the password again"stty -echo
read p3stty echo
done
clearecho "Screen is unlocked"exit
-
7/28/2019 Unix Lab Manual12
10/35
OUTPUT:
[root@localhost unix]$ sh terloc.shenter passwordconfirm password
confirmation mistake[root@localhost unix]$ sh terloc.shenter passwordconfirm passwordscreen lockedto open the lock , re-enter the passwordwrong passwordtry again re-enter the passwordscreen unlocked[root@localhost unix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
11/35
7)Writeashellscriptwhichacceptscommand lineargumentsandchecks
whetheritisavalidloginnameornot.Ifitisvalid,printthe loginnameand
homedirectory.
if [ $# -eq 0 ]then
echo "No arguments"exit
elsefor x in $*do
if grep "^$x:" /etc/passwd>tempthen
y=`cut -d ":" -f 6 temp`echo "Login name= " $x
echo "Home directory= " $yelseecho "Login name does not exist"
fidone
fi
OUTPUT:
[root@localhost unix]$ sh prg05.sh
No arguments[root@localhost unix]$ sh prg05.sh mca3a10LOGIN NAME:= mca3a10DIR NAME:= /home/mca3a10[root@localhost unix]$ sh prg05.sh mcaLogin doesnt exist[root@localhost unix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
12/35
-
7/28/2019 Unix Lab Manual12
13/35
OUTPUT:
[root@localhost unix]$ sh links.sh 123123 does not exist[root@localhost unix]$ sh links.sh p.sh
ls: file: No such file or directorylinks.sh: [: -eq: unary operator expectedFile has links/home/mca3a6/p.sh/home/mca3a6/q.sh/home/mca3a6/r.sh[root@localhost unix]$ sh links.sh prg02.shprg02.sh does not exist[root@localhost unix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
14/35
currentmonthcalendar inwhichtodays9)Writeashellscripttodisplaydateisreplacedby*or**.
#!bin/basha=`date +%e`
if [ $a -lt 10 ]then
cal|sed "s/$a/*/"else
cal|sed "s/$a/**/"fi
OUTPUT:
[root@localhost unix]$ sh prg10.shNovember 2007Su Mo Tu We Th Fr Sa
1 2 34 5 6 7 8 9 10
11 12 13 ** 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30
[root@localhost unix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
15/35
10)WriteanAWKshell scriptthatfolds longline intonewcolomns.Thus
any linethatexceeds5charactersmustbebroken intoafter5thCharacter
and / beappendedasan indicationoffoldingandprocessingistobe
continuedwithresidue.
awk '{str=$0while(length(str)>5){printf("%s\\\n",substr(str,1,5))str=substr(str,6,length(str)-5);
}print str} ' $@
OUTPUT:
[root@localhost unix]$ sh prg11.shall the students in our college read well told the teacherall t\he st\udent\s in \our c\olleg\
e rea\d wel\l tol\d the\teac\her
[root@localhost unix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
16/35
11)WriteanAWKprogramtofindthetransposeofagivenmatrix.
BEGIN{j=0printf("Entered matrix is\n")
}{for(i=1;i
-
7/28/2019 Unix Lab Manual12
17/35
OUTPUT:
[root@localhost unix]$ awk -f prg8.awk mat1Entered matrix is
1
47
2 3
5 68 9Transpose of matrix is
12
3
4 75 8
6 9[root@localhost unix]$ cat>mat21040
70
20 3050 60
80 90[root@localhost unix]$ cat mat2
1040
70
20 3050 60
80 90[root@localhost unix]$ awk -f prg8.awk mat2Entered matrix is104070
20 3050 6080 90
Transpose of matrix is102030
40 7050 8060 90
[root@localhost unix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
18/35
12)Writeashell programwhichacceptscommand lineargumentandcreates
directorisinhierarchical order.
if [ $# -eq 0 ]then
echo "Insufficient Arguments"exit
fiIFS=`/`for dirname in $*domkdir $dirnamecd $dirname
done
OUTPUT:
[root@localhost unix]$ sh prg9.shInsufficient Arguments[root@localhost unix]$ sh prg9.sh bangalore/rnsit/mca[root@localhost unix]$ cd bangalore
[root@localhostbangalore]$ lsrnsit[root@localhostbangalore]$ cd rnsit[root@localhost rnsit]$ ls
mca[root@localhost rnsit]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
19/35
1. WriteaLEXprogramtocountnumberofblankspaces,characters,
lines,wordsfromagiventext.
%{int bcount=0,ccount=0,lcount=0,wcount=0;
%}%%[^ \t\n]+ {wcount++;ccount+=yyleng;}\n { lcount++; }" " { bcount++;ccount;}. {ccount++; }
%%
main(){printf("Enter the text");
yylex();printf("word=%d\n char=%d\n line=%d\n blankspace=%d\n",wcount,ccount,lcount,bcont);}OUTPUT:[root@localhost unix]$ lex lex1.l[root@localhost unix]$ cc lex.yy.c -ll[[root@localhost unix]$./a.outEnter the text
this is linux red hat enterpise asword=8
char=35line=2blankspace=8[root@localhost unix]$
version
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
20/35
2. WriteaLEXprogramtocountnumberofpositiveandnegative
integersandfractions.%{int pint=0,nint=0,pfr=0,nfr=0;%}
%%\+?[0-9]+-[0-9]+
{pint++;}{nint++;}
\+?[0-9]*\.[0-9]+ {pfr++;}-[0-9]*\.[0-9]+ {nfr++;}. {}%%main(){printf("Enter the no.: ");yylex();
printf("pos int=%d\n neg int=%d",pint,nint);printf("pos frac=%d\n neg frac=%d",pfr,nfr);}
OUTPUT:
[root@localhostunix]$lexlex2.l[root@localhostunix]$cclex.yy.c-ll[root@localhostunix]$./a.outEnter the no.: 5
7
8
-43
-45
-44.6
-7.3
7.8
4.8
pos int=3neg int=2pos frac=2neg frac=2
[mca3a5@localhost mca3a5]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:mca3a5@localhostmailto:mca3a5@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
21/35
3. WriteaLEXprogramtocheckwhetherthegivenarithmeticalexpressionisvalidornotandprintnumberofoperatorsandoperands.
%{
int flag=1;
int oprc=0,digic=0,top=-1;char stack[10];
%}
digit [0-9]+opr[+*/-]
%%
['('] {stack[++top]='(';}[')'] {flag=1;
if((stack[top]!='(') && (top!=-1)){printf("INVALID EXPRN\n");exit(0);
}
top--;}
{digit} {digic++; }{opr}/{digit} {oprc++; }{opr}/['('] {oprc++; }
. { printf("INVALID");exit(0);}%%
main(){
yylex();if(((oprc+1 == digic)||(oprc == digic)) && (top == -1)){
printf("VALID EXPRN\n");printf("NUMBER OF OPERATORS : %d\nNUMBER OF OPERANDS :
%d",oprc
digic);}else{
printf("INVALID EXPN");}
}
-
7/28/2019 Unix Lab Manual12
22/35
OUTPUT:[root@localhostunix]$lex lexexprn.l[root@localhostunix]$cc lex.yy.c -ll[root@localhostunix]$./a.out(3+1)
VALID EXPRNNUMBER OF OPERATORS : 1NUMBER OF OPERANDS : 2[mca3a8@localhost mca3a8]$ ./a.out3*%INVALID
[root@localhostunix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:mca3a8@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:mca3a8@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
23/35
4. WriteaLEXprogramtocheckwhetheragivensentenceissimpleor
compound.%{
int flag=0;%}
%%and flag=1;or flag=1;but flag=1;
%%
main(){
yylex();if(flag)
{ printf("ITS A COMPUND STATEMENT\n");}else{
printf("ITS A SIMPLE STATEMENT");}
}
OUTPUT:
[root@localhostunix]$lex lexsimp.l[root@localhostunix]$cc lex.yy.c -ll[root@localhostunix]$./a.outme and my friend went to the marketme my friend went to the marketITS A COMPUND STATEMENT
[root@localhostunix]$./a.outI am a writerI am a writerITS A SIMPLE STATEMENT
[root@localhostunix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
24/35
5. WriteaLEXprogramtocountnumberofprintfandscanffroma
givencprogramfileandreplacethemwithwriteandreadrespectively.
%{
int pcount=0,scount=0;%}
%%
printf {pcount++,fprintf(yyout,"write ");}scanf {scount++,fprintf(yyout,"read ");}
%%
main()
{yyin = fopen("dee.c","r");yyout = fopen("dee2.c","w");yylex();printf("NUMBER OF PRINTF = %d\n NUMBER OF SCANF =
%d",pcount,scount);}
OUTPUT:
[root@localhostunix]$lex lexcountpfsf.l
[root@localhostunix]$cc lex.yy.c -ll[root@localhostunix]$./a.outNUMBER OF PRINTF = 2NUMBER OF SCANF = 1[mca3a8@localhost mca3a8]$ cat dee2.c#include(stdio.h)main()
{int a,b;write ("ENTER 2 NUM\n");
read ("%d %d",&a,&b);write ("SUM = %d",a+b);
}
[root@localhostunix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:mca3a8@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:mca3a8@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
25/35
6. WriteaLEXprogramtocountnumberofvowelsandconsonants.
%{int vow=0;int con=0;
%}
%%[aeiouAEIOU] {vow++;}[a-zA-Z] {con++;}%%
main(){
printf("enter the string\n");yylex();
printf("\n the number of vowels in a string is %d \n",vow);printf("\n the number of consonents in a string is %d \n",con);}
OUTPUT:
[root@localhost unix]$ lex vowcons.l[root@localhost unix]$ cc lex.yy.c -ll[root@localhost unix]$ ./a.outenter the stringhello friends do unix programs its interesting.
.
the number of vowels in a string is 14
the number of consonents in a string is 26[root@localhost unix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
26/35
1. WriteaYACCprogramtorecognizethegrammer[anb/n>0].Test
whetherthefollowingstringbelongstothisgrammer.%{
#include
%}
%token a b
%%st:st reca endb '\n' {printf("STRING BELONGS TO GRAMMER");}||error '\n'{yyerror("DOES NOT BELONG TO GRAMMER");yyerrok;}reca:enda reca
|;
enda:a;endb:b;
%%
main(){ printf("Enter the text :");
yyparse();}yylex(){
char c;while((c=getchar())==' ');if(c=='a')return a;if(c=='b')return b;
return c;}
yyerror(char *s){
printf("%s",s);}
-
7/28/2019 Unix Lab Manual12
27/35
OUTPUT:[root@localhost unix]$ yacc -d yacc1.y[root@localhost unix]$cc y.tab.c -ll[root@localhost unix]$ ./a.outEnter the text :a
syntax errorDOES NOT BELONG TO GRAMMERbSTRING BELONGS TO GRAMMERaaaaabSTRING BELONGS TO GRAMMERbasyntax errorDOES NOT BELONG TO GRAMMER
[root@localhost unix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
28/35
2. writeaYACCprogramtorecognizethegrammer[a
nbn/n>0].
#include%{
%}
%token a b
%%st:st reca endb '\n'{printf("STRING BELONG TO GRAMMAR ");}|st'\n'{printf("WHR N VALUE IS 0");}||error '\n'{yyerror("DOES NOT BELONG TO THE GRAMMAR");yyerrok;}
;reca:enda reca endb|endaenda:a;
endb:b;
%%
main(){
printf("Enter the text :");yyparse();
}yylex(){
char c;while((c=getchar())==' ');if(c=='a') return a;if(c=='b') return b;return c;
}
yyerror(char *s){
printf("%s",s);}
-
7/28/2019 Unix Lab Manual12
29/35
OUTPUT:[root@localhost unix]$ yacc yacc2.y[root@localhost unix]$ cc y.tab.c -ll[root@localhost unix]$ ./a.outEnter the text :aab
syntax errorDOES NOT BELONG TO THE GRAMMARaabbSTRING BELONG TO GRAMMAR abvsyntax errorDOES NOT BELONG TO THE GRAMMARabSTRING BELONG TO GRAMMAR baasyntax errorDOES NOT BELONG TO THE GRAMMARWHR N VALUE IS 0
[root@localhost unix]$
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
30/35
3. writeaYACCprogramtoevaluateanarithmeticexpression.%{
#include#include#include
#include#define YYSTYPE double%}%token num%left '+' '-'%left '*' '/'%%st: st expr '\n' {printf("value is = %f \n",$2);}
|st '\n'||error '\n' {printf("invalid\n");}
;expr: num {$$=$1;}|expr '+' expr {$$=$1+$3;}|expr '-' expr {$$=$1-$3;}|expr '*' expr {$$=$1*$3;}|expr '/' expr {
if($3==0){
printf("division by zero!\n");exit(0);
}else$$=$1/$3;
}|'(' expr ')';
%%main(){
printf("enter an expression to evaluate:");yyparse();
}yylex()
{int ch;while((ch=getchar())==' ');if(isdigit(ch)|ch=='.'){
ungetc(ch,stdin);scanf("%lf",&yylval);
-
7/28/2019 Unix Lab Manual12
31/35
return num;
}return ch;
}yyerror(char *s)
{ printf("%s",s);}
-
7/28/2019 Unix Lab Manual12
32/35
OUTPUT:
[root@localhostyacc]$ yacc expr.y[root@localhostyacc]$ cc y.tab.c -ll
[root@localhostyacc]$ ./a.outenter an expression to evaluate:4+5*7value is = 39.000000
[root@localhostyacc]$ ./a.outenter an expression to evaluate:6/4value is = 1.500000
[root@localhostyacc]$ ./a.outenter an expression to evaluate:9/0division by zero!
[root@localhostyacc]$
4. writeaYACCprogramwhichrecognizesavalidvariablewhichstartswithletterfollowedbyadigit.
%{#include#include
%}
%token let dig
%%sat:let recld '\n'{printf("ACCEPTED\n");exit(0);}
|let '\n' {printf("ACCEPTED\n");exit(0);}||error '\n' {printf("INVALID");yyerrok;exit(0);}
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
33/35
;
recld:let recld|dig recld|let|dig
;
%%
yylex(){
char ch;while((ch=getchar())==' ');if(isalpha(ch))
return let;if(isdigit(ch))
return dig;return ch;
}
yyerror(char *s){
printf("%s",s);
}
main(){
printf("ENTER THE STRING\n");yyparse();
}
OUTPUT:
[root@localhostyacc]$ yacc yacc4.y[root@localhostyacc]$ cc y.tab.c -ll[root@localhostyacc]$ ./a.out
ENTER THE STRINGa4ACCEPTED
[root@localhostyacc]$ ./a.outENTER THE STRING7er
syntax errorINVALID[root@localhostyacc]$ ./a.outENTER THE STRING
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
34/35
$l
syntax errorINVALID[root@localhostyacc]$ ./a.outENTER THE STRINGsumACCEPTED
[root@localhostyacc]$
5. writeaYACC&LEXprogramto identifyvalid ifandif-elsestatement.
LEXPROGRAM:-%{
#include#include#include"y.tab.h"
%}
%%if return IF;else return ELSE;[a-zA-Z0-9+\-\*/=]+ return STAT;[a-zA-Z0-9+\-\*/=&!]+ return CONDITION;. return yytext[0];%%
YACCPROGRAM:-%{
int v=1;
%}%token IF STAT CONDITION ELSE%nonassoc IF%nonassoc ELSE
%%ifstruct: IF expr state1
|IF expr state1 ELSE state1 %prec ELSE;
state1: stmt|"{"state"}"
|ifstruct %prec IF;
state: stmt|stmt state|ifstruct|ifstruct state;
expr: "("CONDITION")"|"("STAT")"
mailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhostmailto:root@localhost -
7/28/2019 Unix Lab Manual12
35/35
;
stmt: STAT ";"|";";
%%
int yyerror(char *s){v=0;
}
main(){
yyparse();if(v)
printf("valid");else
printf("invalid");}
OUTPUT:
[root@localhostyacc]$lexlex55.l[root@localhostyacc]$yacclex55.y[root@localhostyacc]$cclex.yy.cy.tab.c-ll[root@localhostyacc]$./a.outENTERTHEIF-ELSESTATEMENTif(a>b)
big=a;
else
big=b;
valid[root@localhostyacc]$./a.outENTERTHEIF-ELSESTATEMENT
if(a