0% found this document useful (0 votes)
143 views16 pages

SS and CD Lab - Programs

The document describes a systems programming and compiler design laboratory course. It includes the following: 1) Several programs to be written using LEX and YACC involving tasks like counting elements in a file, recognizing arithmetic expressions and grammars, and more. 2) Unix programming assignments involving shell scripts and C programs for tasks like reversing command line arguments, checking file permissions, finding the largest file in a directory, and more. 3) A compiler design assignment to implement the syntax-directed definition of if-then and if-then-else statements. 4) Instructions state that examinations will involve one LEX and one YACC problem and one programming exercise from the Unix programming section.

Uploaded by

Anjali Pujar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views16 pages

SS and CD Lab - Programs

The document describes a systems programming and compiler design laboratory course. It includes the following: 1) Several programs to be written using LEX and YACC involving tasks like counting elements in a file, recognizing arithmetic expressions and grammars, and more. 2) Unix programming assignments involving shell scripts and C programs for tasks like reversing command line arguments, checking file permissions, finding the largest file in a directory, and more. 3) A compiler design assignment to implement the syntax-directed definition of if-then and if-then-else statements. 4) Instructions state that examinations will involve one LEX and one YACC problem and one programming exercise from the Unix programming section.

Uploaded by

Anjali Pujar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16



66DQG&'ODE3URJUDPV

Systemprogrammingandcompilerdesignlaboratory
SubjectCode:06CSL68I.A.Marks:25
Hours/Week:03ExamHours:03
TotalHours:42ExamMarks:50
PARTA
LEXandYACCPrograms:
ExecutethefollowingprogramsusingLEX:
1)a.Programtocountthenumberofcharacters,words,spacesandlinesin
agiveninputfile.
b.ProgramtocountthenumbersofcommentlinesinagivenCprogram.
Alsoeliminatethemandcopytheresultingprogramintoseparatefile.
2)a.Programtorecognizeavalidarithmeticexpressionandtorecognizethe
identifiersandoperatorspresent.Printthemseparately.
b.Programtorecognizewhetheragivensentenceissimpleor
compound.
3)Programtorecognizeandcountthenumberofidentifiersinagiven
inputfile.
ExecutethefollowingprogramsusingYACC:
4)a.Programtorecognizeavalidarithmeticexpressionthatuses
operators+,,*
and/.
b.Programtorecognizeavalidvariable,whichstartswithaletter,
followedbyanynumberoflettersordigits.
5)a.Programtoevaluateanarithmeticexpressioninvolvingoperators+,
,*and/.
b.Programtorecognizestringsaaab,abbb,abandausingthe
grammar
(an
bn
,n>=0).
6)Programtorecognizethegrammar(a
n
b,n>=10).

PARTB
UnixProgramming:
1.a)Nonrecursiveshellscriptthatacceptsanynumberofarguments
andprints
themintheReverseorder,(Forexample,ifthescriptisnamedrargs,
thenexecutingrargsABCshouldproduceCBAonthestandard
output).
b)Cprogramthatcreatesachildprocesstoreadcommandsfromthe
standardinputandexecutethem(aminimalimplementationofa
shelllikeprogram).Youcanassumethatnoargumentswillbe
passedtothecommandstobeexecuted.
2.a)Shellscriptthatacceptstwofilenamesasarguments,checksifthe
permissionsforthesefilesareidenticalandifthepermissionsare
identical,outputsthecommonpermissions,otherwiseoutputseachfile
namefollowedbyitspermissions.
b)Cprogramtocreateafilewith16bytesofarbitrarydatafromthe
beginning
andanother16bytesofarbitrarydatafromanoffsetof48.Display
thefilecontentstodemonstratehowtheholeinfileishandled.
3.a)Shellfunctionthattakesavaliddirectorynamesasanargumentand
recursively
descendsallthesubdirectories,findsthemaximumlengthofanyfilein
thathierarchyandwritesthismaximumvaluetothestandardoutput.
b)Cprogramthatacceptsvalidfilenamesascommandlinearguments
andfor
eachofthearguments,printsthetypeofthefile(Regularfile,
Directoryfile,Characterspecialfile,Blockspecialfile,Symbolic
linketc.)
4.a)Shellscriptthatacceptsfilenamesspecifiedasargumentsand
createsashell
scriptthatcontainsthisfileaswellasthecodetorecreatethesefiles.
Thusifthescriptgeneratedbyyourscriptisexecuted,itwould
recreatetheoriginalfiles(Thisissameasthebundlescriptdescribed
byBrainW.KernighanandRobPikeinTheUnixProgramming
Environment,PrenticeHallIndia).
b)Cprogramtodothefollowing:Usingfork()createachildprocess.

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
Thechildprocessprintsitsownprocessidandidofitsparentandthenexits.
Theparentprocesswaitsforitschildtofinish(byexecutingthewait(
))andprintsitsownprocessidandtheidofitschildprocessand
thenexits.
CompilerDesign:
5.WriteaCprogramtoimplementthesyntaxdirecteddefinitionofifE
thenS1andifEthenS1elseS2.(ReferFig.8.23inthetextbook
prescribedfor06CS62CompilerDesign,AlfredVAho,RaviSethi,
JeffreyDUllman:CompilersPrinciples,TechniquesandTools,
AddisonWesley,2007.)
6.Writeayaccprogramthatacceptsaregularexpressionasinputand
produceitsparsetreeasoutput.

