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

Size: px
Start display at page:

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

Transcription

1 Bottom up prsing Generl ide LR(0) SLR LR(1) LLR To best exploit JvCUP, should understnd the theoreticl bsis (LR prsing); 1 Top-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 by hnd but JvCUP (nd ycc) genertes prser from spec; Bottom up prser uses right most derivtion Top down uses left most derivtion; Less grmmr trnsltion is required, hence the grmmr looks more nturl; Intuition: bottom-up prse postpones decisions bout which production rule to pply until it hs more dt thn ws vilble to top-down. Will explin lter 2 1

2 Bottom up prsing Strt with string of terminls; Exmple: Build up from leves of prse Grmmr: tree; pply productions bckwrds; When rech strt symbol & exhusted input, done; Shift-reduce is common bottomup technique. Notice the blue d should not be reduced into B in step 2. S rm Be rm de rm bcde rm bbcde How to get the right reduction steps? Reduce bbcde to S: b bcde bcde bc de d e d e B e Be S S Be bc b B d 3 Sententil form Sententil Form ny string tht cn be derived from non-terminls. Cn consist of terminls nd non terminls. Exmple: E E+T E + id T+id id + id Sententil forms: E+id, T+id,... Right sententil form: obtined by right most derivtion Sentence Sententil form with no non-terminls; id+id is sentence. 4 2

3 Hndles S rm Be rm de rm bcde rm bbcde S Be bc b B d Informlly, hndle of sententil form is substring tht cn be reduced. bc is hndle of the right sententil form bcde, becuse bc, nd fter bc is replced by, the resulting string de is still right sententil form. Is d hndle of bcde? No. this is becuse bcbe is not right sententil form. Formlly, hndle of right sententil form γ is production β nd position in γ where the string β my be found nd replced by. If S * 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 substring β is hndle. 5 Hndles in expression exmple E T + E T T int * T int (E) Consider the string: int * int + int The rightmost derivtion E rm T+E rm T+T rm T+int rm int*t +int rm int*int +int For unmbiguous grmmr, there is exctly one hndle for ech right-sententil form. The question is, how to find the hndle? Observtion: The substring to the right of hndle contins only terminl symbols. 6 3

4 Shift-reduce prsing Brek the input string into two prts: un-digested prt nd semidigested prt Left prt of input prtly processed; Right prt completely unprocessed. int foo (double n) { return (int) n+1 ; } Shifted, prtly reduced So fr unprocessed Use stck to keep trck of tokens seen so fr nd the rules lredy pplied bckwrds (reductions) Shift next input token onto stck When stck top contins good right-hnd-side of production, reduce by rule; Importnt fct: Hndle is lwys t the top of the stck. 7 Shift-reduce min loop Shift: 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 be empty), then reduce the stck to β. The α is clled hndle. Recognizing hndles is key! ccept: S is t the top of the stck nd input now empty, done Error: other cses. 8 4

5 Exmple 1 Prse Stck Remining input Prser ction Grmmr: S > E E > T E + T T > id (E) Input string: (id) (id)$ Shift prenthesis onto stck ( id)$ Shift id onto stck (id )$ Reduce: T id (pop RHS of production, push LHS, input unchnged) (T )$ Reduce: E T (E )$ Shift right prenthesis (E) $ Reduce: T (E) T $ Reduce: E T E $ Reduce: S E S $ Done: ccept S rm E rm T rm (E) rm (T) rm (id) 9 Shift-Reduce Exmple 2 S E E T E + T T id (E) Prse Stck Remining Input ction ( (id (T (E (E+ (E+id (E+T (E (E) T E S (id + id) $ id + id) $ + id) $ + id) $ + id) $ id) $ ) $ ) $ ) $ $ $ $ $ Shift ( Shift id Reduce T id Reduce E T Shift + Shift id Reduce T id Reduce E E+T; (Ignore: E T) Shift ) Reduce T (E) Reduce E T Reduce S E ccept Input: (id + id) (id +id) (T +id) (E +id ) (E+T ) (E) T E S Note tht it is the reverse of the following rightmost derivtion: S rm E rm T rm (E) rm (E+T ) rm (E +id ) rm (T +id) rm (id +id) 10 5

6 Conflicts during shift reduce prsing Reduce/reduce conflict stck input... (E+T ) Which rule we should use, E E+T or E T? Shift/reduce conflict ifstt if (E) S if (E) S else S stck Input... if (E) S else... Both reduce nd shift re pplicble. Wht we should do next, reduce or shift? 11 LR(K) 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 - number of input symbols to select prser ction Most generl prsing technique for deterministic grmmrs. Efficient, Tble-bsed prsing Prses by shift-reduce Cn mssge grmmrs less thn LL(1) Cn hndle lmost ll progrmming lnguge structures LL LR CFG In generl, not prcticl: tbles too lrge (10^6 sttes for C++, d). Common subsets: SLR, LLR (1). 12 6

7 LR Prsing continued Dt structures: Stck of sttes {s} ction tble ction[s,]; T Goto tble Goto[s,X]; X N In LR prsing, push whole sttes on stck Stck of sttes keeps trck of wht we ve seen so fr (left context): wht we ve shifted & reduced & by wht rules. Use ction tbles to decide shift vs reduce Use Goto tble to move to new stte 13 Min loop of LR prser Initil stte S0 strts on top of stck; Given stte St stte on top of stck nd the next input token : If (ction[st, ] == shift Si) Push new stte Si onto stck Cll yylex to get next token If (ction[st, ] == reduce by Y X1 Xn) Pop off n sttes to find Su on top of stck Push new stte Sv = Goto[Su,Y] onto stck If (ction[st, ] == ccept), done! If (ction[st, ] == error), cn t continue to successful prse. 14 7

8 Exmple LR prse tble Stte on TOS ction Goto id + ( ) $ E T 0 S4 S3 S1 S2 1 S5 ccept 2 R2 R2 R2 R2 R2 3 S4 S3 S6 S2 4 R4 R4 R4 R4 R4 5 S4 S3 S8 6 S5 S7 (1) E E + T (2) E T (3) T (E) (4) T id 7 R3 R3 R3 R3 R3 8 R1 R1 R1 R1 R1 If (ction[st, ] == shift), Push new stte ction[st, ] onto stck, Cll yylex to get next token If (ction[st, ] == reduce by Y X1 Xn), Pop off n sttes to find Su on top of stck, Push new stte Sv = Goto[Su,Y] onto stck We explin how to construct this tble lter. 15 Stte on ction Goto TOS id + ( ) $ E T 0 S4 S3 S1 S2 1 S5 ccept 2 R2 R2 R2 R2 R2 3 S4 S3 S6 S2 4 R4 R4 R4 R4 R4 5 S4 S3 S8 6 S5 S7 7 R3 R3 R3 R3 R3 8 R1 R1 R1 R1 R1 (1) E E + T (2) E T (3) T (E) (4) T id Stte stck Remining Input Prser ction S0 id + (id)$ Shift S4 onto stte stck, move hed in input S0S4 + (id)$ Reduce 4) T id, pop stte stck, goto S2, input unchnged S0S2 + (id)$ Reduce 2) E T, goto S1 S0S1 + (id)$ Shift S5 S0S1S5 (id)$ Shift S3 S0S1S5S3 id)$ Shift S4 (sw nother id) S0S1S5S3S4 )$ Reduce 4) T id, goto S2 S0S1S5S3S2 )$ Reduce 2) E T, goto S6 S0S1S5S3S6 )$ Shift S7 S0S1S5S3S6S7 $ Reduce 3) T (E), goto S8 S0S1S5S8 $ Reduce 1) E E + T, goto S1 * S0S1 $ ccept 16 8

9 Types of LR prsers LR (k) SLR (k) -- Simple LR LLR (k) Lookhed LR k = # symbols lookhed 0 or 1 in this clss Drgon book hs generl cses Strt with simplest: LR(0) prser 17 LR (0) prser dvntges: Simplest to understnd, Smllest tbles Disdvntges No lookhed, so too simple-minded for rel prsers Good cse to see how to build tbles, though. We ll use LR(0) constructions in other LR(k) prsers Key to LR prsing is recognizing hndles Hndle: sequence of symbols encoded in top stck sttes representing right-hnd-side of rule we wnt to reduce by. 18 9

