Title: CS 321 Programming Languages and Compilers
1CS 321Programming Languages and Compilers
2Bottom-up Parsing Shift-reduce parsing
- Grammar H L ? EL E
- E ? a b
- Input aab
- has parse tree
L
L
E
L
E
a
a
b
3Data for Shift-reduce Parser
- Input string sequence of tokens being checked
for grammatical correctness - Stack sentential form representing the input
seen so far - Trees Constructed the parse trees that have been
constructed so far at some point in the parsing
4Operations of shift-reduce parser
- Shift move input token to the set of trees as a
singleton tree. - Reduce coalesce one or more trees into single
tree (according to some production). - Accept terminate and accept the token stream as
grammatically correct. - Reject Terminate and reject the token stream.
5A parse for grammar H
- Input Stack Action Trees
- aab Shift
- ab a Reduce E? a
- ab E Shift
-
- ab E Shift
-
a
6A parse for grammar H
- Input Stack Action Trees
- b Ea Reduce E? a
- b EE Shift
- b EE Shift
7A parse for grammar H
- Input Stack Action
Trees - EEb Reduce E?b
- EEE Reduce L?E
- EEL Reduce L?EL
b
L
8A parse for grammar H
- Input Stack Action Trees
- EL Reduce L?EL
- L Acc
9Bottom-up Parsing
- Characteristic automata for an LR grammar
tells when to shift/reduce/accept or reject - handles
- viable prefixes
10A parse for grammar H
- Input Stack Action StackInput
- aab Shift aab
- ab a Reduce E? a aab
- ab E Shift Eab
- ab E Shift
Eab - b Ea Reduce E? a Eab
- b EE Shift EEb
- b EE Shift EEb
- EEb Reduce E?b EEb
- EEE Reduce L?E EEE
- EEL Reduce L?EL EEL
- EL Reduce L?EL EL
- L Accept L
11Bottom-up parsing?
- StackInput
- L
- EL
- EEL
- EEE
- EEb
- EEb
- EEb
- Eab
- Eab
- Eab
- aab
- aab
-
12Handles and viable prefixes
- Stackremaining input sentential form
- Handle the part of the sentential form that
is reduced in each step - Viable prefix the prefix of the sentential
form in a right-most derivation that do not
extend beyond the end of the handle - E.g. viable prefixes for H (E)(E L a
b) - Viable prefixes form a regular set.
13Characteristic Finite State Machine (CFSM)
- Viable prefixes of H are recognized by this CFSM
1
L
L
2
5
6
E
E
0
a
a
3
b
b
4
14How a Bottom-Up Parser Works
- Run the CFSM on symbols in the stack
- If a transition possible on the incoming input
symbol, then shift, else reduce. - Still need to decide which rule to use for the
reduction.
15Characteristic automaton
Viable Prefixes
start
aab leads to state 3 after a Eab leads to
state 3 after Ea EEb leads to state 4 after
EEb EEE leads to state 2 after EEE EEL
leads to state 6 after EEL EL leads to state 6
after EL
16Characteristic automaton
start
State Action 0,5 shift (if possible) 1 accept 2 r
educe L?E, if EOF shift otherwise 3 reduce
E?a 4 reduce E?b 6 reduce L?EL
17Example expression grammar
- E ? ET T
- T ? TP P
- P ? id
- ididididhas parse tree
E
T
E
T
E
P
T
E
P
id
T
P
id
P
id
id
18A parse in this grammar
- idididid Shift
- ididid id Reduce
- ididid P Reduce
- ididid T Reduce
- ididid E Shift
- ididid E Shift
- idid Eid Reduce
- idid EP Reduce
- idid ET Reduce
- idid E Shift
- idid E Shift
19A parse in this grammar (cont.)
- id E id Reduce
- id EP Reduce
- id ET Reduce
- id E Shift
- id E Shift
- Eid Reduce
- EP Reduce
- ET Reduce
- E Reduce
- E Accept
20Characteristic Finite State Machine
- The CFSM recognizes viable prefixes (strings of
grammar symbols that can appear on the stack)
T
1
5
8
E
id
4
id
P
0
id
T
P
2
7
9
P
3
21Definitions
- Rightmost derivation
- Right-sentential form
- Handle
- Viable prefix
- Characteristic automaton
22Rightmost Derivation
Definition A rightmost derivation in G is a
derivation
such that for each step i, the rightmost
non-terminal in
is replaced to obtain
1.
is not right-most
2.
is right-most
23Right-sentential Forms
Definition A right-sentential form is any
sentential form that occurs in a right-most
derivation.
E.g., any of these
24Handles
Definition Assume the i-th step of a rightmost
derivation is wiuiAvi ? ui?viwi1 Then,
(?, ui?) is the handle of wi1
In an unambiguous grammar, any sentence has a
unique rightmost derivation, and so we can talk
about the handle rather than a handle.
25The Plan
- Construct a parser by first constructing the
CFSM, then constructing GOTO and ACTION tables
from it. - Construction has two parts
- LR(0) construction of the CFSM
- SLR(1) construction of tables
26Constructing the CFSM States
The states in the CFSM are created by taking the
closure of LR(0) items
L ? E L L ? E L L ? E L L ? E L
Given a production L ? E L, these are all
induced LR(0) items
27What is ?
The in L ? E L represents the state of
the parse.
L
L
E
Only this part of the tree is fully developed
28A State in the CFSM Closure of LR(0) Item
For set I of LR(0) items, calculate
closure(I) 1. if A ? ? B ? is in
closure(I), then for every production B
? ?, B ? ? is in closure(I) 2.
closure(I) is the smallest set with property (1)
29Closure of LR(0) Item Example
H L ? EL E E ? a b
closure(L ? E L) L ? E L , L ?
E L , L ? E, E ? a , E ? b
30LR(0) Machine
- Given grammar G with goal symbol S, augment the
grammar by adding new goal symbol S and
production S ? S. - States sets of LR(0) items
- Start state closure(S ? S)
- All states are considered to be final (set of
viable prefixes closed under prefix) - transition(I, X) closure(A ? ?X? A
? ?X????I).
31Example LR(0) CFSM Construction
H L? L L ? EL E E ?
a b
Augment the grammar
Initial State is closure of this augmenting
rule
L? L L ? EL L ? E E ? a E ? b
closure(L? L)
32Example Transitions from I0
transition( , L) L ? L
transition( , E) L ? E L , L ? E
transition( , a ) E ? a
transition( , b) E ? b
There are no other transitions from I0. There
are no transitions possible from I1. Now consider
the transitions from I2.
33Transitions from I2
L ? E L L ? EL L ? E E ? a E ? b
transition( , )
transition( , L) L ? EL
New state
transition( , E)
transition( , a)
transition( , b)
34The CFSM Transition Diagram for H
L ? L
L
L ? E L L ? EL L ? E E ? a E ? b
L? L L ? EL L ? E E ? a E ? b
L ? E L L ? E
E
E
L
L ? EL
a
a
E ? a
b
b
E ? b
35Characteristic Finite State Machine for H
1
L
L
2
5
6
E
E
0
a
a
3
b
b
4
36How LR(1) parsers work
- GOTO table transition function of characteristic
automaton in tabular form - ACTION table State? ? ?Action
- Procedure
- Use the GOTO table to run the CFSM over the
stack. Suppose state reached at top of stack is
?. - Take action given by ACTION(?,a), where a is
incoming input symbol.
37Action Table for H
38SLR(1) Parser Construction
- GOTO table is the move function from the LR(0)
CFSM. - ACTION table is constructed from the CFSM as
follows - If state i contains A ? ?a?, then ACTION(i,a)
Shift. - If state i contains A ? ?, then ACTION(i,a)
Reduce A????(But, if A is L, action is Accept.) - Otherwise, reject.
- But,...
39SLR(1) Parser Construction
- Rules for the ACTION table can involve
shift/reduce conflicts. - So the actual rule for reduce actions is
- If state i contains A ? ?, then ACTION(i,a)
Reduce A ? ?, for all a ? FOLLOW(A). - E.g. state 2 for grammar H yields a shift/reduce
conflict. Namely, should you shift the or
reduce by L?E. This is resolved by looking at
the follow set for L. - Follow(L)
40FIRST and FOLLOW sets
- FIRST(?) a ? ? ? ?? a? ? ? a ?? ?
- FOLLOW(A) a ? ? S ?? ?Aa?
41Calculating FIRST sets
- Create table Fi mapping N to ? ? ? initially,
Fi(A) ? for all A. - Repeat until Fi does not change
- For each A ? ??? P, Fi(A) Fi(A) ?
FIRST(?, Fi) - where FIRST(?, Fi) is defined as follows
- FIRST(?, Fi) ?
- FIRST(a?, Fi) a
- FIRST(B?, Fi) Fi(B) ??FIRST(b,Fi), if ??Fi(B)
Fi(B), o.w.
42Calculating FOLLOW sets
- Calculate FOLLOW sets
- Create table Fo N ? ? ? initially, Fo(A)
? for all A, except Fo(S) - Repeat until Fo does not change
- For each production A ? ?B?, Fo(B) Fo(B)
? FIRST(?) - ? - For each production A ? ?B? Fo(B) Fo(B) ?
Fo(A) - For each production A ? ?B???if ?? FIRST(?),
Fo(B) Fo(B) ? Fo(A)