Title: Week 2 Lecture 1
1Week 2 Lecture 1
Compiler Construction
- Bottom Up (Shift reduce, LR parsing)
- SLR, LR(0) parsing
- SLR parsing table
2Bottom-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
3example
- E -gt E T T
- T -gt T F F
- F -gt (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
4Reductions made by shift-reduce parser
5Stack implementation of a shift reduce Parser
Actions - shift, reduce, accept
Configurations of a shift-reduce parser on input
id1 id2 id3
6Shift-reduce conflicts
Stmt -gt if Expr then Stmt if Expr then Stmt
else Stmt other
STACK if Expr then Stmt
INPUT Else
7LR(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
8The LR Parser
INPUT
sm
LR Parsing Program
STACK
OUTPUT
Xm
sm-1
Xm-1
goto
action
s0
Aho, Sethi and Ullman (p.217)
9LR Parsing Table
State
goto
action
Page 219 Aho, Sethi and Ullman
10T
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
11Moves of an LR parser on id id id
(1) E -gt E T (2) E -gt T (3) T -gt T F (4) T
-gt F (5) F -gt (E) (6) F -gt id
12Constructing SLR parsing tables
- Closure operation
- GOTO operation
- The set of items construction
- SLR parsing table
13The 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 -gt a . Bb is in closure(I) and B -gt g is a
production, then add the item B -gt .g to I if it
is not already there. Apply this rule until no
more new items can be added to closure(I) - E -gt E
- E -gt E T
- T -gt T F F
- F -gt (E) id
Initially I is the set of one item E-gt.E
14Closure of E-gt.E
15The GOTO operation
- Move the dot over one symbol
- This becomes the kernel item for a new state
- Compute its closureI1 E -gtE ., E -gt E .
T - goto(I1 ) E -gt E . T
- Closure(I1) ??
16First 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
17First Sets
- If X is a terminal, then FIRST(X) is X
- IF X -gt e is a production, then add e to FIRST(X)
- IF X is a nonterminal and X -gt Y1Y2Yk 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).
18FIRST sets
- (1) E -gt E
- (2) E -gt E T
- (3) E -gt T
- (4) T -gt T F
- (5) F -gt (E)
- (6) F -gt id
19The 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-gta.ab is in Ii and goto(Ii, a) Ij, then
set actioni,a to shift j. A must be a terminal. - If A-gta. is in Ii, then set actionI, a to
reduce A -gt a for all a in FOLLOW(A) here A
may not be S - If S -gt S . is in Ii then set action i, to
accept.
20Follow 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 -gt aBb, then
everything in FIRST(b) except for e is placed in
FOLLOW(B) - If there is a production A -gt aB, or a production
A -gt aBb where FIRST(b) contains e (i.e. B gt e),
then everything in FOLLOW(A) is in FOLLOW(B).
21Follow Sets
- (1) E -gt E
- (2) E -gt E T
- (3) E -gt T
- (4) T -gt T F
- (5) F -gt (E)
- (6) F -gt id