Instructions:
Intheexamination,acombinationofoneLEXandoneYACCproblem
hastobeaskedfromPartAforatotalof25marksandoneprogramming
exercisefromPartBhastobeaskedforatotalof25marks.

PartA(LEXandYACC)

1a.Countcharacters,linesandwords
%{
ints=0,w=0,l=1,c=0
%}
%%
[\n]*{l+=yyleng}
[\t]*{s+=yyleng}
[^\n\t]*[\t]{w++c+=yyleng1s++}
[^\n\t]*[]{w++c+=yyleng1s++}
[^\n\t]*[\n]{w++c+=yyleng1l++}
[^\n\t]*{w++c+=yyleng}
%%
main(intargc,char**argv)
{
if(argc!=2)
{
printf("\nIncorrectusage!!\n")
return1
}
yyin=fopen(*(argv+1),"r")
if(!yyin)
{
printf("\nCannotopenthefile!\n")
return1
}
yylex()
printf("\nChar=%d\nSpaces=%d\nWords=%d\nLines=%d\n",c,s,w,l)
}

1b.CommentLines
%{
intcom=0
%}
%%
\/\*[^*]*\*(\*|([^*/][^*]*\*))*\/com++
\/\/[^\n]*[\n]com++
%fprintf(yyout,"%%")
.|[\n]fprintf(yyout,yytext)
%%
main(intargc,char**argv)
{
if(argc!=3)
{
printf("\nArgumentspassedinwrongmanner\n")
return1
}

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
yyin=fopen(*(argv+1),"r")
yyout=fopen(*(argv+2),"w")
if(!(yyin&&yyout))
{
printf("\nSpecifiedfilecannotbeopened!")
return1
}
yylex()
printf("\nTotlanumberofcommentlines=%d\n",com)
}

