Web view1) Write a shell script that accepts command line arguments and prints themin reverse...

35
1) Write a shell script that accepts command line arguments and prints them echo "Checking the number of arguments" if [ $# then -eq 0 ] echo "No arguments exit supplied" fi n=$# while do [ $n -gt 0 ] eval echo \$$n n=`expr $n - 1` done

Transcript of Web view1) Write a shell script that accepts command line arguments and prints themin reverse...

Page 1: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

1) Write a shell script that accepts command line arguments and prints themin reverse order.

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

Page 2: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

OU T P U T :

[root@localhost unix]$Checking the number ofNo arguments supplied[root@localhost unix]$Checking the number ofgreatare indians

sh prg01.sharguments

sh prg01.sharguments

indians are great

[root@localhost unix]$

Page 3: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

2) Write a shell script, which takes two filenames as command line arguments and checks, whether two files have identical file permissions and print. If not, print filenames and their permissions separately.

if [ $# -eq 0 ]then

echoexit

elif [then

echoexit

else echo

fi

"No filename given"

$# -eq 1 ]

"Only one filename is given"

"Two filenames given"

if [ !then

echoexit

fi

-e $1 -a ! -e $2 ]

"Both files does not exist"

if [ !then

echoexit

fiif [ !then

echoexit

fi

-e $1 ]

"File 1 does not exist"

-e $2 ]

"File 2 does not exist"

x=`ls -l $1 | cut -c 2-10`y=`ls -l $2 | cut -c 2-10`

if [ $x = $y ]then

echoelse

echoechoecho

fi

"Common permissions"

"Uncommon"File 1:""File 2:"

permissions"$x$y

Page 4: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

OU T P U T :

[mca3a6@localhost mca3a6]$sh prg2.shInsufficient number of arguments[root@localhost unix]$sh prg2.sh a1only one file name is given,Insufficient[root@localhost unix]$[root@localhost unix]$sh prg2.sh f1 f2TwoTwo

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@localhost unix]$ _

Page 5: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

3) Write a shell script to check whether the given string is palindrome or not.

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 ]then

echo "The given string is not a palindrome"exit

fi

len=`expr $len - 1`i=`expr $i + 1`

done

OU T P U T :

[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]$

Page 6: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

4) Write a shell script to do the following actions:- a) Accept two filenames as an argument.b) Sort both the files to temporary file and merge the sorted file to get the

final output. Finally remove the temporary file.

if [ $# -ne 2 ]then

echo "Invalid arguments"exit

fi

sort $1>tempfile1sort $2>tempfile2

echo "Sorted Merged file is"

sort -m tempfile1 tempfile2 | sort -nrm tempfile1 tempfile2

OU T P U T :

[root@localhost unix]$ sh file.shInvalid arguements

[root@localhost unix]$ sh file.sh matInvalid 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]$

Page 7: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No
Page 8: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

5) Write a shell script to accept a number and print the digits of that numberin reverse order.

echo "Enter a number: "read num rev=0

if [ $num -lt 0 ]then

echo "Positive number has to be entered"exit

fi

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

OU T P U T :

[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]$

Page 9: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

6) Write a shell script to lock the terminal.

clear

echo "Enter your password"stty -echo read p1 stty echo

echo "Confirm your password: "stty -echoread p2

stty echoif [ "$p1" != "$p2" ]then

echo "Confirmation mistake"exit

fi

clearecho "Screen locked"echo "To open the lock re-enter the password"stty -echo read p3 stty echo

while [ "$p3" != "$p1" ]do

echo "Wrong password"echo "Enter the password again"stty -echo

read p3 stty echo done

clearecho "Screen is unlocked"exit

Page 10: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

OU T P U T :

[root@localhost unix]$ sh terloc.shenter passwordconfirm password confirmation mistake

[root@localhost unix]$ sh terloc.sh enter passwordconfirm password screen lockedto open the lock , re-enter the password wrong passwordtry again re-enter the password screen unlocked[root@localhost unix]$

Page 11: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

7) Write a shell script which accepts command line arguments and checks whether it is a valid login name or not. If it is valid, print the login name and home directory.

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= " $xecho "Home directory= " $y

elseecho "Login name does not exist"

fi done fi

OU T P U T :

[root@localhost unix]$ sh prg05.shNo 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]$