10 LR Tbles Given grmmr G, identify possible sttes for prser. Sttes encpsulte wht we ve seen nd shifted nd wht re reduced so fr Steps to construct LR tble: Construct sttes using LR(0) configurtions (or items); Figure out trnsitions between sttes 19 Configurtion configurtion (or item) is rule of G with dot in the right-hnd side. 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 been shifted. To continue prse, we must see token tht could begin string derivble from Y. Nottionl convention: X, Y, Z: symbol, either terminl or non-terminl, b, c : terminl α, β, γ: sequence of terminls or non-terminls 20 10

11 Set of configurtions X YZ mens X hs been shifted. To continue prse, we must see token tht could begin string derivble from Y. Tht is, we need to see token in First(Y) (or in Follow(Y) if Y ε) Formlly, need to see token t such tht Y * t β for some β Suppose Y α β lso in G. Then these configs correspond to the sme prse stte: X YZ Y α Y β Since the bove configurtions represent the sme stte, we cn: Put them into set together. dd ll other equivlent configurtions to chieve closure. (lgorithm lter) This set represents one prser stte: the stte the prser cn be in while prsing string. 21 Trnsitions between sttes Prser goes from one stte to nother bsed on symbols 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 LR(0) prser bsed on this F 22 11

12 Constructing item sets & closure Strting Configurtion: ugment Grmmr with symbol S dd production S S to grmmr Initil item set I0 gets S S Perform Closure on S S (Tht completes prser strt stte.) Compute Successor function to mke next stte (next item set) 23 Computing closure Closure(I) 1. Initilly every item in I is dded to closure(i) 2. If α B β is in closure(i) for ll productions B γ, dd B γ 3. Repet step 2 until set gets no more dditions. Exmple Given the configurtion set: { E E+T} Wht is the closure of { E E+T}: E E + T by rule 1 E T by rule 2 T (E) by rule 2 nd 3 T id by rule 2 nd 3 (1) E E + T (2) E T (3) T (E) (4) T id 24 12

13 Building stte trnsitions LR Tbles need to know wht stte to goto fter shift or reduce. Given Set C & symbol X, we define the set C = Successor (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 by symbol X from one item set to nother; move to the right of X in ll items where dot is before X; remove ll other items; compute closure. C X C 25 Successor exmple Given I= {E E + T, E T, T (E), T id } Wht is successor(i, ( )? move the fter ( : T ( E ) compute the closure: T ( E) E E + T E T T (E) T id (1) E E + T (2) E T (3) T (E) (4) T id 26 13

14 Construct the LR(0) tble Construct F={I0, I1, I2,..., In} Stte i is determined by Ii. The prsing ctions for stte i re: if α is in Ii, then set ction[i, ] to reduce α for ll inputs (if is not S ) If S S is in Ii, then set ction[i, $] to ccept. if α β is in Ii nd successor(ii, )=Ij, then set ction[i,j] to shift j. ( is terminl) The goto trnsitions for stte i re constructed for ll non-terminls using the rule: if successor(ii,)=ij, then goto[i, ]=j. ll entries not defined by bove rules re errors. The initil stte I0 is the one constructed from S S. 27 Steps of constructing LR(0) tble 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 tble nd Goto tble. (0) E E (1) E E + T (2) E T (3) T (E) (4) T id 28 14

15 Configurtion set Successor I0: E' E I1 E E+T I1 E T I2 T (E) I3 T id I4 I1: E' E ccept (dot t end of E rule) E E +T I5 I2: E T Reduce 2 (dot t end) I3: T ( E) I6 E E+T I6 E T I2 T (E) I3 T id I4 I4: T id Reduce 4 (dot t end) I5: E E+ T I8 T (E) I3 T id I4 I6: T (E ) I7 E E +T I5 I7: T (E) Reduce 3 (dot t end) I8: E E+T Reduce 1 (dot t end) Item sets exmple 29 Trnsition digrm I 0 E E' E E E + T E T T (E) T id I 1 E' E E E + T id I 3 I 4 T id id + id ( I 5 E E + T T (E) T id + T E E + T I 7 I 8 T (E) I 2 T E T T ( T ( E) E E + T E T T (E) T id E I 6 T (E ) E E + T ) ( 30 15

16 The prsing tble Stte on TOS ction Goto id + ( ) $ E T 0 S4 S S5 ccept 2 R2 R2 R2 R2 R2 3 S4 S R4 R4 R4 R4 R4 5 S4 S3 8 6 S5 S7 7 R3 R3 R3 R3 R3 8 R1 R1 R1 R1 R1 31 Prsing n erroneous input Stte stck Input Prser ction S0 id + +$ Shift S4 (0) E E (1) E E + T (2) E T (3) T (E) (4) T id S0 S4 + +$ Reduce 4) T id, pop S4, Goto S2 S0 S2 + +$ Reduce 2) E T, pop S2, Goto S1 S0 S1 + +$ Push S5 S0 S1 S5 +$ No ction [S5, +] Error! Stte on ction Goto TOS id + ( ) $ E T 0 S4 S3 S1 S2 1 S5 ccept 2 R2 R2 R2 R2 R2 3 S4 S3 S6 S2 4 R4 R4 R4 R4 R4 5 S4 S3 S8 6 S5 S7 7 R3 R3 R3 R3 R3 8 R1 R1 R1 R1 R

17 Subset construction nd closure I 1 S S S S S I 0 S S' S S S S S S' S I 2 S S I 4 S S I 3 S I 0 S' S S S S S I 2 S S S S S S I 4 S I 3 33 LR(0) grmmr grmmr is LR(0) 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 tble, for ech stte, either shift or reduce 2. There is t most one complete item α in ech configurtion set. No reduce/reduce conflict in tble, for ech stte, use sme reduction rule for every input symbol. Very few grmmrs meet the requirements to be LR(0)

18 I 2 S E E E+T T T id (E) id[e] I 1 E I 0 E' E E E + T E' E E E + T E T T (E) T id T id[e] T E T id ( I 3 T id T id [E] T id [ I 4 T ( E) E E + T E T T (E) T id T id[e] + id I 9 T id[ E] E E +T E T T (E) T id T id[e] E ( E I 5 I 10 E E + T T (E) T id T id[e] + T id[e ] E E + T ] T id[e] I 11 ( + T (E ) E E + T T E E + T I 7 Incomplete digrm T (E) I 6 ) I 8 35 SLR Prse tble (incomplete) Stte on TOS ction Goto id + ( ) $ [ ] E T 0 S4 S S5 ccept 2 R2 R2 R2 R2 (0) S E (1) E E+T (2) E T (3) T id (4) T (E) (5) T id[e] 3 S4 S R3 R3 R3 S9 R3 5 S4 S3 8 6 S5 S7 7 R5 R5 R5 R5 8 R1 R1 R1 R1 9 S4 S S5 S11 11 R5 R5 R5 R

19 Stte stck Input Prser ction S0 id [ id ] + id $ Shift S4 S0 S4 id [ id ] + id $ S9 S0 S4id S9[ id ] + id $ shift S4 S0 S4id S9[ S4id ] +id $ Reduce T id S0 S4id S9[ S2T ] + id $ Reduce E T S0 S4id S9[ S10E ] + id $ S11 S0 S4id S9[ S10E S11] + id $ Reduce T id[e] S0 S2T + id $ Reduce E T S0 S1E + id $ S5 S0 S1E S5+ id $ S4 S0 S1E S5+ S4id $ Reduce T id S0 S1E S5+ S8T $ Reduce E E+T S0 S1E $ ccept 37 I 0 S E E E+T T V=E T id (E) id[e] V id E I 1 E' E E E + T E' E E E + T E T E V=E T (E) T id T id[e] V id I 2 T E > T id I 4 T id T id [E] V id Shift/reduce conflict: T id T id [E] Reduce/reduce conflict: T id V id 38 19

20 SLR Prse tble (incomplete) Stte on TOS ction Goto id + ( ) $ [ ] E T 0 S4 S S5 ccept 2 R2 R2 R2 R2 3 S4 S R4 R4 R4 S9 R4 (0) S E (1) E E+T (2) E T (3) E V=E (4) T id (5) T (E) (6) T id[e] (7) V id 5 S4 S3 8 6 S5 S7 7 R5 R5 R5 R5 8 R1 R1 R1 R LR(0) key points Construct LR tble Strt with ugmented grmmr (S S) 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, symbol). Build ction nd Goto tbles from sttes nd trnsitions. Tbles 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 RHS. n Item set combines vrious pths the prser might hve tken so fr, to diverge s more input is prsed. LR(0) grmmrs re esiest LR to understnd, but too simple to use in rel life prsing

21 Simple LR(1) prsing: SLR LR(0) One LR(0) stte mustn t hve both shift nd reduce items, or two reduce items. So ny complete item (dot t end) must be in its own stte; prser will lwys reduce when in this stte. SLR Peek hed t input to see if reduction is pproprite. Before reducing by rule XYZ, see if the next token is in Follow (). Reduce only in tht cse. Otherwise, shift. 41 Construction for SLR tbles 1. Construct F = {I0, I1,... In }, the LR(0) item sets. 2. Stte i is Ii. The prsing ctions for the stte re: ) If α is in Ii then set ction[i,] to reduce > α for ll in Follow() ( is not S'). b) If S' S is in Ii then set ction[i,$] to ccept. c) If α β is in Ii nd successor(ii, ) = Ij, then set ction[i,] to shift j ( must be terminl). 3. The goto trnsitions for stte i re constructed for ll non-terminls using the rule: If successor(ii, ) = Ij, then Goto [i, ] = j. 4. ll entries not defined by rules 2 nd 3 re errors. 5. The initil stte is closure of set with item S S

22 Properties of SLR Pickier rule bout setting ction tble is the only difference from LR(0) tbles; If G is SLR it is unmbiguous, but not vice vers; Stte cn hve both shift nd reduce items, if Follow sets re disjoint. 43 SLR Exmple Item sets I0 nd successor (I0, id): E' E E E + T E T T (E) T id T id[e] id T id T id [E] E' E E E + T T T (E) id id[e] LR(0) prser sees both shift nd reduce, but SLR prser consults Follow set: Follow(T) = { +, ), ], $ } so T id mens reduce on + or ) or ] or $ T id [E] mens shift otherwise (e.g. on [ ) 44 22

23 SLR Exmple 2 E' E E E + T E T E V = E T (E) T id V id id T id V id E' E E E + T T V = E T (E) id V id Two complete LR(0) items, so reduce-reduce conflict in LR(0) grmmr, but: Follow(T) = { +, ), $ } Follow(V) = { = } Disjoint, so no conflict. Seprte ction entries in tble. 45 SLR grmmr grmmr is SLR if the following two conditions hold: If items α β nd B γ re in stte, then terminl Follow(B). no shift-reduce conflict on ny stte. This mens the successor function for x from tht set either shifts to new stte or reduces, but not both. For ny two complete items α nd B β in stte, the Follow sets must be disjoint. (Follow() Follow(B) is empty.) no reduce-reduce conflict on ny stte. If more thn one non-terminl could be reduced from this set, it must be possible to uniquely determine which using only one token of lookhed. Compre with LR(0) grmmr: 1. For ny configurtion set contining the item α β, there is no complete item B γ in tht set. 2. There is t most one complete item α in ech configurtion set. Note tht LR(0) SLR 46 23

