(Chapter 1 - 4) Compiler construction (Elements of Compiler Design) by Dr. M. Joseph - Adobe Scan 18 May 2023

You might also like

Download as pdf
Download as pdf
You are on page 1of 58
fer KO. OGua des CH - COMPILER CONSTRUCTION | Elements of Compiler Design By be. M. Tevegl CONTENTS re ry 16 21 24 22 25 24 25 25 217 3.1 35 ‘Runtime Storage Organization ~ 34 7.2.6 Activation Records 35 7.2.7 Runtime Storage Allocation” fh orater 8 le ‘Symbol Tables and Errors 43 8.1 Symbol Tables 428 4 452 51 | pJehapter 9 52 7 Intermediate Code Generation 538 £92 posttix 579 | 9.2. symtax‘Trees Chapter 4 Syntax Directed Translations 6.1 Atebutes 6.2 Syntax Directed Translation 6.3. Synthesized Translation | 68 Inherited Tronstation {88 Translation of Parse Tree 6.6 Translation of Assignment statements 6.7 Translation of Boolean functions 6.8 Syntax Directed Translation for postfix chapter? ‘Type checking und Runtime Environments 9.3. Three Address Codes co 6h 6.1 61 63 64 64 6s 62 mM 7 75 73. 75. 75: 76 7 7 78. an aL a2 82 Ba 9.1 ol 91 95 9.4 Types of Three Address coves 105 Loops raph) 115 Feephole Optization Appendix A Questions Aopen B Questions CHAPTER 1 WORDS, SENTENCES AND LANGUAGES In the binning wus the WORD And the WORD war with God, (and the WORD was God. = The Be, A Language is won knowing les it teachet ye to dunk diferent + Larryvail and Randal Schwartz worn In singler way & Word can be detned as a set of characters. which ghes a mc For example: ‘oy’ a word defined trom the set of characters lor the alphabets avalable In English Language. 1 danctes masculine gender chi. SENTENCES A sanience Is sat of words which ghes a meaning. ngu208 Isa too used to communicate with others I ls defined over the Sentences in tum woud, whl wil hp us In understanding concept of compler Consincton. Coneier the folowing sertanc, In Engish 1AM GOING TO MARKET, Suet Tis simp sentence is Syrtactcaly, Grammatcaty coneet ‘Tam a1@ some sentences which ar not sytactcaly vorect, tor Example ‘The bo going to hast We can now understand the sentence should bo vatten amor fee, i sytecicaly canecat In broader sense ‘Tre wo eso some Sentences which are not Grammtelly correct, for Example The boy Is go 10 hostel {it he cowect sentence is ‘The boy Is gorg to hostel Tho above simaie analogs wil exp ‘The above dlecuslon can be interpreted Language -- Wore Semonce Set of sentences == Sat of statements Le a Program in a HLL To cenyout contain task a sentence must bo writen, which should sally ho folowing 1. Sytacticaly Corect, 2 Grammatically corect 43 An acon must alo be essocioted. Then 41 ust be understood by the Executor lor execution (must be made ready for execution. In a vey simlar way, the statement of @ program must slaty tho folowing In campier. LB 1, Syrtactealy correct (Loveal Anaysor) 2, Grammatiaty correct (Syntax Anayser) 3. An action must aso bo assacited| Syrtax Osected Trenton) 4. R must be understood by he Executor fr wacuton (Cove generation and Executon) (ete: There I another phase cated code oplimzation wich is an optoral hase of the compte The reader can relate the above analogy when learning the phases o the complet in the subsequent sections. ‘COMPILER Compl is program, which convers @ program wrtten in any High level language Into an Objoct Program, COMPILER (Source progr) ‘ransator can coment one HLL mo any ober HLL also But compler genraty comets ‘in in LLL orl.The Compled programs are efgibe for execution. So ¥ ts must fr ery ream to be comened ito 2 obec progam. INTERPRETER INTERPRETER Is also 8 program in which the code ot cate wich o2es in he cone memory of 8 machine ena Feed tov an Insruction of machine, which resides in the,cotrol. memory ol micro-progemiied ren. Tis ero code genres th cont! egal fer exocuton, The Compe framion Set Computer rormaly rns interpreter 11 PHASES OF A COMPILER ‘The block clagram shows various phases of the comple clearly Fig, 1.8 PHASES OF THE COMPILER LEXICAL ANALYSER SYNTAX ANALYSER This recelves vald tokens from the scanner st the Grammar and H5- paces te vad syria SYNTAX DIRECTED TRANSLATION(SDT) ‘SOT is a notational fame work, of the vaiks syrtacueal consiuets recewed trom the syntax anaysot. to make & sutabie for intermediate code gereration. This ataches actlone for res oF subroutnes 10 the grammer. INTERMEDIATE CODE GENERATION dite code generator receNes syriax dlected transated syreacteal corenrcts {elowing intermediate codes and ropasent thom in any one of the 1, Post 2, Syntax Toe ond 3. Thee actress code, CGuneraly tee address codes aro widely used intermediate codes. CODE OPTIMIZER The code optimizer receives Intermadiate codes and optimize tem considering the factors tke algortnm, memory, execution time, and others. by applying code improving transformations. This phase removes the redundant codes, ts an optional phase CODE GENERATOR 8 optimized Inter medlate codes and generates the code (or exact. ‘account the architecture of the mache, while generating the code The anvibutes may be type, value, scope table management. This can alsa be called as “book as code generator phases. Tis section can be calad as “Wont end” of the compiler, These 1p ace Lodependent_phases.__ ‘Te other two phases Code Optiizer and Code Generator can togethar constitute 8 pass. Generally tls ls caled pass 2. Theso phases are machine dependent. This pass 2 Is called "back ere of the compiler, = a Fig. 1D. Back End of the compier ‘21. the programming language, ike identies, keywords, constants and others. a4 | aie Apelet No-S sct Syrte Seay” See 4 ee cy flilobat real iy) 2, Poshive Closure: This 10 get one oF motw concaterations, ard i i roprosented by’ +. IWenifier names 3, ci ra LL! == ths wail gle one or mote corcatenations of the language L . words bn, float, cha. z x: a” == val generate ine folowing fanguage Constants 3.44,2000 444 ad tage Purcration marks. : 5 22 LANGUAGES AND STRINGS Tor Language can be defines as sat of sings, wich are delined over the characte se. i a “The cheater sot can bp caled as Alphabet and also represented by F. coer otal. The sing can be defined 23 INPUT BUFFERS aperations posse are concalpnation 13s tho program, charactor by charactor and generates vad ‘must bo stored tomporaily. Wo can say the ip sting must be the reacer, are dscussed here cHaMSGEE to make a decision wh tha Rap of an example, 24 TRANSISTION DIAGRAMS Ils a degrameatic representation tke fowchan (FC), used for lexical analyzers 10 recagnza tokens. In a transition diagram the folowing notations are talloned: 1. sates - represented by creles (simlar to the boxes in FC) 2 edges - used lot connecting the states (ilar 10 arows i FC) Re oly ioe thw protic of Ba rsp p stcempgs 25 REGULAR FXPRESSIONS(RE) Lica toe Kd o_ctaton we 0 ceea e amge wich um exes te loro Sin The regu expressons are deed ont the aphabat GE wx! to repre te cha st andl ko ced eaten. 1-7 Is the rogusr expression and (Is te arguage dencts by tat reguiar expression. 2. lubes - an Up character showing the transsion . Wo wil see some examples of reguar expressions. 1. gh? == Is tegsar exresson fr generating irpgor numbacs where it ——+ Wie wil see some examples for tanstion diagrams. 2c this is called regular ceintion. “I represens the or operator. (Not logleal \wanmission diagrams, i tho feguar expression for ganerating vaabio names. alte. end dit! 9}112}.-19. tha vegdarexpesson for goeaieg so or mote no concatenation of regier exzression for an empty sing, 2.6 FINITE AUTOMATA, : ‘The automata which satisy the following conditions can be called as Nencacre Atonat, th [em aan ene as : Pub THe General transition degre! for % fk o 2 Hore 0 - Is stat or idl stare 3 + is al of accopt state ‘The reader con understand trom the above graph that “2° con appear O instance or any number of instances in the language generated by the regular expression a” 262. Deterministic Finke Automata (OFA) ‘Tha fnte automata which satsly the folowing conditions can be called as Deteminisie fete agoraia 1 Thao shoud nave. one stat ste 2. These should have one of more aceopting ses 3 Thee cannot bee wanstlons posse and, an be only one transtion possible on a particular Yp symbol ao So 26.3 Conversion of RE into NFA ; The steps Involved are Usted below Here the folowing notations are sod 4 =~ ina sate 1 =~ final state (== regular expression N= NEA 1. For the RE, e, consiuct the NEA =O-© Fig, 263 2. For the RE a in 2, the NFA wit be = OO > Fig, 284 uw {Lea CONVERSION NFA INTO DFA : ‘This section discuss about tho constuction of DFA sols fom tho NFA, Before we ‘ascuss the consrucion of DFA ses. we wil see folowing things. 1. € = Closure of a state 1 2, € - Closute of a sot N 23. Move functions for a set N on an kp symbol ‘at and 4. Wanstion table Fig. 26.13 ‘The transiton table for a” is given below. cue L input a cal | 1 2 (1.3) 2 Fig. 26.14 2a The DFA = _&. Start ‘The tanshion tabio Is as shown below: ip symbat iF ny stato | in NFA. The G-clstra() wal Incude al the states which are roachable from on «It Includes | alse Example Consider the NEA for a” Cons he NEA a0, he «ees wl be fg neste acl trom 0 on €. © 10, 1.9) Let us Name this NFA sot as N, & - closure of N (Here N is an NFA set) 2a 285 STEPS FOR CONSTRUCTION OF OFA SETS : This cludes slates tha ere reachable cn © tom any state | in the sett Hore | == lia stare Example N= Any NFA stato D == Any OFA stato: —- an Up symbol 1. Fad & « closure () and Name N and mak he state (marking is tor Wontiaton ee) 4s tho first DFA state D. 2 2 Find € - closure of Move (0, a) and check whether Ris akosdy marked OFA oe sate re ten Name & Dy ard sso mark OTanO, ] = 0,18 is aanay ‘avalable then mark OTran that aoady avalable state ‘This includes athe slates. that are reachable rom any state iN on a — This step must be done for at + Rogar sepranion for @° Dp Dy... 0, # posse, ‘fp symbots for O anc! find the OFA states For any OFA sate D,, Dz Dy .. O, do the siap 2 for all the n up symbols MF “2 tie step 2 tor at P_symbo YOU fd no now OFA sates. OTren(D. al represents the OFA transtion of any OFA state O on the Vp a = 0, means, the OFA state D reaches D, on a Fig. 26:17 2 © Steps the reader wil be able to draw OTran. This is thing but 5 table mom ‘The above can be easly understood wih the help of an example, o ow pmol: Conver ta flowing RE io tw coraspondng NFA ar ao Di ihe Oran, ,— 2 RE: a ‘i Null nT) Let us say itis Nt ‘Now we can detaly discuss the conversion of NFA into DFA. This involves the construction of DFA seis from NFA : Fo ba ead aD Ala partring Leo atpoue lr Cy nd Cp. tray be soected as town ‘S2y (80) 1s « group, and A 8, C ave same Yanstions onal fp 6 Atay be slogan rereneiatve ind A wil repace a he 8. © 4 ‘4. The dead state may Bi Terral A out Goad Hite ea ‘any transitions, and has no tanstions on al the Yip symbols. “Trove ean be no more new stees {5 femve of the snes Tat aA Sa Let us draw the OTran a3 follows. __ Extmple + (Continuation of previous example) We wall an WP symbol : take an example for applying the above steps. Stotes ‘ 2 4 Vp : OTren A B O1P : Min OTran 8 8 saw ip ember Fig. 28:19 7 2.66 STEPS FOR MINIMIZATION OF DFA : x 8 al ‘is section discusses about the mothed for obtaing riimized OFA This is cole QO 8 in OTran. Tis wil result the OFA, we are intrested. , Fig. 2.6.20 ‘he collection of OFA states wo have obizined wil bbe considered a8 input and tho {As per the above steps f i ‘output \al be minimized OFA states. i bens ‘ine collection of OFA sites can be named as C. i 4» Up symbol a i 2s 2.7 EXAMPLES 5 = a8 = AB Both has firal states y+ Cp CH AB AB can't be luther diced, So A can be selected as a reprosertatve ‘A wil replace the occurence of B. So the Min OTran wil be Tp symbol 5 Stop 2: Finding the states of OFA € - closute (0) = (0) = A Move (A Fi. 2821 AE + Closure Move (A al) = (1, 24.9} = Be Move (a by = is} ; LE + Cosure Mowe (A. 0) = (5,6 8 9) = Co The OFA wil be : —@©®) : anaes ean oe a (Move (Bb) { ) This wil nolp in ring the OFA. Fig. 2822 Ls - coeur ovove (8b) = (How a) = 1) \ - Closura (Move (C, a)) = — ‘Now replace A by a Number 0. (Move (Cb) = (7) i S + Closure (Move (C, b)) = 16.7, 8, 9} = € ‘Move (0, 2) = {3} Fig, 2823, i L 27 26 “Ce - cose ove (0,9) = 0 Move (0. 8) = 4} Ce = Coste (Mov Mowe (Ea) = 1) Xe = coeur (Move Ean) = a. ‘Move (E. b) = {7} \e Cone (low € ) = - Step 3: DTRAN of OFA ‘Step 4: Minimizing DFA = aBcoe = (A) (B0) (CE) = A) @) (C) Step 5: Minimized OTRAN Tnput symbols ‘Step 8: OFA PROBLEM No, 2: Reguler Expression given is Step 1: NEA for (aibya:bialb) ‘Step 2 : Finding the states in DFA, - Closure @) = (0,1, 3)» A Move (A @) = 42} € ; Closure (Move(A, al) = (2.6.6, 8} = 8 Move (A, 8) = {4} € = Closure (Move, (Ab) = (4,86. 8} = C Move (8. a) = {7} 29 + Gesure @ove (8, al) ~ 17. 10, Move (8, 0) ~ {9} = Closure (Move (8. bi) = (9, 10, 11, 13} = E Move (C. 8) = {7 + Closure (Move (C, a)) = O Move (©, 5) = (a) € = Gosue (Move (C, b)) = E Move (0, 8) = (12) - Closure (Move 12, 15} = F Move (0. b) = {14} © - Closure (Move (0, b)) = {14 15) = G Move (E. a) = {12} € - Closure (Move (E, a)) = F ‘Move (E, b) = (14) € - Closure (Move (E, b)) = & Move F8) = () € - Closure (Move (F, a)) = — Move (Fb) = i} € - Closure (Move (F, b)) ~ _. Move (G, 9) = {) € - Closure (Move Move (6,0) = £1) € - Closure (Move (G, b)) = — 220 An oie vom ‘Steps 3: OTRAN of OFA 4 Minimizing OTRAN ABCDEFG (ABCOE}FG) (AVBCNDENFS) We Slop 5: Hn OTRAN, Taput Symbols at 0, ai) = 8 Ody ~ (24567 Lew ee PROBLEM No. 3 : Regular Expression given 's (a[b)" abb (2) >)" 28867111. A IT} = ‘stop 1: NFA for (a:bI"abb«aib)” 2456714, 12 18. 15,16 17} = 267811 19,18 158 T= 20 5,6.7.9.11, 12 14 15, 16.97) 2H Step 2: The states in DFA € - Closure () = {0,124.7} 7 A Move (A, 8) > (% a) = 1.234678) +8 by = (245.67) 26 an t 223 steps 3 + OTRAN of CFA ‘Slop 6: DFA input Symbols some Lee Serer x 5 S | a o-oTamo a : : © | : : ‘ ‘step 4: Minimizing OTAAN SSeS pe beqpad | pe trye : beriqiy | eee Stops 3 : DTRAN of OFA Step 2: Finding the sates in OFA © = Closure (0) = {0} = A States -6 ‘Stap 4: Minmiting OTRAN Move (B= = Closure (Move (8.1) = (2) = © Move (6. 0) = {) © = Closure (Mave (C. =. 3) Move (C1) » Taput Symbols - Closure (Move (©, 1) = 19.4.5, 7 10) = O Move (0. 0) = € - Closure (Move (0. 0}) = (4. 5.6.79 10} « E Move (0. 3) F - Closure (Move (0, 1)) = {4 5.78. 9. 10) ‘Step 6: OFA move (E. 0) = {6h € = Closure (Move (E, 0}) = E ‘€ = Closure (Move (F, 0)) = E Move (F, 1) = (8) - Closure (Move (. mek 226 Thea Move (C. 1) = PROBLEM No. § : Given Regular Expression is 4(0 | 123)" Step 1: NFA for 401123)" = Closure (MovelC. 1)) = j6} - 0 Move (C, 2) = Move (C, 3) = Move (C, 4) Move (E, 2) = {} Move (A, 0) = Move 2) = {9} Move (A. 1) = Move (A 2) = Move (A, 3) = € + Closure (Move (E. 3)) = (2.3.5, 8,9, 10) = F Move (& 4) = f Mow & 4) = 1) € - Chosue 1 ve (0) = 44 Move (8, 0) = ae - Closure (ove (@, 0}) = (29. 4,5.8 10) = © E - Closure Move (Fo) = & Move (@. 1) = (8) § Mow (1) ~ 6) e - Ciosure (ove (8. 1)) = (6) = 2 Rae a 229 228 Mowe (F.2) = (1 a Move (Fal ft 5 step 6 : OFA Move (F. 3) > start ‘Step 3: DTRAN of OFA 1 Tnpat eymbo States |. _. eae ce co nmooe> PROBLEM No. 6 : Regular Expression given is (12|543)111 Step 1: NFA Ip Step 4 : Minimizing OFA ABCOEF (aney(BCF}) Input symbols en i Step 2: The states in DFA are rote © - Closure (0) = (0. 1,4} w A MovelA. 1) = {2} E - Closure (Move(A. 1) Move @. 2) = (3) = Closure (Move(®, 21) Move (8. 3) = 15 Move (@. 4) = 1) Move (8, 5) = (1 Move (C= 1) Move (C. 2) = {} Move (©. 3) = () Move {C, 4) = {6) € = Closure (M Move (C. 5) = 1} Move (0. 1) = {9} € - Closure (Move(D, 1)) Move (0. 2) = { Move (0. 3) = (} Meve (0. 4) = {) Move (0, 5) = {} Move (E. 1) = {} Move (E 2) = (7 Move (€, 3) = {7} € - Closure (MovelE. 3)) C4) Move (€. 4) = {} 13, 8} = 0 (o} = & (hm F {7, 8} = G 232 Move (5) Move fF Move (G, 2) = {} Move (G3) = { Move (6. 4) = { } Move (G. 5) = { } Move (H. 1 € + Closure (Movet! Move (H, 2) = (} 1) -4) Move 3) = () Move (. 4) = {} Move (I. §) = {} ‘Step 3: OTRAN of DFA Sep 4 : Minimizing OTRAN (aBcoEFGHIM) macerHy(0<)0) ABKOEEONE ‘Step 5: Min OTRAN AAR MwA Move(A, a) = (3, 10) 2 i 2s € + Cloewe (Mow (8. a) = 8 Move (B, 8) = (8. 2) = Closure (Move (8, b)) = C Mowe (C. a) = {2 = Closure Move Mowe (C. B) = {8 = + Closure (Move (C. b) = C Steps 3 : DTRAN of OFA aay Step 4: Minimizing OTRAN (aac) “ ‘Step 5: Min OTRAN ing tho states of DFA, © = Closure ) = (0.1.24 7) = A Move(A, 0) = {3, 8) - Closure (ove (a, Movel@, 1) = (5, 15) + (245879) =O Moa(C, 0) = {9. 8) Gone (6, 0) = 8 4.5.6 7.10, 11,12 147) = E Closure (MovelE.)) = (1, 2,3, 4,6, 7.819, 12 13, 14, 16, 17) = F More 1) = (5. 18) (1.2.4.5,5. 7.10 12,15, 1416, 17) = G Minimizing OTRAN Mowe 1) » 15. ABCOEFGHI © Closure (Move (F, 1)) = {124.56 7.8 11, 12 14, 15, 16, 17) = H ee Moe(G, 0) = (2. 8 1 ee Closure (Move (G, 0) = F : eVORARE os i 2 Step §: Min OTRAN, i [PROBLEM No. 9 : Regular Expression glen ie (0}1)"1 Input symbole 1: NA 4 | suates — | | | | mz mmo> Stop 6: OFA is 245678) =6 b> Feng he sates in OFA © - Closure @) = 101.247) 2A Move (A, a) = 43. 8} = Closure (Move (A, a)) = (1.2.3, 4,678) = B Move (A. 8) = (8) € = Goosue (Move (A.B) = (1,2.4,5.6 7) = 6 Move (8, 2) = (3. 8) € + Gosue (Move (8, a) < 8 = Closure (Move (C, b)) = ¢ aa 28 bsp s: 2 atin. OTRAN of OFA Move (0. 8) = (9. 8) TRAN of DFA Step 4: Mirimiztion of OFA A8CDE (ABOVE) (acy@ove) (AC}BNONED BOVE) aH ; Ras CHAPTER 3 [START svMBOL: CONTEXT FREE GRAMMARS. One nontermina! in a grammar 6 secs ‘a J@. The other non pec lamevane. Tho citer nonderminis of he pomean pects the sings. cur frat, He page PRODUCTION RULE: “ESUS CHRIST CONTEXT FREE GRAMMARS(CFG): ‘The contest trea grammars are usehl in spacihing the ‘ogremming language. it also ealed Backus-NairForm GNA) description imply 09 called as & preducton clso. Brame: It can simply be cates a8 grammar inthe subsequent discussions. : co A Grammar has our qunitles namoly Tern: 1. Teminal 2 Non Termin Nomterials: 2A Stan Symbol er ‘An mame for simsie arthmetc expression is gen below: g-- 8 E--ee |W can alto sey ths B+ ee Ex: ET. F and otha. e-see Genera the nonterminals represented by the uppercase ltrs bs r Here there are TERMINALS: | Hae tars a We productions, These are the symbols which ore used to any language in sing! Tomioals at the terminals ae mak: twrms the tokens can be called as terminals. We ca varlous syntactical constructs. oo Ex: while, do and others. i : Generally the terminals are represented by lowercase letters. san Symbol : : e ut q 32 ‘other hand the terminals does ot dere any string (These is can be reduced 10 any non-erminal. These are called, We can understand the dorvaions and expansons by the towing example: 4 E-- E+E £-- €E e-- derivations of the grammar for the string “id+id*id* is given as follows: E> E+E (Sipansion by E ~~ E+E] E =~ E+E [Gxpansion by E -~ E*E] == wserE [Expansion by —~ id} == W+K"E [Expansion by E -= Ht] Fg aetna (Bunion by E = 1] "Similarly the string “id+Id*id" can be reduced to the starting ‘non-terminal “E". W+id7ig" £ == W+W'E [reduction by E -+ Id} E+ W+E*E [reduction by E -~ 1 J E+ EFE*E [reduction by E ~~ id) E-+8+E {reduction by E -+ EE] EWE [reduction by E ~~» E+E] 33 } Tw atovo cehaton can be gaphcay Eo melas Expansion by © = | a | TA | ios al lal 4 Tho tree in the above figure is called the parse tree. 34 33 AMBIGUOUS GRAMMAR 412 Grammar produces more than one can be calea as ambiguous grammar. TMs can expained wih tho help ofthe flowing example Te CG is = G: E-+ Ese Ese f+ sting given is * 13d +i can be two arse trees drawn for the above Up sting as gen below. /N | i ' a id Parse t00 > Fig 28 parse vee 1 (Carded rom the producion E —- €*€) Parse Tree 2: Fig 20 Parse wee 2 for the sams ip suing from the above CFG. So we mbiguous grammar, be parsed by certain type of parsersidiscussed in deta because of ke ambiguous rate. Its ackisable at the embigity eloro parsing The ambiouty can be elniated by dsambiguatny hae ‘ejected for parsing Dove case the vad perse tea Is given below: JN JN — a—-m @ Fig 4. Valid Parse toe, ‘Tis parse too sald to be val because 8 sts the generly accepted nies. Genery the camutors ar done om tho tam o he ee. The oper * tas her pty Sperator +. So the mutipication must be done frst In the above tse these rues are Satslled. We can select this as valid tee for parsing and reject tho other tree 36 z PNA trea hatanan se ieinenepsininn ou PARSERS Lappy isthe man who getedh understanding + THE BIBLE : TAX ANALYSER: i receives valid tokens from the scanner and checks them against the Grammer and ‘he valid syntactical constructs. This ls also called Parser. i err RECURSION: | gtr i earphone x neste as follows _ : An» 06 ersone ip cong wtf Wa pee! ebol (tre 0 rortomina! is rectly clad eI, Yopdowm pace cat ace uch wh rset Tis mst bn Tiles ble Bia tlonag met tt recuse grammar : Ans ai Lett econ ination: aa w= wee mere KY ore grammar symbols and € Is epzton We wll S60 an example fr the above: Lat recuse grammar: €-+ e+tit pay. the above nue: Hoa Ais E:X bs 41: Y 1ST: So aller loft recursion elimination: e--1E Now tr 4 coro wih is oe alate fre any png sg wh | eg gars be erie er Nabe) ' 5 OR eYez ora ames hs rarest 4.1 PREDICTIVE PARSER: ; aan aan eae in NOW RECURSIVE PREDICTIVE PARSER ganar symbol iv is'a top-down parser. As a name implies Kis not recur Tis noes ho ftowiog # Steps ined in computation of AST ; 1. It X Is @ terminal, then FIRST (X) is X tol 1. UP Butler Eo: Frsfa)~ (i) ad 1X ls a nontormingl, then FIRST(X) is sot of terminals derived from X. 4. Parsing rouine and parsing tblo Eo FIASTIA) and F = (€) Tete Salk ep aes i ae a eed So FIRSTIF) © {() 2 m5 Th wed Yo ante at pa tig tre, Ti [21 1X een STON wa neat 5 right end marker. 3 Consider @ production a Ftv sack b aon says te_geamnar sbi The ganar sb wi | ‘ethet non-terminals or terminals. Initially the ‘ack Is pushed with °S* on the top of the Sone F sack Ater iat, a8 parsing progresses the grammar symbols are pushed. This Sis wal, ae la-amaunes the corbpletion of parsing, { “a t “4 X= i i hero FIRST@) = {a} FIRST(Y) = (c} ta pratow aso) = fe = i Thay is can dove fe 3 rotten Wich sles the above cones. t 4. It there Is @ production"X + Y then FIRSTON since FIRST(L) aS discussed in the above section. ve wit compte FIRST ke slow gna oo ce: = ula A= 18" one FASTEN ase Then Bae i whatever In FOLLOW(A) wil be in FOLLOW(E). ores “ The anove gaara be ton a i Lean eee i =, SL-Yeanstr ts one alo nt = tre : FOLLOW} wi Pave te toms esl FOLLOW) bi, St" ] ‘suppose FOLLOW( + poe f ‘Then FOLLOW(L ’) fy ving the supa. he FAST canbe coated 6 lors tag 13 CONSTRUCTION OF PARSING TABLE T [As discussed already It is goreraly @ two dimensional array It has rows as Non-Terminais, £ ard columns a8 terminals, tho symbel ‘Sis used as the last colima. Tha parsing routine wore in conjunction wih his tabla. FIRST(L) £ Steps involved In coneteuction of parsing table T = 1. production A = X then do the folowing steps 1.1 For each tarminal “at in FIRSTOQ, add A 412 COMPUTATION OF FOLLOW FOLLOW determines the set of terminals which can appear Immediately ater 2 grammat symbol. FOLLOW in simpler words, set of terminals which follow the particular grammer T (A. 2] (is « grammar symbol) : i = I Steps involved In computation of FOLLOW i 1.2 FIRSTOQ| has, then foreach tinal bin i FOLLOW), add A X 10 T [A b} 1. WS lee staring Non terminal, than FOLLOW(S) wil have “S" In ts FOLLOW 15 JLFIRST(X) has « and '$"is in FOLLOW(A), Eg: FOLLOW(S) = {S}; $i a start symbol then add A> X to T (A, §) 2 WA» xe Where A B oro non terminals and X, Y are grammar symbols, which may bes: te eee Make all the undefined enties as eros. then FOLLOW(G) © FRSTIV) except « : . 4s “rte, =e; tn ne gue san wer ene dea et Sato (my JHC t We wil see an example here under. . > a eb «1 toung gan, a coat ho rcv pang wa eect wrabr the vp stg “a, aj is succassly persed O10. Loss oy 2 Sa ieee caring roniemicl (ie sat symbol wl be speed the grammar poco th I hand aie nomtorinal of the tt production wll De ‘he ord proutan poses lo raeurson The fo recur pat cane eis ‘ts cuamed antl. Tow productions ator emiraion f fet recusion are Hed blow: { hero -(@) step for FOLLOW : i ¥ ‘Check any possibity of applying the steps to all othor productions to find FOLLOW(S). (®) Consider L' =, SL" ‘Apply step 2 pS A= xe FOLLOW(E) wil have FIRSTIV) except « simiar 10 A = xBY eae bes st" FOLLOW (A) wi be in FOLLOW(G) 5 FOWOWL) © FOLLOW! 6 $0 we have to find FOLLOW) and Include In FOLLOWIL will be in FOLLOWS) er the above discussions ard (0) fever In FOLLOW(L) wil be FOLLOW(L.’) and those wil be in FOLLOW(S), seu rly step 2 Aaxey bare FIRST(Y) wil be In FOLLOW(B) except « : s-u 49 Construction of Parsing table It Is a two dimensional erry having non-terminals as rows and terminals as columns. FIRST( ) ) wl be in FOLLOW). FIRST() ) is {)) Deceuse i I « termina = FOLLOW) a (@) = FOULOWL) = FOLLOWL FOWOWIL © {1} As per alscussion (0) Wnatover ls FOLLOW(L? wal be FOLLOW(S) e Let us summarze the FOLLOWS of the grammar G as ghen below FOLLOWS) = (8. I) FOLLOW) » FOLLOW) FOLLOW = 0} Is included as the last column. In this case the non terminals are S, LL and the torminats are ans ‘Therelore table (1) wil bo as shown below : Now the fiting up of entries in the Fig 4.1. Parsing table Format les next task, a0 v opty step 1.1 in construction of parsing table 8 a, crust be entered 10 T {5, a] becausa A = X must be entered to TA. al for at te terminals in FIRSTIA) ‘Skmiary $= (4 for ho entry TIS. (1 ‘Aoaly stop 1.2 1 FIRSTOQ) has € then for each taxminal ‘B's FOLLOW(A), Pop a oe Hack and adeancn Wp povaar 2 FOLLOW(S) = {5} and A does not havo ‘in FIRST(S). a 1 : sop E. simarty check for the possible applications of steps for construction of parsing table das j Push with b*, SL since the entry TIL%. 4) Since TU] = L' =, SL>. step 3 Consider FIRST) = {6 a) sss Jays [pop 'y' of trom the slack and advance Yip L+s pnt, stop 2 Ce Nee aS ae Bah wit Search wey = S= a steps a a8 op "a" from the stack and advance the Up z pointer, stop 2 { syle Ss j push l= esme TL. ))=L> Fray mato at he unlined ets 88 eer. pS ee an rH j j } | ‘| mak ets. i sibs | i : i ~ (ris probe Is very sinter Yo tbe provous appeabe to this 380) ompuaion of FOLOW FOUOWS) = {5 9) rouow FOLLOWIT ‘) ‘problem So all tho dscussions wal bs Te Tha given Up sting Is soccesstaly parsed. ning ble: ae ap a | t z eae predctve purer on lop (ana) T phase te et os ot 2a EE || = trad eres we roe na 120 Fig. 414, Parsing table EG ‘Output Tm a * TF oan gn ne anes ee ete ito ores made by prodicive parser on the Input sting (a, 1. 0) Tae Teal ang] Opa - | zi a 1 om oT [ee a [ae Tr are waa = ssh = vi sg 1 @ ta) imader can check whether the Up sings are successtuly parsed of not 4413 Corader the folowing grammar 6 Ese TIT TTF LF F-@le fonsiuct the prodcthe parsing labo Els the stat symbol Jot recursion for E & T, so fo recursion must be olminated. lot recursion elimination E-te Ety Tete TF Tate ec Fei CCompation of FIRST FIRSTIE) > FIRST) = FRSTEF) = FIRSHIE') = ite} FIRST) = These. ‘robably clear to the readers inco already two examples have been discussed. Computation of FOLLOW (Retor the steps in section FOLLOWE) = (s 0 since Eis the stan symbol + step 1 Consider F Here FIRST; FOLLOW (E) excepts step 2 FIRSTO) = {)) FOLLOWE) = (5. )) Cy consiger Este Everything in FOLLOW (€) wil be in FOLLOW (9 + sep 3 FOLLOW, ) e consi Beate Everything In FIRST(E:) val be In FOLLOWT) except ¢ step 2 FOLLOWM = f+, E> Te: matever in FOLLONE } wil be in FOLLOW re readers can try with varlous Up stiegs, and can understand Hew parang ( Cone { FOLLOWM = 1+.) St * ys dias cons mee a tea ae wWnatever in FOLLO' il bo FOLLOW(T ) - step 3 oie Fouowt) = { a sume ina sings conser ! pen 1 8 ; ering FAST wl boi 2 dtd 3 i ler the reader's comariance the parsing moves are shown for some Up sinngs. vp string Output push wih € = TE* since TIE. wl} > € ~ TE” fig 4.10 moves in parsing “i [ep string | ‘Output ; = | ‘There no enty i tho Lele parsing cant bo done. | The Up stnng Is levab. fig 41%, parsing moves for “is" Fig 41g Parsing tabla, Tre glen ast by peo Fs ” fap! pLgaga' be ay : : F bump a4 ——— | f £ i i RST (6) = FIRST (A) = (a,b) Ao 36 Aue t Ne: 3-48 Ecvenyting In FIRST (S) is In FOLLOW (A) except E uoMA) = feb ue (@) Aa= 8A onfting fn FIRST(A) Is in FOLLO' FOULOMS) = (8 a. b ue @) & Aue (1) FOWOMS) = (8. 2D) FOWLOWA) = (a. bb Slnco the cols MIS. b] M has 2 entes the guen grammar is not @ ULi}) grammar Pars uty Graumans 4 Sama: Shove poring ttle) Fas no mily delned ers, can be called as UL) grammar For example the tee grammars discussed sbove are UL(!) grammars ‘The folowing grammar is not of LL) since fi ambiguous in natuze. $= 18s Ss sale cob (the wader an uy an prove the above gama pot of LH) The grammar 8 -- cc c-- cue ‘The rules are { 8 -= 00 c+ ec Cer Fest? : I t i FIRST(S) = FIRST(C) = {, d) Rue (3) & Rul (1) FOLLOW" : FOLLOWS) = {s Rule (1) FowLomc) Rule 3.1 Moves made by predtne parsar on the musing "485" { s-=cC i i everthing FIASTIC) wit be FOLLOWIC) Pue 2. FOULOWe) ~ [S. & et : t FoWows) = {st i FOLLOWO) = (5. 6 oD eS Tie geing cant be Wher reduced. The parsing cannot be suecessuly done ‘ho parser encounters 2 erat parang table : The rues are interpreted iferenty 4a SIMIFT - REDUCE PARSER 7% reader can role reduction and dedation discus (Ghepter 31). Shits and reductions aro done 19 va the process of constrscoan of Bavom-up parse Wee, for Ine undefined entry of Mare von Grammar Is LL) gF=mnmar, Moves mace by pradicive parser of ded = Consider the fotowng grammar § == CC, cna end Consider the Up sting = eded Let us aoe how the Hip sting i6 reduced tothe stating Nonderinal Roa S wang > ote® sc 2 ch@) Ratton by © -* -6 sa | 4s i = @e Redivatlon by © -* oC Th pats suncesly ped -@c ~ Redueton by © —= 4 a -@ Seca cere rene ee 1 tp sings can beater succes of rot eucaniy pare Hee ee ey can gman ha sre wag 8 Hck HHO. ‘Sitrty sits end re Bolore wo further oi undies ve nanae ot + wings 2(b5009) reaitiion helps Tm coreiucting the parse vee oF right ro Example: A = axd Jacuss we wil see about hanctos. x--e ore ‘ets 8 nandt, t reduces to X, and helps in consurucsion of the Parsa Wee the Up string. eduction by X = © eduction by A -+ ax Example G: E--E+E e--4 up siting : id + 1d + IS the right most derivation. E-+ E+E e-+ E+ E+E e--E+e+s e--E+i+ id E--W+ +d Bottom-up parsing Hore ‘kt’ Is the handle. 425 the shit-reduce parser smmar symbols. and the stack 1s intialy pushed wth Li also 'S' is used ag the night end marke STACK Implementation of! the terminal $. ‘The Up buffer ts keeping the Up sting. Here ‘he parsing actions Is the SA parser wil be as flows = gas © (Reduce by a nonterminal. This =a [2 Reduce ronerminal must be pushed 1 tack, By replacing the handles) eS 3, Accept is val and parsing 's ‘cessfully done) “ Eror ‘yrtax error, parser calla recovery routine) We wit see an example how the stack Implementation ts dono for the shi-reduce parser. Grammar G 8 -~ co c-~ ct c-+4 426 s@ i 3s | Fesiee ovo == oo | | ca rs | om se@ i $ | Reduce by C -= 6 = | $ ‘accept parsing is suceesstuly done Fig. 42A Shit - Reduce Parser The reader can understand how we reached the staring nonterminal, by ust stack Irom the above example. 42.1 EXAMPLES Stack Implementation of Shit Raduce Parser (SRP) 1. Construct shin Reduced Parser 1 ‘made by the parser for Input strings Ow rir Midis and the grammars E 2 ES & E--EtE E-+ id following Grammar & show the move i i i i i : eS ee eee [Stack Implementetion of shin reavce parser for Wl + Ad * id T Sick Tapa ang ran rs Tre sa sd ees Reduce by E--id s snes sna © as fn seo ves atue by Eis see 1s i = see Ps = : s Reduce by E-=d see ‘ fucuce by E-=E*€ see ‘ ase oy ents | sé | s ‘The simng is sucosstuly | 2. Stack implementation of shit reduce parser on Input string Id + id - ld Tp Bing ation 1 wees oa sess | Raduce by E-=id 8a § svi wuss sna “4s Reduce by E-~id “4s shit 4s Shit 8 Reduce by Eid s | parse cart be | succestay done, a eet wea 8 sacs elena 40 er Sino. ‘Stack Reduce by E-=id shit Reduce by E-~E+E : 10. sce) Reduce by E-+(E) pel se | shin wz] se she 13. SEN Reduce by E~~1d «| see i shit 15, ‘S(ETE) Reduce by E--E*E 16. ‘s(e) Reduce by E-~(E) ” SE | te vp sting is successtaty parsed eee ty i ee ; ns 3 Reduce by E~~E*E ‘The string can't be further reduced the enor te ait eduee pat ote faving Grammar and show the mover uci cass of grammars calad operator sce by a paar for the imput SENDS. pede (9 ose oto goat ‘operator grammar g-2cc monsee MCW 1 a gama sass he fcwing conten a an be cand a OPES premmer. £ € eruaon of shit reiuce parse for Input 9 eae 1. No production rule should have 7 on as 4 no rducon restos nave to aoucent Nomar CC sample Redee by C—=d L ewEre 2 SabeR & SU EserE s-A s-a shit E> Leta L-us shit Lew Ls aot Reduce by C-=6 Reduce by C-=cC edves by $=*00 ‘The Up sting s successtuy parsed | Reduce by © =e 1 | \ yammar because the Nemterminals €.0P are adjacent ‘grammars which are not OR ere glen below shit reduce parser on input sting dade 1. $+ AS“ Two Nonterminais adjacenty placed ‘Stack implementation of ‘ation oa sab Reduce by C—=d A= SA shit Asa Reduce by C-=d cc) Teo Nocioni ecacerty loco’ Reduce by $00 aa G+ Reduce by C--=-d cra shit ‘The sting can't be futher reduced, £0 the parsing cant be further done, re three operator precedence relations and. : ee a cia enne Urey are represerad by b lor ab Oi ‘TRAILING( ) ) ) (> w ree aby re % Hore () are trmicals and i a Nontermina, then set (=) Precedence Table ‘The Pracedence - relallons exist among the terminals of any grammar G, can be } cconvenlently represented in’ a tabla called procedence tabi. Thi precedence - relations among the terminals of G are cisjolt oF nol, can be found by constructing the precedence table. era “The above discussed precedence relations can be represented ina table cated procedenc table. The precedence table for the above discussed example Is given below. 49 Let us pe Ming = Ws i po 0 tw sco leer ‘be ping ad Input end marker. I os os vatie nT Me S8°k Is pushed with §. The parsing ‘Steps for Operator - Precedence Parsing TS tes eae Te . rm tt nt ioe tee pce dat 1 aoe © res) ee ee 3. tPe> ¢ tnen:Redue Ga) p05 01 sym ne bac! sean ‘eaches <, tom tbe sab (Un ist synod ls mechs). ‘Announce error it undelined erty encounters. seuss parsing with:e simple example. (Reler fy 42; Precedence tbe) Up . Sak Tp aig Fig 438 Parsing tho lip sting 0 iy S~t-R san Lerr low AaL ‘Al undefined enties are errors : a ra E+EtE Es Finding LEADING & TRAILING Computation of precedence relations Step 1 = seet LEADING(E) = {+. *, 1) TRAILING) = {+, *, i) jt sh | ean>

You might also like