2a.Validarithmeticexpression
%{
#include<string.h>
intvalid,i,j,k,temp,temp1,top=1,num[40]
chararr[40][10],st[80],op[40]
%}
%%
[azAZ][azAZ09]*{
strcpy(arr[j++],yytext)
if(st[top1]=='i')
valid=1
elseif(st[top1]=='o')
st[top1]='i'
else
st[top++]='i'
}
[09]+{
num[k++]=atoi(yytext)
if(st[top1]=='i')
valid=1
elseif(st[top1]=='o')
st[top1]='i'
else
st[top++]='i'
}
[\+/*%^&|]{
op[temp++]=yytext[0]
if(st[top1]=='i')
st[top1]='o'
else
valid=1
}
"("{
if(st[top1]=='('||st[top1]=='$'||st[top1]=='o')
st[top++]='('
else
valid=1
}
")"{
if(st[top1]=='(')
top
elseif(st[top1]=='i'&&st[top2]=='(')
{
top=2
if(st[top1]=='o')
st[top1]='i'
elseif(st[top1]=='i')
valid=1
else
st[top++]='i'
}
else
valid=1
}
.valid=1
\nreturn
%%
check()
{
if(!(temp|k|j))
return0
if(valid)
return0
if(top==2&&st[top1]=='i')

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
top
if(top==1&&st[top1]=='$')
top
if(top==0)
return1
return0
}
main()
{
st[top1]='$'
printf("\nEntertheexpression\n")
yylex()
if(check())
{
printf("\nValidexpression!\n")
if(j>0)
printf("\nIdentifierspresentare")
for(i=0i<ji++)
printf("\n%s",arr[i])
if(k>0)
printf("\nNumbersusedare")
for(i=0i<ki++)
printf("\n%d",num[i])
if(temp>0)
printf("\nOperatorspresentare")
for(i=0i<tempi++)
printf("\n%c",op[i])
}
else
printf("\nInvalidexpression!\n")
}

2b.Simpleorcomplexstatement
%{
intvalid
%}
%%
[azAZ][](and|but|if|then|else|nevertheless)[][azAZ]{valid=1}
.|[\n]
%%
main()
{
printf("\nEnterthetext")
yylex()
if(valid)
{
printf("\nStatementiscompound!\n")
}
else
{
printf("\nStatementissimple!\n")
}
}

3.Identifiers(method1)
%{
charch
intid
%}
%%
^[\t]*(int|float|double|char){
ch=input()
while(1)
{
if(ch==',')
id++
elseif(ch=='')
{
id++

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
break
}
ch=input()
}
}
.|[\n]
%%
intmain(intargc,char**argv)
{
if(argc!=2)
{
printf("\nImproperusage!\n")
return1
}
yyin=fopen(*(argv+1),"r")
if(!yyin)
{
printf("\nSpecifiedfilecannotbeopened!\n")
return1
}
yylex()
printf("\nTotalidentifiersis%d\n",id)
}

3.Identifiers(method2)
%{
#include<ctype.h>
charch,arr[20]
intid,i,j
inttest(char*)
%}
%%
^[\t]*("int"|"float"|"double"|"char")[\t]*{
ch=input()
while(1)
{
if(ch=='\n'||ch==0)
break
if(ch==','||ch=='')
{
arr[i]=0
i=0
j=test(arr)
if(j!=1)
{
id+=j
printf("\n%sisa%s",arr,j?"identifier":"nonidentifier")
}
if(ch=='')
break
ch=input()
continue
}
arr[i++]=ch
ch=input()
}
}
.|[\n]
%%
yywrap()
{
return1
}
intrno(char*a,intstate)
{
if(*a=='='&&state==0&&!(*a=0))
return1
if(isdigit(*a)&&state==1)
state=1
if(*a==']'&&state==1)
state=0
if(*a==0&&state==0)
return1
if(*a=='['&&state==0)

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
state=1
a++
returnrno(a,state)
}
inttest(char*a)
{
char*b=a
inti
while(*b=='')
b++
if(!isalpha(*b))
return0
while(*b!=0)
{
if(*b=='='&&!(*b=0))
return1
if(*b=='[')
{
i=rno(b++,1)
b
if(i==1)
*b=0
returni
}
if(!isalnum(*b))
return0
b++
}
return1
}
intmain(intargc,char**argv)
{
if(argc!=2)
{
printf("\nImproperusage!\n")
return1
}
yyin=fopen(*(argv+1),"r")
if(!yyin)
{
printf("\nSpecifiedfilecannotbeopened!\n")
return1
}
yylex()
printf("\nTotalidentifiersis%d\n",id)
}

4a.Validarithmeticexpression(method1)
LEXpart
%{
#include"y.tab.h"
%}
%%
[azAZ_][azAZ_09]*returnid
[09]+(\.[09]*)?returnnum
[+/*]returnop
.returnyytext[0]
\nreturn0
%%

YACCpart
%{
#include<stdio.h>
intvalid=1
%}
%tokennumidop
%%
start:id'='s''
s:idx
|numx

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
|''numx
|'('s')'x

x:ops
|''s
|

%%
intyyerror()
{
valid=0
printf("\nInvalidexpression!\n")
return0
}
intmain()
{
printf("\nEntertheexpression:\n")
yyparse()
if(valid)
{
printf("\nValidexpression!\n")
}
}

4a.Validarithmeticexpression(method2)
LEXpart
%{
#include"y.tab.h"
%}
%%
[azAZ_][azAZ_09]*returnid
[09]+(\.[09]*)?returnnum
[+/*]returnop
.returnyytext[0]
\nreturn0
%%

YACCpart
%{
#include<stdio.h>
intvalid=1
%}
%tokennumidop
%left''op
%%
start:idy'='s''
y:op
|''
|

s:xopx
|x''x
|'('s')'
|x

x:id
|''num
|num

%%
intyyerror()
{
valid=0
printf("\nInvalidexpression!\n")
return0
}
intmain()
{
printf("\nEntertheexpression:\n")
yyparse()

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
if(valid)
{
printf("\nValidexpression!\n")
}
}

4b.Validvariable
LEXpart
%{
#include"y.tab.h"
%}
%%
[azAZ]returnletter
[09]returndigit
.returnyytext[0]
\nreturn0
%%
YACCpart
%{
#include<stdio.h>
intvalid=1
%}
%tokendigitletter
%%
start:letters
s:letters
|digits
|

%%
intyyerror()
{
printf("\nItsnotaidentifier!\n")
valid=0
return0
}
intmain()
{
printf("\nEnteranametotestedforidentifier")
yyparse()
if(valid)
{
printf("\nItisaidentifier!\n")
}
}

5a.Evaluatevalidarithmeticexpression
LEXpart
%{
#include"y.tab.h"
intexternyylval
%}
%%
[09]+{yylval=atoi(yytext)returnnum}
.returnyytext[0]
\nreturn0
%%

YACCpart
%
#include<stdio.h>
intvalid=0,temp
%}

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
%tokennum
%left'+'''
%left'*''/'
%nonassocUMINUS
%%
expr1:expr{temp=$1}
expr:expr'+'expr{$$=$1+$3}
|expr''expr{$$=$1$3}
|expr'*'expr{$$=$1*$3}
|expr'/'expr{if($3==0){valid=1$$=0}else{$$=$1/$3}}
|'('expr')'{$$=$2}
|''expr{$$=1*$2}
|num{$$=yylval}

%%
intyyerror()
{
printf("\nInvalidexpression!\n")
valid=2
return0
}
intmain()
{
printf("\nEntertheexpressiontobeevaluated\n")
yyparse()
if(valid==1)
{
printf("\nDivisionby0!\n")
}
if(valid==0)
{
printf("\nValidexpression!\n")
printf("Thevalueevaluatedis%d\n",temp)
}
}

5b.Languagea^nb^n(n>=0)
LEXpart
%{
#include"y.tab.h"
%}
%%
areturnA
breturnB
.|[\n]return0
%%
YACCpart
%{
#include<stdio.h>
intvalid=1
%}
%tokenAB
%%
start:AstartB
|

%%
intyyerror()
{
valid=0
printf("\nPatternnotmatched!\n")
return0
}
intmain()
{
printf("\nEnterthepattern")
yyparse()
if(valid)
{
printf("\nValidpattern!\n")

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
}
}

6.Languageoftypea^nb(n>=10)
LEXpart
%{
#include"y.tab.h"
%}
%%
areturnA
breturnB
.|[\n]return0
%%

YACCpart
%{
#include<stdio.h>
intvalid=1
%}
%tokenAB
%%
start:AAAAAAAAAAsB
s:As
|

%%
intyyerror()
{
valid=0
printf("\nPatternnotmatched!\n")
return0
}
intmain()
{
printf("\nEnterthepattern")
yyparse()
if(valid)
{
printf("\nValidpattern!\n")
}
}

PartB(UnixProgramming)

1a.Nonrecursivelyprinttheargumentsinreverseorder
temp=$#
while[$tempge1]
do
evalecho\$$temp
temp=`expr$temp1`
done

1b.Childprocesstoreadcommands
#include<stdio.h>
#include<sys/types.h>
main()
{
intpid
charcmd[15]
pid=fork()
if(pid<0)
{

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
printf(ForkError!\n)
exit(0)
}
elseif(pid==0)
{
printf(\nEntertheCommand:)
scanf(%s,cmd)
system(cmd)
exit(0)
}
}

2a.Filepermissions
temp1=`lsl$1|cutc110`
temp2=`lsl$2|cutc110`
if[$temp1=$temp2]then
echoThefileshavecommonPermissions
echo$temp1
else
echoThepermissionfor$1is:
echo$temp1
echoThepermissionfor$2is:
echo$temp2
fi

2b.Holeinthefile
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
charbuf1[]=abcdefghijklmnop
charbuf2[]=ABCDEFGHIJKLMNOP
intmain()
{
intfd
fd=create(file.dat,0)
write(fd,buf1,16)
lseek(fd,48,SEEK_SET)
write(fd,buf2,16)
system(odcfile.dat)
exit(0)
}

3a.Maximumlength
lslR$1|grepv^d|sortnr+45|head1>file1
#lslR$1|grepv^d|sortnrk5|headn1>file1
string=`cutdf9file1`
length=`cutdf5file1`
echoTheMaximumlengthfile,$stringis:$string:

3b.Typeoffile
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
main(intargc,char*argv[])
{
structstatst
inti
for(i=1i<argci++)
{
if(lstat(argv[i],&st))
{
printf("%sdoesnotexist!",argv[i])
continue
}
printf("%sisa",argv[i])
switch(st.st_mode&S_IFMT)

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
{
caseS_IFDIR:printf("directoryfile\n")
break
caseS_IFCHR:printf("characterdevicefile\n")
break
caseS_IFBLK:printf("blockdevicefile\n")
break
caseS_IFREG:printf("regularfile\n")
break
caseS_IFLNK:printf("symbolliclinkfile\n")
break
caseS_IFIFO:printf("FIFOfile\n")
}
}
}

4a.Bundlescript
echo#tounbundled,shthisfile
fori
do
echoecho$i1>&2
echocat>$i<<`endof$i`
cat$i
echoendof$i
done

4b.Pidsofchildandparent
#include<stdio.h>
#include<sys/types.h>
intmain()
{
intstatus,ppid,mpid,pid
pid=fork()
if(pid<0)
{
printf(Errorinforkingachild!)
}
if(pid==0)
{
ppid=getppid()
printf(\nIamChildExecutingandMyParentIDis:%d,ppid)
mpid=getpid()
printf(\nMyownID:%d,mpid)
kill()
exit(0)
}
pid=waitpid(pid,&status,0)
mpid=getpid()
printf(\nIamparentwithID:%d\nMychildwithID:%d\n,mpid,pid)
}

PartB(CompilerDesign)

5.SDD(method1)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
intrparsecondition(char[],int,char*,int)
voidgen(char[],char[],char[],int)
intmain()
{
intcounter=0,stlen=0,elseflag=0
charstmt[60]
charstrB[54]
charstrS1[50]

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
charstrS2[45]
printf("formatof'if'statement\nexample..\n")
printf("if(a<b)then(s=a)\n")
printf("if(a<b)then(s=a)else(s=b)\n\n")
printf("enterthestatement\n")
scanf("%[^\n]",stmt)
stlen=strlen(stmt)
counter=counter+2
counter=parsecondition(stmt,counter,strB,stlen)
if(stmt[counter]==')')
counter++
counter=counter+3
counter=parsecondition(stmt,counter,strS1,stlen)
if(stmt[counter+1]=='')
{
printf("\nparsingtheinputstatement")
gen(strB,strS1,strS2,elseflag)
return0
}
if(stmt[counter]==')')
counter++
counter=counter+3
counter=parsecondition(stmt,counter,strS2,stlen)
counter=counter+2
if(counter==stlen)
{
elseflag=1
printf("\nparsingtheinputstatement")
gen(strB,strS1,strS2,elseflag)
return0
}
return0
}
intparsecondition(charinput[],intcntr,char*dest,inttotallen)
{
intindex=0,pos=0
while(input[cntr]!='('&&cntr<=totallen)
cntr++
if(cntr>=totallen)
return0
index=cntr
while(input[cntr]!=')')
cntr++
if(cntr>=totallen)
return0
while(index<=cntr)
dest[pos++]=input[index++]
dest[pos]='\0'
returncntr
}
voidgen(charB[],charS1[],charS2[],intelsepart)
{
intBt=101,Bf=102,Sn=103
printf("\n\tif%sgoto%d",B,Bt)
printf("\n\tgoto%d",Bf)
printf("\n%d",Bt)
printf("%s",S1)
if(!elsepart)
printf("\n%d",Bf)
else
{
printf("\n\tgoto%d",Sn)
printf("\n%d:%s",Bf,S2)
printf("\n%d",Sn)
}
}

5.SDD(method2)
#include<stdio.h>
#include<stdlib.h>
exitstatus()
{
printf("\nInvalidstatement!")

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
exit(0)
}
main()
{
chararr[50],arr1[25],arr2[25],marr[25]
intcntr,i
printf("\nEntertheexpression")
scanf("%s",arr)
if(arr[0]=='i'&&arr[1]=='f'&&arr[2]=='(')
{
cntr=3
i=0
while(arr[cntr]!=')')
marr[i++]=arr[cntr++]
cntr++
marr[i]=0
}
else
exitstatus()
if(arr[cntr]=='t'&&arr[cntr+1]=='h'&&arr[cntr+2]=='e'&&arr[cntr+3]=='n'&&arr[cntr+4]=='(')
{
cntr+=5
i=0
while(arr[cntr]!=')')
arr1[i++]=arr[cntr++]
cntr++
arr1[i]=0
if(arr[cntr]==''&&arr[cntr+1]==0)
printf("59:if(%s)goto60\ngoto61\n60:(%s)",marr,arr1)
else
if(arr[cntr]=='e'&&arr[cntr+1]=='l'&&arr[cntr+2]=='s'&&arr[cntr+3]=='e'&&arr[cntr+4]=='(')
{
cntr+=5
i=0
while(arr[cntr]!=')')
arr2[i++]=arr[cntr++]
cntr++
arr2[i]=0
if(arr[cntr]==''&&arr[cntr+1]==0)
printf("59:if(%s)goto60\ngoto62\n60:(%s)goto62\n61:(%s)\n62:",marr,arr1,arr2)
else
exitstatus()
}
else
exitstatus()
}
else
exitstatus()
}

6.Parsetree
%{
#include<ctype.h>
charstr[20]
inti=0
%}
%tokenid
%left'+''/''*'''
%%
E:S{infix_postfix(str)}
S:S'+'T|S''T
|T
T:T'*'F|T'/'F
|F
F:id|'('S')'

%%
#include<stdio.h>
main()
{
printf("\nEnteranidentifier:")
yyparse()
}

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
yyerror()
{
printf("invalid")
}
yylex()
{
charch=''
while(ch!='\n')
{
ch=getchar()
str[i++]=ch
if(isalpha(ch))returnid
if(ch=='+'||ch=='*'||ch==''||ch=='/')returnch
}
str[i]='\0'
return(0)
exit(0)
}
voidpush(charstack[],int*top,charch)
{
stack[++(*top)]=ch
}
charpop(charstack[],int*top)
{
return(stack[(*top)])
}
intprec(charch)
{
switch(ch)
{
case'/':
case'*':return2
case'+':
case'':return1
case'(':return0
default:return1
}
}
voidinfix_postfix(charinfix[])
{
inttop=1,iptr=1,pptr=1
charpostfix[20],stack[20],stksymb,cursymb
push(stack,&top,'\0')
while((cursymb=infix[++iptr])!='\0')
{
switch(cursymb)
{
case'(':push(stack,&top,cursymb)
break
case')':stksymb=pop(stack,&top)
while(stksymb!='(')
{
postfix[++pptr]=stksymb
stksymb=pop(stack,&top)
}
break
case'*':
case'/':
case'+':
case'':while(prec(stack[top])>=prec(cursymb))
postfix[++pptr]=pop(stack,&top)
push(stack,&top,cursymb)
break
default:if(isalnum(cursymb)==0)
{
printf("Errorininput!")
exit(0)
}
postfix[++pptr]=cursymb
}
}
while(top!=1)

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO





66DQG&'ODE3URJUDPV
postfix[++pptr]=pop(stack,&top)
printf("%s\n",postfix)
}

Note:ThisisDFAforprogram1b

Asstate20,21
and22remain
asitissowe
obtainthestate
2regular
expressionas
\/\*[^*]\*
(\*||([^*\]
[^*]*\*))*\/

FREE WEBSITE (HTTP://WWW.WEEBLY.COM/?UTM_SOURCE=INTERNAL&UTM_MEDIUM=FOOTER&UTM_CAMPAIGN=3)

POWERED BY

VTU

KWWSSURJUDPVYWXZHHEO\FRPVVDQGFGODEKWPO



You might also like