220 likes | 2.8k Views
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
E N D
Week 2 – Lecture 1 Compiler Construction Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing 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 • 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
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
Stack implementation of a shift reduce Parser Actions - shift, reduce, accept Configurations of a shift-reduce parser on input id1 + id2 * id3
Shift-reduce conflicts Stmt -> if Expr then Stmt | if Expr then Stmt else Stmt other STACK …if Expr then Stmt INPUT Else …$
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
… … 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)
State goto LR Parsing Table action Page 219 Aho, Sethi and Ullman
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
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
Constructing SLR parsing tables • Closure operation • GOTO operation • The set of items construction • SLR parsing table
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]}
Closure of {[E’->.E]} E’->.E
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) = ??
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
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).
FIRST sets (1) E’ -> E (2) E -> E + T (3) E -> T (4) T -> T * F (5) F -> (E) (6) F -> id
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.
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). *
Follow Sets (1) E’ -> E (2) E -> E + T (3) E -> T (4) T -> T * F (5) F -> (E) (6) F -> id