compiler lab manual (1)

Upload: mohan-deva

Post on 07-Jul-2018

254 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/18/2019 Compiler Lab Manual (1)

    1/82

      Chameli Devi School Of Engineering, Indore

    CDGI'S

    CHAMELIDEVI SCHOOL OF ENGINEEING, INDOE!

    DE"A#MEN# OF COM"$#E SCIENCE

    CO$SE FILE CON#EN#

    %ear Cla&&Sem (ranch S)*+ec Fac)l- Name2013-14 Sem-VII

    CS-A &B

    CSE Compiler

    Design

    Mr. Ajay ais!al

    Conen

    Lab Manual of Compiler Design Page 1

    .!Sco/e of he co)r&e

    0!Di&ci/line& involved in i

    1!A*&rac vie2 for a com/iler

    3!Fron4end and *ac54end a&5&

    6!Mod)le&

    7!Li& of "racical&

    8!LIN$9 OS

    :!C;;

  • 8/18/2019 Compiler Lab Manual (1)

    2/82

      Chameli Devi School Of Engineering, Indore

    CHAMELI DEVI GO$" OF INS#I#$#ES, INDOE!

    Chameli Devi School Of Engineering!

    De/armen of Com/)er Science = Informaion #echnolog-

    Com/iler De&ign La*oraor-

      Com/iler De&ign >CS48?.@ "racical

     %ear 2013-2014B

    Name  

    oll No!  

    (ranch  

    Seme&er """"""""""""""""""""""""""""""""""""""" 

    Secion  

    S)*+ec  

      Cerified *-

    #o$al %ra$ial '  %ra$ials per(orme)' 

    *a+l$y ,ameSigna$+re

    Lab Manual of Compiler Design Page 2

  • 8/18/2019 Compiler Lab Manual (1)

    3/82

      Chameli Devi School Of Engineering, Indore

    CHAMELI DEVI GO$" OF INS#I#$#ES, INDOE

    DE"A#MEN# OF COM"$#E SCIENCE = INFOMA#ION #ECHNOLOG%

    COM"ILE DESIGN LA(OA#O% CS48?.B

    "AC#ICAL LIS#

    S!NO Name of "racical1.

    Im/lemen a "rogram o co)n characer of a given &ring 2iho) )&ing

    &/ace = 2ih +sing &/ace for he &ring a handle of a &ring i& a &)*&ring

    ha mache& he righ &ide of a /rod)cion r)le! !

    2.

    Creae a file Comiler!ccB = Im/lemen a "rogram o read all he conen of aCom/iler!cc ho2 man- line&, ho2 man- 2ord& and ho2 man- characer in

    he fileB !

    3.

    rie a /rogram for im/lemenaion of Deermini&ic Finie A)omaa  DFAB

    for he &ring& acce/ed *- a***, a**, a*,aB!

    4.

    Con&r)cion of Minimiaion of Deermini&ic Finie A)omaa  for he givendiagram = recognie he &ring aa ; *Ba***B!

    .

    Con&r)c a /rogram for ho2 o Com/)e FIS# B = FOLLO B &-m*ol for

    LL. B grammar, if he ConeJ free grammar for LL.B Con&r)cion i&KK!!

    /.

    Con&r)c a O/eraor "recedence "ar&er for he follo2ing given grammar

    and al&o com/)e Leading B and railing B &-m*ol& of he given grammar!

    .

    "rogram )&ing LE9 o co)n he n)m*er of characer&, 2ord&, &/ace& and

    line& in a given in/) file!

    .

    "rogram )&ing LE9 o co)n he n)m*er& of commen line& in a given C

    /rogram! Al&o eliminae hem and co/- he re&)ling /rogram ino &e/arae

    file!

    Lab Manual of Compiler Design Page 3

  • 8/18/2019 Compiler Lab Manual (1)

    4/82

      Chameli Devi School Of Engineering, Indore

    .

    "rogram )&ing LE9 o recognie a valid arihmeic eJ/re&&ion and o

    recognie he idenifier& and o/eraor& /re&en! "rin hem &e/arael-!

    10."rogram )&ing LE9 o recognie 2heher a given &enence i& &im/le or

    com/o)nd!

    11.

    "rogram )&ing LE9 o recognie and co)n he n)m*er of idenifier& in a

    given in/) file!

    12.

    Im/lemen a %ACC %e Anoher Com/iler Com/iler B /rogram o recognie a

    valid arihmeic eJ/re&&ion ha )&e& o/eraor& ;, 4, and !

    13.

    Im/lemen %ACC %e Anoher Com/iler Com/iler B /rogram o recognie a

    valid varia*le, 2hich &ar& 2ih a leer, follo2ed *- an- n)m*er of leer& or

    digi&!

    14.

    %ACC %e Anoher Com/iler Com/iler B /rogram o recognie &ring&

    aaa*, a***, a* and a )&ing he grammar a n*n, nP ?B!

    1.

    "rogram o recognie he ConeJ free grammar a n*n, nP .?B, here a = *

    are in/) &-m*ol& of he grammar!

    16.

    rie a C /rogram o im/lemen he &-naJ4direced definiion of if E hen

    S. and if E hen S. el&e S0!

    Lab Manual of Compiler Design Page 4

  • 8/18/2019 Compiler Lab Manual (1)

    5/82

      Chameli Devi School Of Engineering, Indore

    CHAMELI DEVI GO$" OF INS#I#$#ES, INDOE

    Chameli Devi School Of Engineering

    De/armen of Com/)er Science = Informaion #echnolog-

    (a&ic Com/)er Engineering La*oraor-

    "racical Li&

    S!No! "rac,ical Da,e of EJ/erimen, Da,e of S)*mi&&ion Signa,)re = emar5&

    .! "rac,ical .0! "rac,ical 0

    1! "rac,ical 13! "rac,ical 36! "rac,ical 67! "rac,ical 78! "rac,ical 8:! "rac,ical :Q! "rac,ical Q.?! "rac,ical .?..! "rac,ical ...0! "rac,ical .0.1! "rac,ical .1.3! "rac,ical .3

    .6! "rac,ical .6

    .7! "rac,ical .7

    .8! "rac,ical .8

    .:! "rac,ical .:

    .Q! "rac,ical .Q0?! "rac,ical 0?0.! "rac,ical 0.00! "rac,ical 0001! "rac,ical 0103! "rac,ical 0306! "rac,ical 06

    Head of De/armen Fac)l-

    Lab Manual of Compiler Design Page 5

  • 8/18/2019 Compiler Lab Manual (1)

    6/82

      Chameli Devi School Of Engineering, Indore

    LA( MAN$AL

    S$(

  • 8/18/2019 Compiler Lab Manual (1)

    7/82

      Chameli Devi School Of Engineering, Indore

    Co)r&e &co/e

    Aim#o learn $eni+es o( a mo)ern ompiler

    Main referenceCompilers 5 %riniples6 #eni+es an) #ools6 Seon) E)i$ion 7y Al(re) V. Ao6 8a9i Se$i6e((ery D. :llman

    S)//lemenar- reference&

    Mo)ern ompiler ons$r+$ion in a9a 2n)

      e)i$ion A)9ane) Compiler Design an)

    Implemen$a$ion 7y M+ni;.

    S)*+ec&Sanning?

    Syn$a= Analysis >%arsing?

    Syn$a= Dire$e) #ransla$ion

    In$erme)ia$e Co)e @enera$ion

    8+n-$ime en9ironmen$s

    Co)e @enera$ion

    Maine In)epen)en$ p$imia$ion

    Com/iler learningIsn$ i$ an ol) )isipline

    es6 i$ is a !ell-es$a7lise) )isipline

    Algori$ms6 me$o)s an) $eni+es are researe) an) )e9elope) in early s$ages o(

    omp+$er siene gro!$

    #ere are many ompilers aro+n) an) many $ools $o genera$e $em a+$oma$ially

    So6 !y !e nee) $o learn i$

    Al$o+g yo+ may ne9er !ri$e a (+ll ompiler

    B+$ $e $eni+es !e learn is +se(+l in many $as;s li;e !ri$ing an in$erpre$er (or a srip$ing

    lang+age6 9ali)a$ion e;ing (or (orms an) so on.

    Lab Manual of Compiler Design Page 7

  • 8/18/2019 Compiler Lab Manual (1)

    8/82

      Chameli Devi School Of Engineering, Indore

    #erminolog-

    Com/ilera program $a$ $ransla$es an executable program in one lang+age in$o an executable program

    in ano$er lang+age. !e e=pe$ $e program pro)+e) 7y $e ompiler $o 7e 7e$$er6 in some

     !ay6 $an $e original

    Iner/reera program $a$ rea)s an executable program an) pro)+es $e res+l$s o( r+nning $a$

    program. +s+ally6 $is in9ol9es e=e+$ing $e so+re program in some (asion. +r o+rse is

    mainly a7o+$ ompilers 7+$ many o( $e same iss+es arise in in$erpre$ers.

    Di&ci/line& involved Algori$ms

  • 8/18/2019 Compiler Lab Manual (1)

    9/82

      Chameli Devi School Of Engineering, Indore

    A+$oma$i grap layo+$ >grap9i6 (or e=ample?

    E=$en)ing an e=is$ing programming lang+age

    %rogram analysis an) impro9emen$ $ools

    A*&rac vie2Compilers $ransla$e (rom a so+re lang+age >$ypially a ig le9el lang+age? $o a (+n$ionally

    e+i9alen$ $arge$ lang+age  >$ypially $e maine o)e o( a par$i+lar maine or a maine-

    in)epen)en$ 9ir$+al maine?.

    Compilers (or ig le9el programming lang+ages are among $e larger an) more omple=

    piees o( so($!are

    • riginal lang+ages inl+)e) *or$ran an) Co7ol

    • ($en m+l$i-pass ompilers >$o (aili$a$e memory re+se?

    • Compiler )e9elopmen$ elpe) in 7e$$er programming lang+age )esign

    • Early )e9elopmen$ (o+se) on syn$a$i analysis an) op$imia$ion

    • Commerially6 ompilers are )e9elope) 7y 9ery large so($!are gro+ps

    • C+rren$ (o+s is on op$imia$ion an) smar$ +se o( reso+res (or mo)ern 8ISC >re)+e)

    ins$r+$ion se$ omp+$er? ari$e$+res.

    CompilerSourcecode

    errors   Machinecode

    8eognies legal >an) illegal? programs

    • @enera$e orre$ o)e

    • Manage s$orage o( all 9aria7les an) o)e

    • Agreemen$ on (orma$ (or o7je$ >or assem7ly? o)e

    Lab Manual of Compiler Design Page 9

  • 8/18/2019 Compiler Lab Manual (1)

    10/82

      Chameli Devi School Of Engineering, Indore

    "rinci/le& of Com/iler De&ign Sylla7+s

    Inrod)cion o Com/iler'

    #ransla$or iss+es6 !y $o !ri$e ompiler6 ompila$ion proess in 7rie(6 (ron$ en) an) 7a;en)

    mo)el6 ompiler ons$r+$ion $ools6 In$erpre$er an) $e rela$e) iss+es6 Cross ompiler6

    Inremen$al ompiler6 Boo$ s$rapping.

    1.LeJical Anal-&i&8e9ie! o( le=ial analysis' alpa7e$6 $o;en6 le=ial error6 Blo; sema$i o( le=ial

    analyser6 A+$oma$i ons$r+$ion o( le=ial analyser >

  • 8/18/2019 Compiler Lab Manual (1)

    11/82

      Chameli Devi School Of Engineering, Indore

    DA@ genera$ing o)e (rom a DA@6 Dynami programming6 Co)e genera$or-genera$or

    onep$.

    7! Code O/imi&aionInrod)cion,  lassi(ia$ion o( p$imisa$ion6 priniple so+res o(

    p$imisa$ion6 m )epen)en$ p$imisa$ion6 m in)epen)en$ op$imisa$ion6 p$imisa$ion

    o( 7asi 7lo;s6 loops in (lo!graps6 p$imising $rans(orma$ion' ompile $ime e9al+a$ion6Common s+7-e=pression elimina$ion6 9aria7le propaga$ion6 o)e Mo9emen$6 s$reng$

    re)+$ion6 )ea) o)e elimina$ion an) loop op$imisa$ion6 loal op$imisa$ion6 DA@ 7ase)

    loal op$imisa$ion. @lo7al op$imisa$ion' on$rol an) )a$a (lo! analysis6 on$rol (lo!

    analysis-onep$s an) )e(ini$ion6 )a$a (lo! analysis6 )a$a (lo! analysis6 Comp+$ing )a$a

    (lo! in(orma$ion6 mee$ o9er pa$s6Da$a (lo! e+a$ions. I$era$i9e )a$a (lo! analysis'

    A9aila7le e=prns6 li9e range i)en$i(ia$ion.

    Definiion

    A ompiler is a omp+$er program >or se$ o( programs? $a$ $rans(orms so+re o)e !ri$$en in

    a programming lang+age  >$e source language? in$o ano$er omp+$er lang+age >$e target

    language6 o($en a9ing a 7inary (orm ;no!n as object code?.

    #he Anal-&i&4S-nhe&i& Model of Com/ilaion

    #ere are $!o par$s $o ompila$ion'

     Analysis )e$ermines $e opera$ions implie) 7y $e so+re program !i are reor)e) in a

    $ree s$r+$+re

    Synthesis  $a;es $e $ree s$r+$+re an) $ransla$es $e opera$ions $erein in$o $e $arge$

    program

    Oher #ool& ha $&e he Anal-&i&4S-nhe&i& Model

    Editors >syn$a= iglig$ing?

    Pretty printers >e.g. Do=ygen?

    Static checkers >e.g. e.g. #eF an) e.g. VHD

  • 8/18/2019 Compiler Lab Manual (1)

    12/82

      Chameli Devi School Of Engineering, Indore

    Gro)/ing of /ha&e&

    Incremenal com/iler#e $erm incremenal com/iler may re(er $o $!o )i((eren$ $ypes o( ompiler.

    • Impera$i9e programming

    • In$era$i9e %rogramming

    In impera$i9e programming an) so($!are )e9elopmen$6 an inremen$al ompiler is one $a$

     !en in9o;e)6 $a;es only $e changes  o( a ;no!n se$ o( so+re (iles an) updates  any

    orrespon)ing o+$p+$ (iles >in $e ompilers $arge$ lang+age6 o($en 7y$eo)e? $a$ may

    alrea)y e=is$ (rom pre9io+s ompila$ions. By e((e$i9ely building upon  pre9io+sly ompile)o+$p+$ (iles6 $e inremen$al ompiler a9oi)s $e !as$e(+l reompila$ion en$ire so+re (iles6

    Lab Manual of Compiler Design Page 12

  • 8/18/2019 Compiler Lab Manual (1)

    13/82

      Chameli Devi School Of Engineering, Indore

     !ere mos$ o( $e o)e remains +nange). *or mos$ inremen$al ompilers6 ompiling a

    program !i$ small anges $o i$s so+re o)e is +s+ally near ins$an$aneo+s. I$ an 7e sai)

    $a$ an inremen$al ompiler re)+es $e gran+lari$y o( a lang+ages $ra)i$ional ompila$ion

    +ni$s !ile main$aining $e lang+ages seman$is6 s+ $a$ $e ompiler an appen) an)

    replae smaller par$s.

    Cro&& com/ilerA ross ompiler is a ompiler apa7le o( rea$ing e=e+$a7le o)e (or a pla$(orm o$er $an

    $e one on !i $e ompiler is r+n.

    Cross ompiler $ools are +se) $o genera$e e=e+$a7les (or em7e))e) sys$em or m+l$iple

    pla$(orms.

    I$ is +se) $o ompile (or a pla$(orm +pon !i i$ is no$ (easi7le $o )o $e ompiling6 li;e miro

    on$rollers $a$ )on$ s+ppor$ an opera$ing sys$em.

    %ases o( a Compiler

    Source Program

    Lexical Analyzer1

    Syntax Analyzer2

    Semantic Analyzer3

    Intermediate Code

    Generator

    4

    Code Optimizer5

    Code Generator

    !arget Program

    Sym"ol#ta"le

    $anager

    %rror &andler

    Lab Manual of Compiler Design Page 13

  • 8/18/2019 Compiler Lab Manual (1)

    14/82

      Chameli Devi School Of Engineering, Indore

    "rogram R?.

    1.  Im/lemen a "rogram o co)n characer of a given &ring  2iho))&ing &/ace  =  2ih )&ing &/ace  for he &ring a handle of a &ring i& a

    &)*&ring ha mache& he righ &ide of a /rod)cion r)le!

    lexical analysis scannin!"

    token stream   1

    ident

    #$al#

    3

    a''ign

    %

    2

    num"er

    10

    4

    time'

    %

    1

    ident

    #$al#

    5

    plu'

    %

    1

    ident

    #i#

    &o'en num(er

    &o'en $alue 

    syn&ax analysis parsin!"

    syntax tree

    iden& ) num(er * iden& + iden&

    ,erm

    -xpression

    S&a&emen&

    LeJical Anal-&i&

    S$ream o( ara$ers is gro+pe) in$o $o;ens

    E=amples o( $o;ens are i)en$i(iers6 reser9e) !or)s6 in$egers6 )o+7les or (loa$s6 )elimi$ers6opera$ors an) speial sym7ols in$ aG a J a K 2G in$ reser9e) !or) a

    i)en$i(ier G speial sym7ol a i)en$i(ier J opera$or ai)en$i(ier K opera$or 2 in$eger ons$an$ G speial sym7ol

    E=amples o( #o;en

    #o;en' A se+ene o( ara$ers $o 7e $rea$e) as a

    single +ni$.

    L E=amples o( $o;ens.

    8eser9e) !or)s >e.g. *egin6 end6 &r)c6 if e$.?

    ey!or)s >integer 6 true e$.?

    pera$ors >K6 &&6 KK e$?

    I)en$i(iers >9aria7le names6 proe)+re names6 parame$er names?

    n+meri6 s$ring6 ara$er ons$an$s e$.?

    %+n$+a$ion mar;s >'6 6 e$.?

    Lab Manual of Compiler Design Page 14

  • 8/18/2019 Compiler Lab Manual (1)

    15/82

      Chameli Devi School Of Engineering, Indore

    A ! &ring 2ih )&ing &/ace!

    So)rce CodeNinl+)eOios$ream.P9oi) main>?Q

    ar R30Gin$ nJ0Go+$OOTEn$er $e S$ringT OOTUnTG inPPG (or>in$ iJ0GRiJU0GiKK?

      Q  nJnK1G

     W o+$OOT?G

      W

    Inp+$'

    #ype any s$rings !i$ om7ina$ions o( le$$ers

    +$p+$'

    #o$al ,o. o( le$$ers ara$ers o( gi9en s$ring.

    Lab Manual of Compiler Design Page 15

  • 8/18/2019 Compiler Lab Manual (1)

    16/82

      Chameli Devi School Of Engineering, Indore

    ( ! &ring 2iho) )&ing &/ace!

    So)rce Code

    Ninl+)e Oios$reamP+sing namespae s$)GNinl+)e Oonio.PNinl+)e OiomanipPN inl+)e Os$ringP

    ons$ in$ SIXE J 100G9oi) inp+$ >arY?G9oi) !or)Co+n$ >arY?G9oi) longZor) >arY6 in$?G9oi) n+m7ers >arY?G

    9oi) o+$p+$in$ le$$erCo+n$R?G

    in$ main>?Q

    ar s$ring R100 J Q0WGar !or)R100 J Q0WGin$ = J 0G in$ n J 0Gin$ le$$erCo+n$R100 J Q0WG

    inp+$>s$ring?G

     !or)Co+n$>s$ring?GlongZor)>s$ring6 =?Gn+m7ers >s$ring?Go+$p+$le$$erCo+n$?Gre$+rn 0G

    W9oi) inp+$ >ar Yen$er?Q

    o+$OOTEn$er sen$ene>s? TOO s$)''en)lGs$)''in.ge$line>en$er6 SIXE?Gin$ len J s$rlen> en$er ?G

      W9oi) !or)Co+n$ >ar Y!or)2?Q

    in$ n$ J 0G

     !ile>Y!or)2 J U0?Q

     !ile>isspae>Y!or)2??Q

    KK!or)2GW

    Lab Manual of Compiler Design Page 16

  • 8/18/2019 Compiler Lab Manual (1)

    17/82

      Chameli Devi School Of Engineering, Indore

    i(>Y!or)2 J U0?QKKn$G !ile>isspae>Y!or)2? && Y!or)2 J U0?KK!or)2G

    WWs$)''o+$OOT,+m7er o( !or)s' TOOn$OOen)lG

    W9oi) o+$p+$in$ le$$erCo+n$R?Q

      (or >in$ l J 0G l O 2/G lKK?  Q  i( >le$$erCo+n$Rl P 0?  Q

      o+$ OO le$$erCo+n$Rl OO T T OO ar>a K l? OO en)lG  W

      WW

    9oi) n+m7ers >ar Y!or)2?Q

    in$ n+m J 0Gin$ amo+n$ J s$rlen>!or)2?G

    (or>in$ i J 0G i O amo+n$G iKK?Qi(>is)igi$>!or)2Ri??n+mKKG

    Ws$)''o+$OOTDigi$s' TOOn+mOOen)lG

    W9oi) longZor) >ar Y$emp6 in$ = ?

    Q  in$ o+n$er J 0G  in$ ma="!or) J -1G in$ leng$ J in$>s$rlen>$emp??G

     (or>in$ iJ0G iOleng$G iKK?

      Q  i(>$empRi J ?  Q  o+n$erKKG

      Welse i(>$empRiJJ ?

    Lab Manual of Compiler Design Page 17

  • 8/18/2019 Compiler Lab Manual (1)

    18/82

      Chameli Devi School Of Engineering, Indore

    Q  i(>o+n$er P ma="!or)?  Q  ma="!or) J o+n$erG  W

      o+n$er J 0G  W Ws$)''o+$ OOT

  • 8/18/2019 Compiler Lab Manual (1)

    19/82

      Chameli Devi School Of Engineering, Indore

    "rogram R?0

     Creae a file Comiler!ccB = Im/lemen a "rogram o read all he conenof a Com/iler!cc  ho2 man- line&, ho2 man- 2ord& and ho2 man-

    characer in he fileB !

    So)rce CodeNinl+)eOs$)io.Pin$ main>?

    Qin$ noJ06no!J06nolJ0G*ITUn en$er $e so+re (ile name Comiler!ccT?Gge$s>(name?G(rJ(open>(name6TrT?Gi(>(rJJ,:

  • 8/18/2019 Compiler Lab Manual (1)

    20/82

      Chameli Devi School Of Engineering, Indore

    Ninl+)e O(s$reamPNinl+)e Oios$reamPNinl+)e Os$ringPNinl+)e O$ypeP

    +sing namespae s$)G

    in$ main>?Q  s$ring nameG name o( $e (ile $o 7e opene) 

    ar G ara$er $o 7e rea) in$o $e loop 

    (s$ream (ile,ameG )elare $e (s$ream o7je$ 

    lis$ o( in$egers $o ol) $e o+n$ in(orma$ion

      in$ ara$ers J 06  !or)s J 06

    sen$enes J 06lines J 0G

      lis$ o( ons$an$s +se) $o )e(ine !or)s6 lines6 an) sen$enes  ons$ ar E?G ignore $e ne=$ ara$er in $e 7+((er  (ile,ame.open>name."s$r>??G on9er$ name $o a -s$yle s$ring

    o+n$ J 1G

      W !ile >(ile,ame?G

      +se a !ile loop $o per(orm $e re+ire) opera$ions

      !ile > (ile,ame.eo(>??  Q

    Lab Manual of Compiler Design Page 20

  • 8/18/2019 Compiler Lab Manual (1)

    21/82

      Chameli Devi School Of Engineering, Indore

    ar pre9CarG $ra; $e las$ ara$er analye)

    (ile,ame.ge$>?G ge$ ea ara$er (rom $e (ileo+$ OO G an) prin$ i$ $o $e sreen

    ara$ers KKG o+n$ $e ara$ers in $e (ile

    o+n$ $e !or)s in $e (ile

    i( >> JJ Bpre9Car J B JJ SE,# ? o+n$ $e sen$enes in $e (ileQ

      sen$enes KKGW en) o( $e sen$ene i(

     i( > JJ E?G lear $e (ail s$a$e  (ile,ame.lose>?G lose $e (ile 

    )isplay a s+mmary o( $e (ile analysis  o+$ OO TUn#ere are T OO ara$ers OO T ara$ers in $is (ile.UnTG  o+$ OO T#ere are T OO !or)s OO T !or)s in $is (ile.UnTG  o+$ OO T#ere are T OO sen$enes OO T sen$enes in $is (ile.UnTG

      o+$ OO T#ere are T OO lines OO T lines in $is (ile.UnTG re$+rn 0G

    W

    Lab Manual of Compiler Design Page 21

  • 8/18/2019 Compiler Lab Manual (1)

    22/82

      Chameli Devi School Of Engineering, Indore

    In/)

    #ype any s$rings !i$ om7ina$ions o( le$$ers ara$ers o( sen$ene..Cons$r+$ a+$oma$a.

    O)/)

    Se+enes o( $ransi$ion s$a$es !i$ aep$ing s$a$es.

    Lab Manual of Compiler Design Page 22

  • 8/18/2019 Compiler Lab Manual (1)

    23/82

      Chameli Devi School Of Engineering, Indore

    "rogram R?1

    rie a /rogram for im/lemenaion of Deermini&ic Finie A)omaa  

    DFAB for he &ring& acce/ed *- a***, a**, a*,aB!

    Deermini&ic finie a)omaa DFAB

    A )e$erminis$i (ini$e a+$oma$on >D*A? is a -$+ple' >S6 \6 #6 s6 A?

    ] an alpa7e$ >\?

    ] a se$ o( s$a$es >S?

    ] a $ransi$ion (+n$ion ># ' S ^ \ _ S?.

    ] a s$ar$ s$a$e

    ] a se$ o( aep$ s$a$es

    #e maine s$ar$s in $e s$ar$ s$a$e an) rea)s in a s$ring o( sym7ols (rom i$s alpa7e$. I$+ses $e $ransi$ion (+n$ion # $o )e$ermine $e ne=$ s$a$e +sing $e +rren$ s$a$e an) $e

    sym7ol j+s$ rea). I(6 !en i$ as (inise) rea)ing6 i$ is in an aep$ing s$a$e6 i$ is sai) $o aep$$e s$ring6 o$er!ise i$ is sai) $o reje$ $e s$ring. #e se$ o( s$rings i$ aep$s (orm alang+age6 !i is $e lang+age $e D*A reognies.

     Non4Deermini&ic Finie A)omaon N4DFAB

    A ,on-De$erminis$i *ini$e A+$oma$on >,*A? is a -$+ple' >S6 \6 #6 s6 A?

    ] an alpa7e$ >\?

    ] a se$ o( s$a$es >S?

    ] a $ransi$ion (+n$ion >#' S ^ \ _ S?.

    ] a s$ar$ s$a$e

    ] a se$ o( aep$ s$a$es

    Zere %>S? is $e po!er se$ o( S an) ` is $e emp$y s$ring. #e maine s$ar$s in $e s$ar$s$a$e an) rea)s in a s$ring o( sym7ols (rom i$s alpa7e$. I$ +ses $e $ransi$ion rela$ion # $o)e$ermine $e ne=$ s$a$e>s? +sing $e +rren$ s$a$e an) $e sym7ol j+s$ rea) or $e emp$ys$ring. I(6 !en i$ as (inise) rea)ing6 i$ is in an aep$ing s$a$e6 i$ is sai) $o aep$ $e s$ring6

    o$er!ise i$ is sai) $o reje$ $e s$ring. #e se$ o( s$rings i$ aep$s (orm a lang+age6 !i is

    Lab Manual of Compiler Design Page 23

  • 8/18/2019 Compiler Lab Manual (1)

    24/82

      Chameli Devi School Of Engineering, Indore

    $e lang+age $e ,*A reognies.

    So)rce Code

    Ninl+)eOs$)io.PNinl+)eOios$ream.PNinl+)eOs$)li7.PNinl+)eOonio.P9oi) main>?Qin$ n6m6s$ar$6n(6psGar s$rR20Glrsr>?Gprin$(>Ten$er no o( s$a$esT?Gsan(>T[)T6&n?Gprin$(>Ten$er no o( inp+$sT?Gsan(>T[)T6&m?Gons$r+$ing 7+((ersin$ YY$ranJne! in$Y RnG(or>in$ iJ0GiOnGiKK?Q$ranRiJne! in$RmGW(or>iJ0GiOnGiKK?

    Q (or>in$ jJ0GjOmGjKK? Q  prin$(>Ten$er ne=$ s$a$e (or presen$ s$a$e [) on inp+$[) T6i6j?G  san(>T[)T6&$ranRiRj?G WWprin$(>Ten$er s$ar$ing s$a$eT?Gsan(>T[)T6&s$ar$?G

    prin$(>Ten$er no o( (inal s$a$esT?G

    san(>T[)T6&n(?Gin$Y (inalJne! in$Rn(G(or>iJ0GiOn(GiKK?Qprin$(>Ten$er $e s$a$eT?Gsan(>T[)T6&(inalRi?GW

    prin$(>Ten$er s$ringT?Gsan(>T[sT6s$r?G

    iJ0GpsJs$ar$G

    Lab Manual of Compiler Design Page 24

  • 8/18/2019 Compiler Lab Manual (1)

    25/82

      Chameli Devi School Of Engineering, Indore

     !ile>s$rRiJU0?Q psJ$ranRpsRs$rRi-4G iKKGW

    (or>iJ0GiOnGiKK?Q  i(>psJJ(inalRi?  Q  prin$(>Taep$e)T?G  7rea;G  WW)ele$ing 7+((er)ele$e (inalG(or>iJ0GiOnGiKK?

    Q)ele$e $ranRiGW  )ele$e $ranGW

    Lab Manual of Compiler Design Page 25

  • 8/18/2019 Compiler Lab Manual (1)

    26/82

      Chameli Devi School Of Engineering, Indore

    "rogram R?3

    Con&r)cion of minimiaion of Deermini&ic Finie A)omaafor he given diagram = recognie he &ring aa ; *Ba***B!

    Minimizing Finite AutomataConsi)er $e (ini$e a+$oma$on so!n in (ig+re 1 !i aep$s $e reg+lar se$ )eno$e) 7y $e

    reg+lar e=pression >aa K 7?Ya7>77?Y. Aep$ing s$a$es are olore) yello! !ile reje$ing s$a$esare 7l+e.

    Fig)re . 4 ecognier for aa ; *Ba***B

    Closer e=amina$ion re9eals $a$ s$a$es s2  an) s  are really $e same sine $ey are 7o$

    aep$ing s$a$es an) 7o$ go $o s/ +n)er $e inp+$ 7 an) 7o$ go $o s3 +n)er an a. So6 !y

    no$ merge $em an) (orm a smaller maine In $e same manner6 !e o+l) arg+e (ormerging s$a$es s0 an) s. Merging s$a$es li;e $is so+l) pro)+e a smaller a+$oma$on $a$

    aomplises e=a$ly $e same $as; as o+r original one.

    *rom $ese o7ser9a$ions6 i$ seems $a$ $e ;ey $o ma;ing (ini$e a+$oma$a smaller is $oreognie an) merge e+i9alen$ s$a$es. #o )o $is6 !e m+s$ agree +pon $e )e(ini$ion o(e+i9alen$ s$a$es. Here is one (orm+la$ion o( !a$ Moore )e(ine) as in)is$ing+isa7le s$a$es.

    Definiion. Two states in a finite automaton are equivalent   if and only if fore!ery string x" if is started in either state with x as input" it either accepts inboth cases or rejects in both cases.

    Ano$er !ay $o say $is is $a$ $e maine )oes $e same $ing !en s$ar$e) in ei$er s$a$e.

    #is is espeially neessary !en (ini$e a+$oma$a pro)+e o+$p+$.

    Lab Manual of Compiler Design Page 26

  • 8/18/2019 Compiler Lab Manual (1)

    27/82

      Chameli Devi School Of Engineering, Indore

    #!o +es$ions remain. *irs$6 o! )oes one (in) e+i9alen$ s$a$es6 an) $en6 e=a$ly o!9al+a7le is $is in(orma$ion Ze sall ans!er $e seon) +es$ion (irs$ 7y pro9i)ing aorollary $o a (amo+s $eorem pro9en long ago 7y Myill R3 an) ,ero)e R4.

    Corollar-!  #or a deterministic finite automaton " the minimum number of

    states in any e$ui!alent deterministic finite automaton is the same as thenumber of e$ui!alence classes of %s states&

    Zi$ one more o7ser9a$ion6 !e sall 7e a7le $o presen$ an algori$m (or $rans(orming ana+$oma$on in$o i$s smalles$ e+i9alen$ maine.

    Fact & E$ui!alent states go to e$ui!alent states under all inputs&

    ,o! !e ;no! $a$ i( !e an (in) $e e+i9alene lasses >or gro+ps o( e+i9alen$ s$a$es? (oran a+$oma$on6 $en !e an +se $ese as $e s$a$es o( $e smalles$ e+i9alen$ maine. #emaine so!n in (ig+re 1 !ill 7e +se) as an e=ample (or $e in$+i$i9e )is+ssion $a$ (ollo!s.

    *or e=ample6 $e inp+$ a lea)s (roms1 $o s in gro+p B an) inp+$ 7 lea)s $o $o s2 in gro+p A.?

    in s$a$e' s0 s1 s3 s4 s s/

    a lea)s $o' B B B B B B

    7 lea)s $o' B A B B B A

    s1an) s/? an) $e res$ o( $e s$a$es in $e gro+p sine i$ lea)s $o gro+p A (or $ese $!o ins$ea)o( gro+p B. #+s $e s$a$es in $e se$ Qs06 s36 s46 sW anno$ 7e e+i9alen$ $o $ose in $e se$

    Qs16 s/W an) !e m+s$ par$i$ion B in$o $!o gro+ps. ,o! !e a9e $e gro+ps'

    A J Qs26 sW6 B J Q s06 s36 s46 sW6 C J Q s16 s/W

    an) $e ne=$ e=amina$ion o( !ere $e inp+$s lea) so!s +s $a$ s3 is no$ e+i9alen$ $o $e

    res$ o( gro+p B. Ze m+s$ par$i$ion again.

    Lab Manual of Compiler Design Page 27

  • 8/18/2019 Compiler Lab Manual (1)

    28/82

      Chameli Devi School Of Engineering, Indore

    Con$in+ing $is proess +n$il !e anno$ )is$ing+is 7e$!een $e s$a$es in any gro+p 7yemploying o+r inp+$ $es$s6 !e en) +p !i$ $e gro+ps'

    A J Qs26 sW6 B J Qs06 s46 sW6 C J Qs1W6 D J Qs3W6 E J Q s/W.

    In 9ie! o( $e a7o9e $eore$ial )e(ini$ions an) res+l$s6 i$ is easy $o arg+e $a$ all o( $e s$a$esin ea gro+p are e+i9alen$ 7ea+se $ey all go $o $e same gro+ps +n)er $e inp+$s a an)7. #+s in $e sense o( Moore $e s$a$es in ea gro+p are $r+ly in)is$ing+isa7le. Ze alsoan laim $a$ )+e $o $e orollary $o $e Myill-,ero)e $eorem6 any a+$oma$on $a$ aep$s

    >aa K 7?Ya7>77?Y m+s$ a9e a$ leas$ (i9e s$a$es.B+il)ing $e minim+m s$a$e (ini$e a+$oma$on isno! ra$er s$raig$(or!ar). Ze merely +se $e e+i9alene lasses >o+r gro+ps? as s$a$esan) pro9i)e $e proper $ransi$ions. #is gi9es +s $e (ini$e a+$oma$on pi$+re) in (ig+re 2.

    Fig)re 0 4 A Minimal A)omaon

    Here is $e s$a$e minimia$ion algori$m.

    #e omple=i$y o( $is algori$m is >n2? sine !e e; all o( $e s$a$es ea $ime !ee=e+$e $e repea$ loop an) mig$ a9e $o e=e+$e $e loop n $imes sine i$ mig$ $a;e aninp+$ o( leng$ n $o )is$ing+is 7e$!een $!o s$a$es. A (as$er algori$m !as la$er )e9elope) 7yHopro($.

    Lab Manual of Compiler Design Page 28

  • 8/18/2019 Compiler Lab Manual (1)

    29/82

      Chameli Devi School Of Engineering, Indore

    So)rce CodeNinl+)eOs$)io.PNinl+)eOios$ream.P

    Ninl+)eOs$ring.PNinl+)eOs$)li7.PNinl+)eOonio.P9oi) main>?Q

    in$ ns$a$es6minp+$s6s$ar$6n(6psGar s$rR20Glrsr>?Gprin$(>Ten$er no o( s$a$esT?Gsan(>T[)T6&ns$a$es?Gprin$(>Ten$er no o( inp+$sT?G

    san(>T[)T6&minp+$s?Gons$r+$ing 7+((ersin$ YY$ranJne! in$Y Rns$a$esG(or>in$ iJ0GiOns$a$esGiKK?Q

    $ranRiJne! in$Rminp+$sGW(or>iJ0GiOns$a$esGiKK?Q

    (or>in$ jJ0GjOminp+$sGjKK?Q

    prin$(>Ten$er ne=$ s$a$e (or presen$ s$a$e [) on inp+$[) T6i6j?Gsan(>T[)T6&$ranRiRj?G

    WWprin$(>Ten$er s$ar$ing s$a$eT?Gsan(>T[)T6&s$ar$?G

    prin$(>Ten$er no o( (inal s$a$esT?Gsan(>T[)T6&n(?Gin$Y (inalJne! in$Rn(G(or>iJ0GiOn(GiKK?Q

    prin$(>Ten$er $e s$a$eT?Gsan(>T[)T6&(inalRi?G

    Win$ Ys$a$egro+pJne! in$Rns$a$es6YYgro+pgro+pJne! in$YRns$a$esGmemse$>s$a$egro+p6-16ns$a$esYsieo(>in$??G

    in$ YYgro+ps$a$eJne! in$YRns$a$esG(or>iJ0GiOns$a$esGiKK?Q

    gro+ps$a$eRiJne! in$Rns$a$esK1Ggro+pgro+pRiJne! in$R2G

    Lab Manual of Compiler Design Page 29

  • 8/18/2019 Compiler Lab Manual (1)

    30/82

      Chameli Devi School Of Engineering, Indore

    memse$>gro+pgro+pRi6-162Ysieo(>in$??Gmemse$>gro+ps$a$eRi6-16>ns$a$esK1?Ysieo(>in$??G

    W

    (or>iJ0GiOn(GiKK?

    Qs$a$egro+pR(inalRiJ0Ggro+ps$a$eR0R(inalRiJ1G

    W

    gro+ps$a$eR0Rns$a$esJ10Gmeans a par$i$ion a9ing (inal s$a$es

    (or>iJ0GiOns$a$esGiKK?Q

    i(>s$a$egro+pRiJ0?Q

    s$a$egro+pRiJ1Ggro+ps$a$eR1RiJ1G

    WW

    gro+ps$a$eR1Rns$a$esJ100Gmeans a par$i$ion a9ing no (inal s$a$esin$ gro+po+n$J2Gin$ o+n$J06angeG

    minimia$ion s$ar$s ere)oQ

    angeJ0G(or>in$ jJ0GjOminp+$sGjKK?Q

    in$ las$gro+p6presen$gro+p6s$a$e6la$es$gro+po+n$6ma=gro+po+n$J0G(or>in$ gro+pJ0Ggro+pOgro+po+n$Ggro+pKK?Q o+n$J0G

     !ile>gro+ps$a$eRgro+pRo+n$J1&&o+n$Ons$a$es?Qo+n$KKGWgro+pgro+pRgro+pR0Js$a$egro+pR$ranR o+n$ Rj G

    gro+pgro+pRgro+pR1Jgro+ps$a$eRgro+pRns$a$esGW(or> gro+pJ0Ggro+pOgro+po+n$Ggro+pKK?Qla$es$gro+po+n$Jgro+po+n$G

    las$gro+pJgro+pgro+pRgro+pR0Go+n$J0G !ile>o+n$Ons$a$es?Q

    i(>>s$a$eJgro+ps$a$eRgro+pRo+n$?JJ1?Q

    presen$gro+pJs$a$egro+pR$ranRo+n$RjGi(>presen$gro+pJlas$gro+p?

    Lab Manual of Compiler Design Page 30

  • 8/18/2019 Compiler Lab Manual (1)

    31/82

      Chameli Devi School Of Engineering, Indore

    QangeJ1G(in) (or any gro+p going $o presen$gro+pin$ (lagJ0G(or>in$

    anygro+pJ0Ganygro+pOla$es$gro+po+n$Ganygro+pKK?Q

    i(>gro+pgro+pRanygro+pR0JJpresen$gro+p

    &&gro+pgro+pRanygro+pR1JJgro+pgro+pRgro+pR1?

    Q(lagJ1G7rea;GW

    Wange gro+pgro+p

    ange s$a$egro+pange gro+ps$a$e an) gro+po+n$anygro+pJ(lagJJ1

    anygro+p'la$es$gro+po+n$KKGgro+pgro+pRanygro+pR0Jpresen$gro+pGgro+pgro+pRanygro+pR1Jgro+pgro+pRgro+p

    R1G

    s$a$egro+pRo+n$Janygro+pG

    gro+ps$a$eRanygro+pRo+n$J1Ggro+ps$a$eRgro+pRo+n$J-1Ggro+ps$a$eRanygro+p

    Rns$a$esJgro+pgro+pRgro+pR1GW

    W

    o+n$KKGWen) o( !ile

      i(>ma=gro+po+n$Ola$es$gro+po+n$?Qma=gro+po+n$Jla$es$gro+po+n$GWWe;ing all $e gro+ps (or loop

      gro+po+n$Jma=gro+po+n$GWe;ing all $e inp+$s (or loop

      W!ile>angeJ0?Gen) o( minimia$ion

    prin$(>TUnUn@ro+psUnUnT?G(or>iJ0GiOgro+po+n$GiKK?Q

    prin$(>T[) T6i?G(or>in$ jJ0GjOns$a$esGjKK?

    Lab Manual of Compiler Design Page 31

  • 8/18/2019 Compiler Lab Manual (1)

    32/82

      Chameli Devi School Of Engineering, Indore

    Qi(>gro+ps$a$eRiRjJ-1? prin$(>T [) T6j?GWprin$(>TUnT?G

    W)ele$ing 7+((er)ele$e s$a$egro+pG)ele$e (inalG(or>iJ0GiOns$a$esGiKK?Q

    )ele$e $ranRiG)ele$e gro+ps$a$eRiG)ele$e gro+pgro+pRiG

    W)ele$e gro+pgro+pG

    )ele$e $ranG)ele$e gro+ps$a$eG

    W

    Lab Manual of Compiler Design Page 32

  • 8/18/2019 Compiler Lab Manual (1)

    33/82

      Chameli Devi School Of Engineering, Indore

    IN"$#

    ecognier for aa ; *Ba***B

    O$#"$#

    A Minimal A)omaon for aa ; *Ba***B

    Lab Manual of Compiler Design Page 33

  • 8/18/2019 Compiler Lab Manual (1)

    34/82

      Chameli Devi School Of Engineering, Indore

    "rogram R?6

    Con&r)c a /rogram for ho2 o calc)lae FIS# B = FOLLO B &-m*ol

    for LL. B grammar, if he ConeJ free grammar for LL.B Con&r)cion i&

    SaBDBCC7CDE*Eg*(

     Com/)e FIS# B = FOLLO B &-m*ol for LL. B grammar #e ons$r+$ion o( a pre)i$i9e parser is ai)e) 7y $!o (+n$ions assoia$e) !i$ a grammar@. #ese (+n$ions6 *I8S# an) *F? (or all grammar sym7ols F6 apply $e (ollo!ing r+les +n$il no more$erminals or ` an 7e a))e) $o any *I8S# se$'

    1. I( F is $erminal6 $en *I8S#>F? is QFW.

    2.I( F _ ` is a pro)+$ion6 $en a)) ` $o *I8S#>F?.

    3.I( F is non$erminal an) F _1 2 c ; . is a pro)+$ion6 $en plae a in *I8S#>F? i( (orsome i6 a is in *I8S#>i?6 an) ` is in all o( *I8S#>1?6 c 6 *I8S#>i-1?G $a$ is6 16 c 6i-1 ⇒ `. I( ` is in *I8S#>j? (or all j J 16 26 c 6 ;6 $en a)) ` $o *I8S#>F?. *or e=ample6 e9ery$ing in*I8S#>1? is s+rely in

    *I8S#>F?. I( 1 )oes no$ )eri9e `6 $en !e a)) no$ing more $o *I8S#>F?6 7+$ i( 1 ⇒ `6 $en !e a)) *I8S#>2? an) so on.

    ,o!6 !e an omp+$e *I8S# (or any s$ring F1F2 . . . Fn as (ollo!s. A)) $o *I8S#>F1F2 cFn? all $e non-` sym7ols o( *I8S#>F1?. Also a)) $e non-` sym7ols o( *I8S#>F2? i( ` is in*I8S#>F1?6 $e non-` sym7ols o( *I8S#>F 3? i( ` is in 7o$ *I8S#>F 1? an) *I8S#>F2?6 an) so

    on. *inally6 a)) ` $o *I8S#>F1F2 c Fn? i(6 (or all i6 *I8S#>F i? on$ains `.

    Lab Manual of Compiler Design Page 34

  • 8/18/2019 Compiler Lab Manual (1)

    35/82

      Chameli Devi School Of Engineering, Indore

    FOLLOAB

    De(ine * E ? h i)

    #en'

    *I8S#>E? J *I8S#>#? J *I8S#>*? J Q> 6 i)W

    *I8S#>E? J QK6 `W

    *I8S#>#? J QY6 `W

    *

  • 8/18/2019 Compiler Lab Manual (1)

    36/82

      Chameli Devi School Of Engineering, Indore

    *a7A7?JQaWG

    2.I( a pro)+$ion o( $is $ype A-PBCD... means all are 9aria7le or non-$erminal $eni( (irs$>B? )ono$ on$ains n+ll $en (irs$>A?J(irs$>B?

    s$op ere.else$en also e; (or ne=$ non $erminal li;e C ere same as a7o9e s$ep an)(irs$>A?J*irs$>B?K(irs$>C?.i( !e ge$ n+ll s$op $ere.

    *

  • 8/18/2019 Compiler Lab Manual (1)

    37/82

      Chameli Devi School Of Engineering, Indore

    So)rce Code

    Ninl+)eTs$)io.TNinl+)eOonio.Par arrayR10R206$empR10G

    in$ 6nG9oi) (+n>in$6in$R?Gin$ (+n2>in$ i6in$ j6in$ pR6in$ ;ey?Q

    in$ ;Gi(>;ey?Q

    (or>;J0G;?re$+rn 1Gelse re$+rn 0G

    WW

    9oi) (+n>in$ i6in$ pR?Qin$ j6;6;eyG(or>jJ2GarrayRiRjJGjKK?Q

    i(>arrayRiRj-1JJ?Q

    i(>arrayRiRjPJA&&arrayRiRjOJX?Q;eyJ0G(+n2>i6j6p6;ey?G

    WelseQ

    ;eyJ1Gi(>(+n2>i6j6p6;ey??$empRKKJarrayRiRjGi(>arrayRiRjJJ&&pR0J-1?Q $a;ing 66 as n+ll sym7ol.

    i(>arrayRpR0RpR1PJA&&arrayRpR0RpR1OJX?Q

    ;eyJ0G

    (+n2>pR06pR16p6;ey?GW

    elsei(>arrayRpR0RpR1J&&arrayRpR0RpR1J?

    Qi(>(+n2>pR06pR16p6;ey??$empRKKJarrayRpR0RpR1GW

    WW

    W

    WW

    Lab Manual of Compiler Design Page 37

  • 8/18/2019 Compiler Lab Manual (1)

    38/82

      Chameli Devi School Of Engineering, Indore

    9oi) main>?Q

    in$ pR26i6jGlrsr>?Gprin$(>TEn$er $e no. o( pro)+$ions 'T?G

    san(>T[)T6&n?Gprin$(>TEn$er $e pro)+$ions 'UnT?G(or>iJ0GiOnGiKK?san(>T[sT6arrayRi?G(or>iJ0GiOnGiKK?Q

    J-16pR0J-16pR1J-1G(+n>i6p?Gprin$(>T*irs$>[? ' R T6arrayRiR0?G(or>jJ0GjOJGjKK?prin$(>T[6T6$empRj?G

    prin$(>TU7 .UnT?Gge$>?G

    WW

    IN"$#

    SaBDBCC7C

    DE*Eg*(

    O$#"$#

    En$er $e no. o( pro)+$ions '/En$er $e pro)+$ions 'SaBDBC

    C7CDE*Eg*(*irs$>S? ' R a .*irs$>B? ' R .*irs$>C? ' R 76 .*irs$>D? ' R g66( .*irs$>E? ' R g6 .*irs$>*? ' R (6 .

    Lab Manual of Compiler Design Page 38

  • 8/18/2019 Compiler Lab Manual (1)

    39/82

      Chameli Devi School Of Engineering, Indore

    "rogram R?7

    Con&r)c a O/eraor "recedence "ar&er for he follo2ing givengrammar and al&o com/)e Leading B and railing B &-m*ol& ofhe given grammar!

    pera$or-%ree)enepera$or-%ree)ene %arser%arserO/eraor grammar 

    small6 7+$ an impor$an$ lass o( grammars !e may a9e an e((iien$ opera$or pree)ene

    parser >a si($-re)+e parser? (or an opera$or grammar.

    In an operator grammar 6 no pro)+$ion r+le an a9e'

    -P a$ $e rig$ si)e

    -P $!o a)jaen$ non-$erminals a$ $e rig$ si)e.

    %ree)ene%ree)ene 8ela$ions8ela$ionsIn opera$or-pree)ene parsing6 !e )e(ine $ree )isjoin$ pree)ene rela$ions 7e$!eener$ain pairs o( $erminals.

    a O.

    7 7 as iger pree)ene $an aa J] 7 7 as same pree)ene as a

    a.P 7 7 as lo!er pree)ene $an a

    #e )e$ermina$ion o( orre$ pree)ene rela$ions 7e$!een $erminals are 7ase) on $e

    $ra)i$ional no$ions o( assoia$i9i$y an) pree)ene o( opera$ors. >:nary min+s a+ses a

    pro7lem?.

    #e in$en$ion o( $e pree)ene rela$ions is $o (in) $e an)le o( a rig$-sen$en$ial (orm6

    O.  !i$ mar;ing $e le($ en)6

    J] appearing in $e in$erior o( $e an)le6 an)

     .P mar;ing $e rig$ an).

    In o+r inp+$ s$ring fa1a

    2...a

    nf6 !e inser$ $e pree)ene rela$ion 7e$!een $e pairs o(

    $erminals >$e pree)ene rela$ion ol)s 7e$!een $e $erminals in $a$ pair?.

    Lab Manual of Compiler Design Page 39

  • 8/18/2019 Compiler Lab Manual (1)

    40/82

      Chameli Devi School Of Engineering, Indore

    :sing:sing pera$orpera$or -%ree)ene-%ree)ene 8ela$ions8ela$ions

    E -P EKE h E-E h EYE h EE h EE h >E? h -E h i)

    #en $e inp+$ s$ring i)Ki)Yi) !i$ $e pree)ene rela$ions inser$e) !ill 7e'

    f O. i)

    .P K O

    . i)

    .P Y O

    . i)

    .P f

    pera$or-%ree)enepera$or-%ree)ene  %arsing%arsing  Algori$mAlgori$m 

    #he in/) &ring i& 2U, he iniial &ac5 i& U and a a*le hold& /recedence relaion&

    *e2een cerain erminal&

     Algorithm:se$ p $o poin$ $o $e (irs$ sym7ol o( !f Gre/ea forever  if  > f is on $op o( $e s$a; and p poin$s $o f ? hen re)rn  el&e Q

    le$ a 7e $e $opmos$ $erminal sym7ol on $e s$a; an) le$ 7 7e $e sym7ol poin$e) $o7y pG

      if  > a O. 7 or a J] 7 ? hen Q Y SHI*# Y  p+s 7 on$o $e s$a;G  a)9ane p $o $e ne=$ inp+$ sym7olG  W

      el&e if  > a .P 7 ? hen Y 8ED:CE Y  re/ea  pop s$a;

      )nil  > $e $op o( s$a; $erminal is rela$e) 7y O. $o $e $erminal mos$ reen$ly poppe)?G

      el&e error>?G  W

    Lab Manual of Compiler Design Page 40

  • 8/18/2019 Compiler Lab Manual (1)

    41/82

      Chameli Devi School Of Engineering, Indore

    pera$or-%ree)enepera$or-%ree)ene %arsing%arsing Algori$mAlgori$m 55 E=ampleE=ample

    stack input action

    f i)Ki)Yi)f f O.

    i) si($

    fi) Ki)Yi)f i) .P K re)+e E i)f Ki)Yi)f si($

    fK i)Yi)f si($

    fKi) Yi)f   i).P Y re)+e E i)

    fK Yi)f si($

    fKY i)f si($

    fKYi) f i).P f re)+e E i) 

    fKY f Y.P f re)+e E EYE 

    fK f K

    .

    P f re)+e E EKEf f aep$

    Disa)9an$agesDisa)9an$ages o(o( pera$orpera$or %ree)ene%ree)ene %arsing%arsing

    Di&advanage&Di&advanage& ''I$ anno$ an)le $e +nary min+s >$e le=ial analyer so+l) an)le $e +nary min+s?.

    Small lass o( grammars.

    Di((i+l$ $o )ei)e !i lang+age is reognie) 7y $e grammar.

    Advanage&Advanage& ''

    simple

    po!er(+l eno+g (or e=pressions in programming lang+ages

    Lab Manual of Compiler Design Page 41

  • 8/18/2019 Compiler Lab Manual (1)

    42/82

      Chameli Devi School Of Engineering, Indore

    So)rce Code

    Ninl+)eOios$ream.PNinl+)eOs$)io.PNinl+)eOonio.P

    Ninl+)eO$ype.PNinl+)eOs$ring.P

    ar YYarrG on$ains pro)+$ions (or )i((eren$ ,on $erminalsa9ing non $erminals a$ arrRiR0 an) res$ on$ains pro)+$ionsYYYYYYYYYYYYYYYYYYYYYYYYIM%8#A,#YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYremem7er sym7ol is j+s$ ano$er $erminal $ereYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

    e=amples o( pro)+$ions +se) 7y programS-Pa>#? is +se) $o )e(ine m+l$iple pro)+$ions (or $e same ,on$erminal#-P#6SS

    arr !ill on$ain $e pro)+$ions as (ollo!s 0 1 2 3 4 / 10arrR0 S a > # ?arrR1 # # 6 S S

    in$ Y(lags$a$eG $o see !e$er lea)ing as alrea)y 7een (o+n) (or a ,on $erminalar YY(o+n)lea)Gon$ains $e alrea)y (o+n) lea)ing (or a ,on $erminalin$ Y,$Sym7olsG+se) $o re)+e $ime omple=i$y 7y s$oring !ere $epro)+$ions (or a non $erminal are s$ore) in arrar YY(o+n)$railGon$ains $e alrea)y (o+n) $railing (or a ,on $erminalin$ YY$railgoes$oG$o $ell !i ,on #erminals $railing goes $o !ose $railingin$ YYlea)goes$oG$o $ell !i ,on #erminals lea)ing goes $o !ose lea)ing

    in$ s$rmerge+ni+e>arY)es$6ons$ ar Yso+re?Q  in$ s$rleng$Js$rlen>so+re?6angeJ0G  (or>in$ iJ0GiOs$rleng$GiKK?  Q  i(>s$rr>)es$6so+reRi??  Q  )es$Rs$rlen>)es$?K1JU0G

      )es$Rs$rlen>)es$?Jso+reRiG  angeJ1G

    Lab Manual of Compiler Design Page 42

  • 8/18/2019 Compiler Lab Manual (1)

    43/82

      Chameli Devi School Of Engineering, Indore

    W  W  re$+rn angeGW

    9oi) lea)ing>in$ no"o("non$erminals?Qin$ non$erminalsJ0Gar @amma6s$rR10JQU0WG

     !ile>non$erminalsOno"o("non$erminals?Q  (or>in$ eale$$erJ1GarrRnon$erminalsReale$$erJU0Geale$$erKK?  Q  @ammaJarrRnon$erminalsReale$$erG

      i(>is+pper>@amma??  Q  lea)goes$oR ,$Sym7olsR$oasii>@amma?-/ Rlea)goes$oR,$Sym7olsR$oasii>@amma?-/R0K1 Jnon$erminalsG  lea)goes$oR ,$Sym7olsR$oasii>@amma?-/ R0KKG  on$in+eG  W  else  Q  i(>@ammaJJU=0?  Q7rea;GW  i(>@ammaJJ?  Qon$in+eGW

      s$rR0J@ammaG  s$rR1JU0G  s$rmerge+ni+e>(o+n)lea)Rnon$erminals6s$r?G

      W

      !ile>arrRnon$erminalsReale$$erK1JU=0&&  arrRnon$erminalsReale$$erK1J?

      Q  eale$$erKKG  W  Wnon$erminalsKKGW

      in$ angeJ0G

    Lab Manual of Compiler Design Page 43

  • 8/18/2019 Compiler Lab Manual (1)

    44/82

      Chameli Devi School Of Engineering, Indore

    )o  Q  angeJ0G  (or>in$ iJ0GiOnon$erminalsGiKK?

      Q  (or>in$ jJ1GjOJlea)goes$oRiR0GjKK?  Q  angehJs$rmerge+ni+e>(o+n)lea)Rlea)goes$oRiRj6(o+n)lea)Ri?G  W  W  W  !ile>ange?G

    W9oi) $railing>in$ no"o("non$erminals?Qin$ non$erminalsJ0Gar Del$a6s$rR10JQU0WG

     !ile>non$erminalsOno"o("non$erminals?Q  in$ eale$$erJs$rlen>arrRnon$erminals?-1G  (or>Geale$$erP0Geale$$er--?  Q

      Del$aJarrRnon$erminalsReale$$erG YYYYYYYalpa B  i(>is+pper>Del$a??  Q  $railgoes$oR ,$Sym7olsR$oasii>Del$a?-/ R$railgoes$oR,$Sym7olsR$oasii>Del$a?-/R0K1 Jnon$erminalsG  $railgoes$oR ,$Sym7olsR$oasii>Del$a?-/ R0KKG

      i(>arrRnon$erminalsReale$$er-1J&&  eale$$er-1P0?  QDel$aJarrRnon$erminalsReale$$er-1G

    i(>is+pper>Del$a??Q s$rR0JDel$aG s$rR1JU0G s$rmerge+ni+e>(o+n)$railRnon$erminals6s$r?G

    W  W

    Lab Manual of Compiler Design Page 44

  • 8/18/2019 Compiler Lab Manual (1)

    45/82

      Chameli Devi School Of Engineering, Indore

    W B alpa YYYYY alpa  else

      Q  i(>Del$aJJ?  Qon$in+eGW

      s$rR0JDel$aG  s$rR1JU0G  s$rmerge+ni+e>(o+n)$railRnon$erminals6s$r?G  Del$aJarrRnon$erminalsReale$$er-1G

    i(>is+pper>Del$a?&&eale$$er-1P0?

    Q

     $railgoes$oR ,$Sym7olsR$oasii>Del$a?-/ R$railgoes$oR,$Sym7olsR$oasii>Del$a?-/R0K1 Jnon$erminalsG

     $railgoes$oR ,$Sym7olsR$oasii>Del$a?-/ R0KKG

    W  W

      !ile>eale$$er-1P0&&  arrRnon$erminalsReale$$er-1J?

      Q  eale$$er--G  W  Wnon$erminalsKKGW  in$ angeJ0G  )o

      Q  angeJ0G  (or>in$ iJ0GiOnon$erminalsGiKK?  Q  (or>in$ jJ1GjOJ$railgoes$oRiR0GjKK?  Q  angehJs$rmerge+ni+e>(o+n)$railR$railgoes$oRiRj6(o+n)$railRi?G  W  W

      W  !ile>ange?G

    Lab Manual of Compiler Design Page 45

  • 8/18/2019 Compiler Lab Manual (1)

    46/82

      Chameli Devi School Of Engineering, Indore

    W9oi) main>?Qin$ n$G

    lrsr>?Gprin$(>TEn$er no.o( non$erminals 'T?Gsan(>T[)T6&n$?GarrJne! arYRn$G(o+n)lea)Jne! arYRn$G(o+n)$railJne! arYRn$G(lags$a$eJne! in$Rn$Glea)goes$oJne! in$YRn$G$railgoes$oJne! in$YRn$G

    ,$Sym7olsJne! in$R2/G(or >in$ iJ0GiOn$GiKK?Q  arrRiJne! arR100G  (o+n)lea)RiJne! arR10G  memse$>(o+n)lea)Ri6U0610?G  (o+n)$railRiJne! arR10G  memse$>(o+n)$railRi6U0610?G  (lags$a$eRiJ0G  lea)goes$oRiJne! in$Rn$G  lea)goes$oRiR0J0G  $railgoes$oRiJne! in$Rn$G  $railgoes$oRiR0J0G prin$(>TEn$er non $erminal T?G inPParrRiR0G (l+sall>?G prin$(>TEn$er %ro)+$ion (or [------PT6arrRiR0?G ge$s>arrRiK1?G

     ,$Sym7olsR$oasii>arrRiR0?-/JiG

    W

    ar pro)R0Glea)ing>n$?G$railing>n$?Go+$OOen)lOOen)lG  (or>iJ0GiOn$GiKK?

      Q  prin$(>Tlea)ing >[?--P Q [s WUnT6arrRiR06(o+n)lea)Ri?G

    Lab Manual of Compiler Design Page 46

  • 8/18/2019 Compiler Lab Manual (1)

    47/82

      Chameli Devi School Of Engineering, Indore

    prin$(>T$railing >[?--P Q [s WUnT6arrRiR06(o+n)$railRi?G  )ele$e arrRiG  )ele$e (o+n)lea)RiG  )ele$e (o+n)$railRiG

      )ele$e lea)goes$oRiG  )ele$e $railgoes$oRiG  W)ele$e arrG)ele$e (lags$a$eG)ele$e (o+n)lea)G)ele$e ,$Sym7olsG)ele$e (o+n)$railG)ele$e $railgoes$oG

    )ele$e lea)goes$oGW

    Lab Manual of Compiler Design Page 47

  • 8/18/2019 Compiler Lab Manual (1)

    48/82

      Chameli Devi School Of Engineering, Indore

    "rogram R?8

    "rogram )&ing LE9 o co)n he n)m*er of characer&, 2ord&, &/ace& and

    Line& in a given in/) file!

    LeJical Anal-er

    #e main $as; o( $e le=ial analyer is $o rea) $e inp+$ so+re program6 sanning $e

    ara$ers6 an) pro)+e a se+ene o( $o;ens  $a$ $e parser an +se (or syn$a$i analysis.

    #e in$er(ae  may 7e $o 7e alle) 7y $e parser $o pro)+e one $o;en a$ a $ime Main$ain

    in$ernal s$a$e o( rea)ing $e inp+$ program >!i$ lines? Ha9e a (+n$ion ge$,e=$#o;enk $a$ !ill rea) some ara$ers a$ $e +rren$ s$a$e o( $e inp+$ an) re$+rn a $o;en $o $e parser

    $er $as;s o( $e le=ial analyer inl+)e S;ipping or i)ing !i$espae an) ommen$s

    eeping $ra; o( line n+m7ers (or error repor$ing Some$imes i$ an also pro)+e $e

    anno$a$e) lines (or error repor$s %ro)+e $e 9al+e o( $e $o;en p$ional' Inser$ i)en$i(iers in$o

    $e sym7ol $a7le

    Cara$er no$ ommon?6 m+s$ ;eep $ra; o( nes$ing $o (in) en) o( ommen$s May pro)+e

    i))en  $o;ens6 (or on9eniene o( sanner s$r+$+re Al!ays pro)+e an en)-o(-(ile $o;en

    Impor$an$ $a$ +o$e) s$rings an) ommen$s )on$ ge$ s$+; i( an +ne=pe$e) en) o( (ile

    o+rs

    Lab Manual of Compiler Design Page 48

  • 8/18/2019 Compiler Lab Manual (1)

    49/82

      Chameli Devi School Of Engineering, Indore

    So)rce Code

    [Qin$ J06 7lJ06 lnJ06 !rJ0G[W[[RUn QlnKKG!rKKGWRU$ Q7lKKG!rKKGWRT T Q7lKKG!rKKGWRUnU$ QKKGW[[in$ main>?

    Q*ITEn$er $e (ilename' T?Gsan(>T[sT6 (ile?GyyinJ(pGyyle=>?Gprin$(>TCara$erJ[)UnBlan;J[)Un

  • 8/18/2019 Compiler Lab Manual (1)

    50/82

      Chameli Devi School Of Engineering, Indore

    IN"$#

    A inp+$ (ile >.)o or any (orma$?6 o+n$s n+m7er o( ara$ers6 !or)s6 spaes an)

  • 8/18/2019 Compiler Lab Manual (1)

    51/82

      Chameli Devi School Of Engineering, Indore

    "rogram R?:

    "rogram )&ing LE9 o co)n he n)m*er& of commen line& in a given CC;;o! 9al+es are $ransmi$$e) 7e$!een par$so( a program? an) $ere are $ools $o elp !i$ $is $as;.

    LeJical Anal-&i& or ScanningB

    #e ara$er s$ream inp+$ is gro+pe) in$o meaning(+l +ni$s alle) leJeme&6 !i are $enmappe) in$o o5en&6 $e la$$er ons$i$+$ing $e o+$p+$ o( $e le=ial analyer. *or e=ample6any one o( $e (ollo!ing

    =3 J y K 3G  =3 J y K 3 G  =3 JyK 3 G

    7+$ no$

    = 3 J y K 3G !o+l) 7e gro+pe) in$o $e le=emes =36 J6 y6 K6 36 an) G.

    A $o;en is a O$o;en-name6a$$ri7+$e-9al+eP pair. *or e=ample

    1. #e le=eme =3 !o+l) 7e mappe) $o a $o;en s+ as Oi)61P. #e name i) is sor$ (ori)en$i(ier. #e 9al+e 1 is $e in)e= o( $e en$ry (or =3 in $e sym7ol $a7le pro)+e) 7y$e ompiler. #is $a7le is +se) $o pass in(orma$ion $o s+7se+en$ pases.

    2. #e le=eme J !o+l) 7e mappe) $o $e $o;en OJP. In reali$y i$ is pro7a7ly mappe) $o apair6 !ose seon) omponen$ is ignore). #e poin$ is $a$ $ere are many )i((eren$i)en$i(iers so !e nee) $e seon) omponen$6 7+$ $ere is only one assignmen$ sym7ol

    J.3. #e le=eme y is mappe) $o $e $o;en Oi)62P

    Lab Manual of Compiler Design Page 51

  • 8/18/2019 Compiler Lab Manual (1)

    52/82

      Chameli Devi School Of Engineering, Indore

    4. #e le=eme K is mappe) $o $e $o;en OKP.. #e le=eme 3 is some!a$ in$eres$ing an) is )is+sse) (+r$er in s+7se+en$ ap$ers.

    I$ is mappe) $o On+m7er6some$ingP6 7+$ !a$ is $e some$ing. n $e one an)$ere is only one 3 so !e o+l) j+s$ +se $e $o;en On+m7er63P. Ho!e9er6 $ere an 7ea )i((erene 7e$!een o! $is so+l) 7e prin$e) >e.g.6 in an error message pro)+e)

    7y s+7se+en$ pases? an) o! i$ so+l) 7e s$ore) >(i=e) 9s. (loa$ 9s )o+7le?. %eraps$e $o;en so+l) poin$ $o $e sym7ol $a7le !ere an en$ry (or $is ;in) o( 3 is s$ore).Ano$er possi7ili$y is $o a9e a separa$e n+m7ers $a7le.

    /. #e le=eme G is mappe) $o $e $o;en OGP.

    ,o$e $a$ non-signi(ian$ 7lan;s are normally remo9e) )+ring sanning. In C6 mos$ 7lan;s arenon-signi(ian$. Blan;s insi)e s$rings are an e=ep$ion.

    ,o$e $a$ !e an )e(ine i)en$i(iers6 n+m7ers6 an) $e 9ario+s sym7ols an) p+n$+a$ion !i$o+$ +sing re+rsion >ompare !i$ parsing 7elo!?.

    Lab Manual of Compiler Design Page 52

  • 8/18/2019 Compiler Lab Manual (1)

    53/82

      Chameli Devi School Of Engineering, Indore

    So)rce Code

    [Q

    in$ omJ0G[W[[TYTRUnKTYT QomKKG(prin$(>yyo+$6 T T?GW[[in$ main>?Qprin$(>TZri$e a C programUnT?Gyyo+$J(open>To+$p+$T6 T!T?Gyyle=>?G

    prin$(>TCommen$J[)UnT6om?Gre$+rn 0GW

    Lab Manual of Compiler Design Page 53

  • 8/18/2019 Compiler Lab Manual (1)

    54/82

      Chameli Devi School Of Engineering, Indore

    O$#"$#

    fle= p17.lf le=.yy. 5llf.a.o+$Zri$e a C programNinl+)eOs$)io.Pin$ main>?Q  in$ a6 7G  Y(loa$ GY  prin$(>Haik?G  Yprin$(>Hellok?GYWRC$rl-)Commen$J1

    fa$ o+$p+$Ninl+)eOs$)io.Pin$ main>?Q  in$ a6 7G  prin$(>Haik?GW

    Lab Manual of Compiler Design Page 54

  • 8/18/2019 Compiler Lab Manual (1)

    55/82

      Chameli Devi School Of Engineering, Indore

    "rogram R?Q

    "rogram )&ing LE9 o recognie a valid arihmeic eJ/re&&ion and o recognie he idenifier& and o/eraor& /re&en! "rin hem &e/arael-!

    Some 8eg+lar E=pressions (or *le=

    UTRTYUT s$ring

    TU$ThTUnTUT T !i$espae >mos$ ommon (orms?

    Ra-A-X

    Ra-A-X"Ra-A-X0-"Y i)en$i(ier' allo!s a6 aF6 a4"" 

    R0-YT.TR0-K allo!s . 7+$ no$ .

    R0-KT.TR0-Y allo!s . 7+$ no$ .

    R0-YT.TR0-Y allo!s . 7y i$sel(

    #e +ser m+s$ s+pply a le=ial analyer $o rea) $e inp+$ s$ream an) omm+nia$e $o;ens

    >!i$ 9al+es6 i( )esire)? $o $e parser. #e le=ial analyer is an in$eger-9al+e) (+n$ion alle)

    yyle=. #e (+n$ion re$+rns an in$eger6 $e $o;en n+m7er6 represen$ing $e ;in) o( $o;en rea).

    I( $ere is a 9al+e assoia$e) !i$ $a$ $o;en6 i$ so+l) 7e assigne) $o $e e=$ernal 9aria7leyyl9al.

    #e parser an) $e le=ial analyer m+s$ agree on $ese $o;en n+m7ers in or)er (or

    omm+nia$ion 7e$!een $em $o $a;e plae. #e n+m7ers may 7e osen 7y a6 or osen

    7y $e +ser. In ei$er ase6 $e N )e(ine meanism o( C is +se) $o allo! $e le=ial

    analyer $o re$+rn $ese n+m7ers sym7olially. *or e=ample6 s+ppose $a$ $e $o;en name

    DI@I# as 7een )e(ine) in $e )elara$ions se$ion o( $e a spei(ia$ion (ile. #e rele9an$

    por$ion o( $e le=ial analyer mig$ loo; li;e'

    yyle=>?Q

      e=$ern in$ yyl9alG

      in$ G

      . . .

      J ge$ar>?G

      . . .  s!i$> ? Q

    Lab Manual of Compiler Design Page 55

  • 8/18/2019 Compiler Lab Manual (1)

    56/82

      Chameli Devi School Of Engineering, Indore

    . . .

      ase 0'

      ase 1'

      . . .

      ase '

      yyl9al J -0G

      re$+rn> DI@I# ?G

      . . .

      W

      . . .

    #e in$en$ is $o re$+rn a $o;en n+m7er o( DI@I#6 an) a 9al+e e+al $o $e n+merial 9al+e o(

    $e )igi$. %ro9i)e) $a$ $e le=ial analyer o)e is plae) in $e programs se$ion o( $e

    spei(ia$ion (ile6 $e i)en$i(ier DI@I# !ill 7e )e(ine) as $e $o;en n+m7er assoia$e) !i$ $e

    $o;en DI@I#.

    #is meanism lea)s $o lear6 easily mo)i(ie) le=ial analyersG $e only pi$(all is $e nee) $o

    a9oi) +sing any $o;en names in $e grammar $a$ are reser9e) or signi(ian$ in C or $e

    parserG (or e=ample6 $e +se o( $o;en names i( or !ile !ill almos$ er$ainly a+se se9ere

    )i((i+l$ies !en $e le=ial analyer is ompile). #e $o;en name error is reser9e) (or error

    an)ling6 an) so+l) no$ 7e +se) nai9ely.

    As men$ione) a7o9e6 $e $o;en n+m7ers may 7e osen 7y a or 7y $e +ser. In $e )e(a+l$

    si$+a$ion6 $e n+m7ers are osen 7y a. #e )e(a+l$ $o;en n+m7er (or a li$eral ara$er is

    $e n+merial 9al+e o( $e ara$er in $e loal ara$er se$. $er names are assigne)

    $o;en n+m7ers s$ar$ing a$ 2.

    #o assign a $o;en n+m7er $o a $o;en >inl+)ing li$erals?6 $e (irs$ appearane o( $e $o;en

    name or li$eral in $e )elara$ions se$ion an 7e imme)ia$ely (ollo!e) 7y a nonnega$i9e

    in$eger. #is in$eger is $a;en $o 7e $e $o;en n+m7er o( $e name or li$eral. ,ames an) li$erals

    no$ )e(ine) 7y $is meanism re$ain $eir )e(a+l$ )e(ini$ion. I$ is impor$an$ $a$ all $o;en

    n+m7ers 7e )is$in$.

    Lab Manual of Compiler Design Page 56

  • 8/18/2019 Compiler Lab Manual (1)

    57/82

      Chameli Devi School Of Engineering, Indore

    *or is$orial reasons6 $e en)mar;er m+s$ a9e $o;en n+m7er 0 or nega$i9e. #is $o;en

    n+m7er anno$ 7e re)e(ine) 7y $e +serG $+s6 all le=ial analyers so+l) 7e prepare) $o

    re$+rn 0 or nega$i9e as a $o;en n+m7er +pon reaing $e en) o( $eir inp+$.

    A 9ery +se(+l $ool (or ons$r+$ing le=ial analyers is $e Qo7KKGWR? Q7KKGW[[in$ main>?Qprin$(>TEn$er $e e=pressionUnT?Gyyle=>?Gi(>o7-7JJ0?Qprin$(>TVali) e=pressionUnT?GWelseQprin$(>TIn9ali) e=pressionT?GWprin$(>TUnA))J[)UnS+7J[)UnM+lJ[)UnDi9J[)UnT6a6s6m6)?Gprin$(>Tpera$ors are' UnT?G

    Lab Manual of Compiler Design Page 57

  • 8/18/2019 Compiler Lab Manual (1)

    58/82

      Chameli Devi School Of Engineering, Indore

    i(>(laga?prin$(>TKUnT?Gi(>(lags?prin$(>T-UnT?Gi(>(lagm?prin$(>TYUnT?Gi(>(lag)?prin$(>TUnT?Gre$+rn 0GW

    O$#"$#

    fle= p2a.lf le=.yy. 5llf.a.o+$En$er $e e=pression>aK7Y?a is an i)en$i(ier7 is an i)en$i(ier is an i)en$i(ier

    RC$rl-)Vali) e=pressionA))J1S+7J0M+lJ1Di9J0pera$ors are'KY

    Lab Manual of Compiler Design Page 58

  • 8/18/2019 Compiler Lab Manual (1)

    59/82

      Chameli Devi School Of Engineering, Indore

    "rogram R.1

    "rogram )&ing LE9 o recognie 2heher a given &enence i& &im/le orcom/o)nd!

    [Qin$ (lagJ0G[W[[>TTRaARn,R)DTT?h>TTRoRr8TT?h>TTR7BR+:R$#TT? Q(lagJ1GW[[in$ main>?Qprin$(>TEn$er $e sen$eneUnT?Gyyle=>?Gi(>(lagJJ1?prin$(>TUnCompo+n) sen$eneUnT?Gelseprin$(>TUnSimple sen$eneUnT?Gre$+rn 0GW

    Lab Manual of Compiler Design Page 59

  • 8/18/2019 Compiler Lab Manual (1)

    60/82

      Chameli Devi School Of Engineering, Indore

    O$#"$#

    fle= p27.lf le=.yy. 5llf.a.o+$En$er $e sen$eneI am %oojaI am %oojaRC$rl-)Simple sen$ene

    f.a.o+$En$er $e sen$eneCSE or ISECSE or ISERC$rl-)Compo+n) sen$ene

    Lab Manual of Compiler Design Page 60

  • 8/18/2019 Compiler Lab Manual (1)

    61/82

      Chameli Devi School Of Engineering, Indore

    "rogram R.3

    "rogram )&ing LE9 o recognie and co)n he n)m*er of idenifier& in a

    given in/) file!

    Tin$T?h>T(loa$T?h>TarT?h>TaseT?h>T)e(a+l$T?h >Ti(T?h>T(orT?h>Tprin$(T?h>Tsan(T? Qprin$(>T[s is a;ey!or)UnT6 yy$e=$?GWQi)W Qprin$(>T[s is an i)en$i(ierUnT6 yy$e=$?G o+n$KKGWQno$i)W Qprin$(>T[s is no$ an i)en$i(ierUnT6 yy$e=$?GW[[in$ main>?Q*ITUnEn$er $e (ilename' T?G

    Lab Manual of Compiler Design Page 61

  • 8/18/2019 Compiler Lab Manual (1)

    62/82

      Chameli Devi School Of Engineering, Indore

    san(>T[sT6 (ile?G(pJ(open>(ile6TrT?GyyinJ(pGyyle=>?Gprin$(>T#o$al i)en$i(iers are' [)UnT6 o+n$?Gre$+rn 0GW

    O$#"$#fa$ P inp+$in$(loa$(0ga)are ase)e(a+l$prin$(san(

    fle= p3.lf le=.yy. 5ll

    f.a.o+$En$er $e (ilename' inp+$in$ is a ;ey!or)(loa$ is a ;ey!or)( is no$ an i)en$i(ier0g is no$ an i)en$i(ier is an i)en$i(iera is an i)en$i(ier) is an i)en$i(ierare is an i)en$i(ier

    ase is a ;ey!or))e(a+l$ is a ;ey!or)prin$( is a ;ey!or)san( is a ;ey!or)$o$al i)en$i(iers are' 4

    Lab Manual of Compiler Design Page 62

  • 8/18/2019 Compiler Lab Manual (1)

    63/82

      Chameli Devi School Of Engineering, Indore

    "rogram R.6

    %ACC %e Anoher Com/iler Com/iler B /rogram o recognie a validarihmeic eJ/re&&ion ha )&e& o/eraor& ;, 4, and !

     (a&ic S/ecificaion&

    ,ames re(er $o ei$er $o;ens or non$erminal sym7ols. a re+ires $o;en names $o 7e)elare) as s+. In a))i$ion6 (or reasons )is+sse) in Se$ion 36 i$ is o($en )esira7le $oinl+)e $e le=ial analyer as par$ o( $e spei(ia$ion (ileG i$ may 7e +se(+l $o inl+)e o$erprograms as !ell. #+s6 e9ery spei(ia$ion (ile onsis$s o( $ree se$ions' $e )elara$ions6

    >grammar? r+les6 an) programs. #e se$ions are separa$e) 7y )o+7le peren$ [[ mar;s.>#e peren$ [ is generally +se) in a spei(ia$ions as an esape ara$er.?

    In o$er !or)s6 a (+ll spei(ia$ion (ile loo;s li;e

    )elara$ions  [[  r+les  [[  programs

    #e )elara$ion se$ion may 7e emp$y. Moreo9er6 i( $e programs se$ion is omi$$e)6 $eseon) [[ mar; may 7e omi$$e) alsoG

    $+s6 $e smalles$ legal a spei(ia$ion is

    [[  r+les

    Blan;s6 $a7s6 an) ne!lines are ignore) e=ep$ $a$ $ey may no$ appear in names or m+l$i-ara$er reser9e) sym7ols. Commen$s may appear !ere9er a name is legalG $ey areenlose) in Y . . . Y6 as in C an) %

  • 8/18/2019 Compiler Lab Manual (1)

    64/82

      Chameli Devi School Of Engineering, Indore

    A li$eral onsis$s o( a ara$er enlose) in single +o$es . As in C6 $e 7a;slas U is anesape ara$er !i$in li$erals6 an) all $e C esapes are reognie). #+s

    Un ne!line  Ur re$+rn

      U single +o$e   UU 7a;slas U  U$ $a7  U7 7a;spae  U( (orm (ee)  U=== === in o$al

    *or a n+m7er o( $enial reasons6 $e ,:< ara$er >U0 or 0? so+l) ne9er 7e +se) ingrammar r+les.

    I( $ere are se9eral grammar r+les !i$ $e same le($ an) si)e6 $e 9er$ial 7ar h an 7e

    +se) $o a9oi) re!ri$ing $e le($ an) si)e. In a))i$ion6 $e semiolon a$ $e en) o( a r+le an7e )roppe) 7e(ore a 9er$ial 7ar. #+s $e grammar r+les

    A ' B C D G  A ' E * G  A ' @ G

    an 7e gi9en $o a as

    A ' B C D

      h E *  h @  G

    I$ is no$ neessary $a$ all grammar r+les !i$ $e same le($ si)e appear $oge$er in $egrammar r+les se$ion6 al$o+g i$ ma;es $e inp+$ m+ more rea)a7le6 an) easier $oange.

    I( a non$erminal sym7ol ma$es $e emp$y s$ring6 $is an 7e in)ia$e) in $e o79io+s !ay'

    emp$y ' G

    ,ames represen$ing $o;ens m+s$ 7e )elare)G $is is mos$ simply )one 7y !ri$ing

    [$o;en name1 name2 . . .

    in $e )elara$ions se$ion. >See Se$ions 3 6 6 an) / (or m+ more )is+ssion?. E9ery nameno$ )e(ine) in $e )elara$ions se$ion is ass+me) $o represen$ a non$erminal sym7ol. E9erynon$erminal sym7ol m+s$ appear on $e le($ si)e o( a$ leas$ one r+le.

    ( all $e non$erminal sym7ols6 one6 alle) $e s$ar$ sym7ol6 as par$i+lar impor$ane. #eparser is )esigne) $o reognie $e s$ar$ sym7olG $+s6 $is sym7ol represen$s $e larges$6

    mos$ general s$r+$+re )esri7e) 7y $e grammar r+les. By )e(a+l$6 $e s$ar$ sym7ol is $a;en$o 7e $e le($ an) si)e o( $e (irs$ grammar r+le in $e r+les se$ion. I$ is possi7le6 an) in (a$

    Lab Manual of Compiler Design Page 64

  • 8/18/2019 Compiler Lab Manual (1)

    65/82

      Chameli Devi School Of Engineering, Indore

    )esira7le6 $o )elare $e s$ar$ sym7ol e=plii$ly in $e )elara$ions se$ion +sing $e [s$ar$;ey!or)'

    [s$ar$ sym7ol

    #e en) o( $e inp+$ $o $e parser is signale) 7y a speial $o;en6 alle) $e en)mar;er. I( $e$o;ens +p $o6 7+$ no$ inl+)ing6 $e en)mar;er (orm a s$r+$+re !i ma$es $e s$ar$sym7ol6 $e parser (+n$ion re$+rns $o i$s aller a($er $e en)mar;er is seenG i$ aep$s $einp+$. I( $e en)mar;er is seen in any o$er on$e=$6 i$ is an error.

    I$ is $e jo7 o( $e +ser-s+pplie) le=ial analyer $o re$+rn $e en)mar;er !en appropria$eGsee se$ion 36 7elo!. :s+ally $e en)mar;er represen$s some reasona7ly o79io+s I s$a$+s6s+ as en)-o(-(ile or en)-o(-reor).

    2' A$ions

    Zi$ ea grammar r+le6 $e +ser may assoia$e a$ions $o 7e

    per(orme) ea $ime $e r+le is reognie) in $e inp+$ proess. #ese a$ions may re$+rn9al+es6 an) may o7$ain $e 9al+es re$+rne) 7y pre9io+s a$ions. Moreo9er6 $e le=ialanalyer an re$+rn 9al+es (or $o;ens6 i( )esire).

    An a$ion is an ar7i$rary C s$a$emen$6 an) as s+ an )o inp+$ an) o+$p+$6 all s+7programs6an) al$er e=$ernal 9e$ors an) 9aria7les. An a$ion is spei(ie) 7y one or more s$a$emen$s6enlose) in +rly 7raes Q an) W. *or e=ample6

    A ' > B ?  Q ello> 16 Ta7T ?G W

    an)

    FFF ' XXX  Q prin$(>Ta messageUnT?G  (lag J 2G W

    are grammar r+les !i$ a$ions.

    Lab Manual of Compiler Design Page 65

  • 8/18/2019 Compiler Lab Manual (1)

    66/82

      Chameli Devi School Of Engineering, Indore

    So)rce Code

    yy$e=$?G re$+rn ,:MBE8GWRa-A-XK Qre$+rn IDGWRU$K GUn Qre$+rn 0GW. Qre$+rn yy$e=$R0GW[[

    ACC[QNinl+)eOs$)io.P[W[$o;en ,:MBE8 ID[le($ K -[le($ Y [[e=pr' e=pr K e=pr

      he=pr - e=pr  he=pr Y e=pr  he=pr e=pr  h-,:MBE8  h-ID  h>e=pr?  h,:MBE8  hID  G[[

    main>?Qprin$(>TEn$er $e e=pressionUnT?Gyyparse>?Gprin$(>TUnE=pression is 9ali)UnT?Ge=i$>0?GWin$ yyerror>ar Ys?Qprin$(>TUnE=pression is in9ali)T?G

    e=i$>0?GW

    Lab Manual of Compiler Design Page 66

  • 8/18/2019 Compiler Lab Manual (1)

    67/82

      Chameli Devi School Of Engineering, Indore

    O$#"$#

    fle= p4a.lfya 5) p4a.yf le=.yy. y.$a7. 5llf.a.o+$En$er $e e=pression>aY7K?E=pression is 9ali)

    f.a.o+$En$er $e e=pression>aK/-?E=pression is in9ali)

    Lab Manual of Compiler Design Page 67

  • 8/18/2019 Compiler Lab Manual (1)

    68/82

      Chameli Devi School Of Engineering, Indore

    "rogram R.6

    %ACC %e Anoher Com/iler Com/iler B /rogram o recognie a valid

    varia*le, 2hich &ar& 2ih a leer, follo2ed *- an- n)m*er of leer& or

    digi&!

    a $+rns $e spei(ia$ion (ile in$o a C program6 !i parses $e inp+$ aor)ing $o$e spei(ia$ion gi9en. #e algori$m +se) $o go (rom $e spei(ia$ion $o $e parser isomple=6 an) !ill no$ 7e )is+sse) ere >see $e re(erenes (or more in(orma$ion?. #eparser i$sel(6 o!e9er6 is rela$i9ely simple6 an) +n)ers$an)ing o! i$ !or;s6 !ile no$s$ri$ly neessary6 !ill ne9er$eless ma;e $rea$men$ o( error reo9ery an) am7ig+i$ies

    m+ more ompreensi7le.

    So)rce Code

    yy$e=$?G re$+rn DI@I#GWRa-A-XK Qre$+rn

  • 8/18/2019 Compiler Lab Manual (1)

    69/82

      Chameli Devi School Of Engineering, Indore

    main>?Qyyparse>?Gprin$(>T#e s$ring is a 9ali) 9aria7leUnT?GWin$ yyerror>ar Ys?Qprin$(>T$is is no$ a 9ali) 9aria7leUnT?Ge=i$>0?GW

    O$#"$#

    fle= p47.lfya 5) p47.yf le=.yy. y.$a7. 5llf.a.o+$inp+$34#e s$ring is a 9ali) 9aria7le

    f.a.o+$

    (ile#is is no$ a 9ali) 9aria7le

    Lab Manual of Compiler Design Page 69

  • 8/18/2019 Compiler Lab Manual (1)

    70/82

      Chameli Devi School Of Engineering, Indore

    "rogram R.7

    Im/lemen a /rogram of %ACC %e Anoher Com/iler Com/iler B o recognie

    &ring& aaa*, a***, a* and a )&ing he grammar a n*n, nP ?B!

    %acc %e Anoher Com/iler4Com/ilera pro9i)es a general $ool (or imposing s$r+$+re on $e inp+$ $o a omp+$er program. #e

    a +ser prepares a spei(ia$ion o( $e inp+$ proessG $is inl+)es r+les )esri7ing $e

    inp+$ s$r+$+re6 o)e $o 7e in9o;e) !en $ese r+les are reognie)6 an) a lo!-le9el ro+$ine

    $o )o $e 7asi inp+$. a $en genera$es a (+n$ion $o on$rol $e inp+$ proess. #is

    (+n$ion6 alle) a parser6 alls $e +ser-s+pplie) lo!-le9el inp+$ ro+$ine >$e le=ial analyer?

    $o pi; +p $e 7asi i$ems >alle) $o;ens? (rom $e inp+$ s$ream. #ese $o;ens are organie)

    aor)ing $o $e inp+$ s$r+$+re r+les6 alle) grammar r+lesG !en one o( $ese r+les as

    7een reognie)6 $en +ser o)e s+pplie) (or $is r+le6 an a$ion6 is in9o;e)G a$ions a9e $e

    a7ili$y $o re$+rn 9al+es an) ma;e +se o( $e 9al+es o( o$er a$ions.

    a is !ri$$en in a por$a7le )iale$ o( CR1 an) $e a$ions6 an) o+$p+$ s+7ro+$ine6 are in C as

     !ell. Moreo9er6 many o( $e syn$a$i on9en$ions o( a (ollo! C.

    #e ear$ o( $e inp+$ spei(ia$ion is a olle$ion o( grammar r+les. Ea r+le )esri7es an

    allo!a7le s$r+$+re an) gi9es i$ a name. *or e=ample6 one grammar r+le mig$ 7e

    )a$e ' mon$"name )ay 6 year G

    Here6 )a$e6 mon$"name6 )ay6 an) year represen$ s$r+$+res o( in$eres$ in $e inp+$ proessG

    pres+ma7ly6 mon$"name6 )ay6 an) year are )e(ine) else!ere. #e omma 6 is enlose)

    in single +o$esG $is implies $a$ $e omma is $o appear li$erally in $e inp+$. #e olon an)

    semiolon merely ser9e as p+n$+a$ion in $e r+le6 an) a9e no signi(iane in on$rolling $e

    inp+$. #+s6 !i$ proper )e(ini$ions6 $e inp+$

    +ly 46 1/

    mig$ 7e ma$e) 7y $e a7o9e r+le.

    Lab Manual of Compiler Design Page 70

  • 8/18/2019 Compiler Lab Manual (1)

    71/82

      Chameli Devi School Of Engineering, Indore

    An impor$an$ par$ o( $e inp+$ proess is arrie) o+$ 7y $e le=ial analyer. #is +ser ro+$ine

    rea)s $e inp+$ s$ream6 reogniing $e lo!er le9el s$r+$+res6 an) omm+nia$es $ese

    $o;ens $o $e parser. *or is$orial reasons6 a s$r+$+re reognie) 7y $e le=ial analyer is

    alle) a $erminal sym7ol6 !ile $e s$r+$+re reognie) 7y $e parser is alle) a non$erminal

    sym7ol. #o a9oi) on(+sion6 $erminal sym7ols !ill +s+ally 7e re(erre) $o as $o;ens.

    #ere is onsi)era7le lee!ay in )ei)ing !e$er $o reognie s$r+$+res +sing $e le=ial

    analyer or grammar r+les. *or e=ample6 $e r+les

    mon$"name ' a n G

      mon$"name ' * e 7 G

      . . .

      mon$"name ' D e G

    mig$ 7e +se) in $e a7o9e e=ample. #e le=ial analyer !o+l) only nee) $o reognie

    in)i9i)+al le$$ers6 an) mon$"name !o+l) 7e a non$erminal sym7ol. S+ lo!-le9el r+les $en)

    $o !as$e $ime an) spae6 an) may omplia$e $e spei(ia$ion 7eyon) as a7ili$y $o )eal

     !i$ i$. :s+ally6 $e le=ial analyer !o+l) reognie $e mon$ names6 an) re$+rn an

    in)ia$ion $a$ a mon$"name !as seenG in $is ase6 mon$"name !o+l) 7e a $o;en.

  • 8/18/2019 Compiler Lab Manual (1)

    72/82

      Chameli Devi School Of Engineering, Indore

    In mos$ ases6 $is ne! r+le o+l) 7e slippe) in $o a !or;ing sys$em !i$ minimal e((or$6

    an) li$$le )anger o( )isr+p$ing e=is$ing inp+$.

    #e inp+$ 7eing rea) may no$ on(orm $o $e spei(ia$ions. #ese inp+$ errors are )e$e$e)

    as early as is $eore$ially possi7le !i$ a le($-$o-rig$ sanG $+s6 no$ only is $e ane o(

    rea)ing an) omp+$ing !i$ 7a) inp+$ )a$a s+7s$an$ially re)+e)6 7+$ $e 7a) )a$a an +s+ally

    7e +i;ly (o+n). Error an)ling6 pro9i)e) as par$ o( $e inp+$ spei(ia$ions6 permi$s $e

    reen$ry o( 7a) )a$a6 or $e on$in+a$ion o( $e inp+$ proess a($er s;ipping o9er $e 7a) )a$a.

    In some ases6 a (ails $o pro)+e a parser !en gi9en a se$ o( spei(ia$ions. *or

    e=ample6 $e spei(ia$ions may 7e sel( on$ra)i$ory6 or $ey may re+ire a more po!er(+l

    reogni$ion meanism $an $a$ a9aila7le $o a. #e (ormer ases represen$ )esign errorsG$e la$$er ases an o($en 7e orre$e) 7y ma;ing $e le=ial analyer more po!er(+l6 or 7y

    re!ri$ing some o( $e grammar r+les. Zile a anno$ an)le all possi7le spei(ia$ions6 i$s

    po!er ompares (a9ora7ly !i$ similar sys$emsG moreo9er6 $e ons$r+$ions !i are

    )i((i+l$ (or a $o an)le are also (re+en$ly )i((i+l$ (or +man 7eings $o an)le. Some

    +sers a9e repor$e) $a$ $e )isipline o( (orm+la$ing 9ali) a spei(ia$ions (or $eir inp+$

    re9eale) errors o( onep$ion or )esign early in $e program )e9elopmen$.

    So)rce Code

  • 8/18/2019 Compiler Lab Manual (1)

    73/82

      Chameli Devi School Of Engineering, Indore

    G[[main>?Qprin$(>TEn$er $e s$ringUnT?Gi(>yyparse>?JJ0?Q

    prin$(>TVali)UnT?GWWyyerror>ar Ys?Qprin$(>T[sUnT6s?GW

    O$#"$#

    fle= p7.lfya 5) p7.yf le=.yy. y.$a7. 5llf.a.o+$En$er $e s$ringaa77RC$rl-)

    Vali)

    f.a.o+$En$er $e s$ringaa7syn$a= error

    Lab Manual of Compiler Design Page 73

  • 8/18/2019 Compiler Lab Manual (1)

    74/82

      Chameli Devi School Of Engineering, Indore

    "rogram R.8

    "rogram o recognie he ConeJ free grammar a n*n, nP .?B, here a = *

    are in/) &-m*ol& of he grammar!

    A coneJ4free grammar CFGB i& a &e of rec)r&ive re2riing r)le& or  productionsB

    )&ed o generae /aern& of &ring&!

    C/ consis&s o &he olloin! componen&s

    • a se& o terminal symbols hich are &he charac&ers o &he alpha(e& &ha& appear in &he s&rin!s

    !enera&ed (y &he !rammar.

    • a se& o nonterminal symbols hich are placeholders or pa&&erns o &erminal sym(ols &ha& can

    (e !enera&ed (y &he non&erminal sym(ols.

    • a se& o productions hich are rules or replacin! or reri&in!" non&erminal sym(ols on &he

    le& side o &he produc&ion" in a s&rin! i&h o&her non&erminal or &erminal sym(ols on &he ri!h&

    side o &he produc&ion".

    • a start symbol hich is a special non&erminal sym(ol &ha& appears in &he ini&ial s&rin! !enera&ed

    (y &he !rammar.

    ,o !enera&e a s&rin! o &erminal sym(ols rom a C/ e

    • e!in i&h a s&rin! consis&in! o &he s&ar& sym(ol

    • pply one o &he produc&ions i&h &he s&ar& sym(ol on &he le& hand sie replacin! &he s&ar&

    sym(ol i&h &he ri!h& hand side o &he produc&ion

    • epea& &he process o selec&in! non&erminal sym(ols in &he s&rin! and replacin! &hem i&h &he

    ri!h& hand side o some correspondin! produc&ion un&il all non&erminals ha$e (een replaced (y

    &erminal sym(ols.

    Lab Manual of Compiler Design Page 74

  • 8/18/2019 Compiler Lab Manual (1)

    75/82

      Chameli Devi School Of Engineering, Indore

    Finding all he Sring& Generaed *- a CFG

    ,here are se$eral ays &o !enera&e &he possi(ly inini&e" se& o s&rin!s !enera&ed (y a !rammar. e

    ill sho a &echni:ue (ased on &he num(er o produc&ions used &o !enera&e &he s&rin!.

    /ind &he s&rin!s !enera&ed (y &he olloin! C/

    --> w c d | b e | s

      --> ; |

     

    0. pplyin! a& mos& ero produc&ions e canno& !enera&e any s&rin!s.

    1. pplyin! a& mos& one produc&ion s&ar&in! i&h &he s&ar& sym(ol" e can !enera&e ;cd

  • 8/18/2019 Compiler Lab Manual (1)

    76/82

      Chameli Devi School Of Engineering, Indore

    e can repea& &his process or an ar(i&rary num(er o s&eps A and ind all &he s&rin!s &he !rammar can

    !enera&e (y applyin! A produc&ions.

    So)rce Code

    ?Qprin$(>TEn$er $e s$ringUnT?Gi(>yyparse>?JJ0?Qprin$(>TVali)UnT?GWWyyerror>ar Ys?

    Qprin$(>TerrorUnT?G

    Lab Manual of Compiler Design Page 76

  • 8/18/2019 Compiler Lab Manual (1)

    77/82

      Chameli Devi School Of Engineering, Indore

    W

    O$#"$#

    fle= p/.lfya 5) p/.yf le=.yy. y.$a7. 5llf.a.o+$En$er $e s$ringaaaaaaaaaaa7Vali)

    f.a.o+$

    En$er $e s$ringaa7error

    Lab Manual of Compiler Design Page 77

  • 8/18/2019 Compiler Lab Manual (1)

    78/82

      Chameli Devi School Of Engineering, Indore

    "rogram R.:

    rie a C /rogram o im/lemen he &-naJ4direced definiion of if E hen

    S. and if E hen S. el&e S0!

    Y Inp+$ $o $e program is ass+me) $o 7e syn$a$ially orre$. #e e=pression o( i( s$a$emen$6(or $r+e on)i$ion an) s$a$emen$ (or (alse on)i$ion are enlose) in paren$esis Y

    Some programming lang+ages permi$ $e +ser $o +se !or)s li;e i(6 !i are normallyreser9e)6 as la7el or 9aria7le names6 pro9i)e) $a$ s+ +se )oes no$ on(li$ !i$ $e legal+se o( $ese names in $e programming lang+age. #is is e=$remely ar) $o )o in $e

    (rame!or; o( aG i$ is )i((i+l$ $o pass in(orma$ion $o $e le=ial analyer $elling i$ $isins$ane o( i( is a ;ey!or)6 an) $a$ ins$ane is a 9aria7le. #e +ser an ma;e a s$a7 a$ i$6+sing $e meanism )esri7e) in $e las$ s+7se$ion6 7+$ i$ is )i((i+l$.

    A n+m7er o( !ays o( ma;ing $is easier are +n)er a)9isemen$. :n$il $en6 i$ is 7e$$er $a$ $e;ey!or)s 7e reser9e)G $a$ is6 7e (or7i))en (or +se as 9aria7le names. #ere are po!er(+ls$ylis$i reasons (or pre(erring $is6 any!ay.

    10' A)9ane) #opis

    #is se$ion )is+sses a n+m7er o( a)9ane) (ea$+res o( a.

    Sim+la$ing Error an) Aep$ in A$ions

    #e parsing a$ions o( error an) aep$ an 7e sim+la$e) in an a$ion 7y +se o( marosACCE%# an) E888. ACCE%# a+ses yyparse $o re$+rn $e 9al+e 0G E888a+ses $e parser $o 7ea9e as i( $e +rren$ inp+$ sym7ol a) 7een a syn$a= errorG yyerror isalle)6 an) error reo9ery $a;es plae. #ese meanisms an 7e +se) $o sim+la$e parsers !i$ m+l$iple en)mar;ers or on$e=$-sensi$i9e syn$a= e;ing.

    Aessing Val+es in Enlosing 8+les.

    An a$ion may re(er $o 9al+es re$+rne) 7y a$ions $o $e le($ o( $e +rren$ r+le. #emeanism is simply $e same as !i$ or)inary a$ions6 a )ollar sign (ollo!e) 7y a )igi$6 7+$ in$is ase $e )igi$ may 7e 0 or nega$i9e. Consi)er

    sen$ ' a)j no+n 9er7 a)j no+n  Q loo; a$ $e sen$ene . . . W  G

      a)j ' #HE Q ff J #HEG W  h :,@ Q ff J :,@G W

      . . .  G

    Lab Manual of Compiler Design Page 78

  • 8/18/2019 Compiler Lab Manual (1)

    79/82

      Chameli Devi School Of Engineering, Indore

    no+n ' D@  Q ff J D@G W  h C8,E  Q i(> f0 JJ :,@ ?Q

      prin$(> T!a$UnT ?G  W  ff J C8,EG  W  G  . . .

    In $e a$ion (ollo!ing $e !or) C8,E6 a e; is ma)e $a$ $e pree)ing $o;en si($e) !as no$ :,@. 79io+sly6 $is is only possi7le !en a grea$ )eal is ;no!n a7o+$ !a$mig$ pree)e $e sym7ol no+n in $e inp+$. #ere is also a )is$in$ly +ns$r+$+re) (la9ora7o+$ $is. ,e9er$eless6 a$ $imes $is meanism !ill sa9e a grea$ )eal o( $ro+7le6 espeially

     !en a (e! om7ina$ions are $o 7e e=l+)e) (rom an o$er!ise reg+lar s$r+$+re.

    So)rce Code

    Ninl+)e Os$)io.PNinl+)e Os$)li7.PNinl+)e Os$ring.P

    in$ parseon)i$ion>arR6in$6arY6in$?G

    9oi) gen>ar R6ar R6arR6in$?Gin$ main>?Q  in$ o+n$er J 06s$len J06else(lagJ0G  ar s$m$R/0G on$ains $e inp+$ s$a$emen$  ar s$rBR4G ol)s $e e=pression (or i(

    on)i$ion  ar s$rS1R0G ol)s $e s$a$emen$ (or $r+e

    on)i$ionar s$rS2R4G ol)s $e s$a$emen$ (or (alse

    on)i$ion

      prin$(>T*orma$ o( i( s$a$emen$ Un E=ample...UnT?G  prin$(>Ti( >aO7? $en >sJa?GUnT?G  prin$(>Ti( >aO7? $en >sJa? else >sJ7?GUnUnT?G  prin$(>TEn$er $e s$a$emen$ UnT?G  ge$s>s$m$?G  s$len J s$rlen>s$m$?G  o+n$er J o+n$er K 2G inremen$ o9er i(  o+n$er J parseon)i$ion>s$m$6o+n$er6s$rB6s$len?G  i(>s$m$Ro+n$erJJ??

    Lab Manual of Compiler Design Page 79

  • 8/18/2019 Compiler Lab Manual (1)

    80/82

      Chameli Devi School Of Engineering, Indore

    o+n$erKKG  o+n$er J o+n$er K 3G inremen$ o9er $en  o+n$er J parseon)i$ion>s$m$6o+n$er6s$rS16s$len?G  i(>s$m$Ro+n$erK1JJG?  Q reae) en) o( s$a$emen$6 genera$e $e o+$p+$  prin$(>TUn %arsing $e inp+$ s$a$emen$....T?G  gen>s$rB6s$rS16s$rS26else(lag?G 

    re$+rn 0G  W  i(>s$m$Ro+n$erJJ??  o+n$erKKG inremen$ o9er ?  o+n$er J o+n$er K 3G inremen$ o9er else  o+n$er J parseon)i$ion>s$m$6o+n$er6s$rS26s$len?G  o+n$er J o+n$er K 2G mo9e $o $e en) o(

    s$a$emen$  i(>o+n$er JJ s$len?  Q genera$e $e o+$p+$  else(lag J 1G  prin$(>TUn %arsing $e inp+$ s$a$emen$....T?G  gen>s$rB6s$rS16s$rS26else(lag?G  re$+rn 0G  W  re$+rn 0G

    WY *+n$ion ' parseon)i$ion  Desrip$ion ' #is (+n$ion parses $e s$a$emen$

    (rom $e gi9en in)e= $o ge$ $e s$a$emen$ enlose)in >?

      Inp+$ ' S$a$emen$6 in)e= $o 7egin sear6 s$ring$o s$ore $e on)i$ion6 $o$al s$ring leng$

      +$p+$ ' 8e$+rns 0 on (ail+re6 ,on ero o+n$er9al+e on s+ess

    Y

    in$ parseon)i$ion>ar inp+$R6in$ n$r6ar Y)es$6in$ $o$allen?Q  in$ in)e= J 06pos J 0G  !ile>inp+$Rn$rJ > && n$r OJ $o$allen?  n$rKKG  i(>n$r PJ $o$allen?  re$+rn 0G  in)e= J n$rG  !ile >inp+$Rn$rJ??

      n$rKKG  i(>n$r PJ $o$allen?

    Lab Manual of Compiler Design Page 80

  • 8/18/2019 Compiler Lab Manual (1)

    81/82

      Chameli Devi School Of Engineering, Indore

    re$+rn 0G  !ile>in)e=OJn$r?  )es$RposKK J inp+$Rin)e=KKG  )es$RposJU0G n+ll $ermina$e $e s$ring  re$+rn n$rG non ero 9al+eWY *+n$ion ' gen >?  Desrip$ion ' #is (+n$ion genera$es $ree

    a))ress o)e  Inp+$ ' E=pression6 s$a$emen$ (or $r+e on)i$ion6

    s$a$emen$ (or (alse on)i$ion6 (lag $o )eno$e i($e else par$ is presen$ in $e s$a$emen$

      o+$p+$ '#ree a))ress o)eY9oi) gen>ar BR6ar S1R6ar S2R6in$ elsepar$?Q in$ B$ J1016B( J 1026Sn J103G prin$(>TUnU$I( [s go$o [)T6B6B$?G prin$(>TUnU$go$o [)T6B(?G prin$(>TUn[)' T6B$?G prin$(>T[sT6S1?G i(>elsepar$?  prin$(>TUn[)' T6B(?G else

     Q prin$(>TUnU$go$o [)T6Sn?G  prin$(>TUn[)' [sT6B(6S2?G  prin$(>TUn[)'T6Sn?G WW

    Lab Manual of Compiler Design Page 81

  • 8/18/2019 Compiler Lab Manual (1)

    82/82

      Chameli Devi School Of Engineering, Indore

    O$#"$#

    *orma$ o( i( s$a$emen$E=ample ...

    i( >aO7? $en >sJa?Gi( >aO7? $en >sJa? else >sJ7?G

    En$er $e s$a$emen$i( >aO7? $en >=Ja? else >=J7?G

    %arsing $e inp+$ s$a$emen$....I( >aO7? go$o 101go$o 102101' >=Ja?

    go$o 103102' >=J7?103'