24 SLR 1. S S 2. S dc 3. S db 4. c In S3 there is reduce/shift conflict: It cn be R4 or shift. By looking t the Follow set of, the conflict is removed. ction Goto b c d $ S S0 S2 1 S1 S2 S3 4 S3 S5 R4 S4 S6 S5 R2 S6 R3 S0: S' S S dc S db S S1: S S d S2: S d c S d b c c S3: S dc c S4: S d b b S5: S dc S6: S db 47 Prse trce Stte stck Input Prser ction S0 dc$ Shift S2 S0 S2d c$ Shift S3 S0 S2d S3c $ shift S5 S0 S2d S3c S5 $ Reduce 2 S0 S1S $ ccept 48 24

25 Non-SLR exmple 1. S S 2. S dc 3. S db 4. S 5. c S0: S' S S dc S db S c c S S9: c d S1: S S S2: S d c S d b c S7: S c S3: S dc c S4: S d b S8: S S3 hs shift/reduce conflict. By looking t Follow(), both nd b re in the follow set. So under column we still don t know whether to reduce or shift. b S5: S dc S6: S db 49 The conflict SLR prsing tble ction Goto b c d $ S S0 S9 S2 1 7 S1 S2 S3 4 S3 S5/R5 R5 S4 S6 S5 R2 S6 R3 S7 S8 S8 R4 S9 R5 R5 Follow() = {, b} 50 25

26 LR(1) prsing Mke items crry more informtion. LR(1) item: X1...Xi Xi+1...Xj, tok Terminl tok is the lookhed. Mening: hve sttes for X1...Xi on stck lredy expect to put sttes for Xi+1...Xj onto stck nd then reduce, but only if token following Xj is tok tok cn be $ Split Follow() into seprte cses Cn cluster items nottionlly: [ α, /b/c] mens the three items: [ α, ] [ α, b] [ α, c] Reduce α to if next token is or b or c {, b, c } Follow() 51 LR(1) item sets More items nd more item sets thn SLR Closure: For ech item [ α Bβ, ] in I, for ech production B γ in G, nd for ech terminl b in First(β), dd [B γ, b] to I (dd only items with the correct lookhed) Once we hve closed item set, use LR(1) successor function to compute trnsitions nd next items. Exmple: Initil item: [S S, $] Wht is the closure? [S dc, $] [S db, $] [S, $] [ c, ] S S S dc db c 52 26

27 LR(1) successor function Given I n item set with [ α Xβ, ], dd [ α X β, ] to item set J. successor(i,x) is the closure of set J. Similr to successor function to LR(0), but we propgte the lookhed token for ech item. Exmple S0: S' S, $ S dc, $ S db, $ S, $ c, c S d S1: S S, $ S2: S d c, $ S d b, $ c, b S9: c, 53 LR(1) tbles ction tble entries: If [ α, ] Ii, then set ction[i,] to reduce by rule α ( is not S'). If [S S, $] Ii then set ction[i,$] to ccept. If [ α β, b] is in Ii nd succ(ii, ) = Ij, then set ction[i,] to shift j. Here is terminl. Goto entries: For ech stte I & ech non-terminl : If succ(ii, ) = Ij, then Goto [i, ] = j

28 LR(1) digrm 1. S S 2. S dc 3. S db 4. S 5. c S0: S' S, $ S dc, $ S db, $ S, $ c, c S S9: c, d S1: S S, $ S2: S d c, $ S d b, $ c, b S7: S, $ c S3: S dc, $ c, b S4: S d b, $ S8: S, $ b S5: S dc, $ S6: S db, $ 55 Crete the LR(1) prse tble ction Goto b c d $ S S0 S9 S2 1 7 S1 S2 S3 4 S3 S5 R5 S4 S6 S5 R2 S6 R3 S7 S8 S8 R4 S9 R

29 nother LR(1) exmple Crete the trnsition digrm 0) S' S 1) S 2) 3) b S0: S' S, $ S, $, /b b, /b b S S9: b, /b S1: S S, $ S2: S, $, $ b, $ S7:, /b, /b b, /b b b S3: S, $ S4:, $, $ b, $ b S5: b, $ S8:, /b S6:, $ 57 Prse tble stte ction Goto b $ S S0 S7 S9 1 2 S1 ccept S2 S4 S5 3 S3 R1 S4 S4 S5 6 S5 R3 S6 R2 S7 S7 S9 8 S8 R2 R2 S9 R3 R

30 Prse trce stck remining input prse ction S0 bb$ S9 S0S9 b$ R3 b S0S2 b S4 S0S2S4 b S4 S0S2S4S4 b S5 S0S2S4S4S5 $ R3 b S0S2S4S4S6 $ R2 S0S2S4S6 $ R2 S0S2S3 $ R1 S S0S1 $ ccept 59 LR(1) grmmr grmmr is LR(1) if the following 2 conditions re stisfied for ech configurtion set: For ech item [ α β, b] in the set, there is no item in the set of the form [B γ, ] In the ction tble, this trnsltes to no shift/reduce conflict. If there re two complete items [ α, ] nd [B β, b] in the set, then nd b should be different. In the ction tble, this trnsltes to no reduce/reduce conflict Compre with the SLR grmmr For ny item α β in the set, with terminl, there is no complete item B γ in tht set with in Follow(B). For ny two complete items α nd B β in the set, the Follow sets must be disjoint. Note tht SLR(1) LR(1) LR(0) SLR(1) LR(1) 60 30

