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 being checked for grmmticl correctness Stck: sententil form representing the input seen so fr Trees Constructed: the prse trees tht hve been constructed so fr t some point in the prsing 3
Opertions of shift-reduce prser Shift: move input token to the set of trees s singleton tree. Reduce: colesce one or more trees into single tree (ccording to some production). Accept: terminte nd ccept the token strem s grmmticlly correct. Reject: Terminte nd reject the token strem. 4
A prse for grmmr H Input Stck Action Trees ;;b Shift ;;b Reduce fi ;;b Shift ;b ; Shift ; 5
A prse for grmmr H Input Stck Action Trees ;b ; Reduce fi ; ;b ; Shift ; b ;; Shift ; ; 6
A prse for grmmr H Input Stck Action Trees $ ;;b Reduce fib ; ; b $ ;; Reduce fi ; ; $ ;; Reduce fi; b ; ; b 7
A prse for grmmr H Input Stck Action Trees $ ; Reduce fi; ; ; $ Acc b ; ; b 8
Bottom-up Prsing Chrcteristic utomt for n R grmmr: tells when to shift/reduce/ccept or reject hndles vible prefixes 9
A prse for grmmr H Input Stck Action Stck+Input ;;b Shift ;;b ;;b Reduce fi ;;b ;;b Shift ;;b ;b ; Shift ;;b ;b ; Reduce fi ;;b ;b ; Shift ;;b b ;; Shift ;;b $ ;;b Reduce fib ;;b $ ;; Reduce fi ;; $ ;; Reduce fi; ;; $ ; Reduce fi; ; $ Accept 10
Bottom-up prsing? Stck+Input ; ;; ;; ;;b ;;b ;;b ;;b ;;b ;;b ;;b ;;b ; ; b ; ; b 11
Hndles nd vible prefixes Stck+remining input = sententil form Hndle = the prt of the sententil form tht is reduced in ech step Vible prefix = the prefix of the sententil form in right-most derivtion tht do not extend beyond the end of the hndle.g. vible prefixes for H: (;)*( b) Vible prefixes form regulr set. 12
Chrcteristic Finite Stte Mchine (CFSM) Vible prefixes of H re recognized by this CFSM: 0 1 2 ; 5 6 b 3 b 4 13
How Bottom-Up Prser Works Run the CFSM on symbols in the stck If trnsition possible on the incoming input symbol, then shift, else reduce. Still need to decide which rule to use for the reduction. 14
Chrcteristic utomton strt 0 1 2 ; 5 6 Vible Prefixes b 3 4 b ;;b leds to stte 3 fter ;;b leds to stte 3 fter ; ;;b leds to stte 4 fter ;;b ;; leds to stte 2 fter ;; ;; leds to stte 6 fter ;; ; leds to stte 6 fter ; 15
Chrcteristic utomton strt 0 b 1 2 3 4 b ; 5 6 Stte Action 0,5 shift (if possible) 1 ccept 2 reduce, if OF shift otherwise 3 reduce 4 reduce b 6 reduce ; 16
xmple: expression grmmr fi +T T T fi T*P P P fi id id+id+id+id hs prse tree: T P + + T P id + T P id T P id id 17
A prse in this grmmr id+id+id+id Shift +id+id+id id Reduce +id+id+id P Reduce +id+id+id T Reduce +id+id+id Shift id+id+id + Shift +id+id +id Reduce +id+id +P Reduce +id+id +T Reduce +id+id Shift id+id + Shift 18
A prse in this grmmr (cont.) +id + id Reduce +id +P Reduce +id +T Reduce +id Shift id + Shift $ +id Reduce $ +P Reduce $ +T Reduce $ Reduce $ Accept 19
Chrcteristic Finite Stte Mchine The CFSM recognizes vible prefixes (strings of grmmr symbols tht cn pper on the stck): 1 + id T 5 8 0 id T P 4 2 * id P * P 7 9 3 20
Definitions Rightmost derivtion Right-sententil form Hndle Vible prefix Chrcteristic utomton 21
Rightmost Derivtion Definition: A rightmost derivtion in G is derivtion: S w1 i wi+1 K w K such tht for ech step i, the rightmost non-terminl in is replced to obtin w i + 1 w i 1. ; ;; ; ; ; ; ;; ;;b is not right-most 2. ; ; b ;; b ; ; b ; ;b ;;b is right-most 22
Right-sententil Forms Definition: A right-sententil form is ny sententil form tht occurs in right-most derivtion. ; ; b ;; b ; ; b ; ;b ;;b.g., ny of these 23
Hndles Definition: Assume the i-th step of rightmost derivtion is: w i =u i Av i u i αv i =w i+1 Then, (α, u i α ) is the hndle of w i+1 In n unmbiguous grmmr, ny sentence hs unique rightmost derivtion, nd so we cn tlk bout the hndle rther thn hndle. 24
The Pln Construct prser by first constructing the CFSM, then constructing GOTO nd ACTION tbles from it. Construction hs two prts: R(0) construction of the CFSM SR(1) construction of tbles 25
Constructing the CFSM: Sttes The sttes in the CFSM re creted by tking the closure of R(0) items Given production ;, these re ll induced R(0) items ; ; ; ; 26
Wht is is? The in ; represents the stte of the prse. Only this prt of the tree is fully developed ; 27
A Stte in the CFSM: Closure of R(0) Item For set I of R(0) items, clculte closure(i): 1. if A α B β is in closure(i), then for every production B γ, B γ is in closure(i) 2. closure(i) is the smllest set with property (1) 28
Closure of R(0) Item: xmple H: ; b closure({ ; }) = { ;, ;,,, b} 29
R(0) Mchine Given grmmr G with gol symbol S, ugment the grmmr by dding new gol symbol S nd production S fi S. Sttes = sets of R(0) items Strt stte = closure({s fi S}) All sttes re considered to be finl (set of vible prefixes closed under prefix) trnsition(i, X) = closure({a fi X b A fi Xb I}). 30
xmple: R(0) CFSM Construction Augment the grmmr: H: ; b Initil Stte is closure of this ugmenting rule : closure({ }) = I 0 : ; b 31
xmple: Trnsitions from I0 trnsition( I0, ) = { } = I1 trnsition( I0, ) = { ;, } = I2 trnsition( I 0, ) = { } = trnsition( I 0, b) = { b } = I 3 I 4 There re no other trnsitions from I 0. There re no trnsitions possible from I 1. Now consider the trnsitions from I 2. 32
Trnsitions from I 22 trnsition( I 2,;) = I 5 : ; ; b I 5 New stte: 6 trnsition(, ) = { ; }= I I2 I 3 trnsition(, ) = 5 trnsition(, ) = 5 I I trnsition(, b) = I5 I4 33
The CFSM Trnsition Digrm for H I 1 : I 0 : ; b b I 2 : I 3 ; : ; b I 5 : ; ; b I 6 : ; I 4 : b 34
Chrcteristic Finite Stte Mchine for H 0 1 2 ; 5 6 b 3 b 4 35
How R(1) prsers work GOTO tble: trnsition function of chrcteristic utomton in tbulr form ACTION tble: Stte Procedure: fiaction Use the GOTO tble to run the CFSM over the stck. Suppose stte reched t top of stck is s. Tke ction given by ACTION(s,), where is incoming input symbol. 36
Action Tble for H 0 1 2 3 4 5 6 b ; $ Shift Shift Accept Shift Reduce Reduce Reduce Reduce b Reduce b Shift Shift Reduce ; 37
SR(1) Prser Construction GOTO tble is the move function from the R(0) CFSM. ACTION tble is constructed from the CFSM s follows: If stte i contins A fi b, then ACTION(i,) = Shift. If stte i contins A fi, then ACTION(i,) = Reduce Afi (But, if A is, ction is Accept.) Otherwise, reject. But,... 38
SR(1) Prser Construction Rules for the ACTION tble cn involve shift/reduce conflicts. So the ctul rule for reduce ctions is: If stte i contins A fi, then ACTION(i,) = Reduce A fi, for ll FOOW(A)..g. stte 2 for grmmr H yields shift/reduce conflict. Nmely, should you shift the ; or reduce by fi. This is resolved by looking t the follow set for. Follow() = {$} 39
FIRST nd FOOW sets FIRST() = { * b} { * } FOOW(A) = { S * Ab} 40
Clculting FIRST sets Crete tble Fi mpping N to {}; initilly, Fi(A) = F for ll A. Repet until Fi does not chnge: For ech A fi P, Fi(A) := Fi(A) FIRST(, Fi) where FIRST(, Fi) is defined s follows: FIRST(, Fi) = {} FIRST(b, Fi) = {} FIRST(Bb, Fi) = Fi(B) FIRST(b,Fi), if Fi(B) Fi(B), o.w. 41
Clculting FOOW sets Clculte FOOW sets Crete tble Fo : N fi {$}; initilly, Fo(A) = F for ll A, except Fo(S) = {$} Repet until Fo does not chnge: For ech production A fi Bb, Fo(B) := Fo(B) FIRST(b) - {} For ech production A fi B, Fo(B) := Fo(B) Fo(A) For ech production A fi Bb, if FIRST(b), Fo(B) := Fo(B) Fo(A) 42