Top-down vs Bottom-up. Bottom up parsing. Sentential form. Handles. Handles in expression example

Size: px
Start display at page:

Download "Top-down vs Bottom-up. Bottom up parsing. Sentential form. Handles. Handles in expression example"

Transcription

1 Bottom up prsing Generl e LR0) LR LR1) LLR o est exploit JvCUP, should understnd the theoreticl sis LR prsing); op-down vs Bottom-up Bottom-up more powerful thn top-down; Cn process more powerful grmmr thn LL, will explin lter. Bottom-up prsers re too hrd to write y hnd ut JvCUP nd ycc) genertes prser from spec; Bottom up prser uses right most derivtion op down uses left most derivtion; Less grmmr trnsltion is required, hence the grmmr looks more nturl; Intuition: ottom-up prse postpones decisions out which production rule to pply until it hs more dt thn ws ville to top-down. Will explin lter 1 2 Bottom up prsing trt with string of terminls; xmple: Build up from leves of prse Grmmr: tree; pply productions ckwrds; When rech strt symol & exhusted input, done; hift-reduce is common ottomup technique. Notice the lue d should not e reduced into B in step 2. rm Be rm de rm cde rm cde How to get the right reduction steps? Reduce cde to : cde cde c de d e d e B e Be Be c B d ententil form ententil Form ny string tht cn e derived from non-terminls. Cn consist of terminls nd non terminls. xmple: ententil forms:,,... Right sententil form: otined y right most derivtion entence ententil form with no non-terminls; is sentence. 3 4 Hndles rm Be rm de rm cde rm cde Be c B d Informlly, hndle of sententil form is sustring tht cn e reduced. c is hndle of the right sententil form cde, ecuse c, nd fter c is replced y, the resulting string de is still right sententil form. Is d hndle of cde? No. this is ecuse cbe is not right sententil form. Formlly, hndle of right sententil form γ is production β nd position in γ where the string β my e found nd replced y. If * rm αw rm αβw, then β in the position fter α is hndle of αβw. When the production β nd the position re cler, we simply sy the sustring β is hndle. 5 Hndles in expression exmple int * int ) Conser the string: int * int int he rightmost derivtion rm rm rm int rm int* int rm int*int int For unmiguous grmmr, there is exctly one hndle for ech right-sententil form. he question is, how to find the hndle? Oservtion: he sustring to the right of hndle contins only terminl symols. 6 1

2 hift-reduce prsing Brek the input string into two prts: un-digested prt nd semigested prt Left prt of input prtly processed; Right prt completely unprocessed. int foo doule n) { return int) n1 ; } hifted, prtly reduced o fr unprocessed Use stck to keep trck of tokens seen so fr nd the rules lredy pplied ckwrds reductions) hift next input token onto stck When stck top contins good right-hnd-se of production, reduce y rule; hift-reduce min loop hift: If cn t perform reduction nd there re tokens remining in the unprocessed input, then trnsfer token from the input onto the stck. Reduce: If we cn find rule α, nd the contents of the stck re βα for some β β my e empty), then reduce the stck to β. he α is clled hndle. Recognizing hndles is key! ccept: is t the top of the stck nd input now empty, done rror: other cses. Importnt fct: Hndle is lwys t the top of the stck. 7 8 xmple 1 Prse tck Remining input )$ Prser ction hift prenthesis onto stck )$ hift onto stck )$ Reduce: pop RH of production, push LH, input unchnged) )$ Reduce: )$ hift right prenthesis ) $ Reduce: ) $ Reduce: $ Reduce: $ Done: ccept Grmmr: > > > ) rm rm rm ) rm ) rm ) Input string: ) hift-reduce xmple 2 Prse tck Remining Input ction ) ) $ ) $ ) $ ) $ ) $ ) $ ) $ ) $ ) $ $ $ $ $ hift hift Reduce Reduce hift hift Reduce Reduce ; Ignore: ) hift ) Reduce ) Reduce Reduce ccept ) Input: ) ) ) ) ) ) Note tht it is the reverse of the following rightmost derivtion: rm rm rm ) rm ) rm ) rm ) rm ) 9 10 Conflicts during shift reduce prsing Reduce/reduce conflict stck input... ) Which rule we should use, or? hift/reduce conflict iftt if ) if ) else stck Input... if ) else... Both reduce nd shift re pplicle. Wht we should do next, reduce or shift? LRK) prsing Left-to-right, Rightmost derivtion with k-token lookhed. L - Left-to-right scnning of the input R - Constructing rightmost derivtion in reverse k - numer of input symols to select prser ction Most generl prsing technique for deterministic grmmrs. fficient, le-sed prsing Prses y shift-reduce Cn mssge grmmrs less thn LL1) Cn hndle lmost ll progrmming lnguge structures LL LR CFG In generl, not prcticl: tles too lrge 10^6 sttes for C, d). Common susets: LR, LLR 1)

3 LR Prsing continued Dt structures: tck of sttes {s} ction tle ction[s,]; tle [s,x]; X N In LR prsing, push whole sttes on stck tck of sttes keeps trck of wht we ve seen so fr left context): wht we ve shifted & reduced & y wht rules. Use ction tles to dece shift vs reduce Use tle to move to new stte Min loop of LR prser Initil stte 0 strts on top of stck; Given stte t stte on top of stck nd the next input token : If ction[t, ] == shift i) Push new stte i onto stck Cll yylex to get next token If ction[t, ] == reduce y Y X1 Xn) Pop off n sttes to find u on top of stck Push new stte v = [u,y] onto stck If ction[t, ] == ccept), done! If ction[t, ] == error), cn t continue to successful prse xmple LR prse tle tte on O ction ) $ ccept 2 R2 R2 R2 R2 R R4 R4 R4 R4 R ) 2) 3) ) 4) 7 R3 R3 R3 R3 R3 8 R1 R1 R1 R1 R1 If ction[t, ] == shift), Push new stte ction[t, ] onto stck, Cll yylex to get next token If ction[t, ] == reduce y Y X1 Xn), Pop off n sttes to find u on top of stck, Push new stte v = [u,y] onto stck tte on ction O ) $ ccept 2 R2 R2 R2 R2 R R4 R4 R4 R4 R R3 R3 R3 R3 R3 8 R1 R1 R1 R1 R1 tte stck Remining Input Prser ction 01 $ ccept We explin how to construct this tle lter )$ hift 4 onto stte stck, move hed in input )$ Reduce 4), pop stte stck, goto 2, input unchnged )$ Reduce 2), goto 1 )$ hift 5 )$ hift 3 )$ hift 4 sw nother ) )$ Reduce 4), goto 2 )$ Reduce 2), goto 6 )$ hift 7 $ Reduce 3) ), goto $ Reduce 1), goto 1 * 1) 2) 3) ) 4) ypes of LR prsers LR k) LR k) -- imple LR LLR k) Lookhed LR k = # symols lookhed 0 or 1 in this clss Drgon ook hs generl cses trt with simplest: LR0) prser LR 0) prser dvntges: implest to understnd, mllest tles Disdvntges No lookhed, so too simple-minded for rel prsers Good cse to see how to uild tles, though. We ll use LR0) constructions in other LRk) prsers Key to LR prsing is recognizing hndles Hndle: sequence of symols encoded in top stck sttes representing right-hnd-se of rule we wnt to reduce y