31 LR(1) tbles continued LR(1) tbles cn get big exponentil in size of rules Cn we keep the dditionl power we got from going SLR LR without tble explosion? LLR! We split SLR(1) sttes to get LR(1) sttes, mybe too ggressively. Try to merge item sets tht re lmost identicl. Tricky bit: Don t introduce shift-reduce or reduce-reduce conflicts. 61 LLR pproch Just sy LLR (it s lwys 1 in prctice) Given the numerous LR(1) sttes for grmmr G, consider merging similr sttes, to get fewer sttes. Cndidtes for merging: sme core (LR(0) item) only differences in lookheds Exmple: S1: X α β, /b/c S2: X α β, c/d S12: X α β, /b/c/d 62 31

32 Sttes with sme core items S0: S' S, $ S, $, /b b, /b b S S9: b, /b S1: S S, $ S2: S, $, $ b, $ S7:, /b, /b b, /b b b S3: S, $ S4:, $, $ b, $ b S5: b, $ S8:, /b 0) S' S 1) S 2) 3) b S6:, $ 63 Merge the sttes S0: S' S, $ S, $, /b b, /b b b S S59: b, /b/$ S1: S S, $ S2: S, $, $ b, $ S47:, /b/$, /b/$ b, /b/$ b S3: S, $ S4:, $, $ b, $ b S5: b, $ S68:, /b/$ 0) S' S 1) S 2) 3) b S6:, $ 64 32

33 Merge the sttes S0: S' S, $ S, $, /b b, /b b b S S59: b, /b/$ S1: S S, $ S2: S, $, $ b, $ S47:, /b/$, /b/$ b, /b/$ b S3: S, $ S68:, /b/$ 0) S' S 1) S 2) 3) b Follow()={ b $ } 65 fter the merge Wht hppened when we merged? Three fewer sttes Lookhed on items merged. In this cse, lookhed in merged sets constitutes entire Follow set. So, we mde SLR(1) grmmr by merging. Result of merge usully not SLR(1)

34 conflict fter merging 1) S Bc Cd bbd bcc 2) B e 3) C e S0: S' S, $ S Bc, $ S Cd, $ S bbd, $ S bcc,$ S S1: S S, $ S2: S Bc, $ S Cd, $ B e, c C e,d e S3: B e, c C e, d b S4: S b Bd, $ S b Cc, $ B e, d C e, c e S5: B e, d C e, c 67 Prcticl considertion mbiguity in LR grmmrs G: G produces multiple rightmost derivtions. (i.e. cn build two different prse trees for one input string.) Remember: E E + E E * E (E) id We dded terms nd fctors to force unmbiguous prse with correct precedence nd ssocitivity Wht if we threw the grmmr into n LR-grmmr tble-construction mchine nywy? Conflicts = multiple ction entries for one cell We choose which entry to keep, toss others 68 34

35 Precedence nd ssocitivity in JvCUP E S0: E' E E E+E E E*E E (E) E id S1: E E E E +E E E * E ( S2: E ( E) E E+E E E*E E (E) + * ( S4: E E+ E E E+E E E*E E (E) E id S5: E E* E E E+E E E*E E (E) E id * E + + E S8: E E*E E E +E E E *E E E + E E * E (E) id * S7: E E+E E E +E E E *E id S3: E id E S6: E (E ) E E +E E E *E ) S9: E (E) 69 JvCup grmmr terminl PLUS, TIMES; precedence left PLUS; precedence left TIME; E::=E PLUS E E TIMES E ID Wht if the input is x+y+z? When shifting + conflicts with reducing production contining +, choose reduce Wht if the input is x+y*z? Wht if the input is x*y+z? 70 35

36 Trnsition digrm for ssignment expr S1: S S, $ S id V=E V id E V n S S0: S' S, $ S id, $ S V=E, $ V id, = V S3: S V =E, $ S2: S id, $ V id, = id = S4: S V= E, $ E V, $ E n, $ V id, $ E V n id S5: S V=E, $ S6: E V, $ S7: E n, $ S8: V id, $ 71 Why re there conflicts in some rules in ssignments? P S2: S P, $ Non LR(1) grmmr P m Pm S0: S' P, $ P m, $ P Pm, $ P, $ P, m P m, m P Pm, m m S1: P m, $/m It is n mbiguous grmmr. There re two rightmost/leftmost derivtions for sentence m: P Pm m P m *** Shift/Reduce conflict found in stte #0 between P ::= (*) nd P ::= (*) m under symbol m 72 36

37 slightly chnged grmmr, still not LR P S2: S P, $ Non LR(1) grmmr P m m P S0: S' P, $ P m, $ P mp, $ P, $ m S1: P m, $ P m P, $ P m, $ P mp, $ P, $ P S3: P mp, $ It is n mbiguous grmmr. There re two prse trees for sentence m: P mp m P m Reduce/Reduce conflict found in stte #1 between P ::= m (*) nd P ::= (*) under symbols: {EOF} Produced from jvcup 73 Modified LR(1) grmmr S0: S' P, $ P, $ P mp, $ P m S1: S P, $ S2: P m P, $ P mp, $ P, $ P LR(1) grmmr P m P S3: P mp, $ Note tht there re no conflicts The derivtion: P mp mmp mm 74 37

38 nother wy of chnging to LR(1) grmmr LR(1) grmmr P Q Q m m Q P S1: S P, $ S0: S' P, $ P Q, $ P, $ Q m, $ Q mq, $ m S2: Q m, $ Q m Q, $ Q m, $ Q mq, $ Q S3: Q mq, $ 75 LR grmmrs: comprison LR(0) SLR(1) LLR LR(1) CFG dvntges Disdvntges LR(0) Smllest tbles, esiest to build Indequte for mny PL structures SLR(1) LLR(1) LR(1) More inclusive, more inform?on thn LR(0) Sme size tbles s SLR, more lngs, efficient to build Most precise use of lookhed, most PL structures we wnt Mny useful grmmrs re not SLR (1) empiricl, not mthem?cl Tbles order of mgnitude > SLR(1) 76 38

39 The spce of grmmrs CFG Unmbiguous CFG LL(1) LR(1) LLR(1) SLR(1) LR(0) 77 The spce of grmmrs Wht re used in prctice CFG Unmbiguous CFG LL(1) LR(1) LLR(1) SLR(1) LR(0) 78 39

