You are on page 1of 48

NARAYANA ENGINEERING COLLEGE::NELLORE

CSE DEPARTMENT
III-B.TECH II-SEM

LANGUAGE PROCESSORS LAB
NARAYANA ENGINEERING COLLEGE::NELLORE
DEPARTMENT : CSE
LANGUAGE PROCESSORS LAB
III-B.TECH II-SEM (2007-08)
1. Develop a lexical analyzer to recognize a few patterns in PASCL, C and FO!A"
#$%& identifiers, Constants, co''ents, operators, etc(
). *rite a progra' to parse +sing ,r+te force tec-ni.+e of top down parsing
/. Develop on LL#1( parser #constr+ct parse ta,le also(.
0. Develop an operator precedence parsed #constr+ct parse ta,le also(
1. Develop a rec+rsive descent parser
2. *rite a progra' for generating for vario+s inter'ediate code for's
i( !-ree address code ii( Polis- notation
3. *rite a progra' to si'+late -eap storage allocation strategy
4. 5enerate Lexical analyzer +sing L$%.
6.5enerate 7ACC specification for a few syntactic categories.
18.5iven any inter'ediate code for' i'ple'ent code opti'ization tec-ni.+es.
PROGRAM ---1
/
***************************************************************
*******
C-pr!r"# $ %#p&'#'($ L')%*"& A("&+,'r $ r'*!(%,' -'+.r/01 D"$"
$+p'01 %/'($%2%'r01 (3#4'r01 p"r'($5'0%01 0+#4&01 "(/ p'r"$r0....
***************************************************************
*******/
A&!r%$5#: -
1.start.
).9ntialize t-e sy',ol ta,le wit- :eywords.
/.ead to:en ,y to:en fro' t-e inp+t string.
0.;sing finite a+to'ation c-ec: for :eywords, identifiers, constants < t-en
Operators s+ccessively.
1.9f not-ing 'atc-es print an error 'essage.
2.;ntil all to:ens are over, repeat a,ove t-ree steps.
3.Print to:en infor'ation.
4.Stop.
A%#:- C-pr!r"# $ %#p&'#'($ L')%*"& A("&+,'r $ r'*!(%,' -'+.r/01 D"$" $+p'01
%/'($%2%'r01 (3#4'r01 p"r'($5'0%01 0+#4&01 "(/ p'r"$r0....
=incl+de>stdio.-?
=incl+de>conio.-?
=incl+de>ctype.-?
=incl+de>stdli,.-?
=incl+de>conio.-?
void 'ain#(
@
c-ar ipstrA/88B,te'p,t'pA11B,fna'eA18BC
c-ar parnA2BD@E#E,E(E,E@E,EFE,EAE,EBEFC
c-ar sy',A18BD@E.E,E,E,E&E,ECE,E>E,E?E,EGE,EHE,E=EFC
c-ar opsA2BD@EIE,EJE,EDE,EKE,ELE,EMEFC
c-ar :eywdA4BA18BD@N'ainN,NifN,NelseN,Nswitc-N,NvoidN,NdoN,Nw-ileN,NforNFC
c-ar datyA2BA18BD@NintN,Nc-arN,NfloatN,Ndo+,leN,NstringN,NlongintNFC
int posD8,i,O,:,flagi,ipC
F9L$ LfidC
clrscr#(C
67 I(p3$ %0 $"-'( 2r# 2("#' 2%&'76
printf#Nenter filena'e w-ic- is to ,e parsedPnN(C
scanf#NQsN,fna'e(C
fidDfopen#fna'e,NrN(C
w-ile##ipDgetc#fid((RD$OF(
ipstrAposIIBDipC
ipstrAposBDEP8EC
printf#NQsN,ipstr(C
printf#NPtPtlexicanalysisPnSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSPnN(C
for#iD8CipstrAiBRDEP8ECiII(
@
if##ipstrAiBRDE E(<<#ipstrAiBRDEP8E(<<#ipstrAiBRDETE(<<#ipstrAiBRDEPnE((
@
te'pDipstrAiBC
67 C5'*- 2r p"r'($5'0%076
for#OD8CO>2COII(
@
if#te'pDDparnAOB(
printf#NPnQc Pt parant-esisN,te'p(C
F
67C5'*- 2r 0+#4&076
for#OD8CO>18COII(
@
if#te'pDDsy',AOB(
printf#NPnQc Pt sy',olN,te'p(C
F
67 C5'*- 2r p'r"$r076
for#OD8CO>2COII(
@
if#te'pDDopsAOB(
printf#NPnQc Pt operatorN,te'p(C
F
67 C5'*- 2r /%!%$076
if#isdigit#ipstrAiB((
@
:D8C
w-ile##isdigit#ipstrAiB((UUipstrAiBDDE.E(
t'pA:IIBDipstrAiIIBC
t'pA:BDEP8ECiJJC
printf#NPnQs Pt n+',erN,t'p(C
F
67 C5'*- 2r "&p5"4'$0 (-'+.r/01 /"$" $+p'01 %/'($%2%'r0) 76
if#isalp-a#ipstrAiB((
@
:D8C
w-ile#isalp-a#ipstrAiB((
t'pA:IIBDipstrAiIIBC
t'pA:BDEP8ECiJJC
flagiD1C
for#OD8CO>4COII(
@
if##strc'p#t'p,:eywdAOB((DD8(
@ printf#NPn Qs :eywordN,t'p(CflagiD8C,rea:CF
F
for#OD8CO>2COII(
@
if##strc'p#t'p,datyAOB((DD8(
@ printf#NPn Qs Data!ypeN,t'p(CflagiD8C,rea:CF
F
if#flagiDD1(
printf#NPn Qs Pt 9dentifiersN,t'p(C
F67A&p5"4'$0 &p *&0%(!76
F67I2 &p *&0%(!76
F67 M"%( 2r &p *&0%(!76
getc-#(C
F
O3$p3$:-
E($'r 2%&' ("#' $ 4' p"r0'/& /:6p"/#"8"6&p6%p.$)$
9%/ #%(()
:";4-*<
2=>;200?=><@
L')%*"& "("&+0%0
9%/ ------ -'+.r/
#%( ------ I/'($%2%'r0
( ------ P"r'($5'0%0
) ------ P"r'($5'0%0
: ------ p"r'($5'0%0
" ------ I/'($%2%'r0
; ------ Op'r"$r
4 ------ I/'($%2%'r0
- ------ Op'r"$r
* ------ I/'($%2%'r0
< ------ S+#4&
2=> ------ N3#4'r
; ------ p'r"$r
200 ------ (3#4'r
? ------ Op'r"$r
=> ------ (3#4'r
< ------ S+#4&
@ ------ P"r'($5'0%0
PROGRAM --- 2
/
***************************************************************
*******
Ar%$' " C-pr!r"# $ /'&'$' *##'($ &%('0 2r# " 2%&' "(/ pr%($ .%$5 &%('
(3#4'r0
***************************************************************
*******/
A&!r%$5#: -
Start
ead file na'e
Copy entire file content in to rr.txt file ,y re'oving co''ent
lines
ead t-e contents of file rr.txt and print t-e'.
9f new line co'es t-en first print t-e line n+',er.
Stop.
A%#:- Ar%$' " C-pr!r"# $ /'&'$' *##'($ &%('0 2r# " 2%&' "(/ pr%($ .%$5
&%(' (3#4'r0
=incl+de>stdio.-?
=incl+de>conio.-?
'ain#(
@
int lnSnoD1C
c-ar fna'eA)8B,c-1,c-),c-C
F9L$ Lfp1,Lfp),LfpC
clrscr#(C
printf#N$nter filena'e wit- pat-N(C
gets#fna'e(C
fpDfopen#fna'e,NrN(C
fp1Dfopen#Nrr.txtN,NwN(C
w-ile##c-1Dfgetc#fp((RD$OF(
@
if#c-1DDEKE(
@
if##c-)Dfgetc#fp((DDELE(
@
w-ile##c-1RDELE(<<#c-)Dfgetc#fp((RDEKE(
@c-1Dc-)Cc-)Dfgetc#fp(CF
F
else
@
fp+tc#c-1,fp1(C
fp+tc#c-),fp1(C
F
F
else
fp+tc#c-1,fp1(C
F
fclose#fp(C
fclose#fp1(C
getc-#(C
67 R'"/%(! /"$" 2r# 2%&' rr.$)$ .5%*5 *($"%(0 ))).$)$ 2%&' /"$"
4+ r'#9%(! $5' *##'($ &%('0
r'"/ *($'($0 2r# 2%&' rr.$)$ "(/ pr%($ $5'# .%$5 &%(' (3#4'r076
fp)Dfopen#Nrr.txtN,NrN(C
printf#NQdN,lnSno(C
w-ile##c-Dfgetc#fp)((RD$OF(
@
if#c-DDEPnE(
printf#NPnQd N,IIlnSno(C
else
printf#NQcN,c-(C
F
fclose#fp)(C
getc-#(C
F
O3$p3$:-
$nter filena'e wit- pat- /:6p"/#"8"6&p6%p.$)$
1Kprogra' for c-ec:ing co''ent linesK
2
= void 'in#(
> @ aD,JcC
B
C )/0D)88I/0C
7 F
PROGRAM ---=
/
***************************************************************
*******
C-pr!r"# $ !'('r"$' %($'r#'/%"$' */'0
") P&%05 ($"$%(
4) R'9'r0' p&%05 ($"$%(
*) T5r'' "//r'00 */' !'('r"$%(
***************************************************************
*******/
Pr!r"# = ") T *(9'r$ " %(2%) ')pr'00%( %($ r'9'r0' p&%05 (p0$2%))
')pr'00%(.
ALGORITHM:-
1. Start
). ead t-e infix expression fro' :ey,oard.
/. Ass+'e t-at t-e infix expression yo+ -ave read is stored in t-e c-aracter
Array infix.
0. P+s- = in to stac:.
1. For eac- c-aracter in infix array perfor' t-e following actions
Vegin

if c-aracter is /%!%$ r "&p5"4'$ t-en enter t-e sa'e in to postfix
array#res+ltant array( .
if c-aracter is p'( 4r"*'0 t-en , p+s- t-at into t-e stac:
if c-aracter is *&0'/ 4r"*'0 t-en, pop t-e stac: top ele'ents
+ntil top of t-e stac: contains open ,races.
if c-aracter contains any "r%$5#'$%* p'r"$r0,
Vegin
if top of stac: ele'ent is e.+al to = t-en, p+s- t-e
operator into stac:.
$lse if t-e top of stac: ele'ent priority is less t-an t-e
priority of t-e new c-aracter t-en p+s- t-e c-aracter in to
stac:.
$lse pop t-e ele'ents fro' stac: +ntil t-e priority of t-e top
of t-e stac: ele'ent is less t-an t-e c-aracter priority t-at is
to ,e inserted.
$nd
$nd
2. 9f no 'ore c-aracters are presented in infix expression t-en pop ele'ents fro'
Stac: +ntil top of stac: ele'ent is = and p+s- t-at ele'ent in to postfix array.

P305 D3(*$%(:
1. Start
). 9f top of t-e stac: is reac-ed 'ax size of t-e stac: t-en, t-e stac: is f+ll.
/.$lse
9nsert t-e ele'ent into stac:
0. $nd
Pp D3(*$%(:
1. start
). if top of t-e stac: is n+ll t-en, t-e stac: is e'pty
/.else
Delete t-e ele'ent fro' t-e stac: positioned ,y t-e top
0. $nd
A%#:- G'('r"$%(! %($'r#'/%"$' */' %( r'9'r0' p&%05 ($"$%(.
= incl+de>stdio.-?
=incl+de>conio.-?
=incl+de>ctype.-?
=incl+de>'at-.-?
int topDJ1C
c-ar stac:A)8BC
'ain#(
@
c-ar infixA)8BC
clrscr#(C
printf#Nenter t-e infix expressionN(C
67 R'"/ %(2%) ')pr'00%( 76
gets#infix(C
67 C(9'r$ %(2%) $ r'9'r0' p&%05 ($"$%( 76
intopo#infix(C
getc-#(C
F
intopo#c-ar infixA)8B(
@
c-ar postfixA)8B,s,tC
int indexD8,posD8,lC
lDstrlen#infix(C 67 L 0$r%(! &'(!$576
p+s-#E=E(C
67 R'"/ (' 4+ (' *5"r"*$'r 76
w-ile#index>l(
@
sDinfixAindexBC
switc-#s(
@
case E#E& p+s-#s(C
,rea:C
case E(E&tDpop#(C
w-ile#tRDE#E(
@
postfixAposIIBDtC
tDpop#(C
F
,rea:C
caseEIE&
caseEJE&
caseELE&
caseEKE&
caseEME&w-ile#pre#stac:AtopB(?Dpre#s((
@
tDpop#(C
postfixAposIIBDtC
F
p+s-#s(C
,rea:C
defa+lt& postfixAposIIBDsC
,rea:C
F
indexIIC
F
w-ile#top?8(
@
tDpop#(C
postfixAposIIBDtC
F
postfixAposIIBDEP8EC
p+ts#postfix(C
F
p+s-#c-ar c(
@
topIIC
stac:AtopBDcC
F
pop#(
@
c-ar cC
cDstac:AtopBC
topJJC
ret+rn#c(C
F
pre#c-ar c (
@
if#cDDEME(
ret+rn#1(C
else if#cDDEKEUU cDDELE(
ret+rn #0(C
else if# cDDEIEUU cDD EJE(
ret+rn#/(C
else
ret+rn#)(C
F
OUTPUT :-
1( $nter t-e infix expression "?47*6(/-'E2)
!-e e.+ivalent reverse polis- notation isW. "4*7/'2E-6?
)( $nter t-e infix expression "74E*
!-e e.+ivalent reverse polis- notation isW. "4*E7
Pr!r"# = 4) T *(9'r$ " %(2%) ')pr'00%( %($ p&%05 (pr'2%)) ')pr'00%(.
ALGORITHM:-
1. Start
). ead t-e infix expression fro' :ey,oard.
/. Ass+'e t-at t-e infix expression yo+ -ave read is stored in t-e c-aracter
Array infix.
2. P+s- = in to stac:.
3. For eac- c-aracter in infix array perfor' t-e following actions read
c-aracters fro' last
Vegin

if c-aracter is /%!%$ r "&p5"4'$ t-en enter t-e sa'e in to postfix
array#res+ltant array( .
if c-aracter is *&0'/ 4r"*'0 t-en , p+s- t-at into t-e stac:
if c-aracter is p'( 4r"*'0 t-en, pop t-e stac: top ele'ents
+ntil top of t-e stac: contains open ,races.
if c-aracter contains any "r%$5#'$%* p'r"$r0,
Vegin
if top of stac: ele'ent is e.+al to = t-en, p+s- t-e
operator into stac:.
$lse if t-e top of stac: ele'ent priority is greater t-an or
e.+al to t-e priority of t-e new c-aracter t-en p+s- t-e
c-aracter in to stac:.
$lse pop t-e ele'ents fro' stac: +ntil t-e priority of t-e top
of t-e stac: ele'ent is less t-an t-e c-aracter priority t-at is
to ,e inserted.
$nd
$nd
2. 9f no 'ore c-aracters are presented in infix expression t-en pop ele'ents fro'
Stac: +ntil top of stac: ele'ent is = and p+s- t-at ele'ent in to postfix array.
3. Finally reverse t-e res+ltant array t-en t-at is t-e polis- notation for t-e infix
expression.
P305 D3(*$%(:
1. Start
). 9f top of t-e stac: is reac-ed 'ax size of t-e stac: t-en, t-e stac: is f+ll.
/.$lse
9nsert t-e ele'ent into stac:
0. $nd
Pp D3(*$%(:
1. start
). if top of t-e stac: is n+ll t-en, t-e stac: is e'pty
/.else
Delete t-e ele'ent fro' t-e stac: positioned ,y t-e top
0. $nd
Pr!r"# = 4) T *(9'r$ " %(2%) ')pr'00%( %($ p&%05 (pr'2%)) ')pr'00%(.
67 INDIF TO PREDIF 76
= incl+de>stdio.-?
=incl+de>conio.-?
=incl+de>ctype.-?
=incl+de>'at-.-?
int topDJ1C
c-ar stac:A)8BC
'ain#(
@
c-ar infixA)8BC
clrscr#(C
printf#Nenter t-e infix expressionN(C
67 R'"/ %(2%) ')pr'00%( 76
gets#infix(C
67 C(9'r$ %(2%) $ p&%05 ($"$%( 76
intopo#infix(C
getc-#(C
F
intopo#c-ar infixA)8B(
@
c-ar prefixA)8B,s,tC
int index,posD8,l,i,lenC
lDstrlen#infix(C
indexDlJ1C
p+s-#E=E(C
w-ile#index?D8(
@
sDinfixAindexBC
switc-#s(
@
case E(E& p+s-#s(C
,rea:C
case E#E&tDpop#(C
w-ile#tRDE(E(
@
prefixAposIIBDtC
tDpop#(C
F
,rea:C
caseEIE&
caseEJE&
caseELE&
caseEKE&
caseEME&w-ile#pre#stac:AtopB(>pre#s((
@
tDpop#(C
prefixAposIIBDtC
F
p+s-#s(C
,rea:C
defa+lt& prefixAposIIBDsC
,rea:C
F
indexJJC
F
w-ile#top?8(
@
tDpop#(C
prefixAposIIBDtC
F
prefixAposIIBDEP8EC
lenDstrlen#prefix(J1C
for#iDlenCi?D8CiJJ(
printf#NQcN,prefixAiB(C
F
p+s-#c-ar c(
@
topIIC
stac:AtopBDcC
F
pop#(
@
c-ar cC
cDstac:AtopBC
topJJC
ret+rn#c(C
F
pre#c-ar c (
@
if#cDDEME(
ret+rn#1(C
else if#cDDEKEUU cDDELE(
ret+rn #0(C
else if# cDDEIEUU cDD EJE(
ret+rn#/(C
else
ret+rn#2(C
F
OUTPUT :-
1( $nter t-e infix expression "?47*6(/E'?2)
!-e e.+ivalent reverse polis- notation isW. 67?"4*E/?'2
)( $nter t-e infix expression "74E*
!-e e.+ivalent reverse polis- notation isW.E7"4*
Pr!r"# = *) T !'('r"$' T5r'' A//r'00 */'.
ALGORITHM:-
1. Start
). ead t-e infix expression fro' :ey,oard.
/. Ass+'e t-at t-e inp+t expression yo+ -ave read is stored in t-e c-aracter
Array infix.
4. P+s- = in to stac:.
6. For eac- c-aracter in infix array perfor' t-e following actions
Vegin
if c-aracter is /%!%$ r "&p5"4'$ t-en enter t-e sa'e in to postfix
array#res+ltant array( .
if c-aracter is p'( 4r"*'0 t-en , p+s- t-at into t-e stac:
if c-aracter is *&0'/ 4r"*'0 t-en, pop t-e stac: top ele'ents
+ntil top of t-e stac: contains open ,races.
if c-aracter contains any "r%$5#'$%* p'r"$r0,
Vegin
if top of stac: ele'ent is e.+al to = t-en, p+s- t-e
operator into stac:.
$lse if t-e top of stac: ele'ent priority is less t-an t-e
priority of t-e new c-aracter t-en p+s- t-e c-aracter in to
stac:.
$lse pop t-e ele'ents fro' stac: +ntil t-e priority of t-e top
of t-e stac: ele'ent is less t-an t-e c-aracter priority t-at is
to ,e inserted.
$nd
$nd
2. 9f no 'ore c-aracters are presented in infix expression t-en pop ele'ents fro'
Stac: +ntil top of stac: ele'ent is = and p+s- t-at ele'ent in to postfix array.
3. For eac- c-aracter in postfix array do t-e following operation.
Vegin
9f t-e c-aracter is alp-a,et p+s- c-aracter in to stac:.
9f t-e c-aracter is operator t-en pop two c-aracters fro' stac: and
store t-ose in op1 and op) varia,les.
,egin
Print t-ose in t-e for'at of !iDop1 c-aracter op)
9ncre'ent 9 for generating te'porary varia,les.
P+s- t-e new te'porary varia,le !i in to stac:.
$nd
$nd.
P305 D3(*$%(:
1. Start
). 9f top of t-e stac: is reac-ed 'ax size of t-e stac: t-en, t-e stac: is f+ll.
/.$lse
9nsert t-e ele'ent into stac:
0. $nd
Pp D3(*$%(:
1. start
). if top of t-e stac: is n+ll t-en, t-e stac: is e'pty
/.else
Delete t-e ele'ent fro' t-e stac: positioned ,y t-e top
0. $nd
A%#:- G'('r"$%(! %($'r#'/%"$' */' %( r'9'r0' p&%05 ($"$%(.
67 INDIF TO POSTDIF 76
= incl+de>stdio.-?
=incl+de>conio.-?
=incl+de>ctype.-?
=incl+de>'at-.-?
int topDJ1C
c-ar stac:A)8B,postfixA)8B,infixA)8B,ipstA)8BC
'ain#(
@
int iC
clrscr#(C
printf#N$nter infix expressionPn in t-e for' of assign'ent st't.li:e 9"r;')p...N(C
gets#ipst(C
for#iD)Ci>strlen#ipst(CiII(
infixAiJ)BDipstAiBC
infixAiJ)BDEP8EC
p+ts#infix(C
intopo#(C
getc-#(C
eval+a#(C
getc-#(C
F
intopo#(
@
c-ar s,tC
int indexD8,posD8,lC
lDstrlen#infix(C
p+s-#E=E(C
w-ile#index>l(
@
sDinfixAindexBC
switc-#s(
@
case E#E& p+s-#s(C
,rea:C
case E(E&tDpop#(C
w-ile#tRDE#E(
@
postfixAposIIBDtC
tDpop#(C
F
,rea:C
caseEIE&
caseEJE&
caseELE&
caseEKE&
caseEME&w-ile#pre#stac:AtopB(?Dpre#s((
@
tDpop#(C
postfixAposIIBDtC
F
p+s-#s(C
,rea:C
defa+lt& postfixAposIIBDsC
,rea:C
F
indexIIC
F
w-ile#top?8(
@
tDpop#(C
postfixAposIIBDtC
F
postfixAposIIBDEP8EC
F
p+s-#c-ar c(
@
topIIC
stac:AtopBDcC
F
pop#(
@
c-ar cC
cDstac:AtopBC
topJJC
ret+rn#c(C
F
pre#c-ar c (
@
if#cDDEME(
ret+rn#1(C
else if#cDDEKEUU cDDELE(
ret+rn #0(C
else if# cDDEIEUU cDD EJE(
ret+rn#/(C
else
ret+rn#)(C
F
eval+a#(
@
c-ar resA)8B,c-,c-1,c-)C
int i,tD1C
for#iD8Ci>strlen#postfix(CiII(
@
c-DpostfixAiBC
if#isaln+'#c-((
p+s-#c-(C
else
@
c-1Dpop#(C
c-)Dpop#(C
printf#NPn !QdDN,t(C
if#c-)DDE1EUUc-)DDE)EUUc-)DDE/EUUc-)DDE0EUUc-DDX1XUUc-DDX2XUU
c-DX3XUUc-DX4XUUc-DX6X(
printf#N!QcN,c-)(C
else
printf#NQcN,c-)(C
printf#NQcN,c-(C
if#c-1DDE1EUUc-1DDE)EUUc-1DDE/EUUc-1DDE0EUUc-DDX1XUUc-DDX2XUU
c-DX3XUUc-DX4XUUc-DX6X(
printf#N!QcN,c-1(C
else
printf#NQcN,c-1(C
switc-#t(
@
case 1&c-DE1EC
,rea:C
case )&c-DE)EC
,rea:C
case /&c-DE/EC
,rea:C
case 0&c-DE0EC
,rea:C
case 1&c-DE1EC
,rea:C
case 2&c-DE2EC
,rea:C
case 3&c-DE3EC
,rea:C
case 4&c-DE4EC
,rea:C
case 6&c-DE6EC
,rea:C
F
p+s-#c-(C
tIIC
F
FKL for loop closingLK
c-Dpop#(C
printf#NPn QcD!QcN,ipstA8B,c-(C
F
OUTPUT :-
1) $nter t-e infix expression
9n t-e for' of assign'ent state'ent li:e varDexp...R;("?47*)-(/E'?2)
T1;47*
T2;"?T1
T=;/E'
T>;T=?2
TB;T2-T>
R;TB
2) E($'r $5' %(2%) ')pr'00%(
%( $5' 2r# 2 "00%!(#'($ 0$"$'#'($ &%-' 9"r;')p...R;("E47*)-(/-')
T1;"E4
T2;T17*
T=;/-'
T>;T2-T=
R;T>
PROGRAM --- >
/
***************************************************************
*******
Ar%$' " C-pr!r"# $ %#p&'#'($ BRUTE DORCE M'$5/ 2 p"r0'0(.%$5
4"*-$r"*-%(!)
*****************************************************************
*****/
ALGORITHM:-
Ar%$5#'$%*:

proced+re S#(
,egin
if inp+t sy',ol DEaE t-en
,egin

advance t-e inp+t pointerC
if V#( t-en
,egin
if inp+t sy',ol D EcE
,egin
advance t-e inp+t pointerC
if inp+t pointer c-aracter is end of string t-en
ret+rn tr+eC
else
ret+rn falseC
endC

else
ret+rn falseC
endC
else ret+rn false
endC
endC

proced+re a#(
,egin
,inpt &Dinp+tpointerC
if inp+tsy',ol DEcE t-en
,egin
advance t-e inp+t pointerC
,inpt1&Dinp+tpointerC
if inp+tsy',olDEdE t-en
,egin
advance t-e inp+t pointerC
ret+rn tr+eC
endC
else
,egin
inp+tpointer&D,inpt1C
if inp+tsy',olDEeE t-en
,egin
advance t-e inp+t pointerC
ret+rn tr+eC
endC
else ret+rn falseC
endC
9np+t pointer &D,inptC
if inp+tsy',ol DX,E t-en
,egin
advance t-e inp+t pointerC
ret+rn tr+eC
endC
else
ret+rn falseC
endC


,egin KL'ain proced+re LK

read t-e ter'inal string
if S#( t-en
print Nstring in passedN
else
print Ninvalid stringN
endC
67 Pr!r"# $ /'9'&p r'*3r0%9' /'*'($ p"r0%(! .%$5 4"*-$r"*-%(!76
=incl+de>stdio.-?
=incl+de>conio.-?
c-ar LinptC 67 p%($'r $ $5' %(p3$76
'ain#(
@
c-ar ipstA)8BC
int lenC
clrscr#(C
printf#N!-e prod+ctions are.....Pn sJ?aVc Pn VJ?,KcdKcePnN(C
printf#N$nter t-e string to ,e parsed Pt N(C
gets#ipst(C
inptDipstC
if#S#((
printf#NLLLLString is ParsedLLLPnN(C
else
printf#NLLLL9nvalid StringLLLLLLPnN(C
getc-#(C
F 67 *&0%(! 2 #"%(76
S#(
@
if#LinptDDEaE(
@
inptIIC
if#V#((
@
if#LinptDDEcE(
@
inptIIC
if#LinptDDEP8E(
ret+rn#1(C
else
ret+rn#8(C
F
else
ret+rn#8(C
F
else
ret+rn#8(C
F
F67 *&0%(! 2 0 pr*'/3r'76
V#(
@
c-ar L,inpt,L,inpt1C
,inptDinptC
if#LinptDDEcE(
@
inptIIC
,inpt1DinptC
if#LinptDDEdE(
@
inptIIC
ret+rn#1(C
F
else
@
inptD,inpt1C
if#LinptDDEeE(
@
inptIIC
ret+rn#1(C
F
else
ret+rn#8(C
F
F
else
@
inptD,inptC
if#LinptDDE,E(
@
inptIIC
ret+rn#1(C
F
else
ret+rn#8(C
F
F67 C&0%(! 2 B pr*'/3r' 76
OUTPUT :-
1 ) !-e prod+ctions are.....
0-G"B*
B-G46*/6*'
$nter t-e string to ,e parsed "4*
77770$r%(! %0 p"r0'/777
2 ) !-e prod+ctions are.....
0-G"B*
B-G46*/6*'
$nter t-e string to ,e parsed "*/*
77770$r%(! %0 p"r0'/777
= ) !-e prod+ctions are.....
0-G"B*
B-G46*/6*'
$nter t-e string to ,e parsed "*'*
77770$r%(! %0 p"r0'/777
> ) !-e prod+ctions are.....
0-G"B*
B-G46*/6*'
$nter t-e string to ,e parsed "*'*/
7777I(9"%/ 0$r%(! 777
PROGRAM --- B
/
***************************************************************
*******
Ar%$' " C-pr!r"# $ %#p&'#'($ R'*3r0%9' D'*'($ p"r0%(! .%$5 3$
4"*-$r"*-%(!)
*****************************************************************
*****/
ALGORITHM:-
proced+re e#(

,egin
!#(C
$P9Y$#(C

end
proced+re $P9Y$#(
,egin

if inp+tsy',olDEIE t-en
,egin
advance t-e inp+t pointerC
!#(C
$P9Y$#(C
endC
endC
proced+re !#(

,egin
F#(C
!P9Y$#(C
endC
proced+re !#(

,egin
F#(C
!P9Y$#(C
endC

proced+re !P9Y$#(

,egin
if inp+tsy',olDELE t-en
,egin
advance t-e inp+t pointerC
F#(C
!P9Y$#(C
endC
endC

proced+re f#(
,egin
if inp+tsy',ol D EidE t-en
advance t-e inp+t pointerC
else if inp+tsy',olDElE t-en

,egin
advance t-e inp+t pointerC
$#(C
if inp+tsy',ol DE(E t-en
advance t-e inp+t pointerC
else error#(C
endC
else error#(C
endC

,egin KL'ain progra' LK
read any ter'inal string
$#(C
endC

67 Pr!r"# $ /'9'&p r'*3r0%9' /'*'($ p"r0%(! .%$5 4"*-$r"*-%(!76

=incl+de>stdio.-?
c-ar LaC

'ain#(
@
c-ar sA18BC
clrscr#(C
printf#N$"!$ !Z$ S!9"5N(C
gets#s(C
aDsC
if#$#((

printf#NS!9"5 9S PAS$DN(C
else
printf#invalid stringN(C
getc-#(C
F

int $#(
@
!#(C
$P9Y$#(C
F
int $P9Y$#(
@
if#LaDDEIEE (
@
aIICC
!#(C
$P9Y$#(C
F
F
int !#(
@
F#(C
!P9Y$#(C
F
int !P9Y$#(
@
if#LaDDELE(
@
aIICC
F#(C
!P9Y$#(C
F
F
int F#(
@
if#LaDDEiE(
aII C
else if#LaDDE(E(
@
aIICC
$#(C
if#LaDDE(E(
aII C
else
error#(C
F
else
error#(C
F
int error#(
@
ret+rn#8(C
F
inp+t&J$"!$ !Z$ S!9"5 &#iIi(
o+tp+t&JS!9"5 9S PAS$D

PROGRAM ---C
/
***************************************************************
*******
C-pr!r"# $ %#p&'#'($ H'"p 0$r"!' A&&*"$%(H
***************************************************************
*******/
ALGORITHM:-
[!o i'ple'ent dyna'ic 'e'ory allocation and deal location we are
9'ple'enting -eap storage allocation\
1. Start
). Ass+'e 'e'ory size as 18 ,ytes
/. Allocation&JJJ
ead varia,le na'e and size.
9f contin+o+s availa,le free space is greater t-an or e.+al to varia,le
size t-en allocate. And 'a:e t-ose ,its in allocation array as 1.
9f space not s+fficient t-en allocation is not possi,le.
0. Deal location&JJJ
ead varia,le na'e
Co'pare varia,le na'e wit- varia,le na'e presented in -eap
allocated array. 9f 'atc- is fo+nd free t-e 'e'ory space allocated for
t-at varia,le.
Ya:e valid ,it as zero in -eap array for t-at varia,le.
Allocation ,its in array 'a:e as 8 for t-at partic+lar varia,le 'e'ory.
1. Deal located 'e'ory space will ,e +tilized w-en new varia,le re.+ires
Ye'oryW.
6. Stop.
A%#:- [!o i'ple'ent dyna'ic 'e'ory allocation and deal location we are
9'ple'enting -eap storage allocation\
=incl+de>stdio.-?
=incl+de>conio.-?
=incl+de>string.-?
str+ct -eaps
@
c-ar varA18BC
int noS,yteC
int stS,yteC
int validC
F-eaparrA)8BC
int allocA18B,-arposD8C
void 'ain#(
@
int c-,iC
67 %2 #'#r+ "&&*"$'/ $5'( "&&*Ip0J;1 '&0' 076
for#iD8Ci>18CiII(
allocAiBD8C
w-ile#1(
@
clrscr#(C
printf#NPn enter + r c-oicePnN(C
scanf#NQdN,<c-(C
switc-#c-(
@
case 1&allocat#(C
,rea:C
case )&deallocat#(C
,rea:C
case /& disp#(C
,rea:C
case 0& exit#8(C
getc-#(C
F67C&0%(! 2 0.%$*576
F67 C&0%(! 2 .5%&'76
F67 C&0%(! 2 #"%(76
allocat#(
@
c-ar vna'eA18B,sC
int vsize,i,co+nt,st,:C
printf#NPn enter varia,le na'e&N(C
scanf#NQsN,<vna'e(C
printf#NPn enter -ow 'any ,ytes re.+ired for QsN,vna'e(C
scanf#NQdN,<vsize(C
for#iD8Ci>18CiII(
@ co+ntD8C
w-ile##allocAiBDD1(<<#i>18((
iIIC
stDiC
w-ile#co+nt>vsize(
@
if#allocAiBDD8(
co+ntIIC
else
,rea:C
iIIC
F
if#co+ntDDvsize(
@
strcpy#-eaparrA-arposB.var,vna'e(C
-eaparrA-arposB.validD1C
-eaparrA-arposB.noS,yteDvsizeC
-eaparrA-arposB.stS,yteDstC
for#:DstC:>stIvsizeC:II(
allocA:BD1C
-arposIIC
,rea:C
F
F67C&0%(! 2 2r &p76
if#i?18(
printf#NPn Allocation is not possi,lePnN(C
else
printf#NPn Allocated s+ccesf+llyPnN(C
F67C&0%(! 2 "&&*"$%(76
disp#(
@
int iC
for#iD8Ci>-arposCiII(
@
if#-eaparrAiB.validDD1(
@
printf#NPn "a'e of varia,le QsN,-eaparrAiB.var(C
printf#NPn starting ,yte QdN,-eaparrAiB.stS,yte(C
printf#NPn n+',er of ,ytesQdN,-eaparrAiB.noS,yte(C
F
F
F67 C&0%(! 2 /%0p&"+76
deallocat#(
@
int iC
c-ar vna'A18BC
int staS,yte,lenS,yte,endS,yteC
printf#Nvaria,les presented are...PtN(C
for#iD8Ci>-arposCiII(
printf#NQsN,-eaparrAiB.var(C
printf#N$"!$ ]A9AVL$ "AY$ *Z9CZ 9S !O V$
D$ALLOCA!$DPtN(C
scanf#NQsN,vna'(C
for#iD8Ci>-arposCiII(
@
if#-eaparrAiB.validDD1(
@
if#strc'p#vna',-eaparrAiB.var(DD8(
,rea:C
F
F
if#i>-arpos(
@
-eaparrAiB.validD8C
staS,yteD-eaparrAiB.stS,yteC
lenS,yteD-eaparrAiB.noS,yteC
endS,yteDstaS,yteIlenS,yteC
for#iDstaS,yteCi>endS,yteCiII(
allocAiBD8C
F
F67 C&0%(! 2 /'"& &*"$%(76
OUTPUT :-
enter + r c-oice 1
enter varia,le na'e& a
enter -ow 'any ,ytes re.+ired for a 18
A&&*"$'/ 03**'0023&&+
$nter + r c-oice 1
$nter varia,le na'e& ,
$nter -ow 'any ,ytes re.+ired for , )8
A&&*"$'/ 03**'0023&&+
$nter + r c-oice 1
$nter varia,le na'e&c
$nter -ow 'any ,ytes re.+ired for c /8
A&&*"$%( %0 ($ p00%4&'
$nter + r c-oice /
"a'e of varia,le a
starting ,yte 8
n+',er of ,ytes18
,ytes allocated are 8..6
"a'e of varia,le ,
starting ,yte 18
n+',er of ,ytes)8
,ytes allocated are 18..)6
$nter + r c-oice 1
$nter varia,le na'e&c
$nter -ow 'any ,ytes re.+ired for c 18
A&&*"$'/ 03**'0023&&+
$nter + r c-oice /
"a'e of varia,le a
Starting ,yte 8
"+',er of ,ytes18
Vytes allocated are 8..6
"a'e of varia,le ,
Starting ,yte 18
"+',er of ,ytes)8
Vytes allocated are 18..)6
"a'e of varia,le c
Starting ,yte /8
"+',er of ,ytes18
Vytes allocated are /8../6
$nter + r c-oice )
varia,les presented are... " 4 *
$"!$ ]A9AVL$ "AY$, *Z9CZ 9S !O V$ D$ALLOCA!$D 4
D'"& &*"$'/ 03**'0023&&+
$nter + r c-oice /
"a'e of varia,le a
Starting ,yte 8
"+',er of ,ytes18
Vytes allocated are 8..6
"a'e of varia,le c
Starting ,yte /8
"+',er of ,ytes18
Vytes allocated are /8../6

$nter + r c-oice 0.
#include<stdio.h>
#include<ctype.h>
int top=-1;
char stack[20]inpst[20];
!ain"#
$
clrscr"#;
print%"&enter the strin' that is to (e parsed...)t&#;
'ets"inpst#;
strcat"inpst&*&#;
+,puts"inpst#;,+
constructop"#;
opprec"#;
'etch"#;
-
constructop"#
$
int leni.=0oplen;
char op[20]prect[20][20];
len=strlen"inpst#;
%or"i=0;i<len;i//#
$
i%"0present"inpst[i]op##
op[.//]=inpst[i];
-
op[.]=1)01;
print%"&234 56478257 67494:8;94 687S47 <S.....)n)t&#;
%or"i=0;i<strlen"op#;i//#
print%"&=c)t&op[i]#;
oplen=strlen"op#-1;
%or"i=0;i<=oplen;i//#
$
print%"&)n=c&op[i]#;
%or".=0;.<=oplen;.//#
$
i%"""i==0#>>".==0##??""i==oplen#>>".==oplen###
prect[i][.]=1 1;
else i%"prec"op[i]#>prec"op[.]##
prect[i][.]=1>1;
else i%"prec"op[i]#<prec"op[.]##
prect[i][.]=1<1;
else i%"op[i]==op[.]#
prect[i][.]=1>1;
else
prect[i][.]=1=1;
print%"&)t=c&prect[i][.]#;
-
print%"&)n&#;
-
-
present"char chchar opt[20]#
$
int i;
%or"i=0;i<strlen"opt#;i//#
$
i%"ch==opt[i]#
return"1#;
-
return"0#;
-
opprec"#
$
char chch2s1[20]tost;
int lenile;
len=strlen"inpst#;
push"1*1#;
tost=1*1;
%or"i=0;i<len;i//#
$
ch=inpst[i];
i%""tost==1*1#>>"ch==1*1##
(reak;
i%"prec"tost#<prec"ch##
$ tost=ch; push"ch#;-
else
$
strcpy"s1&)0&#;
le=strlen"s1#;
@hile"prec"tost#>prec"ch##
$
ch=pop"#;
s1[le//]=ch;
@hile""isalpha"stack[top]##>>"stack[top]0=1i1##
$
s1[le//]=pop"#;
-
tost=stack[top];
-
s1[le]=1)01;
tost=stack[top];
i%"strc!p"s1&i&#==0#
push"141#;
i%"strc!p"s1&4/4&#==0#
push"141#;
i%"strc!p"s1&4,4&#==0#
push"141#;
i%"strc!p"s1&4-4&#==0#
push"141#;
i--;
-+,closin' o% 4AS4,+
-+,closin' o% %or loop,+
stack[//top]=1)01;
i%""strc!p"stack&*4&#==0#>>"ch==1*1##
print%"&strin' acepted&#;
else
print%"&strin' is not parsed )n&#;
-+,closin' o% B8<;,+
push"char c#
$
top//;
stack[top]=c;
-
pop"#
$
char c;
c=stack[top];
top--;
return"c#;
-
int prec"char c#
$
i%"c==1i1#
return"C#;
else i%"c==1D1#
return"E#;
else i%"c==1+1?? c==1,1#
return "F#;
else i%" c==1/1?? c== 1-1#
return"2#;
else i%"c==1*1#
return"1#;
else
return"0#;
-

You might also like