4 LR les Given grmmr G, entify possile sttes for prser. ttes encpsulte wht we ve seen nd shifted nd wht re reduced so fr teps to construct LR tle: Construct sttes using LR0) configurtions or items); Figure out trnsitions etween sttes Configurtion configurtion or item) is rule of G with dot in the right-hnd se. If rule XYZ in grmmr, then the configs re XYZ XY Z X YZ XYZ Dot represents wht prser hs gotten in stck in recognizing the production. XYZ mens XYZ on stck. Reduce! X YZ mens X hs een shifted. o continue prse, we must see token tht could egin string derivle from Y. Nottionl convention: X, Y, Z: symol, either terminl or non-terminl,, c : terminl α, β, γ: sequence of terminls or non-terminls et of configurtions X YZ mens X hs een shifted. o continue prse, we must see token tht could egin string derivle from Y. ht is, we need to see token in FirstY) or in FollowY) if Y ε) Formlly, need to see token t such tht Y * t β for some β uppose Y α β lso in G. hen these configs correspond to the sme prse stte: X YZ Y α Y β ince the ove configurtions represent the sme stte, we cn: Put them into set together. dd ll other equivlent configurtions to chieve closure. lgorithm lter) his set represents one prser stte: the stte the prser cn e in while prsing string. rnsitions etween sttes Prser goes from one stte to nother sed on symols processed X YZ Y α Y β Y XY Z Model prse s finite utomton! When stte configurtion set) hs dot t end of n item, tht is F ccept stte Build LR0) prser sed on this F Constructing item sets & closure trting Configurtion: ugment Grmmr with symol dd production to grmmr Initil item set I0 gets Perform Closure on ht completes prser strt stte.) Compute uccessor function to mke next stte next item set) Computing closure ClosureI) 1. Initilly every item in I is dded to closurei) 2. If α B β is in closurei) for ll productions B γ, dd B γ 3. Repet step 2 until set gets no more dditions. xmple Given the configurtion set: { } Wht is the closure of { }: y rule 1 y rule 2 ) y rule 2 nd 3 y rule 2 nd 3 1) 2) 3) ) 4)

5 Building stte trnsitions LR les need to know wht stte to goto fter shift or reduce. Given et C & symol X, we define the set C = uccessor C,X) s: For ech config in C of the form Y α X β, 1. dd Y α X β to C 2. Do closure on C Informlly, move y symol X from one item set to nother; move to the right of X in ll items where dot is efore X; remove ll other items; compute closure. C X C uccessor exmple Given I= {,, ), } Wht is successori, )? move the fter : ) compute the closure: ) ) 1) 2) 3) ) 4) Construct the LR0) tle Construct F={I0, I1, I2,..., In} tte i is determined y Ii. he prsing ctions for stte i re: if α is in Ii, then set ction[i, ] to reduce α for ll inputs if is not ) If is in Ii, then set ction[i, $] to ccept. if α β is in Ii nd successorii, )=Ij, then set ction[i,j] to shift j. is terminl) he goto trnsitions for stte i re constructed for ll non-terminls using the rule: if successorii,)=ij, then goto[i, ]=j. ll entries not defined y ove rules re errors. he initil stte I0 is the one constructed from. teps of constructing LR0) tle 1. ugment the grmmr; 2. Drw the trnsition digrm; 1. Compute the configurtion set item set/stte); 2. Compute the successor; 3. Fill in the ction tle nd tle. 0) 1) 2) 3) ) 4) Configurtion set uccessor I0: ' I1 I1 I2 ) I3 I4 I1: ' ccept dot t end of rule) I5 I2: Reduce 2 dot t end) I3: ) I6 I6 I2 ) I3 I4 I4: Reduce 4 dot t end) I5: I8 ) I3 I4 I6: ) I7 I5 I7: ) Reduce 3 dot t end) I8: Reduce 1 dot t end) Item sets exmple 29 I 2 I 0 rnsition digrm ' ) I 1 ' I 3 I 4 ) ) I 5 ) I 6 ) I 7 I 8 ) ) 30 5

6 he prsing tle tte on O ction ) $ ccept 2 R2 R2 R2 R2 R R4 R4 R4 R4 R R3 R3 R3 R3 R3 8 R1 R1 R1 R1 R1 31 Prsing n erroneous input tte stck Input Prser ction 0 $ hift 4 0) 1) 2) 3) ) 4) 0 4 $ Reduce 4), pop 4, $ Reduce 2), pop 2, $ Push $ No ction [5, ] rror! tte on ction O ) $ ccept 2 R2 R2 R2 R2 R R4 R4 R4 R4 R R3 R3 R3 R3 R3 8 R1 R1 R1 R1 R1 32 uset construction nd closure LR0) grmmr I 0 ' I 2 I 0 ' I 1 ' I 2 I 3 I 4 I 4 grmmr is LR0) if the following two conditions hold: 1. For ny configurtion set contining the item α β, there is no complete item B γ in tht set. No shift/reduce conflict in ny stte in tle, for ech stte, either shift or reduce 2. here is t most one complete item α in ech configurtion set. No reduce/reduce conflict in tle, for ech stte, use sme reduction rule for every input symol. Very few grmmrs meet the requirements to e LR0). I ) [] I 2 I 1 I 0 ' ' ) [] [] I 3 [ I 4 ) ) [] I 9 [ ] ) [] I 5 I 10 ) [] [ ] [] I 11 ] ) I 7 Incomplete digrm ) I 6 ) I 8 35 LR Prse tle incomplete) tte on O ction ) $ [ ] ccept 2 R2 R2 R2 R R3 R3 R3 9 R R5 R5 R5 R5 8 R1 R1 R1 R R5 R5 R5 R5 0) 1) 2) 3) 4) ) 5) [] 36 6

7 tte stck Input Prser ction 0 [ ] $ hift [ ] $ [ ] $ shift [ 4 ] $ Reduce 0 4 9[ 2 ] $ Reduce 0 4 9[ 10 ] $ [ 10 11] $ Reduce [] 0 2 $ Reduce 0 1 $ $ $ Reduce $ Reduce 0 1 $ ccept V= ) [] V I 0 ' V= ) [] V I 2 > I 4 I 1 [] V ' hift/reduce conflict: [] Reduce/reduce conflict: V LR Prse tle incomplete) tte on O ction ) $ [ ] ccept 2 R2 R2 R2 R R4 R4 R4 9 R R5 R5 R5 R5 8 R1 R1 R1 R ) 1) 2) 3) V= 4) 5) ) 6) [] 7) V 39 LR0) key points Construct LR tle trt with ugmented grmmr ) Generte items from productions. Insert the Dot into ll positions Generte item sets or configurtion sets) from items; they re our prser sttes. Generte stte trnsitions from function successor stte, symol). Build ction nd tles from sttes nd trnsitions. les implement shift-reduce prser. View [sttes nd trnsitions] s finite utomton. n Item represents how fr prser is in recognizing prt of one rule s RH. n Item set comines vrious pths the prser might hve tken so fr, to diverge s more input is prsed. LR0) grmmrs re esiest LR to understnd, ut too simple to use in rel life prsing. 40 imple LR1) prsing: LR Construction for LR tles LR0) One LR0) stte mustn t hve oth shift nd reduce items, or two reduce items. o ny complete item dot t end) must e in its own stte; prser will lwys reduce when in this stte. LR Peek hed t input to see if reduction is pproprite. Before reducing y rule XYZ, see if the next token is in Follow ). Reduce only in tht cse. Otherwise, shift. 1. Construct F = {I0, I1,... In }, the LR0) item sets. 2. tte i is Ii. he prsing ctions for the stte re: ) If α is in Ii then set ction[i,] to reduce > α for ll in Follow) is not '). ) If ' is in Ii then set ction[i,$] to ccept. c) If α β is in Ii nd successorii, ) = Ij, then set ction[i,] to shift j must e terminl). 3. he goto trnsitions for stte i re constructed for ll non-terminls using the rule: If successorii, ) = Ij, then [i, ] = j. 4. ll entries not defined y rules 2 nd 3 re errors. 5. he initil stte is closure of set with item