40 Verifying the lnguge generted by grmmr To verify grmmr: every string generted by G is in L every string in L cn be generted by G Exmple: S (S)S ε the lnguge is ll the strings of blnced prenthesis, such s (), (()), () (()()) Proof prt 1: every sentence derived from S is blnced. bsis: empty string is blnced. induction: suppose tht ll derivtions fewer thn n steps produce blnced sentences, nd consider leftmost derivtion of n steps. such derivtion must be of the form: S (S)S *(x)s *(x)y Proof prt 2: every blnced string cn be derived from S Bsis: the empty string cn be derived from S. Induction: suppose tht every blnced string of length less thn 2n cn be derived from S. Consider blnced string w of length 2n. w must strt with (. w cn be written s (x)y, where x, y re blnced. 79 Hierrchy of grmmrs Lnguge 1: ny string of nd b CFG is more powerful thn RE Type n grmmr is more powerful thn type n+1 grmmr Exmple: Σ={, b} The lnguge of ny string consists of nd b CFG b ε Cn be describe by RE The lnguge of plindromes consist of nd b bb b ε Cn be described by CFG, but not RE Lnguge 2: plindromes Lnguge 3 Lnguge 2 Lnguge 5 Lnguge 4 RG Lnguge 1 When grmmr is more powerful, it is not tht it cn describe lrger lnguge. Insted, the power mens the bility to restrict the set. More powerful grmmr cn define more complicted boundry between correct nd incorrect sentences. Therefore, more different lnguges 80 40

41 Metphoric comprison of grmmrs RE drw the rose use stright lines (ruler nd T-squre suffice) CFG pproximte the outline by stright lines nd circle segments (ruler nd compsses) 81 41

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

1.4 Nonregular Languages

1.4 Nonregular Languages 74 1.4 Nonregulr Lnguges The number of forml lnguges over ny lphbet (= decision/recognition problems) is uncountble On the other hnd, the number of regulr expressions (= strings) is countble Hence, ll

More information

Convert the NFA into DFA

Convert the NFA into DFA Convert the NF into F For ech NF we cn find F ccepting the sme lnguge. The numer of sttes of the F could e exponentil in the numer of sttes of the NF, ut in prctice this worst cse occurs rrely. lgorithm:

More information

CS 275 Automata and Formal Language Theory

CS 275 Automata and Formal Language Theory CS 275 Automt nd Forml Lnguge Theory Course Notes Prt II: The Recognition Problem (II) Chpter II.6.: Push Down Automt Remrk: This mteril is no longer tught nd not directly exm relevnt Anton Setzer (Bsed

More information

Closure Properties of Regular Languages

Closure Properties of Regular Languages Closure Properties of Regulr Lnguges Regulr lnguges re closed under mny set opertions. Let L 1 nd L 2 e regulr lnguges. (1) L 1 L 2 (the union) is regulr. (2) L 1 L 2 (the conctention) is regulr. (3) L

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages C 314 Principles of Progrmming Lnguges Lecture 6: LL(1) Prsing Zheng (Eddy) Zhng Rutgers University Ferury 5, 2018 Clss Informtion Homework 2 due tomorrow. Homework 3 will e posted erly next week. 2 Top

More information

Finite Automata. Informatics 2A: Lecture 3. John Longley. 22 September School of Informatics University of Edinburgh

Finite Automata. Informatics 2A: Lecture 3. John Longley. 22 September School of Informatics University of Edinburgh Lnguges nd Automt Finite Automt Informtics 2A: Lecture 3 John Longley School of Informtics University of Edinburgh jrl@inf.ed.c.uk 22 September 2017 1 / 30 Lnguges nd Automt 1 Lnguges nd Automt Wht is

More information

Overview HC9. Parsing: Top-Down & LL(1) Context-Free Grammars (1) Introduction. CFGs (3) Context-Free Grammars (2) Vertalerbouw HC 9: Ch.

Overview HC9. Parsing: Top-Down & LL(1) Context-Free Grammars (1) Introduction. CFGs (3) Context-Free Grammars (2) Vertalerbouw HC 9: Ch. Overview H9 Vertlerouw H 9: Prsing: op-down & LL(1) do 3 mei 2001 56 heo Ruys h. 8 - Prsing 8.1 ontext-free Grmmrs 8.2 op-down Prsing 8.3 LL(1) Grmmrs See lso [ho, Sethi & Ullmn 1986] for more thorough

More information

1.3 Regular Expressions

1.3 Regular Expressions 56 1.3 Regulr xpressions These hve n importnt role in describing ptterns in serching for strings in mny pplictions (e.g. wk, grep, Perl,...) All regulr expressions of lphbet re 1.Ønd re regulr expressions,

More information

For convenience, we rewrite m2 s m2 = m m m ; where m is repeted m times. Since xyz = m m m nd jxyj»m, we hve tht the string y is substring of the fir

For convenience, we rewrite m2 s m2 = m m m ; where m is repeted m times. Since xyz = m m m nd jxyj»m, we hve tht the string y is substring of the fir CSCI 2400 Models of Computtion, Section 3 Solutions to Homework 4 Problem 1. ll the solutions below refer to the Pumping Lemm of Theorem 4.8, pge 119. () L = f n b l k : k n + lg Let's ssume for contrdiction

More information

The transformation to right derivation is called the canonical reduction sequence. Bottom-up analysis

The transformation to right derivation is called the canonical reduction sequence. Bottom-up analysis Bottom-up nlysis Shift-reduce prsing. Constructs the derivtion tree from ottom to top. Reduces the string to the strt symol. Produces reverse right derivtion. Exmple: G(E): 1. E E + T 2. T 3. T T * F 4.

More information

Finite Automata. Informatics 2A: Lecture 3. Mary Cryan. 21 September School of Informatics University of Edinburgh

Finite Automata. Informatics 2A: Lecture 3. Mary Cryan. 21 September School of Informatics University of Edinburgh Finite Automt Informtics 2A: Lecture 3 Mry Cryn School of Informtics University of Edinburgh mcryn@inf.ed.c.uk 21 September 2018 1 / 30 Lnguges nd Automt Wht is lnguge? Finite utomt: recp Some forml definitions

More information

5.1 Definitions and Examples 5.2 Deterministic Pushdown Automata

5.1 Definitions and Examples 5.2 Deterministic Pushdown Automata CSC4510 AUTOMATA 5.1 Definitions nd Exmples 5.2 Deterministic Pushdown Automt Definitions nd Exmples A lnguge cn be generted by CFG if nd only if it cn be ccepted by pushdown utomton. A pushdown utomton

More information

CS 275 Automata and Formal Language Theory

CS 275 Automata and Formal Language Theory CS 275 Automt nd Forml Lnguge Theory Course Notes Prt II: The Recognition Problem (II) Chpter II.5.: Properties of Context Free Grmmrs (14) Anton Setzer (Bsed on book drft by J. V. Tucker nd K. Stephenson)

More information

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages Deprtment of Computer Science, Austrlin Ntionl University COMP2600 Forml Methods for Softwre Engineering Semester 2, 206 Assignment Automt, Lnguges, nd Computility Smple Solutions Finite Stte Automt nd

More information

FABER Formal Languages, Automata and Models of Computation

FABER Formal Languages, Automata and Models of Computation DVA337 FABER Forml Lnguges, Automt nd Models of Computtion Lecture 5 chool of Innovtion, Design nd Engineering Mälrdlen University 2015 1 Recp of lecture 4 y definition suset construction DFA NFA stte

More information

Formal languages, automata, and theory of computation

Formal languages, automata, and theory of computation Mälrdlen University TEN1 DVA337 2015 School of Innovtion, Design nd Engineering Forml lnguges, utomt, nd theory of computtion Thursdy, Novemer 5, 14:10-18:30 Techer: Dniel Hedin, phone 021-107052 The exm

More information

Harvard University Computer Science 121 Midterm October 23, 2012

Harvard University Computer Science 121 Midterm October 23, 2012 Hrvrd University Computer Science 121 Midterm Octoer 23, 2012 This is closed-ook exmintion. You my use ny result from lecture, Sipser, prolem sets, or section, s long s you quote it clerly. The lphet is

More information

CMSC 330: Organization of Programming Languages. DFAs, and NFAs, and Regexps (Oh my!)

CMSC 330: Organization of Programming Languages. DFAs, and NFAs, and Regexps (Oh my!) CMSC 330: Orgniztion of Progrmming Lnguges DFAs, nd NFAs, nd Regexps (Oh my!) CMSC330 Spring 2018 Types of Finite Automt Deterministic Finite Automt (DFA) Exctly one sequence of steps for ech string All

More information

Review for the Midterm

Review for the Midterm Review for the Midterm Stephen A. Edwrds Columi University Fll 2018 The Midterm Structure of Compiler Scnning Lnguges nd Regulr Expressions NFAs Trnslting REs into NFAs: Thompson s Construction Building

More information

Minimal DFA. minimal DFA for L starting from any other

Minimal DFA. minimal DFA for L starting from any other Miniml DFA Among the mny DFAs ccepting the sme regulr lnguge L, there is exctly one (up to renming of sttes) which hs the smllest possile numer of sttes. Moreover, it is possile to otin tht miniml DFA

More information

This lecture covers Chapter 8 of HMU: Properties of CFLs

This lecture covers Chapter 8 of HMU: Properties of CFLs This lecture covers Chpter 8 of HMU: Properties of CFLs Turing Mchine Extensions of Turing Mchines Restrictions of Turing Mchines Additionl Reding: Chpter 8 of HMU. Turing Mchine: Informl Definition B

More information

Parse trees, ambiguity, and Chomsky normal form

Parse trees, ambiguity, and Chomsky normal form Prse trees, miguity, nd Chomsky norml form In this lecture we will discuss few importnt notions connected with contextfree grmmrs, including prse trees, miguity, nd specil form for context-free grmmrs

More information

Formal Languages Simplifications of CFGs

Formal Languages Simplifications of CFGs Forml Lnguges implifictions of CFGs ubstitution Rule Equivlent grmmr b bc ubstitute b bc bbc b 2 ubstitution Rule b bc bbc ubstitute b bc bbc bc Equivlent grmmr 3 In generl: xz y 1 ubstitute y 1 xz xy1z

More information

Handout: Natural deduction for first order logic

Handout: Natural deduction for first order logic MATH 457 Introduction to Mthemticl Logic Spring 2016 Dr Json Rute Hndout: Nturl deduction for first order logic We will extend our nturl deduction rules for sententil logic to first order logic These notes

More information

Lecture 09: Myhill-Nerode Theorem

Lecture 09: Myhill-Nerode Theorem CS 373: Theory of Computtion Mdhusudn Prthsrthy Lecture 09: Myhill-Nerode Theorem 16 Ferury 2010 In this lecture, we will see tht every lnguge hs unique miniml DFA We will see this fct from two perspectives

More information

CSC 473 Automata, Grammars & Languages 11/9/10

CSC 473 Automata, Grammars & Languages 11/9/10 CSC 473 utomt, Grmmrs & Lnguges 11/9/10 utomt, Grmmrs nd Lnguges Discourse 06 Decidbility nd Undecidbility Decidble Problems for Regulr Lnguges Theorem 4.1: (embership/cceptnce Prob. for DFs) = {, w is

More information

Parsing and Pattern Recognition

Parsing and Pattern Recognition Topics in IT Prsing nd Pttern Recognition Week Context-Free Prsing College of Informtion Science nd Engineering Ritsumeikn University this week miguity in nturl lnguge in mchine lnguges top-down, redth-first

More information

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun: CMPU 240 Lnguge Theory nd Computtion Spring 2019 NFAs nd Regulr Expressions Lst clss: Introduced nondeterministic finite utomt with -trnsitions Tody: Prove n NFA- is no more powerful thn n NFA Introduce

More information

First Midterm Examination

First Midterm Examination Çnky University Deprtment of Computer Engineering 203-204 Fll Semester First Midterm Exmintion ) Design DFA for ll strings over the lphet Σ = {,, c} in which there is no, no nd no cc. 2) Wht lnguge does

More information

More on automata. Michael George. March 24 April 7, 2014

More on automata. Michael George. March 24 April 7, 2014 More on utomt Michel George Mrch 24 April 7, 2014 1 Automt constructions Now tht we hve forml model of mchine, it is useful to mke some generl constructions. 1.1 DFA Union / Product construction Suppose

More information

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4 Intermedite Mth Circles Wednesdy, Novemer 14, 2018 Finite Automt II Nickols Rollick nrollick@uwterloo.c Regulr Lnguges Lst time, we were introduced to the ide of DFA (deterministic finite utomton), one

More information

Chapter 2 Finite Automata

Chapter 2 Finite Automata Chpter 2 Finite Automt 28 2.1 Introduction Finite utomt: first model of the notion of effective procedure. (They lso hve mny other pplictions). The concept of finite utomton cn e derived y exmining wht

More information

1 Structural induction

1 Structural induction Discrete Structures Prelim 2 smple questions Solutions CS2800 Questions selected for Spring 2018 1 Structurl induction 1. We define set S of functions from Z to Z inductively s follows: Rule 1. For ny

More information

Lecture 08: Feb. 08, 2019

Lecture 08: Feb. 08, 2019 4CS4-6:Theory of Computtion(Closure on Reg. Lngs., regex to NDFA, DFA to regex) Prof. K.R. Chowdhry Lecture 08: Fe. 08, 2019 : Professor of CS Disclimer: These notes hve not een sujected to the usul scrutiny

More information

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9.

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9. Regulr Expressions, Pumping Lemm, Right Liner Grmmrs Ling 106 Mrch 25, 2002 1 Regulr Expressions A regulr expression descries or genertes lnguge: it is kind of shorthnd for listing the memers of lnguge.

More information

Anatomy of a Deterministic Finite Automaton. Deterministic Finite Automata. A machine so simple that you can understand it in less than one minute

Anatomy of a Deterministic Finite Automaton. Deterministic Finite Automata. A machine so simple that you can understand it in less than one minute Victor Admchik Dnny Sletor Gret Theoreticl Ides In Computer Science CS 5-25 Spring 2 Lecture 2 Mr 3, 2 Crnegie Mellon University Deterministic Finite Automt Finite Automt A mchine so simple tht you cn

More information

Automata and Languages

Automata and Languages Automt nd Lnguges Prof. Mohmed Hmd Softwre Engineering Lb. The University of Aizu Jpn Grmmr Regulr Grmmr Context-free Grmmr Context-sensitive Grmmr Regulr Lnguges Context Free Lnguges Context Sensitive

More information

Chapter 0. What is the Lebesgue integral about?

Chapter 0. What is the Lebesgue integral about? Chpter 0. Wht is the Lebesgue integrl bout? The pln is to hve tutoril sheet ech week, most often on Fridy, (to be done during the clss) where you will try to get used to the ides introduced in the previous

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Orgniztion of Progrmming Lnguges Finite Automt 2 CMSC 330 1 Types of Finite Automt Deterministic Finite Automt (DFA) Exctly one sequence of steps for ech string All exmples so fr Nondeterministic

More information

Lexical Analysis Finite Automate

Lexical Analysis Finite Automate Lexicl Anlysis Finite Automte CMPSC 470 Lecture 04 Topics: Deterministic Finite Automt (DFA) Nondeterministic Finite Automt (NFA) Regulr Expression NFA DFA A. Finite Automt (FA) FA re grph, like trnsition

More information

CS 311 Homework 3 due 16:30, Thursday, 14 th October 2010

CS 311 Homework 3 due 16:30, Thursday, 14 th October 2010 CS 311 Homework 3 due 16:30, Thursdy, 14 th Octoer 2010 Homework must e sumitted on pper, in clss. Question 1. [15 pts.; 5 pts. ech] Drw stte digrms for NFAs recognizing the following lnguges:. L = {w

More information

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014 CMPSCI 250: Introduction to Computtion Lecture #31: Wht DFA s Cn nd Cn t Do Dvid Mix Brrington 9 April 2014 Wht DFA s Cn nd Cn t Do Deterministic Finite Automt Forml Definition of DFA s Exmples of DFA

More information

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1 Chpter Five: Nondeterministic Finite Automt Forml Lnguge, chpter 5, slide 1 1 A DFA hs exctly one trnsition from every stte on every symol in the lphet. By relxing this requirement we get relted ut more

More information

1 Nondeterministic Finite Automata

1 Nondeterministic Finite Automata 1 Nondeterministic Finite Automt Suppose in life, whenever you hd choice, you could try oth possiilities nd live your life. At the end, you would go ck nd choose the one tht worked out the est. Then you

More information

a b b a pop push read unread

a b b a pop push read unread A Finite Automton A Pushdown Automton 0000 000 red unred b b pop red unred push 2 An Exmple A Pushdown Automton Recll tht 0 n n not regulr. cn push symbols onto the stck cn pop them (red them bck) lter

More information

Let's start with an example:

Let's start with an example: Finite Automt Let's strt with n exmple: Here you see leled circles tht re sttes, nd leled rrows tht re trnsitions. One of the sttes is mrked "strt". One of the sttes hs doule circle; this is terminl stte

More information

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb. CMSC 330: Orgniztion of Progrmming Lnguges Finite Automt 2 Types of Finite Automt Deterministic Finite Automt () Exctly one sequence of steps for ech string All exmples so fr Nondeterministic Finite Automt

More information

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2 CMSC 330: Orgniztion of Progrmming Lnguges Finite Automt 2 Types of Finite Automt Deterministic Finite Automt () Exctly one sequence of steps for ech string All exmples so fr Nondeterministic Finite Automt

More information

First Midterm Examination

First Midterm Examination 24-25 Fll Semester First Midterm Exmintion ) Give the stte digrm of DFA tht recognizes the lnguge A over lphet Σ = {, } where A = {w w contins or } 2) The following DFA recognizes the lnguge B over lphet