Page 12: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

8) Write a shell script to determine the number of links of the given file nameas command line arguments.

if [ $# -eq 0 ]then

echo "Insufficient data"echo "Usage: Script name, filename, directory"exit

elif [ $# -eq 2 ]then dir=$2 else

dir=`pwd`fi

cd $dir

if [ ! -e $1 ]then

echo "File $1 does not exist"exit

fi

file=$1set -- `ls -l $file`link=$2

if [ $link -eq 1 ]then

echo "File has no links"else

echo "File has $link links"fi

set -- `ls -i $file`inode=$1

find $dir -inum $inode -print

Page 13: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

OU T P U T :

[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]$

Page 14: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

current month calendar in which today’s9) Write a shell script to displaydate is replaced by * or **.

#!bin/basha=`date +%e` if [ $a -lt 10 ] then

cal|sed "s/$a/*/"else

cal|sed "s/$a/**/"fi

OU T P U T :

[root@localhost unix]$ sh prg10.shNovember 2007

Su Mo Tu We Th Fr Sa1 2 3

4 5 6 7 8 9 1011 12 13 ** 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30

[root@localhost unix]$

Page 15: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

10) Write an AWK shell script that folds long line into new colomns. Thus any line that exceeds 5 characters must be broken into after 5th Character and / be appended as an indication of folding and processing is to be continued with residue.

awk '{str=$0while(length(str)>5){printf("%s\\\n",substr(str,1,5))str=substr(str,6,length(str)-5);

}print str} ' $@

OU T P U T :

[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]$

Page 16: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

11) Write an AWK program to find the transpose of a given matrix.

BEGIN{j=0printf("Entered matrix is\n")

}{for(i=1;i<=NF;i++){a[j++]=$i;printf("%s\t",$i);

}printf("\n");

}END{printf("Transpose of matrix is");for(i=0;i<NF;i++){k=i;printf("\n");for(j=0;j<NR;j++){if(a[k]>=0&&a[k]<=9)printf("%s\t",a[k]);

else printf("%s\t",a[k]); k=k+NF;

}}printf("\n");

}

Page 17: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

OU T P U T :

[root@localhost unix]$ awk -f prg8.awk mat1Entered matrix is147

2 35 68 9

Transpose of matrix is123

4 75 86 9

[root@localhost unix]$ cat>mat2104070

20 3050 6080 90

[root@localhost unix]$ cat mat2104070

20 3050 6080 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]$

Page 18: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

12) Write a shell program which accepts command line argument and creates directoris in hierarchical order.

if [ $# -eq 0 ]then

echo "Insufficient Arguments"exit

fiIFS=`/`for dirname in $*domkdir $dirnamecd $dirname

done

OU T P U T :

[root@localhost unix]$ sh prg9.shInsufficient Arguments[root@localhost unix]$ sh prg9.sh bangalore/rnsit/mca[root@localhost unix]$ cd bangalore[root@localhost bangalore]$ ls rnsit[root@localhost bangalore]$ cd rnsit[root@localhost rnsit]$ lsmca[root@localhost rnsit]$

Page 19: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

1. Write a LEX program to count number of blank spaces, characters, lines, words from a given text.

%{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,bco nt);}OU T P U T : [root@localhost unix]$ lex lex1.l[root@localhost unix]$ cc lex.yy.c -ll[[root@localhost unix]$./a.outEnter the textthis is linux red hat enterpise asword=8char=35 line=2 blankspace=8

[root@localhost unix]$

version

Page 20: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

2. Write a LEX program to count number of positive and negative integers and fractions.

%{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);}

OU T P U T : [root@localhost unix]$ lex lex2.l[root@localhost unix]$ cc lex.yy.c -ll[root@localhost unix]$./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]$

Page 21: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

3. Write a LEX program to check whether the given arithmeticalexpression is valid or not and print number of operators and operands.

%{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");}

}

Page 22: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

OUTPUT:[root@localhost unix]$ lex lexexprn.l[root@localhost unix]$ cc lex.yy.c -ll[root@localhost unix]$./a.out(3+1)

VALID EXPRNNUMBER OF OPERATORS : 1NUMBER OF OPERANDS : 2[mca3a8@localhost mca3a8]$ ./a.out3*% INVALID[root@localhost unix]$

Page 23: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