8 Properties of LR Pickier rule out setting ction tle is the only difference from LR0) tles; If G is LR it is unmiguous, ut not vice vers; tte cn hve oth shift nd reduce items, if Follow sets re disjoint. LR xmple Item sets I0 nd successor I0, ): ' ) [] [] ' ) [] LR0) prser sees oth shift nd reduce, ut LR prser consults Follow set: Follow) = {, ), ], $ } so mens reduce on or ) or ] or $ [] mens shift otherwise e.g. on [ ) LR xmple 2 ' V = ) V V ' V = ) V wo complete LR0) items, so reduce-reduce conflict in LR0) grmmr, ut: Follow) = {, ), $ } FollowV) = { = } Disjoint, so no conflict. eprte ction entries in tle. LR grmmr grmmr is LR if the following two conditions hold: If items α β nd B γ re in stte, then terminl FollowB). no shift-reduce conflict on ny stte. his mens the successor function for x from tht set either shifts to new stte or reduces, ut not oth. For ny two complete items α nd B β in stte, the Follow sets must e disjoint. Follow) FollowB) is empty.) no reduce-reduce conflict on ny stte. If more thn one non-terminl could e reduced from this set, it must e possile to uniquely determine which using only one token of lookhed. Compre with LR0) grmmr: 1. For ny configurtion set contining the item α β, there is no complete item B γ in tht set. 2. here is t most one complete item α in ech configurtion set. Note tht LR0) LR LR dc 3. d 4. c In 3 there is reduce/shift conflict: It cn e R4 or shift. By looking t the Follow set of, the conflict is removed. 0: ' dc d 1: d 2: d c d c c ction 3: dc c 4: d c d $ R4 6 R2 R3 5: dc 6: d Prse trce tte stck Input Prser ction 0 dc$ hift 2 0 2d c$ hift 3 0 2d 3c $ shift 5 0 2d 3c 5 $ Reduce $ ccept

9 Non-LR exmple dc 3. d c 0: ' dc d c c 9: c d 1: 2: d c d c 7: c 3: dc c 4: d 8: 3 hs shift/reduce conflict. By looking t Follow), oth nd re in the follow set. o under column we still don t know whether to reduce or shift. 5: dc 6: d 49 he conflict LR prsing tle ction c d $ /R5 R R2 6 R R4 9 R5 R5 Follow) = {, } 50 LR1) prsing Mke items crry more informtion. LR1) item: X1...Xi Xi1...Xj, tok erminl tok is the lookhed. Mening: hve sttes for X1...Xi on stck lredy expect to put sttes for Xi1...Xj onto stck nd then reduce, ut only if token following Xj is tok tok cn e $ plit Follow) into seprte cses Cn cluster items nottionlly: [ α, //c] mens the three items: [ α, ] [ α, ] [ α, c] Reduce α to if next token is or or c {,, c } Follow) LR1) item sets More items nd more item sets thn LR Closure: For ech item [ α Bβ, ] in I, for ech production B γ in G, nd for ech terminl in Firstβ), dd [B γ, ] to I dd only items with the correct lookhed) Once we hve closed item set, use LR1) successor function to compute trnsitions nd next items. xmple: Initil item: [, $] Wht is the closure? [ dc, $] [ d, $] [, $] [ c, ] dc d c LR1) successor function Given I n item set with [ α Xβ, ], dd [ α X β, ] to item set J. successori,x) is the closure of set J. imilr to successor function to LR0), ut we propgte the lookhed token for ech item. xmple 0: ', $ dc, $ d, $, $ c, c d 1:, $ 2: d c, $ d, $ c, LR1) tles ction tle entries: If [ α, ] Ii, then set ction[i,] to reduce y rule α is not '). If [, $] Ii then set ction[i,$] to ccept. If [ α β, ] is in Ii nd succii, ) = Ij, then set ction[i,] to shift j. Here is terminl. entries: For ech stte I & ech non-terminl : If succii, ) = Ij, then [i, ] = j. 9: c,

10 LR1) digrm 0: ', $ dc, $ d, $, $ c, c 9: c, d 1:, $ 2: d c, $ d, $ c, 7:, $ c 3: dc, $ c, 4: d, $ 8:, $ dc 3. d c 5: dc, $ 6: d, $ Crete the LR1) prse tle ction c d $ R R2 6 R R4 9 R nother LR1) exmple Crete the trnsition digrm 0: ', $, $, /, / 9:, / 1:, $ 2:, $, $, $ 7:, /, /, / 3:, $ 4:, $, $, $ 5:, $ 8:, / 0) ' 1) 2) 3) 6:, $ 57 Prse tle stte ction $ ccept R R3 6 R R2 R2 9 R3 R3 58 Prse trce stck remining input prse ction 0 $ 9 09 $ R $ R $ R $ R2 023 $ R1 01 $ ccept LR1) grmmr grmmr is LR1) if the following 2 conditions re stisfied for ech configurtion set: For ech item [ α β, ] in the set, there is no item in the set of the form [B γ, ] In the ction tle, this trnsltes to no shift/reduce conflict. If there re two complete items [ α, ] nd [B β, ] in the set, then nd should e different. In the ction tle, this trnsltes to no reduce/reduce conflict Compre with the LR grmmr For ny item α β in the set, with terminl, there is no complete item B γ in tht set with in FollowB). For ny two complete items α nd B β in the set, the Follow sets must e disjoint. Note tht LR1) LR1) LR0) LR1) LR1)

11 LR1) tles continued LR1) tles cn get ig exponentil in size of rules Cn we keep the dditionl power we got from going LR LR without tle explosion? LLR! We split LR1) sttes to get LR1) sttes, mye too ggressively. ry to merge item sets tht re lmost enticl. ricky it: Don t introduce shift-reduce or reduce-reduce conflicts. LLR pproch Just sy LLR it s lwys 1 in prctice) Given the numerous LR1) sttes for grmmr G, conser merging similr sttes, to get fewer sttes. Cndtes for merging: sme core LR0) item) only differences in lookheds xmple: 1: X α β, //c 2: X α β, c/d 12: X α β, //c/d ttes with sme core items 0: ', $, $, /, / 9:, / 1:, $ 2:, $, $, $ 7:, /, /, / 3:, $ 4:, $, $, $ 5:, $ 8:, / 0) ' 1) 2) 3) 6:, $ Merge the sttes 0: ', $, $, /, / 59:, //$ 1:, $ 2:, $, $, $ 47:, //$, //$, //$ 3:, $ 4:, $, $, $ 5:, $ 68:, //$ 0) ' 1) 2) 3) 6:, $ Merge the sttes 0: ', $, $, /, / 59:, //$ 1:, $ 2:, $, $, $ 47:, //$, //$, //$ 3:, $ 68:, //$ 0) ' 1) 2) 3) Follow)={ $ } fter the merge Wht hppened when we merged? hree fewer sttes Lookhed on items merged. In this cse, lookhed in merged sets constitutes entire Follow set. o, we mde LR1) grmmr y merging. Result of merge usully not LR1)