More information

Nondeterminism. Nondeterministic Finite Automata. Example: Moves on a Chessboard. Nondeterminism (2) Example: Chessboard (2) Formal NFA

Nondeterminism. Nondeterministic Finite Automata. Example: Moves on a Chessboard. Nondeterminism (2) Example: Chessboard (2) Formal NFA Nondeterminism Nondeterministic Finite Automt Nondeterminism Subset Construction A nondeterministic finite utomton hs the bility to be in severl sttes t once. Trnsitions from stte on n input symbol cn

More information

11.1 Finite Automata. CS125 Lecture 11 Fall Motivation: TMs without a tape: maybe we can at least fully understand such a simple model?

11.1 Finite Automata. CS125 Lecture 11 Fall Motivation: TMs without a tape: maybe we can at least fully understand such a simple model? CS125 Lecture 11 Fll 2016 11.1 Finite Automt Motivtion: TMs without tpe: mybe we cn t lest fully understnd such simple model? Algorithms (e.g. string mtching) Computing with very limited memory Forml verifiction

More information

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38 Theory of Computtion Regulr Lnguges (NTU EE) Regulr Lnguges Fll 2017 1 / 38 Schemtic of Finite Automt control 0 0 1 0 1 1 1 0 Figure: Schemtic of Finite Automt A finite utomton hs finite set of control

