1 / 21

Week 2 – Lecture 1

Week 2 – Lecture 1 Compiler Construction Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR pa rsing table 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

omer
Télécharger la présentation

Week 2 – Lecture 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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. 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. Reductions made by shift-reduce parser

  5. Stack implementation of a shift reduce Parser Actions - shift, reduce, accept Configurations of a shift-reduce parser on input id1 + id2 * id3

  6. Shift-reduce conflicts Stmt -> if Expr then Stmt | if Expr then Stmt else Stmt other STACK …if Expr then Stmt INPUT Else …$

  7. 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. … an a1 ai $ The LR Parser INPUT sm LR Parsing Program STACK OUTPUT Xm sm-1 Xm-1 … goto action s0 Aho, Sethi and Ullman (p.217)

  9. State goto LR Parsing Table action Page 219 Aho, Sethi and Ullman

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

  11. 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. Constructing SLR parsing tables • Closure operation • GOTO operation • The set of items construction • SLR parsing table

  13. 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 -> a .Bb is in closure(I) and B -> g is a production, then add the item B -> .g 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. Closure of {[E’->.E]} E’->.E

  15. The GOTO operation • Move the dot over one symbol • This becomes the kernel item for a new state • Compute its closureI1 = {[E’ ->E .], [E -> E . + T]} goto(I1 +) = {[E -> E + . T]} Closure(I1) = ??

  16. 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. First Sets • If X is a terminal, then FIRST(X) is {X} • IF X -> e is a production, then add e to FIRST(X) • IF X is a nonterminal and X -> Y1Y2…Yk is a production, then place a in FIRST(X) if for some i, a is in FIRST(Yi), and e is in all of First(Y1), …First(Yi-1). If e is in FIRST(Yj) for all j = 1, 2, …k, then add e to FIRST(X).

  18. FIRST sets (1) E’ -> E (2) E -> E + T (3) E -> T (4) T -> T * F (5) F -> (E) (6) F -> id

  19. The SLR parsing table • Construct C = {I0, I1, …., In}, the collection of sets of LR(0) items • State i is constructed from Ii. The parsng actions for state i are determined as follows • If [A->a.ab] is in Ii and goto(Ii, a) = Ij, then set action[i,a] to shift j. A must be a terminal. • If [A->a.] is in Ii, then set action[I, a] to “reduce A -> a” for all a in FOLLOW(A); here A may not be S’ • If [S’ -> S .] is in Ii then set action [i, $] to accept.

  20. Follow Sets • Apply the following rules until no more terminals can be added to any follow set • Place $ in FOLLOW (S’), where S is the start symbol and $ is the input right end marker • If there is a production A -> aBb, then everything in FIRST(b) except for e is placed in FOLLOW(B) • If there is a production A -> aB, or a production A -> aBb where FIRST(b) contains e (i.e. B => e), then everything in FOLLOW(A) is in FOLLOW(B). *

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

More Related