12 conflict fter merging 0: ', $ Bc, $ Cd, $ Bd, $ Cc,$ 1:, $ 2: Bc, $ Cd, $ B e, c C e,d 4: Bd, $ Cc, $ B e, d C e, c e e 1) Bc Cd Bd Cc 2) B e 3) C e 3: B e, c C e, d 5: B e, d C e, c Prcticl consertion miguity in LR grmmrs G: G produces multiple rightmost derivtions. i.e. cn uild two different prse trees for one input string.) Rememer: * ) We dded terms nd fctors to force unmiguous prse with correct precedence nd ssocitivity Wht if we threw the grmmr into n LR-grmmr tle-construction mchine nywy? Conflicts = multiple ction entries for one cell We choose which entry to keep, toss others Precedence nd ssocitivity in JvCUP 0: ' * ) 1: * 3: 2: ) * ) * 4: * ) 5: * * ) 6: ) * * 8: * * ) 9: ) * ) * 7: * 69 JvCup grmmr terminl PLU, IM; precedence left PLU; precedence left IM; ::= PLU IM ID Wht if the input is xyz? When shifting conflicts with reducing production contining, choose reduce Wht if the input is xy*z? Wht if the input is x*yz? 70 rnsition digrm for ssignment expr 0: ', $, $ V=, $ V, = V 1:, $ 3: V =, $ 2:, $ V, = = 4: V=, $ V, $ n, $ V, $ V n 5: V=, $ 6: V, $ 7: n, $ 8: V, $ V= V V n 71 Why re there conflicts in some rules in ssignments? P 0: ' P, $ P m, $ P Pm, $ P, $ P, m P m, m P Pm, m 2: P, $ m 1: P m, $/m *** hift/reduce conflict found in stte #0 etween P ::= *) nd P ::= *) m under symol m Non LR1) grmmr P m Pm It is n miguous grmmr. here re two rightmost/leftmost derivtions for sentence m: P Pm m P m 72 12

13 slightly chnged grmmr, still not LR P 0: ' P, $ P m, $ P mp, $ P, $ 2: P, $ m It is n miguous grmmr. here re two prse trees for sentence m: P mp m P m 1: P m, $ P m P, $ P m, $ P mp, $ P, $ P 3: P mp, $ Non LR1) grmmr P m m P Reduce/Reduce conflict found in stte #1 etween P ::= m *) nd P ::= *) under symols: {OF} Produced from jvcup 73 Modified LR1) grmmr 0: ' P, $ P, $ P mp, $ P m 1: P, $ 2: P m P, $ P mp, $ P, $ Note tht there re no conflicts he derivtion: P mp mmp mm P LR1) grmmr P m P 3: P mp, $ 74 nother wy of chnging to LR1) grmmr 0: ' P, $ P Q, $ P, $ Q m, $ Q mq, $ P m 1: P, $ 2: Q m, $ Q m Q, $ Q m, $ Q mq, $ Q LR1) grmmr P Q Q m m Q 3: Q mq, $ LR grmmrs: comprison dvntges Disdvntges LR0) mllest tles, esiest to uild Indequte for mny PL structures LR1) LLR1) LR1) LR0) LR1) LLR LR1) CFG More inclusive, more inform?on thn LR0) me size tles s LR, more lngs, efficient to uild Most precise use of lookhed, most PL structures we wnt Mny useful grmmrs re not LR 1) empiricl, not mthem?cl les order of mgnitude > LR1) he spce of grmmrs CFG he spce of grmmrs CFG Wht re used in prctice Unmiguous CFG Unmiguous CFG LL1) LR1) LL1) LR1) LLR1) LLR1) LR1) LR1) LR0) LR0)

14 Verifying the lnguge generted y grmmr o verify grmmr: every string generted y G is in L every string in L cn e generted y G xmple: ) ε the lnguge is ll the strings of lnced prenthesis, such s ), )), ) ))) Proof prt 1: every sentence derived from is lnced. sis: empty string is lnced. induction: suppose tht ll derivtions fewer thn n steps produce lnced sentences, nd conser leftmost derivtion of n steps. such derivtion must e of the form: ) *x) *x)y Proof prt 2: every lnced string cn e derived from Bsis: the empty string cn e derived from. Induction: suppose tht every lnced string of length less thn 2n cn e derived from. Conser lnced string w of length 2n. w must strt with. w cn e written s x)y, where x, y re lnced. Hierrchy of grmmrs CFG is more powerful thn R ype n grmmr is more powerful thn type n1 grmmr xmple: Σ={, } he lnguge of ny string consists of nd CFG ε Lnguge 5 Cn e descrie y R he lnguge of plindromes consist of nd ε Cn e descried y CFG, ut not R Lnguge 1: ny string of nd Lnguge 2: plindromes Lnguge 3 Lnguge 2 Lnguge 4 RG Lnguge 1 When grmmr is more powerful, it is not tht it cn descrie lrger lnguge. Insted, the power mens the ility to restrict the set. More powerful grmmr cn define more complicted oundry etween correct nd incorrect sentences. herefore, more different lnguges Metphoric comprison of grmmrs R drw the rose use stright lines ruler nd -squre suffice) CFG pproximte the outline y stright lines nd circle segments ruler nd compsses) 81 14

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the LR() nlysis Drwcks of LR(). Look-hed symols s eplined efore, concerning LR(), it is possile to consult the net set to determine, in the reduction sttes, for which symols it would e possile to perform reductions.

More information

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table TDDD55 Compilers nd Interpreters TDDB44 Compiler Construction LR Prsing, Prt 2 Constructing Prse Tles Prse tle construction Grmmr conflict hndling Ctegories of LR Grmmrs nd Prsers Peter Fritzson, Christoph

More information

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number>

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number> DDD16 Compilers nd Interpreters DDB44 Compiler Construction R Prsing Prt 1 R prsing concept Using prser genertor Prse ree Genertion Wht is R-prsing? eft-to-right scnning R Rigthmost derivtion in reverse

More information

CS 430 Spring Mike Lam, Professor. Parsing

CS 430 Spring Mike Lam, Professor. Parsing CS 430 Spring 2015 Mike Lm, Professor Prsing Syntx Anlysis We cn now formlly descrie lnguge's syntx Using regulr expressions nd BNF grmmrs How does tht help us? Syntx Anlysis We cn now formlly descrie

More information

CS 321 Programming Languages and Compilers. Bottom Up Parsing

CS 321 Programming Languages and Compilers. Bottom Up Parsing CS 321 Progrmming nguges nd Compilers Bottom Up Prsing Bottom-up Prsing: Shift-reduce prsing Grmmr H: fi ; fi b Input: ;;b hs prse tree ; ; b 2 Dt for Shift-reduce Prser Input string: sequence of tokens

More information

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011 CSCI 3130: Forml Lnguges nd utomt Theory Lecture 12 The Chinese University of Hong Kong, Fll 2011 ndrej Bogdnov In progrmming lnguges, uilding prse trees is significnt tsk ecuse prse trees tell us the

More information

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

In the last lecture, we discussed how valid tokens may be specified by regular expressions. LECTURE 5 Scnning SYNTAX ANALYSIS We know from our previous lectures tht the process of verifying the syntx of the progrm is performed in two stges: Scnning: Identifying nd verifying tokens in progrm.

More information

Compilers Spring 2013 PRACTICE Midterm Exam

Compilers Spring 2013 PRACTICE Midterm Exam Compilers Spring 2013 PRACTICE Midterm Exm This is full length prctice midterm exm. If you wnt to tke it t exm pce, give yourself 7 minutes to tke the entire test. Just like the rel exm, ech question hs

More information

Definition of Regular Expression

Definition of Regular Expression Definition of Regulr Expression After the definition of the string nd lnguges, we re redy to descrie regulr expressions, the nottion we shll use to define the clss of lnguges known s regulr sets. Recll

