Presentation is loading. Please wait.

Presentation is loading. Please wait.

410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.

Similar presentations


Presentation on theme: "410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction."— Presentation transcript:

1 410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction

2 410/510 2 of 21 Bottom-up Parsing Shift reduce parsing –Parse from the input string to the start symbol –Begins at the bottom of the parse tree working up towards the root At each step a substring (of terminals and nonterminals comprising the right hand side of a production reduced to a single nonterminal (the left side of a production) A rightmost derivation in reverse

3 410/510 3 of 21 example E ->E + T | T T->T * F | F F -> (E) | id What is the rightmost derivation for id + id * id A handle is a substring which matches the right hand side of a production Handles are pruned

4 410/510 4 of 21 Reductions made by shift-reduce parser Right-sentential formHandleReducing Production id 1 + id 2 * id 3 id 1 F -> id F + id 2 * id 3 FT->F T + id 2 * id 3 TE -> T E + id 2 * id 3 id 2 F -> id E + F * id 3 FT -> F E + T * id 3 id 3 F -> id E + T * FT * FT -> T * F E + T E -> E + T

5 410/510 5 of 21 Stack implementation of a shift reduce Parser Configurations of a shift-reduce parser on input id 1 + id 2 * id 3 Actions - shift, reduce, accept

6 410/510 6 of 21 Shift-reduce conflicts Stmt -> if Expr then Stmt | if Expr then Stmt else Stmt other STACK …if Expr then Stmt INPUT Else …$

7 410/510 7 of 21 LR(k) Parsers LR – Left to right scan of the input, Rightmost derivation, k number of symbols lookahead Most general nonbacktracking shift-reduce parsing method Parses all grammars that predictive parsers can and more Too much work to construct by hand

8 410/510 8 of 21 The LR Parser LR Parsing Program $ a1a1 … aiai …anan action goto s0s0 … X m-1 s m-1 XmXm smsm STACK INPUT OUTPUT Aho, Sethi and Ullman (p.217)

9 LR Parsing Table id+*()$ETF 0s5s4123 1s6acc 2r2s7r2 3r4 4s5s4823 5r6 6s5s493 7s5s410 8s6s11 9r1r7r1 10r3 11r5 State action goto Page 219 Aho, Sethi and Ullman

10 2 3 4 5 6 9 7 8 0 1 10 11 E + T * go to 7 F go to 3 go to 4 go to 5 ( id T * F ( go to 4 go to 5 F ( id T F ( E ) + go to 2 go to 3 go to 6 Page 226 Aho, Sethi and Ullman

11 410/510 11 of 21 Moves of an LR parser on id * id + id (1) E -> E + T (2) E -> T (3) T -> T * F (4) T -> F (5) F -> (E) (6) F -> id

12 410/510 12 of 21 Constructing SLR parsing tables Closure operation GOTO operation The set of items construction SLR parsing table

13 410/510 13 of 21 The Closure Operation If I is the set of items for a grammar G, then closure(I) is the set of items constructed from I by: Initially, every item in I is added to closure(I) If A -> .  B  is in closure(I) and B ->  is a production, then add the item B ->.  to I if it is not already there. Apply this rule until no more new items can be added to closure(I) E’ -> E E -> E + T T -> T * F | F F -> (E) | id Initially I is the set of one item {[E’->. E]}

14 410/510 14 of 21 Closure of {[E’->. E]} E’->. E

15 410/510 15 of 21 The GOTO operation Move the dot over one symbol This becomes the kernel item for a new state Compute its closure I 1 = {[E’ ->E. ], [E -> E. + T]} goto(I 1 +) = {[E -> E +. T]} Closure(I 1 ) = ??

16 410/510 16 of 21 First and Follow Sets First and Follow sets tell when it is appropriate to put the right hand side of some production on the stack in predictive parsing (i.e. for which input symbols) In LR parsing FOLLOW sets are used to tell us if we should reduce a handle or shift an input symbol to produce a bigger handle

17 410/510 17 of 21 First Sets 1.If X is a terminal, then FIRST(X) is {X} 2.IF X ->  is a production, then add  to FIRST(X) 3.IF X is a nonterminal and X -> Y 1 Y 2 …Y k is a production, then place a in FIRST(X) if for some i, a is in FIRST(Y i ), and  is in all of First(Y 1 ), …First(Y i-1 ). If  is in FIRST(Y j ) for all j = 1, 2, …k, then add  to FIRST(X).

18 410/510 18 of 21 FIRST sets (1) E’ -> E (2) E -> E + T (3) E -> T (4) T -> T * F (5) F -> (E) (6) F -> id

19 410/510 19 of 21 The SLR parsing table 1.Construct C = {I 0, I 1, …., I n }, the collection of sets of LR(0) items 2.State i is constructed from I i. The parsng actions for state i are determined as follows a)If [A-> .a  ] is in I i and goto(I i, a) = I j, then set action[i,a] to shift j. A must be a terminal. b)If [A-> . ] is in I i, then set action[I, a] to “reduce A ->  ” for all a in FOLLOW(A); here A may not be S’ c)If [S’ -> S. ] is in I i then set action [i, $] to accept.

20 410/510 20 of 21 Follow Sets Apply the following rules until no more terminals can be added to any follow set 1.Place $ in FOLLOW (S’), where S is the start symbol and $ is the input right end marker 2.If there is a production A ->  B , then everything in FIRST(  ) except for  is placed in FOLLOW(B) 3.If there is a production A ->  B, or a production A ->  B  where FIRST(  ) contains  (i.e. B =>  ), then everything in FOLLOW(A) is in FOLLOW(B). *

21 410/510 21 of 21 Follow Sets (1) E’ -> E (2) E -> E + T (3) E -> T (4) T -> T * F (5) F -> (E) (6) F -> id


Download ppt "410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction."

Similar presentations


Ads by Google