Sei sulla pagina 1di 14

Compiler Construction

(CS-460)

1
Outline

1. SLR(1) Parsing
2. SLR(1) Parsing Table
3. Problems in SLR(1) Parsing
4. LR(1) Items
5. DFA of LR(1) Items
6. LR(1) Parsing
7. Summary

2
Bottom-Up Parsing

Lecture: 17-18

3
SLR(1) Parsing

 The SLR(1) Parsing


 The ‘S’ indicates Simple
 The ‘L’ indicates input be processed from left to right
 The ‘R’ indicates rightmost derivation will be used
 The ‘1’ means one lookahead token will be read
 The SLR(1) parser uses LR(0) items and DFA of
LR(0) items
 The SLR(1) requires Follow Sets to be found
 The two actions (shift & reduce) also remain the
same with slight change in their application

4
SLR(1) Parsing (Continue…)

 SLR(1) Parsing Algorithm


 From a state with production A  A.XB such that X is a
terminal then shift to new state containing a production of
type A  AX.B will be performed
 From a state containing a production A  γ. Such that next
terminal in the input is in Follow(A) set, then reduce action
of A  γ. will be applied
 Suppose S is a start state then S  S’ will indicate
acceptance of input string if next input symbol is $

5
SLR(1) Parsing (Continue…)

 Grammar: E’  E
EE+n|n
 Input: n+n

 SLR(1) Process
 List down LR(0) items for the grammar
 Construct NFA and DFA of LR(0) items
 Determine Follow(E’) and Follow(E) set
 Construct SLR(1) parsing table for grammar
 Perform parsing using LR(1) algorithm

6
SLR(1) Parsing Table

 SLR(1) parsing table is somewhat similar to LR(0)


with slight change in columns
 State
 Input
 Go to

7
Problems in SLR(1)

 Consider the following grammar


S  id | V = E
V  id
EV|n
 Now see the follow sets of S and V
Follow(S) = { $ }
Follow(V) = { $, = }
 What will happen if the parsing process reads input
$ and the current state contains both transitions
including S  id. and V  id.

8
LR(1) Items

 LR(1) Item includes lookahead token with it and its


is written within square brackets
 LR(1) item is usually of them form;
[A  α.β, a]

9
DFA of LR(1) Items

 SLR(1) applies lookahead after the construction of


the DFA of LR(0) items
 The power of general LR(1) method is that it uses a
new DFA that has the lookahead built into its
construction from the start
 DFA in LR(1) can have two types of transitions
1. Given an LR(1) item [A  α.Xβ, a] where X is a terminal,
there is a transition on X to the item [A  αX.β, a]
2. Given an LR(1( item [A  α.Bγ, a] where B is nonterminal,
there are ε transitions to items [B  .β, b] for every
production B  β and for every token b in First(γa)

10
DFA of LR(1) Items (Continue…)

 Identify states and construct DFA of LR(1) items for


the following grammar

A(A)|a

11
LR(1) Parsing

 The LR(1) Parsing or canonical LR(1) Parsing


 The ‘L’ indicates input be processed from left to right
 The ‘R’ indicates rightmost derivation will be used
 The ‘1’ means one lookahead token will be read
 The LR(1) parser uses LR(1) items and DFA of
LR(1) items
 The LR(1) requires First Sets to determine
lookahead token for the state
 The two actions (shift & reduce) also remain the
same with slight change in their application

12
LR(1) Parsing (Continue…)

 LR(1) Parsing Algorithm


 From a state with production [A  A.XB, a] such that X is a
terminal then shift to new state containing a production of
type [A  AX.B, a] will be performed
 From a state containing a production [A  γ., a] such that
next terminal in the input is “a” then reduce action of A  γ.
will be applied
 Suppose S is a start state then S  S’ will indicate
acceptance of input string if next input symbol is $

 Construct LR(1) parsing table for grammar


A  (A) | a
13
Summary

Any Questions?

14

Potrebbero piacerti anche