More information

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5 CS321 Lnguges nd Compiler Design I Winter 2012 Lecture 5 1 FINITE AUTOMATA A non-deterministic finite utomton (NFA) consists of: An input lphet Σ, e.g. Σ =,. A set of sttes S, e.g. S = {1, 3, 5, 7, 11,

More information

COMP 423 lecture 11 Jan. 28, 2008

COMP 423 lecture 11 Jan. 28, 2008 COMP 423 lecture 11 Jn. 28, 2008 Up to now, we hve looked t how some symols in n lphet occur more frequently thn others nd how we cn sve its y using code such tht the codewords for more frequently occuring

More information

Fig.25: the Role of LEX

Fig.25: the Role of LEX The Lnguge for Specifying Lexicl Anlyzer We shll now study how to uild lexicl nlyzer from specifiction of tokens in the form of list of regulr expressions The discussion centers round the design of n existing

More information

Problem Set 2 Fall 16 Due: Wednesday, September 21th, in class, before class begins.

Problem Set 2 Fall 16 Due: Wednesday, September 21th, in class, before class begins. Problem Set 2 Fll 16 Due: Wednesdy, September 21th, in clss, before clss begins. 1. LL Prsing For the following sub-problems, consider the following context-free grmmr: S T$ (1) T A (2) T bbb (3) A T (4)

More information

stack of states and grammar symbols Stack-Bottom marker C. Kessler, IDA, Linköpings universitet. 1. <list> -> <list>, <element> 2.

stack of states and grammar symbols Stack-Bottom marker C. Kessler, IDA, Linköpings universitet. 1. <list> -> <list>, <element> 2. TDDB9 Compilers nd Interpreters TDDB44 Compiler Construction LR Prsing Updted/New slide mteril 007: Pushdown Automton for LR-Prsing Finite-stte pushdown utomton contins lterntingly sttes nd symols in NUΣ

More information

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) * Pln for Tody nd Beginning Next week Interpreter nd Compiler Structure, or Softwre Architecture Overview of Progrmming Assignments The MeggyJv compiler we will e uilding. Regulr Expressions Finite Stte

More information

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών ΕΠΛ323 - Θωρία και Πρακτική Μταγλωττιστών Lecture 3 Lexicl Anlysis Elis Athnsopoulos elisthn@cs.ucy.c.cy Recognition of Tokens if expressions nd reltionl opertors if è if then è then else è else relop

More information

Midterm I Solutions CS164, Spring 2006

Midterm I Solutions CS164, Spring 2006 Midterm I Solutions CS164, Spring 2006 Februry 23, 2006 Plese red ll instructions (including these) crefully. Write your nme, login, SID, nd circle the section time. There re 8 pges in this exm nd 4 questions,

More information

ECE 468/573 Midterm 1 September 28, 2012