More information

Normal Forms for Context-free Grammars

Normal Forms for Context-free Grammars Norml Forms for Context-free Grmmrs 1 Linz 6th, Section 6.2 wo Importnt Norml Forms, pges 171--178 2 Chomsky Norml Form All productions hve form: A BC nd A vrile vrile terminl 3 Exmples: S AS S AS S S

More information

7.2 The Definite Integral

7.2 The Definite Integral 7.2 The Definite Integrl the definite integrl In the previous section, it ws found tht if function f is continuous nd nonnegtive, then the re under the grph of f on [, b] is given by F (b) F (), where

More information

Riemann Sums and Riemann Integrals

Riemann Sums and Riemann Integrals Riemnn Sums nd Riemnn Integrls Jmes K. Peterson Deprtment of Biologicl Sciences nd Deprtment of Mthemticl Sciences Clemson University August 26, 203 Outline Riemnn Sums Riemnn Integrls Properties Abstrct

More information

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.) CS 373, Spring 29. Solutions to Mock midterm (sed on first midterm in CS 273, Fll 28.) Prolem : Short nswer (8 points) The nswers to these prolems should e short nd not complicted. () If n NF M ccepts

More information

Riemann Sums and Riemann Integrals

Riemann Sums and Riemann Integrals Riemnn Sums nd Riemnn Integrls Jmes K. Peterson Deprtment of Biologicl Sciences nd Deprtment of Mthemticl Sciences Clemson University August 26, 2013 Outline 1 Riemnn Sums 2 Riemnn Integrls 3 Properties

More information

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Finite Automt Theory nd Forml Lnguges TMV027/DIT321 LP4 2018 Lecture 10 An Bove April 23rd 2018 Recp: Regulr Lnguges We cn convert between FA nd RE; Hence both FA nd RE ccept/generte regulr lnguges; More

More information

Talen en Automaten Test 1, Mon 7 th Dec, h45 17h30

Talen en Automaten Test 1, Mon 7 th Dec, h45 17h30 Tlen en Automten Test 1, Mon 7 th Dec, 2015 15h45 17h30 This test consists of four exercises over 5 pges. Explin your pproch, nd write your nswer to ech exercise on seprte pge. You cn score mximum of 100

More information

Bottom-Up Parsing. Canonical Collection of LR(0) items. Part II