4. Write a LEX program to check whether a given sentence is simple or 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@localhost unix]$ lex lexsimp.l[root@localhost unix]$ cc lex.yy.c -ll[root@localhost unix]$ ./a.outme and my friend went to the marketme my friend went to the marketITS A COMPUND STATEMENT[root@localhost unix]$ ./a.outI am a writerI am a writerITS A SIMPLE STATEMENT[root@localhost unix]$

Page 24: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

5. Write a LEX program to count number of printf and scanf from agiven c program file and replace them with write and read respectively.

%{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@localhost unix]$ lex lexcountpfsf.l[root@localhost unix]$ cc lex.yy.c -ll[root@localhost unix]$./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@localhost unix]$

Page 25: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

6. Write a LEX program to count number of vowels and consonants.

%{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)

}

OU T P U T :

[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]$

Page 26: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

1. Write a YACC program to recognize the grammer [ an b/n>0] . Test whether the following string belongs to this grammer.

%{#include<stdio.h>

%}

%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);}

Page 27: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

OU T P U T : [root@localhost unix]$ yacc -d yacc1.y[root@localhost unix]$cc y.tab.c -ll[root@localhost unix]$ ./a.outEnter the text :asyntax errorDOES NOT BELONG TO GRAMMERbSTRING BELONGS TO GRAMMERaaaaabSTRING BELONGS TO GRAMMERbasyntax errorDOES NOT BELONG TO GRAMMER[root@localhost unix]$

Page 28: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

2. write a YACC program to recognize the grammer [an bn /n>0].

#include<stdio.h>%{

%}

%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|enda 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);}

Page 29: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

OU T P U T : [root@localhost unix]$ yacc yacc2.y[root@localhost unix]$ cc y.tab.c -ll[root@localhost unix]$ ./a.outEnter the text :aabsyntax 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]$

Page 30: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

3. write a YACC program to evaluate an arithmetic expression.%{

#include<stdio.h>#include<ctype.h>#include<stdlib.h>#include<string.h>#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);

Page 31: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

return num;}return ch;

}yyerror(char *s){

printf("%s",s);}

Page 32: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

OU T P U T :

[root@localhost yacc]$ yacc expr.y[root@localhost yacc]$ cc y.tab.c -ll[root@localhost yacc]$ ./a.outenter an expression to evaluate:4+5*7value is = 39.000000[root@localhost yacc]$ ./a.out enter an expression to evaluate:6/4 value is = 1.500000[root@localhost yacc]$ ./a.out enter an expression to evaluate:9/0 division by zero![root@localhost yacc]$

4. write a YACC program which recognizes a valid variable which startswith letter followed by a digit.

%{#include<stdio.h>#include<ctype.h>

%}

%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);}

Page 33: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

;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();

}

OU T P U T : [root@localhost yacc]$ yacc yacc4.y[root@localhost yacc]$ cc y.tab.c -ll[root@localhost yacc]$ ./a.outENTER THE STRINGa4ACCEPTED[root@localhost yacc]$ ./a.outENTER THE STRING7ersyntax errorINVALID[root@localhost yacc]$ ./a.outENTER THE STRING

Page 34: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

$lsyntax errorINVALID[root@localhost yacc]$ ./a.outENTER THE STRINGsumACCEPTED[root@localhost yacc]$

5. write a YACC & LEX program to identify valid if and if-elsestatement.

LEX PROGRAM :-%{

#include<stdlib.h>#include<stdio.h>#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];%%

YACC PROGRAM:-%{

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")"

Page 35: Web view1) Write a shell script that accepts command line arguments and prints themin reverse order.echo "Checking the number of arguments"if [ $#then-eq 0 ]echo "No

;stmt: STAT ";"

|";";

%%int yyerror(char *s){

v=0;}

main(){

yyparse();if(v)

printf("valid");else

printf("invalid");}

OU T P U T : [root@localhost yacc]$ lex lex55.l[root@localhost yacc]$ yacc lex55.y[root@localhost yacc]$ cc lex.yy.c y.tab.c -ll[root@localhost yacc]$ ./a.outENTER THE IF-ELSE STATEMENTif(a>b)

big=a;

else

big=b;

valid[root@localhost yacc]$ ./a.outENTER THE IF-ELSE STATEMENTif(a<0

invalid[root@localhost yacc]$