CD Manual
CD Manual
NSTI
TUTEOFTECHNOLOGY
Depar
tmentofComput
erSci
ence&Engi
neer
ing
Compi
l
erDesi
gnLabManual
By
,
Suni
lKumarB. L.
Seni
orLecturer
,
Depar
tmentofCSE,
NMAMI T,Nit
te
COMPI
LERDESI
GNLABMANUAL
LEXPROGRAMS:
1.Progr
am t
ocountt
henumberofv
owel
sandconsonant
sinagi
ven
str
ing.
%{
#i
nclude<st
dio.
h>
i
ntvowels=0;
i
ntcons=0;
%}
%%
[aei
ouAEI OU]{vowels++;
}
[a-z
A-Z]{cons++;}
%%
i
nty ywrap()
{
return1;
}
mai n(
)
{
printf
(“Ent
erthestr
ing.
.atendpr
ess^
d\n”
);
yylex()
;
printf
(“Noofv owel
s=%d\nNoof
consonant s=%d\n”
,vowels,
cons)
;
}
2.Progr
am t
ocountt
henumberofchar
act
ers,
wor
ds,
spacesand
l
inesinagi
veni
nputfi
l
e.
%{
#i
ncl
ude<st
dio.
h>
I
ntc=0,
w=0,s=0,l
=0;
%}
WORD [ ^\ t\n,\
.:
]+
EOL[ \n]
BLANK []
%%
{WORD}{ w++; c=c+y yl
eng;}
{BLANK}{ s++; }
{EOL}{l++;}
.{c++;}
%%
i
nty y
wr ap()
{
return1;
}
main(i
ntar gc, char* ar
gv[]
)
{
If(
argc!=2)
{
printf
(“Usage:<./a.out
><sourcefi
le>\
n”)
;
exit(
0);
}
yyin=fopen( argv[
1],
”r”);
yylex();
printf
(“Noofchar acters=%d\nNoofwor ds=%d\nNoof
spaces=%d\ nNoofl ines=%d”,
c,
w,s,
l);
}
3.Pr
ogram t
ocountnoof :
a)+veand–veintegers
b)+veand–vefracti
ons
%{
#i
ncl
ude<stdi
o.h>
i
ntposi
nt=0,negi
nt=0,
posf
ract
ion=0,
negf
ract
ion=0;
%}
%%
[
-]
[0-
9]+{negint
++;}
[
+]?[
0-9]
+{posint++;}
[
+]?[
0-9]
*\.[
0-9]+{posfract
ion++;
}
[
-]
[0-
9]*\.[
0-9]+{negfracti
on++;}
%%
i
ntyywrap()
{
retur
n1;
}
main(
intargc,char* argv[
])
{
If(ar
gc!=2)
{
pri
ntf(
“Usage: <./a.
out><sourcef
il
e>\n”
);
exi
t(0)
;
}
yyin=fopen(argv[1],
”r”
);
yylex();
printf
(“Noof+v eintegers=%d\nNoof–v eint
egers=%d\nNoof
+ve
fracti
ons=%d\ nNoof–v ef r
act
ions=%d\n”
,posi
nt,negi
nt,
posf r
action,negfracti
on) ;
}
4.Program t
ocountthenoofcommentl
ineinagiv
enCpr ogr
am.
Also
el
iminatet
hem andcopythatpr
ogr
am i
ntosepar
atef
il
e
%{
#i
ncl
ude<st
dio.
h>
i
ntcom=0;
%}
%sCOMMENT
%%
“/
*”.
[]
* ”*
/”{com++;}
“/
*”{BEGI NCOMMENT; }
<COMMENT>” */”{BEGIN0;com++;
}
<COMMENT>\ n{ com++;}
<COMMENT>. {
;}
.|
\n{fpri
ntf(
yyout,
”%s”,
yyt
ext)
;
%%
i
ntyywr ap()
{
retur
n1;
}
mai
n(i
ntargc,char* argv[
])
{
I
f(
argc!=2)
{
pri
ntf(
“Usage: <.
/a.out><sour
cefi
l
e><dest
nfi
l
e>\
n”)
;
exi
t(0)
;
}
yy
in=fopen(argv [
1],
”r
”);
yy
out=fopen(ar gv[
2],
”w”);
yy
lex();
pr
intf
(“No.ofcommentl ines=%d\n”
,com);
}
5.Program t
ocountthenoof‘
scanf’
and‘
pri
ntf
’statementsi
naC
program.Replacet
hem wi
th‘
readf
’and‘
wri
tef
’statements
respect
ivel
y.
%{
#i
ncl
ude<st
dio.
h>
i
ntpc=0,
sc=0;
%}
%%
“
pri
ntf”{fpri
ntf
(yy
out,
”writ
ef”
);pc++;
}
“
scanf”{fpri
ntf
(yyout
,”
readf”
);sc++;
}
%%
i
ntyywrap()
{
return1;
}
mai
n(i
ntargc,char* argv[
])
{
i
f(
argc!=2)
{
pri
ntf(
“Usage: <./a.
out><sour
cef
il
e><dest
nfi
le>\
n”)
;
exi
t(0);
}
yy
in=fopen(argv [
1],
”r”
);
yy
out=fopen(ar gv[
2],”
w” )
;
yy
lex();
pr
intf
(“Noofpr i
ntfstatements=%d\nNoofscanf
st
atement s=%d\ n”,pc,sc);
}
6.Pr
ogram tor
ecognizeaval
i
dari
thmet
icexpressi
onandi
dent
if
ythe
i
denti
fi
ersandoperat
orspr
esent
.Pri
ntt
hem separat
ely
.
%{
#incl
ude<stdi
o.h>
#incl
ude<stri
ng.h>
i
ntnoprt=0,nopnd=0,val
id=1,
top=-1,
m,l
=0,
j=0;
charopnd[10][
10],opr
t[
10][
10],
a[100]
;
%}
%%
“
(“{top++; a[
top]=’(‘;}
“
{“{top++; a[
top]=’{‘;}
“
[“{top++; a[
top]=’[‘
; }
“
)”{if(
a[top]!
=’(
‘)
{
vali
d=0;ret urn;
}
el
se
t
op- -
;
}
“
}”{if(
a[top]!
=’{
‘)
{
vali
d=0;ret urn;
}
el
se
t
op- -
;
}
“
]”{if
(a[top]!
=’[
‘
)
{
vali
d=0;ret urn;
}
el
se
t
op- -
;
}
“
+”|”
-“|
”*”|
”/”{ nopr t++;
str
cpy (
oprt
[l
],
yyt
ext
);
l
++;
}
[
0-9]
+|[
a-zA-
Z][
a-zA-Z0-
9_]
*{nopnd++;
str
cpy(
opnd[j
],
yyt
ext
);
j++;
}
%%
i
ntyywrap()
{
retur
n1;
}
mai
n()
{
i
ntk;
pr
intf
(“Entertheexpr ession..atendpr ess^
d\n”
);
yy
lex();
i
f(
vali
d==1&&i ==-1&&( nopnd-noprt)==1)
{
pri
ntf(
“Theexpr essi
oni svali
d\n”);
pri
ntf(
“Theoper atorsare\n”)
;
for
(k=0;k<l;k++)
Printf(“%s\n”,
oprt[
k]);
for
(k=0;k<l;k++)
Printf(“%s\n”,
opnd[k])
;
}
el
se
Pri
ntf(“
Theexpr essionisinval
id”);
}
7.Pr
ogram t
orecogni
zewhet
heragi
vensent
encei
ssi
mpl
eor
compound.
%{
#i
nclude<st
dio.
h>
I
ntis_si
mple=1;
%}
%%
[\t\
n]+[aA]
[nN][
dD][\t\n]
+{is_simple=0;
}
[\t\
n]+[oO][
rR]
[\ t
\n]
+{ i
s_si
mpl e=0;
}
[\t\
n]+[bB]
[uU]
[tT]
[\t\n]+{i
s_simple=0;}
.{
;}
%%
i
ntyywrap()
{
retur
n1;
}
mai
n()
{
i
ntk;
pr
intf
(“Ent
erthesentence.
.atendpress^d”
);
yy
lex()
;
i
f(
is_si
mple==1)
{
Print
f(“
Thegivensentencei
ssimple”
);
}
el
se
{
Print
f(“
Thegivensentencei
scompound”)
;
}
8.Progr
am tor
ecogni
zeandcountt
henumberofi
dent
if
ier
sina
gi
ven
i
nputfi
le.
%{
#i
ncl
ude<st
dio.
h>
i
nti
d=0;
%}
%%
[
a-z
A-Z]
[a-
zA-
Z0-
9_]
*{i
d++;
ECHO;
pri
ntf
(“\
n”)
;}
.
+{;
}
\n{;
}
%%
i
ntyywrap()
{
retur
n1;
}
mai
n(intargc,char*argv[])
{
i
f(ar
gc!=2)
{
pri
ntf(
“Usage: <./a.
out><sourcef
il
e>\
n”)
;
exi
t(0);
}
yyi
n=fopen(argv[1]
,”r”
);
pri
ntf
(“Vali
dident i
fi
resare\n”);
yyl
ex();
pri
ntf
(“Noofi denti
fiers=%d\ n”,
id)
;
}
YACCPROGRAMS:
1.Pr
ogram t
otestt
hev
ali
dit
yofasi
mpl
eexpr
essi
oni
nvol
vi
ng
oper
ators
+,
-,*and/
YaccPar
t
%tokenNUMBERI DNL
%lef
t‘+’‘-
‘
%lef
t‘*’‘
/’
%%
stmt:expNL{pr int
f(“
Vali
dExpressi
on”)
;exi
t(
0);
}
;
exp: exp‘ +’ exp
|exp‘-‘exp
|exp‘*’exp
|exp‘/’exp
|‘
(‘exp‘) ’
|ID
|NUMBER
;
%%
i
ntyyerror(
char* msg)
{
pri
ntf(“
Inv al
idExpressi
on\n”
);
exit
(0);
}
main()
{
pri
ntf(“
Ent ertheexpressi
on\n”
);
yyparse();
}
LexPar
t
%{
#i
ncl
ude“
y.t
ab.
h”
%}
%%
[
0-9]+{returnDIGIT;}
[
a-zA-Z][
a-zA-Z0-
9_]*{ret
urnI
D;}
\n{returnNL; }
.
{ret
urny
ytext
[0]
;}
%%
2.Program t
orecogni
zenest
edI
Fcont
rol
stat
ement
sanddi
spl
ayt
he
l
evelsofnest
ing.
YaccPar
t
%{
#i
ncl
ude“
y.t
ab.
h”
%}
%%
“
if”{returnIF; }
[
sS][0-9]*{
returnS;}
“
<”|”
>”|”==”|
”!=”|
”<=”|
”>=”{ret
urnRELOP;
}
[
0-9]+{r et
urnNUMBER; }
[
a-zA-Z][a-
zA- Z0-9_
]*{ret
urnID;}
\n{; }
.
{ret
urny
ytext
[0]
;}
%%
3.Pr
ogram t
ocheckt
hesy
ntaxofasi
mpl
eexpr
essi
oni
nvol
vi
ng
oper
ators
+,
-,*and/
YaccPar
t
%tokenNUMBERI DNL
%lef
t‘+’‘-
‘
%lef
t‘*’‘
/’
%%
stmt:expNL{pr int
f(“
Vali
dExpressi
on”)
;exi
t(
0);
}
;
exp: exp‘ +’ exp
|exp‘-‘exp
|exp‘*’exp
|exp‘/’exp
|‘
(‘exp‘) ’
|ID
|NUMBER
;
%%
i
ntyyerror(
char* msg)
{
pri
ntf(“
Inv al
idExpressi
on\n”
);
exit
(0);
}
main()
{
pri
ntf(“
Ent ertheexpressi
on\n”
);
yyparse();
}
LexPar
t
%{
#i
ncl
ude“
y.t
ab.
h”
%}
%%
[
0-9]+{returnNUMBER;}
[
a-zA-Z][
a-zA-Z0-
9_]
*{r
etur
nID;
}
\n{returnNL; }
.
{ret
urny
ytext
[0]
;}
%%
4.Program t
orecogni
zeaval
i
dvari
able,
whi chst
art
swi
thal
ett
er,
f
ollowedbyanynumberofl
ett
ersordigi
ts.
YaccPar
t
%tokenDIGITLETTERNLUND
%%
st
mt: var
iabl
eNL{print
f(“
Val
i
dIdent
if
ier
s\n”
);exi
t(
0);
}
;
v
ari
abl
e:LETTERal
phanumer
ic
;
al
phanumer i
c: LETTERal phanumeri
c
|DIGITal phanumer i
c
|UNDal phanumer i
c
|LETTER
|DIGIT
|UND
;
%%
i
ntyyer
ror(char* msg)
{
pri
ntf(“I
nv ali
dExpression\
n”)
;
exit(
0);
}
main()
{
pri
ntf(“Enterthevariabl
ename\n”
);
yyparse();
}
LexPar
t
%{
#i
ncl
ude“
y.t
ab.
h”
%}
%%
[
a-zA-Z]{retur
nLETTER;
}
[
0-9]{returnDIGI
T;}
[
\n]{returnNL;}
[
_]{returnUND; }
.
{ret
urny
ytext
[0]
;}
%%
5.Progr
am t
oev
aluat
eanar
it
hmet
icexpr
essi
oni
nvol
vi
ngoper
ati
ng+,
-,
*and/.
YaccPar
t
%tokenNUMBERI DNL
%lef
t‘+’‘-
‘
%lef
t‘*’‘
/’
%%
stmt:expNL{pr int
f(“
Value=%d\ n”,
$1)
;exit
(0)
;}
;
exp: exp‘ +’exp{$$=$1+$3; }
|exp‘-
‘exp{$$=$1- $3; }
|exp‘*’exp{$$=$1* $3;}
|exp‘/’exp{if(
$3==0)
{
pri
ntf(“
Cannotdi
videby0”);
exit
(0);
}
el
se
$$=$1/ $3;
}
|‘
(‘exp‘)’{$$=$2;}
|ID{$$=$1; }
|NUMBER{$$=$1; }
;
%%
i
ntyyerror(
char* msg)
{
pri
ntf(
“I
nv al
idExpression\n”
);
exit
(0);
}
mai
n()
{
pr
int
f(“
Entert
heexpr
essi
on\
n”)
;
yy
parse()
;
}
LexPar
t
%{
#incl
ude“y
.tab.
h”
exter
ninty
ylval
;
%}
%%
[
0-9]
+{yyl
val
=at
oi(
yyt
ext
);r
etur
nNUMBER;
}
\n{r
etur
nNL;}
.
{ret
urny
ytext
[0]
;}
%%
6.Program tor
ecogni
zest
ri
ngs‘
aaab’
,‘
abbb’
,‘
ab’
and‘
a’usi
ng
grammar
(anbn,
n>=0)
YaccPar
t
%tokenA B NL
%%
st
mt: sNL{pr i
ntf(“
Val
idSt r
ing\n”
);exi
t(
0);
}
;
s:AsB
|
;
%%
i
ntyyerror(
char* msg)
{
pri
ntf(“
Inval
idStr
ing\n”);
exit
(0);
}
main()
{
pri
ntf(“
EntertheStri
ng\n”);
yyparse()
;
}
LexPar
t
%{
#i
ncl
ude“
y.t
ab.
h”
%}
%%
[
aA]{ret
urnA;}
[
bB]{ret
urnB;
}
\n{r
etur
nNL;}
.
{ret
urny
ytext
[0]
;}
%%
7.Pr
ogr
am t
orecogni
zet
hegr anb,
ammar( n>=10)
%tokenA B NL
%%
st
mt: AAAAAAAAAAsBNL
{
Printf
(“Val
i
d”);exi
t(
0);
}
;
s:sA
|
;
i
ntyyerror(
char* msg)
{
pri
ntf(“
Invali
dStri
ng\n”)
;
exit
(0);
}
main()
{
pri
ntf(“
EntertheStri
ng\n”)
;
yyparse();
}
LexPar
t
%{
#i
ncl
ude“
y.t
ab.
h”
%}
%%
[
aA]{r
etur
nA;}
[
bB]{r
etur
nB;
}
\
n{ret
urnNL;
}
.
{ret
urny
ytext
[0]
;}
%%
St
epst
oExecut
eLexPr
ogr
am:
l
ex<pgm name>
cclex.
yy.
c–l
l
./
a.out
St
epst
oexecut
eYACCpr
ogr
am:
Mi
ni-
Clanguagei
sasubsetofCl
anguage,
wit
hli
tt
lev
ari
ati
onsi
nit
ssy
ntax
.
I
nMi
ni-
C,basi
cdat
aty
peav
ail
abl
eisi
nt.Mi
ni-
Cisnotcasesensi
ti
ve.I
nMi
ni-
C
l
anguage,
setofoper
ator
sissubdi
vi
dedi
ntogr
oupsasf
oll
ows:
a.ar
it
hmet
icoper
ator
sofaddi
ti
on,
subt
ract
ion,
mul
ti
pli
cat
ionanddi
vi
sion
b.r
elat
ional
oper
ator
sli
ke==,
!=,
<=,
>=,
<and>
Tr
ansf
erofi
nfor
mat
ionf
rom ort
othei
nputorout
putdev
icesi
scausedbyscan
andpr
intst
atement
swi
thi
nthepr
ogr
am.
Mi
ni-
Ccompi
l
ershoul
dsuppor
tifst
atementandf
orst
atement
.
Feat
uresnoti
ncl
udedar
e:
1.chart
ype,
floatt
ype,
arr
ayt
ypeandanyuserdef
inedt
ypes
2.f
unct
ions
3.whi
l
est
atement
,compoundst
atementet
c..
4.f
il
ehandl
i
ng,
poi
nter
s
5.got
osandhencel
abel
s
Modul
e-1:
Thi
sisamai
nmodul
ewhi
chwi
l
llat
ercal
lot
hermodul
esappr
opr
iat
ely
.
EXEv
ersi
onoft
hismodul
ecanbeusedal
ongwi
thcommandl
i
near
gument
sto
r
unt
hecompl
eteMi
ni-
Ccompi
l
er
Eg:MCOMPI
LE.
EXE TEST.
MC
Her
eTEST.
MCi
sthenameoft
het
esti
nputpr
ogr
am wr
it
teni
nMi
ni-
C.The
modul
eshoul
dcr
eat
eTEST.
ASM f
il
ewhi
chshoul
dcont
aint
heequi
val
ent8086
codef
ort
hei
nputmi
niCpr
ogr
am..
ASM f
il
ehast
obecr
eat
edonl
yift
her
ear
e
noer
ror
sint
hei
nputpr
ogr
am.Thi
smodul
emusthav
efol
l
owi
ngf
unct
ions
1)Funct
ionMYLEX(
)wi
l
lsepar
atet
het
okens.Fort
het
imebei
ngr
est
ri
ct,
t
hisf
unct
iont
osepar
atet
het
okensofonl
yfi
rstl
i
neoft
hepr
ogr
am.The
f
ir
stl
i
neofpr
ogr
am maybe
mai
n();
/*t
hisi
scomment*
/
I
tal
somustdel
etecomment
s.
2)Funct
ionERROR(
)whi
chshoul
ddi
spl
ayer
ror
sli
ke
a)
TEST.
MCf
il
enotf
ound
b)sy
ntaxer
rori
nli
ne‘
n’
c)
semi
col
onexpect
edi
nli
ne‘
n’ et
c..
Si
ncel
i
nenumberal
sohast
obegi
venwi
tht
heer
rormessage,acount
er
f
ort
hel
i
nenumbershoul
dbemai
ntai
ned,
whi
l
ereadi
ngf
rom t
hei
nputf
il
e
3)Funct
ionMAI
N()cal
l
sabov
etwof
unct
ions.I
tshoul
dopent
hei
nputf
il
e
TEST.
MC,scant
hef
ir
stl
i
ne(
iemai
n()
)oft
hei
nputpr
ogr
am andi
fno
er
ror
sar
efoundi
nthatl
i
ne,
shoul
dcr
eat
eout
putf
il
eTEST.
ASM
Modul
e-2:
1)Funct
ion MAI
N()wr
it
ten i
n modul
e 1 mustbe ext
ended t
o handl
e
v
ari
abl
edecl
arat
ionsoft
hei
nputsampl
epr
ogr
am.
2)Funct
ion MYLEX(
)wr
it
ten i
n modul
e 1 mustal
so be ext
ended t
o
separ
atet
okensi
nthev
ari
abl
edecl
arat
ionpar
toft
hei
nputsampl
e
pr
ogr
am
3)A new f
unct
ion VAR_
CHECK(
)mustbe wr
it
ten t
oident
if
ydi
ff
erent
al
l
owedt
ypesofv
ari
abl
es(
Int
hiscasei
tisonl
yint
egert
ype)
.Abi
nar
y
t
reehast
obecr
eat
edf
orst
ori
ngt
hei
nfor
mat
ionofeachandev
ery
v
ari
abl
e.Eachnodeoft
het
reeshoul
dcont
ainv
ari
abl
enameandi
ts
t
ype(
int
hiscasei
tisonl
yint
eger
).Tr
eemustbesor
tedl
exi
cogr
aphi
cal
l
y
basedoni
tsv
ari
abl
enames.Thi
srequi
rest
womor
efunct
ions:onet
o
cr
eat
ethet
reeandanot
hert
osear
chi
nthet
ree.
4)Dat
adef
ini
ti
onsmustbewr
it
tenont
otheout
putf
il
eTEST.
ASM
5)Funct
ionERROR(
)wr
it
teni
nthemodul
e1mustbeext
endedt
oincl
ude
er
rormessagesl
i
ke:
a)redecl
arat
ionofvari
abl
e
b)syntaxerr
orinvar
iabl
edecl
arat
ioni
nli
nen
Sampleinput
:TEST.
MC
main()
{/*thi
siscomment*/
i
nta,
b;
}
Output: TEST.ASM
datasegment
adb?
bdb?
dat asegmentends
codesegment
assumecs: code,ds:
dat
a
start
: movax, dat
a
movds, ax
movah, 4ch
int21h
codeends
endstart
Modul
e-3:
Ar
it
hmet
icExpr
essi
onPar
ser
Thi
smodul
eisf
orpar
singt
hear
it
hmet
icexpr
essi
on.Recur
siv
eDescent
Par
sing(at
ypeoft
opdownpar
sing)i
stobeused.I
nputar
it
hmet
icexpr
essi
on
(
inf
ixnot
ati
on)hast
obescannedf
ort
hesy
mbol
sandt
hesesy
mbol
shav
etobe
pushedont
othest
ack.8086codehast
o begener
atedandst
oredi
ntheout
put
f
il
esi
mul
taneousl
y.Expr
essi
onmayal
sohav
ear
rayel
ement
sasi
tsoper
ands.
1.Funct
ion MYLEX(
)wr
it
ten i
n modul
e1 hast
o beext
ended t
o
separ
atet
okensi
nanar
it
hmet
icexpr
essi
on(t
osi
mpl
i
fywhol
e
t
hing,y
oumayassumet
hati
dent
if
ier
sar
emadeofonl
ysi
ngl
e
al
phabet
s)
2.Anewf
unct
ionEXPRESSI
ON(
)hast
obewr
it
tenwhi
chshoul
dcal
l
anot
herf
unct
ionTERM(
),whi
chi
ntur
nshoul
dcal
lthef
unct
ion
FACTOR(
). FACTOR(
) shoul
d cal
lrecur
siv
ely t
he f
unct
ion
EXPRESSI
ON(
).
Funct
ion FACTOR(
)shoul
d scan t
he i
nputsy
mbol
s oft
he
ar
it
hmet
icexpr
essi
onandpushont
othest
ackt
hei
dent
if
iernameand
i
ts t
ype. Funct
ion TERM(
) shoul
d gener
ate 8086 code f
or
mul
ti
pli
cat
ionanddi
vi
sion.Funct
ionEXPRESSI
ON(
)shoul
dgener
ate
8086codef
oraddi
ti
onandsubt
ract
ion.
[
Not
e:Det
ail
saboutr
ecur
siv
edescentpar
serar
eav
ail
abl
eint
het
ext
book.
St
ruct
ureofcodewi
l
lbeasf
oll
ows:
EXPRESSI
ON()
{
TERM();
EXPRESSI
ON’
()
}
EXPRESSION’()
{if(token==+or- )
MYLEX( );
TERM( )
;
Generatecodeforaddorsubas
POPAX, POPBX; ADDorSUBAX,
BX;
PUSHAX;
EXPRESSION’()
}
TERM()
{FACTOR()
TERM’(
)
}
TERM’(
)
{if(
token==*or/)
MYLEX( )
FACTOR( )
Generatecodef
ormul
anddi
v;
TERM’ ()
}
FACTOR()
{i
f(token==’(‘
)
{
MYLEX( );
EXPRESSION();
I
f(t
oken==‘)’
)
{MYLEX( )
;}
}
El
seif(tokentype==idornum)
{generatecodeforidornum as
MOVAX, token;PUSHAX;
MYLEX();
}
}
3.Funct
ionERROR(
)hast
oext
endedt
oincl
udesomemor
eer
ror
messagesl
i
ke:
a)parenthesi
sexpect
edinl
i
nen
b)vari
ablenotdefi
ned
c)assignexpect
edinli
nenetc…
Sampl
einput:TEST.MC
main()
{inta,
b,d,
e,
f;
i
ntc
c=d+e*((
a-b)
/(e+f
)):
}
Out
put:TEST.ASM
Equival
ent8086assembl
ycode
Modul
e4:
1.Funct
ionSTATEMENT(
)hast
obewr
it
tenwhi
chshoul
dpar
seandgener
ate
8086codef
ort
hef
oll
owi
ngconst
ruct
s
a) If(arelopb)thenassi
gnst
mt1elseassignst
mt2;
(assignstmtscanhaveari
thmeti
cexpressi
onsontheRHS)
b)for( a=1;a<10;
a++)
Not
ethatt
hesecont
rolst
atement
scanber
eal
i
zedbycondi
ti
onal
anduncondi
ti
onalj
umpi
nst
ruct
ionsandcompar
einst
ruct
ionsof8086.
Hencef
unct
ionf
orl
abl
egener
ati
onhast
obewr
it
ten
2.Funct
ionsf
orgener
ati
ng8086codef
orscanandr
eadst
atement
shav
etobe
wr
it
ten.
Eg:
Forr
eadi
ngani
ntegerf
rom key
boar
d,MI
NI-
Cst
atementmaybe
scan(
a);
Equi
valent8086codemaybe:
MOVAH, 01
INT21h
MOVa, AL
Fordi
splay
ingani
nteger
,MI
NI-
Cst
atementmaybe
pr
int(
a)
Equi
val
ent8086codemaybe
MOVDL,a
MOVAH,02
I
NT21h
(i
nfact
,thei
nteger(
2di
git
)hast
obeconv
ert
edt
oASCI
Ibef
oreusi
ng
t
heabov
ecode.Soi
ntegert
oasci
iconv
ersi
oni
sini
ti
all
yrequi
redi
nthe
compi
l
erandt
hent
heasci
ival
uesar
etobeusedast
hesecondpar
amet
er
i
nMOVDL,
a)
3.ERROR()rout
inehastobeext
endedtodispl
ayerr
orsl
i
ke
a)thenexpected
b)parant
hesismissi
nginf
orstat
ement
c)noargumentinscanorpri
ntst
atement et c…
4.Al
lthemodul
eshav
etobel
i
nkedt
ogetacompl
eteMI
NI-
Ccompi
l
er.
SomeOt
hersmal
lCpr
obl
ems:
1.a.Pr
ogr
am i
nCt
oremov
ecomment
sfr
om aCf
il
e
(
bot
hnest
edandnonnest
ed)
b.Pr
ogr
am t
ocheckwhet
hergi
veni
dent
if
ieri
sval
i
dornot
(
aspert
hel
exi
cal
rul
esofi
d)
2.Tor
eadaf
il
econt
aini
ngsy
ntact
ical
l
ycor
rectC pr
ogr
am,anddi
spl
ayt
he
t
okensandt
hei
rcat
egor
iesl
i
kei
d,const
ant
,reser
vedwor
d,r
elat
ionaloper
ator
,
ar
it
hmet
icoper
ator
.
3.Gi
venanar
it
hmet
icexpr
essi
on,usi
ngr
ecur
siv
edescentmet
hod,t
opr
intt
he
or
deri
n whi
ch t
he oper
ati
ons ar
e per
for
med f
orgi
ven pr
ecedence and
associ
ati
vi
tyr
ules.