Bottom-Up Parsing. Canonical Collection of LR(0) items. Part II 2 ottom-up Prsing Prt II 1 Cnonil Colletion of LR(0) items CC_LR(0)_I items(g :ugmented_grmmr){ C = {CLOURE({ })} ; repet{ foreh(i C) foreh(grmmr symol X) if(goto(i,x) && GOTO(I,X) C) C = C {GOTO(I,X)};

More information

CSCI 340: Computational Models. Kleene s Theorem. Department of Computer Science

CSCI 340: Computational Models. Kleene s Theorem. Department of Computer Science CSCI 340: Computtionl Models Kleene s Theorem Chpter 7 Deprtment of Computer Science Unifiction In 1954, Kleene presented (nd proved) theorem which (in our version) sttes tht if lnguge cn e defined y ny

More information

1 Structural induction, finite automata, regular expressions

1 Structural induction, finite automata, regular expressions Discrete Structures Prelim 2 smple uestions s CS2800 Questions selected for spring 2017 1 Structurl induction, finite utomt, regulr expressions 1. We define set S of functions from Z to Z inductively s

More information

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER LANGUAGES AND COMPUTATION ANSWERS

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER LANGUAGES AND COMPUTATION ANSWERS The University of Nottinghm SCHOOL OF COMPUTER SCIENCE LEVEL 2 MODULE, SPRING SEMESTER 2016 2017 LNGUGES ND COMPUTTION NSWERS Time llowed TWO hours Cndidtes my complete the front cover of their nswer ook

More information

Regular Language. Nonregular Languages The Pumping Lemma. The pumping lemma. Regular Language. The pumping lemma. Infinitely long words 3/17/15

Regular Language. Nonregular Languages The Pumping Lemma. The pumping lemma. Regular Language. The pumping lemma. Infinitely long words 3/17/15 Regulr Lnguge Nonregulr Lnguges The Pumping Lemm Models of Comput=on Chpter 10 Recll, tht ny lnguge tht cn e descried y regulr expression is clled regulr lnguge In this lecture we will prove tht not ll

More information

Infinite Geometric Series

Infinite Geometric Series Infinite Geometric Series Finite Geometric Series ( finite SUM) Let 0 < r < 1, nd let n be positive integer. Consider the finite sum It turns out there is simple lgebric expression tht is equivlent to

More information

19 Optimal behavior: Game theory

19 Optimal behavior: Game theory Intro. to Artificil Intelligence: Dle Schuurmns, Relu Ptrscu 1 19 Optiml behvior: Gme theory Adversril stte dynmics hve to ccount for worst cse Compute policy π : S A tht mximizes minimum rewrd Let S (,

More information

Improper Integrals, and Differential Equations

Improper Integrals, and Differential Equations Improper Integrls, nd Differentil Equtions October 22, 204 5.3 Improper Integrls Previously, we discussed how integrls correspond to res. More specificlly, we sid tht for function f(x), the region creted

More information

20 MATHEMATICS POLYNOMIALS

20 MATHEMATICS POLYNOMIALS 0 MATHEMATICS POLYNOMIALS.1 Introduction In Clss IX, you hve studied polynomils in one vrible nd their degrees. Recll tht if p(x) is polynomil in x, the highest power of x in p(x) is clled the degree of

More information

Exercises Chapter 1. Exercise 1.1. Let Σ be an alphabet. Prove wv = w + v for all strings w and v.

Exercises Chapter 1. Exercise 1.1. Let Σ be an alphabet. Prove wv = w + v for all strings w and v. 1 Exercises Chpter 1 Exercise 1.1. Let Σ e n lphet. Prove wv = w + v for ll strings w nd v. Prove # (wv) = # (w)+# (v) for every symol Σ nd every string w,v Σ. Exercise 1.2. Let w 1,w 2,...,w k e k strings,

More information

State Minimization for DFAs

State Minimization for DFAs Stte Minimiztion for DFAs Red K & S 2.7 Do Homework 10. Consider: Stte Minimiztion 4 5 Is this miniml mchine? Step (1): Get rid of unrechle sttes. Stte Minimiztion 6, Stte is unrechle. Step (2): Get rid

More information

Properties of Integrals, Indefinite Integrals. Goals: Definition of the Definite Integral Integral Calculations using Antiderivatives

Properties of Integrals, Indefinite Integrals. Goals: Definition of the Definite Integral Integral Calculations using Antiderivatives Block #6: Properties of Integrls, Indefinite Integrls Gols: Definition of the Definite Integrl Integrl Clcultions using Antiderivtives Properties of Integrls The Indefinite Integrl 1 Riemnn Sums - 1 Riemnn

More information

Non-Deterministic Finite Automata. Fall 2018 Costas Busch - RPI 1

Non-Deterministic Finite Automata. Fall 2018 Costas Busch - RPI 1 Non-Deterministic Finite Automt Fll 2018 Costs Busch - RPI 1 Nondeterministic Finite Automton (NFA) Alphbet ={} q q2 1 q 0 q 3 Fll 2018 Costs Busch - RPI 2 Nondeterministic Finite Automton (NFA) Alphbet

More information

CS 188 Introduction to Artificial Intelligence Fall 2018 Note 7

CS 188 Introduction to Artificial Intelligence Fall 2018 Note 7 CS 188 Introduction to Artificil Intelligence Fll 2018 Note 7 These lecture notes re hevily bsed on notes originlly written by Nikhil Shrm. Decision Networks In the third note, we lerned bout gme trees

More information

CS 330 Formal Methods and Models

CS 330 Formal Methods and Models CS 330 Forml Methods nd Models Dn Richrds, George Mson University, Spring 2017 Quiz Solutions Quiz 1, Propositionl Logic Dte: Ferury 2 1. Prove ((( p q) q) p) is tutology () (3pts) y truth tle. p q p q

More information

Grammar. Languages. Content 5/10/16. Automata and Languages. Regular Languages. Regular Languages

Grammar. Languages. Content 5/10/16. Automata and Languages. Regular Languages. Regular Languages 5//6 Grmmr Automt nd Lnguges Regulr Grmmr Context-free Grmmr Context-sensitive Grmmr Prof. Mohmed Hmd Softwre Engineering L. The University of Aizu Jpn Regulr Lnguges Context Free Lnguges Context Sensitive

More information

Nondeterminism and Nodeterministic Automata

Nondeterminism and Nodeterministic Automata Nondeterminism nd Nodeterministic Automt 61 Nondeterminism nd Nondeterministic Automt The computtionl mchine models tht we lerned in the clss re deterministic in the sense tht the next move is uniquely

More information

Reinforcement learning II

Reinforcement learning II CS 1675 Introduction to Mchine Lerning Lecture 26 Reinforcement lerning II Milos Huskrecht milos@cs.pitt.edu 5329 Sennott Squre Reinforcement lerning Bsics: Input x Lerner Output Reinforcement r Critic

More information

NUMERICAL INTEGRATION. The inverse process to differentiation in calculus is integration. Mathematically, integration is represented by.

NUMERICAL INTEGRATION. The inverse process to differentiation in calculus is integration. Mathematically, integration is represented by. NUMERICAL INTEGRATION 1 Introduction The inverse process to differentition in clculus is integrtion. Mthemticlly, integrtion is represented by f(x) dx which stnds for the integrl of the function f(x) with

More information

Homework 3 Solutions

Homework 3 Solutions CS 341: Foundtions of Computer Science II Prof. Mrvin Nkym Homework 3 Solutions 1. Give NFAs with the specified numer of sttes recognizing ech of the following lnguges. In ll cses, the lphet is Σ = {,1}.

More information

Context-Free Grammars and Languages

Context-Free Grammars and Languages Context-Free Grmmrs nd Lnguges (Bsed on Hopcroft, Motwni nd Ullmn (2007) & Cohen (1997)) Introduction Consider n exmple sentence: A smll ct ets the fish English grmmr hs rules for constructing sentences;

More information

Designing finite automata II

Designing finite automata II Designing finite utomt II Prolem: Design DFA A such tht L(A) consists of ll strings of nd which re of length 3n, for n = 0, 1, 2, (1) Determine wht to rememer out the input string Assign stte to ech of

More information

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton 25. Finite Automt AUTOMATA AND LANGUAGES A system of computtion tht only hs finite numer of possile sttes cn e modeled using finite utomton A finite utomton is often illustrted s stte digrm d d d. d q

More information

Where did dynamic programming come from?

Where did dynamic programming come from? Where did dynmic progrmming come from? String lgorithms Dvid Kuchk cs302 Spring 2012 Richrd ellmn On the irth of Dynmic Progrmming Sturt Dreyfus http://www.eng.tu.c.il/~mi/cd/ or50/1526-5463-2002-50-01-0048.pdf

More information

Theory of Computation Regular Languages

Theory of Computation Regular Languages Theory of Computtion Regulr Lnguges Bow-Yw Wng Acdemi Sinic Spring 2012 Bow-Yw Wng (Acdemi Sinic) Regulr Lnguges Spring 2012 1 / 38 Schemtic of Finite Automt control 0 0 1 0 1 1 1 0 Figure: Schemtic of

More information

The Regulated and Riemann Integrals

The Regulated and Riemann Integrals Chpter 1 The Regulted nd Riemnn Integrls 1.1 Introduction We will consider severl different pproches to defining the definite integrl f(x) dx of function f(x). These definitions will ll ssign the sme vlue

More information

Strong Bisimulation. Overview. References. Actions Labeled transition system Transition semantics Simulation Bisimulation

Strong Bisimulation. Overview. References. Actions Labeled transition system Transition semantics Simulation Bisimulation Strong Bisimultion Overview Actions Lbeled trnsition system Trnsition semntics Simultion Bisimultion References Robin Milner, Communiction nd Concurrency Robin Milner, Communicting nd Mobil Systems 32

More information

NFA DFA Example 3 CMSC 330: Organization of Programming Languages. Equivalence of DFAs and NFAs. Equivalence of DFAs and NFAs (cont.

NFA DFA Example 3 CMSC 330: Organization of Programming Languages. Equivalence of DFAs and NFAs. Equivalence of DFAs and NFAs (cont. NFA DFA Exmple 3 CMSC 330: Orgniztion of Progrmming Lnguges NFA {B,D,E {A,E {C,D {E Finite Automt, con't. R = { {A,E, {B,D,E, {C,D, {E 2 Equivlence of DFAs nd NFAs Any string from {A to either {D or {CD

More information

Coalgebra, Lecture 15: Equations for Deterministic Automata

Coalgebra, Lecture 15: Equations for Deterministic Automata Colger, Lecture 15: Equtions for Deterministic Automt Julin Slmnc (nd Jurrin Rot) Decemer 19, 2016 In this lecture, we will study the concept of equtions for deterministic utomt. The notes re self contined

More information

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata CS103B ndout 18 Winter 2007 Ferury 28, 2007 Finite Automt Initil text y Mggie Johnson. Introduction Severl childrens gmes fit the following description: Pieces re set up on plying ord; dice re thrown or

More information

DIRECT CURRENT CIRCUITS

DIRECT CURRENT CIRCUITS DRECT CURRENT CUTS ELECTRC POWER Consider the circuit shown in the Figure where bttery is connected to resistor R. A positive chrge dq will gin potentil energy s it moves from point to point b through

More information

Math Lecture 23

Math Lecture 23 Mth 8 - Lecture 3 Dyln Zwick Fll 3 In our lst lecture we delt with solutions to the system: x = Ax where A is n n n mtrix with n distinct eigenvlues. As promised, tody we will del with the question of

More information

1. For each of the following theorems, give a two or three sentence sketch of how the proof goes or why it is not true.

1. For each of the following theorems, give a two or three sentence sketch of how the proof goes or why it is not true. York University CSE 2 Unit 3. DFA Clsses Converting etween DFA, NFA, Regulr Expressions, nd Extended Regulr Expressions Instructor: Jeff Edmonds Don t chet y looking t these nswers premturely.. For ech

More information

Turing Machines Part One

Turing Machines Part One Turing Mchines Prt One Hello Hello Condensed Condensed Slide Slide Reders! Reders! Tody s Tody s lecture lecture consists consists lmost lmost exclusively exclusively of of nimtions nimtions of of Turing

More information

CS 275 Automata and Formal Language Theory

CS 275 Automata and Formal Language Theory CS 275 utomt nd Forml Lnguge Theory Course Notes Prt II: The Recognition Prolem (II) Chpter II.5.: Properties of Context Free Grmmrs (14) nton Setzer (Bsed on ook drft y J. V. Tucker nd K. Stephenson)

More information

PART 2. REGULAR LANGUAGES, GRAMMARS AND AUTOMATA

PART 2. REGULAR LANGUAGES, GRAMMARS AND AUTOMATA PART 2. REGULAR LANGUAGES, GRAMMARS AND AUTOMATA RIGHT LINEAR LANGUAGES. Right Liner Grmmr: Rules of the form: A α B, A α A,B V N, α V T + Left Liner Grmmr: Rules of the form: A Bα, A α A,B V N, α V T

More information

Turing Machines Part One

Turing Machines Part One Turing Mchines Prt One Wht problems cn we solve with computer? Regulr Lnguges CFLs Lnguges recognizble by ny fesible computing mchine All Lnguges Tht sme drwing, to scle. All Lnguges The Problem Finite

More information

THEOTY OF COMPUTATION

THEOTY OF COMPUTATION Pushdown utomt nd Prsing lgorithms: Pushdown utomt nd context-free lnguges; Deterministic PDNondeterministic PD- Equivlence of PD nd CFG-closure properties of CFL. PUSHDOWN UTOMT ppliction: Regulr lnguges

More information

Math 520 Final Exam Topic Outline Sections 1 3 (Xiao/Dumas/Liaw) Spring 2008

Math 520 Final Exam Topic Outline Sections 1 3 (Xiao/Dumas/Liaw) Spring 2008 Mth 520 Finl Exm Topic Outline Sections 1 3 (Xio/Dums/Liw) Spring 2008 The finl exm will be held on Tuesdy, My 13, 2-5pm in 117 McMilln Wht will be covered The finl exm will cover the mteril from ll of

More information

5.7 Improper Integrals

5.7 Improper Integrals 458 pplictions of definite integrls 5.7 Improper Integrls In Section 5.4, we computed the work required to lift pylod of mss m from the surfce of moon of mss nd rdius R to height H bove the surfce of the

More information