ECE 468/573 Midterm 1 September 28, 2012 ECE 468/573 Midterm 1 September 28, 2012 Nme:! Purdue emil:! Plese sign the following: I ffirm tht the nswers given on this test re mine nd mine lone. I did not receive help from ny person or mteril (other

More information

10/12/17. Motivating Example. Lexical and Syntax Analysis (2) Recursive-Descent Parsing. Recursive-Descent Parsing. Recursive-Descent Parsing

10/12/17. Motivating Example. Lexical and Syntax Analysis (2) Recursive-Descent Parsing. Recursive-Descent Parsing. Recursive-Descent Parsing Motivting Exmple Lexicl nd yntx Anlysis (2) In Text: Chpter 4 Consider the grmmr -> cad A -> b Input string: w = cd How to build prse tree top-down? 2 Initilly crete tree contining single node (the strt

More information

Eliminating left recursion grammar transformation. The transformed expression grammar

Eliminating left recursion grammar transformation. The transformed expression grammar Eliminting left recursion grmmr trnsformtion Originl! rnsformed! 0 0! 0 α β α α α α α α α α β he two grmmrs generte the sme lnguge, but the one on the right genertes the rst, nd then string of s, using

More information

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata CS 432 Fll 2017 Mike Lm, Professor (c)* Regulr Expressions nd Finite Automt Compiltion Current focus "Bck end" Source code Tokens Syntx tree Mchine code chr dt[20]; int min() { flot x = 42.0; return 7;

More information

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015 Finite Automt Lecture 4 Sections 3.6-3.7 Ro T. Koether Hmpden-Sydney College Wed, Jn 21, 2015 Ro T. Koether (Hmpden-Sydney College) Finite Automt Wed, Jn 21, 2015 1 / 23 1 Nondeterministic Finite Automt

More information

Homework. Context Free Languages III. Languages. Plan for today. Context Free Languages. CFLs and Regular Languages. Homework #5 (due 10/22)

Homework. Context Free Languages III. Languages. Plan for today. Context Free Languages. CFLs and Regular Languages. Homework #5 (due 10/22) Homework Context Free Lnguges III Prse Trees nd Homework #5 (due 10/22) From textbook 6.4,b 6.5b 6.9b,c 6.13 6.22 Pln for tody Context Free Lnguges Next clss of lnguges in our quest! Lnguges Recll. Wht

More information

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis CS143 Hndout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexicl Anlysis In this first written ssignment, you'll get the chnce to ply round with the vrious constructions tht come up when doing lexicl

More information

CSCE 531, Spring 2017, Midterm Exam Answer Key

CSCE 531, Spring 2017, Midterm Exam Answer Key CCE 531, pring 2017, Midterm Exm Answer Key 1. (15 points) Using the method descried in the ook or in clss, convert the following regulr expression into n equivlent (nondeterministic) finite utomton: (

More information

Dr. D.M. Akbar Hussain

Dr. D.M. Akbar Hussain Dr. D.M. Akr Hussin Lexicl Anlysis. Bsic Ide: Red the source code nd generte tokens, it is similr wht humns will do to red in; just tking on the input nd reking it down in pieces. Ech token is sequence

More information

2 Computing all Intersections of a Set of Segments Line Segment Intersection

2 Computing all Intersections of a Set of Segments Line Segment Intersection 15-451/651: Design & Anlysis of Algorithms Novemer 14, 2016 Lecture #21 Sweep-Line nd Segment Intersection lst chnged: Novemer 8, 2017 1 Preliminries The sweep-line prdigm is very powerful lgorithmic design

More information

ASTs, Regex, Parsing, and Pretty Printing

ASTs, Regex, Parsing, and Pretty Printing ASTs, Regex, Prsing, nd Pretty Printing CS 2112 Fll 2016 1 Algeric Expressions To strt, consider integer rithmetic. Suppose we hve the following 1. The lphet we will use is the digits {0, 1, 2, 3, 4, 5,

More information

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string. CS 340, Fll 2014 Dec 11 th /13 th Finl Exm Nme: Note: in ll questions, the specil symol ɛ (epsilon) is used to indicte the empty string. Question 1. [5 points] Consider the following regulr expression;

More information

Context-Free Grammars

Context-Free Grammars Context-Free Grmmrs Descriing Lnguges We've seen two models for the regulr lnguges: Finite utomt ccept precisely the strings in the lnguge. Regulr expressions descrie precisely the strings in the lnguge.

More information

CMSC 331 First Midterm Exam

CMSC 331 First Midterm Exam 0 00/ 1 20/ 2 05/ 3 15/ 4 15/ 5 15/ 6 20/ 7 30/ 8 30/ 150/ 331 First Midterm Exm 7 October 2003 CMC 331 First Midterm Exm Nme: mple Answers tudent ID#: You will hve seventy-five (75) minutes to complete

More information

10.5 Graphing Quadratic Functions

10.5 Graphing Quadratic Functions 0.5 Grphing Qudrtic Functions Now tht we cn solve qudrtic equtions, we wnt to lern how to grph the function ssocited with the qudrtic eqution. We cll this the qudrtic function. Grphs of Qudrtic Functions

More information

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos ΕΠΛ323 - Θωρία και Πρακτική Μταγλωττιστών Lecture 3 Lexicl Anlysis Elis Athnsopoulos elisthn@cs.ucy.c.cy RecogniNon of Tokens if expressions nd relnonl opertors if è if then è then else è else relop è

More information

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs. Lecture 5 Wlks, Trils, Pths nd Connectedness Reding: Some of the mteril in this lecture comes from Section 1.2 of Dieter Jungnickel (2008), Grphs, Networks nd Algorithms, 3rd edition, which is ville online

More information

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1 Deterministic Finite Automt And Regulr Lnguges Fll 2018 Costs Busch - RPI 1 Deterministic Finite Automton (DFA) Input Tpe String Finite Automton Output Accept or Reject Fll 2018 Costs Busch - RPI 2 Trnsition

More information

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing); Bottom up parsing General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing); 1 Top-down vs Bottom-up Bottom-up more powerful than top-down; Can process

More information

CSE 401 Midterm Exam 11/5/10 Sample Solution

CSE 401 Midterm Exam 11/5/10 Sample Solution Question 1. egulr expressions (20 points) In the Ad Progrmming lnguge n integer constnt contins one or more digits, but it my lso contin embedded underscores. Any underscores must be preceded nd followed

More information

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08 CS412/413 Introduction to Compilers Tim Teitelum Lecture 4: Lexicl Anlyzers 28 Jn 08 Outline DFA stte minimiztion Lexicl nlyzers Automting lexicl nlysis Jlex lexicl nlyzer genertor CS 412/413 Spring 2008

More information

Lexical Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Lexical Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay Lexicl Anlysis Amith Snyl (www.cse.iit.c.in/ s) Deprtment of Computer Science nd Engineering, Indin Institute of Technology, Bomy Septemer 27 College of Engineering, Pune Lexicl Anlysis: 2/6 Recp The input

More information

CS 236 Language and Computation. Alphabet. Definition. I.2.1. Formal Languages (10.1)

CS 236 Language and Computation. Alphabet. Definition. I.2.1. Formal Languages (10.1) C 236 Lnguge nd Computtion Course Notes Prt I: Grmmrs for Defining yntx (II) Chpter I.2: yntx nd Grmmrs (10, 12.1) Anton etzer (Bsed on ook drft y J. V. Tucker nd K. tephenson) Dept. of Computer cience,

More information

Reducing a DFA to a Minimal DFA

Reducing a DFA to a Minimal DFA Lexicl Anlysis - Prt 4 Reducing DFA to Miniml DFA Input: DFA IN Assume DFA IN never gets stuck (dd ded stte if necessry) Output: DFA MIN An equivlent DFA with the minimum numer of sttes. Hrry H. Porter,

More information

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or Opertor Precedence Most progrmming lnguges hve opertor precedence rules tht stte the order in which opertors re pplied (in the sence of explicit prentheses). Thus in C nd Jv nd CSX, +*c mens compute *c,

More information

Context-Free Grammars

Context-Free Grammars Context-Free Grmmrs Descriing Lnguges We've seen two models for the regulr lnguges: Finite utomt ccept precisely the strings in the lnguge. Regulr expressions descrie precisely the strings in the lnguge.

More information

Topic 2: Lexing and Flexing

Topic 2: Lexing and Flexing Topic 2: Lexing nd Flexing COS 320 Compiling Techniques Princeton University Spring 2016 Lennrt Beringer 1 2 The Compiler Lexicl Anlysis Gol: rek strem of ASCII chrcters (source/input) into sequence of

More information

CMPSC 470: Compiler Construction

CMPSC 470: Compiler Construction CMPSC 47: Compiler Construction Plese complete the following: Midterm (Type A) Nme Instruction: Mke sure you hve ll pges including this cover nd lnk pge t the end. Answer ech question in the spce provided.

More information

From Dependencies to Evaluation Strategies

From Dependencies to Evaluation Strategies From Dependencies to Evlution Strtegies Possile strtegies: 1 let the user define the evlution order 2 utomtic strtegy sed on the dependencies: use locl dependencies to determine which ttriutes to compute

More information

Lexical Analysis: Constructing a Scanner from Regular Expressions

Lexical Analysis: Constructing a Scanner from Regular Expressions Lexicl Anlysis: Constructing Scnner from Regulr Expressions Gol Show how to construct FA to recognize ny RE This Lecture Convert RE to n nondeterministic finite utomton (NFA) Use Thompson s construction

More information

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing Also known as Shift-Reduce parsing More powerful than top down Don t need left factored grammars Can handle left recursion Attempt to construct parse tree from an input string eginning at leaves and working

More information

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing Undergrdute Compilers Review nd Intro to MJC Announcements Miling list is in full swing Tody Some thoughts on grd school Finish prsing Semntic nlysis Visitor pttern for bstrct syntx trees Some Thoughts

More information

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig CS311H: Discrete Mthemtics Grph Theory IV Instructor: Işıl Dillig Instructor: Işıl Dillig, CS311H: Discrete Mthemtics Grph Theory IV 1/25 A Non-plnr Grph Regions of Plnr Grph The plnr representtion of

More information

P(r)dr = probability of generating a random number in the interval dr near r. For this probability idea to make sense we must have

P(r)dr = probability of generating a random number in the interval dr near r. For this probability idea to make sense we must have Rndom Numers nd Monte Crlo Methods Rndom Numer Methods The integrtion methods discussed so fr ll re sed upon mking polynomil pproximtions to the integrnd. Another clss of numericl methods relies upon using

More information

Announcements. CS 188: Artificial Intelligence Fall Recap: Search. Today. Example: Pancake Problem. Example: Pancake Problem

Announcements. CS 188: Artificial Intelligence Fall Recap: Search. Today. Example: Pancake Problem. Example: Pancake Problem Announcements Project : erch It s live! Due 9/. trt erly nd sk questions. It s longer thn most! Need prtner? Come up fter clss or try Pizz ections: cn go to ny, ut hve priority in your own C 88: Artificil

More information

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string. CS 340, Fll 2016 Sep 29th Exm 1 Nme: Note: in ll questions, the speil symol ɛ (epsilon) is used to indite the empty string. Question 1. [10 points] Speify regulr expression tht genertes the lnguge over

More information

Today. CS 188: Artificial Intelligence Fall Recap: Search. Example: Pancake Problem. Example: Pancake Problem. General Tree Search.

Today. CS 188: Artificial Intelligence Fall Recap: Search. Example: Pancake Problem. Example: Pancake Problem. General Tree Search. CS 88: Artificil Intelligence Fll 00 Lecture : A* Serch 9//00 A* Serch rph Serch Tody Heuristic Design Dn Klein UC Berkeley Multiple slides from Sturt Russell or Andrew Moore Recp: Serch Exmple: Pncke

More information

A Tautology Checker loosely related to Stålmarck s Algorithm by Martin Richards

A Tautology Checker loosely related to Stålmarck s Algorithm by Martin Richards A Tutology Checker loosely relted to Stålmrck s Algorithm y Mrtin Richrds mr@cl.cm.c.uk http://www.cl.cm.c.uk/users/mr/ University Computer Lortory New Museum Site Pemroke Street Cmridge, CB2 3QG Mrtin

More information

CSc 453 Compilers and Systems Software. 6 : Top-Down Parsing I

CSc 453 Compilers and Systems Software. 6 : Top-Down Parsing I C 45 Compilers n ystems oftwre 6 : op-down Prsing I Christin Collberg Deprtment of Computer iene University of rizon ollberg@gmil.om Copyright 2009 Christin Collberg eptember 14, 2009 1 Overview 2 Compiler

More information

CS 241 Week 4 Tutorial Solutions

CS 241 Week 4 Tutorial Solutions CS 4 Week 4 Tutoril Solutions Writing n Assemler, Prt & Regulr Lnguges Prt Winter 8 Assemling instrutions utomtilly. slt $d, $s, $t. Solution: $d, $s, nd $t ll fit in -it signed integers sine they re 5-it

More information

Lexical analysis, scanners. Construction of a scanner

Lexical analysis, scanners. Construction of a scanner Lexicl nlysis scnners (NB. Pges 4-5 re for those who need to refresh their knowledge of DFAs nd NFAs. These re not presented during the lectures) Construction of scnner Tools: stte utomt nd trnsition digrms.

More information

acronyms possibly used in this test: CFG :acontext free grammar CFSM :acharacteristic finite state machine DFA :adeterministic finite automata

acronyms possibly used in this test: CFG :acontext free grammar CFSM :acharacteristic finite state machine DFA :adeterministic finite automata EE573 Fll 2002, Exm open book, if question seems mbiguous, sk me to clrify the question. If my nswer doesn t stisfy you, plese stte your ssumptions. cronyms possibly used in this test: CFG :context free

More information

What are suffix trees?

What are suffix trees? Suffix Trees 1 Wht re suffix trees? Allow lgorithm designers to store very lrge mount of informtion out strings while still keeping within liner spce Allow users to serch for new strings in the originl

More information

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016 Solving Prolems y Serching CS 486/686: Introduction to Artificil Intelligence Winter 2016 1 Introduction Serch ws one of the first topics studied in AI - Newell nd Simon (1961) Generl Prolem Solver Centrl

More information

CS201 Discussion 10 DRAWTREE + TRIES

CS201 Discussion 10 DRAWTREE + TRIES CS201 Discussion 10 DRAWTREE + TRIES DrwTree First instinct: recursion As very generic structure, we could tckle this problem s follows: drw(): Find the root drw(root) drw(root): Write the line for the

More information

Presentation Martin Randers

Presentation Martin Randers Presenttion Mrtin Rnders Outline Introduction Algorithms Implementtion nd experiments Memory consumption Summry Introduction Introduction Evolution of species cn e modelled in trees Trees consist of nodes

More information

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex Long Quiz2 45mins Nme: Personl Numer: Prolem. (20pts) Here is n Tle of Perl Regulr Ex Chrcter Description. single chrcter \s whitespce chrcter (spce, t, newline) \S non-whitespce chrcter \d digit (0-9)

More information

Applied Databases. Sebastian Maneth. Lecture 13 Online Pattern Matching on Strings. University of Edinburgh - February 29th, 2016

Applied Databases. Sebastian Maneth. Lecture 13 Online Pattern Matching on Strings. University of Edinburgh - February 29th, 2016 Applied Dtses Lecture 13 Online Pttern Mtching on Strings Sestin Mneth University of Edinurgh - Ferury 29th, 2016 2 Outline 1. Nive Method 2. Automton Method 3. Knuth-Morris-Prtt Algorithm 4. Boyer-Moore

More information

Java CUP. Java CUP Specifications. User Code Additions. Package and Import Specifications

Java CUP. Java CUP Specifications. User Code Additions. Package and Import Specifications Jv CUP Jv CUP is prser-genertion tool, similr to Ycc. CUP uilds Jv prser for LALR(1) grmmrs from production rules nd ssocited Jv code frgments. When prticulr production is recognized, its ssocited code

More information

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming Lecture 10 Evolutionry Computtion: Evolution strtegies nd genetic progrmming Evolution strtegies Genetic progrmming Summry Negnevitsky, Person Eduction, 2011 1 Evolution Strtegies Another pproch to simulting

More information

1.1. Interval Notation and Set Notation Essential Question When is it convenient to use set-builder notation to represent a set of numbers?

1.1. Interval Notation and Set Notation Essential Question When is it convenient to use set-builder notation to represent a set of numbers? 1.1 TEXAS ESSENTIAL KNOWLEDGE AND SKILLS Prepring for 2A.6.K, 2A.7.I Intervl Nottion nd Set Nottion Essentil Question When is it convenient to use set-uilder nottion to represent set of numers? A collection

More information

TO REGULAR EXPRESSIONS

TO REGULAR EXPRESSIONS Suject :- Computer Science Course Nme :- Theory Of Computtion DA TO REGULAR EXPRESSIONS Report Sumitted y:- Ajy Singh Meen 07000505 jysmeen@cse.iit.c.in BASIC DEINITIONS DA:- A finite stte mchine where

More information

Lexical Analysis and Lexical Analyzer Generators

Lexical Analysis and Lexical Analyzer Generators 1 Lexicl Anlysis nd Lexicl Anlyzer Genertors Chpter 3 COP5621 Compiler Construction Copyright Roert vn Engelen, Florid Stte University, 2007-2009 2 The Reson Why Lexicl Anlysis is Seprte Phse Simplifies

More information

Announcements. CS 188: Artificial Intelligence Fall Recap: Search. Today. General Tree Search. Uniform Cost. Lecture 3: A* Search 9/4/2007

Announcements. CS 188: Artificial Intelligence Fall Recap: Search. Today. General Tree Search. Uniform Cost. Lecture 3: A* Search 9/4/2007 CS 88: Artificil Intelligence Fll 2007 Lecture : A* Serch 9/4/2007 Dn Klein UC Berkeley Mny slides over the course dpted from either Sturt Russell or Andrew Moore Announcements Sections: New section 06:

More information

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona Implementing utomt Sc 5 ompilers nd Systems Softwre : Lexicl nlysis II Deprtment of omputer Science University of rizon collerg@gmil.com opyright c 009 hristin ollerg NFs nd DFs cn e hrd-coded using this

More information

MTH 146 Conics Supplement

MTH 146 Conics Supplement 105- Review of Conics MTH 146 Conics Supplement In this section we review conics If ou ne more detils thn re present in the notes, r through section 105 of the ook Definition: A prol is the set of points

More information

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS 1 COMPUTATION & LOGIC INSTRUCTIONS TO CANDIDATES

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS 1 COMPUTATION & LOGIC INSTRUCTIONS TO CANDIDATES UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS COMPUTATION & LOGIC Sturdy st April 7 : to : INSTRUCTIONS TO CANDIDATES This is tke-home exercise. It will not

More information

Algorithm Design (5) Text Search

Algorithm Design (5) Text Search Algorithm Design (5) Text Serch Tkshi Chikym School of Engineering The University of Tokyo Text Serch Find sustring tht mtches the given key string in text dt of lrge mount Key string: chr x[m] Text Dt:

More information

Lab 1 - Counter. Create a project. Add files to the project. Compile design files. Run simulation. Debug results

Lab 1 - Counter. Create a project. Add files to the project. Compile design files. Run simulation. Debug results 1 L 1 - Counter A project is collection mechnism for n HDL design under specifiction or test. Projects in ModelSim ese interction nd re useful for orgnizing files nd specifying simultion settings. The

More information

Theory of Computation CSE 105

Theory of Computation CSE 105 $ $ $ Theory of Computtion CSE 105 Regulr Lnguges Study Guide nd Homework I Homework I: Solutions to the following problems should be turned in clss on July 1, 1999. Instructions: Write your nswers clerly

More information

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona CSc 453 Compilers nd Systems Softwre 4 : Lexicl Anlysis II Deprtment of Computer Science University of Arizon collerg@gmil.com Copyright c 2009 Christin Collerg Implementing Automt NFAs nd DFAs cn e hrd-coded

More information

Lists in Lisp and Scheme

Lists in Lisp and Scheme Lists in Lisp nd Scheme Lists in Lisp nd Scheme Lists re Lisp s fundmentl dt structures, ut there re others Arrys, chrcters, strings, etc. Common Lisp hs moved on from eing merely LISt Processor However,

More information

George Boole. IT 3123 Hardware and Software Concepts. Switching Algebra. Boolean Functions. Boolean Functions. Truth Tables

George Boole. IT 3123 Hardware and Software Concepts. Switching Algebra. Boolean Functions. Boolean Functions. Truth Tables George Boole IT 3123 Hrdwre nd Softwre Concepts My 28 Digitl Logic The Little Mn Computer 1815 1864 British mthemticin nd philosopher Mny contriutions to mthemtics. Boolen lger: n lger over finite sets

More information

Intermediate Information Structures

Intermediate Information Structures CPSC 335 Intermedite Informtion Structures LECTURE 13 Suffix Trees Jon Rokne Computer Science University of Clgry Cnd Modified from CMSC 423 - Todd Trengen UMD upd Preprocessing Strings We will look t

More information

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries Tries Yufei To KAIST April 9, 2013 Y. To, April 9, 2013 Tries In this lecture, we will discuss the following exct mtching prolem on strings. Prolem Let S e set of strings, ech of which hs unique integer

More information

EECS150 - Digital Design Lecture 23 - High-level Design and Optimization 3, Parallelism and Pipelining

EECS150 - Digital Design Lecture 23 - High-level Design and Optimization 3, Parallelism and Pipelining EECS150 - Digitl Design Lecture 23 - High-level Design nd Optimiztion 3, Prllelism nd Pipelining Nov 12, 2002 John Wwrzynek Fll 2002 EECS150 - Lec23-HL3 Pge 1 Prllelism Prllelism is the ct of doing more

More information

LING/C SC/PSYC 438/538. Lecture 21 Sandiway Fong

LING/C SC/PSYC 438/538. Lecture 21 Sandiway Fong LING/C SC/PSYC 438/538 Lecture 21 Sndiwy Fong Tody's Topics Homework 8 Review Optionl Homework 9 (mke up on Homework 7) Homework 8 Review Question1: write Prolog regulr grmmr for the following lnguge:

More information

CMSC 430, Practice Problems 1 (Solutions)

CMSC 430, Practice Problems 1 (Solutions) CMC 430, Prtie Problems 1 olutios) 1. Cosider the followig grmmr: d or ) true flse. Compute First sets for eh produtio d otermil FIRTtrue) = { true } FIRTflse) = { flse } FIRT ) ) = { } FIRT d ) = FIRT

More information

Answer Key Lesson 6: Workshop: Angles and Lines

Answer Key Lesson 6: Workshop: Angles and Lines nswer Key esson 6: tudent Guide ngles nd ines Questions 1 3 (G p. 406) 1. 120 ; 360 2. hey re the sme. 3. 360 Here re four different ptterns tht re used to mke quilts. Work with your group. se your Power

More information

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an Scnner Termintion A scnner reds input chrcters nd prtitions them into tokens. Wht hppens when the end of the input file is reched? It my be useful to crete n Eof pseudo-chrcter when this occurs. In Jv,

More information

Subtracting Fractions

Subtracting Fractions Lerning Enhncement Tem Model Answers: Adding nd Subtrcting Frctions Adding nd Subtrcting Frctions study guide. When the frctions both hve the sme denomintor (bottom) you cn do them using just simple dding

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Progrmming Lnguges h"p://www.di.unipi.it/~ndre/did2c/plp- 14/ Prof. Andre Corrdini Deprtment of Computer Science, Pis Lesson 5! Gener;on of Lexicl Anlyzers Creting Lexicl Anlyzer with Lex

More information

6.3 Volumes. Just as area is always positive, so is volume and our attitudes towards finding it.

6.3 Volumes. Just as area is always positive, so is volume and our attitudes towards finding it. 6.3 Volumes Just s re is lwys positive, so is volume nd our ttitudes towrds finding it. Let s review how to find the volume of regulr geometric prism, tht is, 3-dimensionl oject with two regulr fces seprted

More information

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009 Deprtment of Computer cience Columbi University mple Midterm olutions COM W4115 Progrmming Lnguges nd Trnsltors Mondy, October 12, 2009 Closed book, no ids. ch question is worth 20 points. Question 5(c)

More information

Slides for Data Mining by I. H. Witten and E. Frank

Slides for Data Mining by I. H. Witten and E. Frank Slides for Dt Mining y I. H. Witten nd E. Frnk Simplicity first Simple lgorithms often work very well! There re mny kinds of simple structure, eg: One ttriute does ll the work All ttriutes contriute eqully

More information

Grade 7/8 Math Circles Geometric Arithmetic October 31, 2012

Grade 7/8 Math Circles Geometric Arithmetic October 31, 2012 Fculty of Mthemtics Wterloo, Ontrio N2L 3G1 Grde 7/8 Mth Circles Geometric Arithmetic Octoer 31, 2012 Centre for Eduction in Mthemtics nd Computing Ancient Greece hs given irth to some of the most importnt

More information

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7.

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7. CS 241 Fll 2017 Midterm Review Solutions Octoer 24, 2017 Contents 1 Bits nd Bytes 1 2 MIPS Assemly Lnguge Progrmming 2 3 MIPS Assemler 6 4 Regulr Lnguges 7 5 Scnning 9 1 Bits nd Bytes 1. Give two s complement

More information

Here is an example where angles with a common arm and vertex overlap. Name all the obtuse angles adjacent to

Here is an example where angles with a common arm and vertex overlap. Name all the obtuse angles adjacent to djcent tht do not overlp shre n rm from the sme vertex point re clled djcent ngles. me the djcent cute ngles in this digrm rm is shred y + + me vertex point for + + + is djcent to + djcent simply mens

More information

CMPUT101 Introduction to Computing - Summer 2002

CMPUT101 Introduction to Computing - Summer 2002 CMPUT Introdution to Computing - Summer 22 %XLOGLQJ&RPSXWHU&LUFXLWV Chpter 4.4 3XUSRVH We hve looked t so fr how to uild logi gtes from trnsistors. Next we will look t how to uild iruits from logi gtes,

More information

Section 10.4 Hyperbolas

Section 10.4 Hyperbolas 66 Section 10.4 Hyperbols Objective : Definition of hyperbol & hyperbols centered t (0, 0). The third type of conic we will study is the hyperbol. It is defined in the sme mnner tht we defined the prbol

More information

INTRODUCTION TO SIMPLICIAL COMPLEXES

INTRODUCTION TO SIMPLICIAL COMPLEXES INTRODUCTION TO SIMPLICIAL COMPLEXES CASEY KELLEHER AND ALESSANDRA PANTANO 0.1. Introduction. In this ctivity set we re going to introduce notion from Algebric Topology clled simplicil homology. The min

More information

cisc1110 fall 2010 lecture VI.2 call by value function parameters another call by value example:

cisc1110 fall 2010 lecture VI.2 call by value function parameters another call by value example: cisc1110 fll 2010 lecture VI.2 cll y vlue function prmeters more on functions more on cll y vlue nd cll y reference pssing strings to functions returning strings from functions vrile scope glol vriles

More information

Before We Begin. Introduction to Spatial Domain Filtering. Introduction to Digital Image Processing. Overview (1): Administrative Details (1):

Before We Begin. Introduction to Spatial Domain Filtering. Introduction to Digital Image Processing. Overview (1): Administrative Details (1): Overview (): Before We Begin Administrtive detils Review some questions to consider Winter 2006 Imge Enhncement in the Sptil Domin: Bsics of Sptil Filtering, Smoothing Sptil Filters, Order Sttistics Filters

More information