Title: Compiler Designs and Constructions
1Compiler Designs and Constructions
- Chapter 7 Bottom-Up Parser
- (page 195-278)
- Objectives
- Shift and Reduce Parsing
- LR Parser
- Canonical LR Parser
- LALR Parser
-
- Dr. Mohsen Chitsaz
2Bottom- Up Parsing (Shift and Reduce)
- 1- S ---gt aAcBe
- 2- A ---gt Ab
- 3- A ---gt b
- 4- B ---gt d
- Input abbcde
3Derivation
4Derivation Tree
abbcde
5Definition of Handles
Handle S gt ?A ? gt ?B? A---gt B
- Implementation
- Shift Push(NextInputSymbol), Advance
- Reduce Pop(Handle), Push(LHS)
- Accept
- Error
6Stack Implementation of Shift-Reduce Parser
7There are Two (2) types of Bottom-Up Parsers
- 1-Operator Precedence Parser
- a CFG is an Operator Grammar IFF
- No ?
- No Adjacent Non-terminal
- E ---gt EE
8Example
- ltSgt ---gt While ltEXPgt doltEXPgt ---gt ltEXPgt
ltEXPgtltEXPgt ---gt ltEXPgt gt ltEXPgtltEXPgt ---gt ltEXPgt
lt ltEXPgtltEXPgt ---gt id - WHY?
9Bottom-up Parsing
- 2- LR (K) Parsers
- L Left to right scanning input
- R Right-most derivation
- K Look Ahead Symbols
- Why LR Parsing?
- Advantages
- Most programming Languages
- Most general non-backtracking
- Error detection
- Cannot parse with recursive-descent
10- Disadvantages
- Parse table harder
- Parse table larger
- Error recovery is harder
- Without YACC
- Types of LR Parser
- SLR Simple
- CLR Canonical
- LALR Look-a-head
11Implementing the Parser as a Finite State Machine
12Stack
- Element of Stack S0, a1, S1, a2, . Sm ,am
- Where
- a grammar Symbol (Token)
- S State
- Stack Operations
- Shift(State, Input) Push(Input), Push(State)
- Reduce (State, Input) Replace (LHS)
- Accept
- Error
13Example
- 1- E ---gt ET
- 2- E ---gt T
- 3- T ---gt TF
- 4- T ---gt F
- 5- F ---gt (E)
- 6- F ---gt id
14State
Action
Goto
15(No Transcript)
16SLR Parse Table
- LR (0) "Item" Original Productions of a grammar
with a dot(.) at a given place in RHS - Example
- X ---gt ABC
- X ---gt .ABC
- X ---gt A.BC
- X ---gt AB.C
- X ---gt ABC.
17- IF X ---gt Produce one item
- X ---gt .
- SLR Table
- Augmented grammar S'---gt S
- Functions
- Closure
- Goto
18- S ---gt a.
- S ---gt a.X
- S ---gt a.Xb
- Closure (item)
- Def Suppose I is a set of items, we define
closure (I) as - Every item in I is in closure (I)
- If A ---gt ?.B ? is in closure (I) and
- B ---gt ? is a production, then add the item
- B ---gt. ? to I (if not already in)
19Example
- E --gt E
- E --gt E T
- E --gt T
- T --gt T F
- Closure of (I)
- E --gt .E
- E --gt .E T
- E --gt .T
- T --gt .T F
20GoTo
- Goto I,X closure of A ---gt ?X. ? such that
- A ---gt? .X ? ? I
- Def
- I0 closure S' ---gt .S
- C I0,I1,...In set of canonical collection of
items for grammar G with starting symbol S
21Example
- If I E' ---gt E.
- E ---gt E. T
- Â
- Then Goto I, is
- Â
- E ---gt E .T T ---gt .T F T ---gt .F F ---gt
.(E) F ---gt .id
22Example 1
- I0CLOSURE (E --gt.E)
- I0E --gt .E
- E --gt .ET
- E --gt .T
- T --gt .TF
- T --gt .F
- F --gt.(E)
- F --gt .id
0. E --gt E 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
23Example 1
- GOTO(I0,E) I1
- E --gt E.
- E --gt E.T
- Â
- GOTO(I0,T) I2
- E --gt T.
- T --gt T.F
- Â
- GOTO(I0,F) I3
- T --gt F.
- GOTO(I0,( ) I4
- F --gt (.E)
- E --gt .ET
- E --gt .T
- T --gt .TF
- T --gt .F
- F --gt .(E)
- F --gt .id
24Example 1
- Â GOTO(I0,id) I5
- F --gt id.
- GOTO(I1,) I6
- E --gt E .T
- T --gt .TF
- T --gt .F
- F --gt .(E)
- F --gt .id
- GOTO(I2,) I7
- T --gt T.F
- F --gt .(E)
- F --gt .id
- GOTO (I4,E) I8
- F --gt (E.)
- E --gt E.T
25Example 1
- GOTO(I6,T) I9
- E --gt ET.
- T --gt T.F
- Â GOTO(I7,F) I10
- T --gt TF.
- GOTO(I8,) ) I11
- F --gt (E).
- GOTO (I4, T ) I2
- GOTO (I4, F ) I3
- GOTO (I4, ( ) I4
- GOTO (I4, id) I5
- GOTO (I6, F ) I3
- GOTO (I6, ( ) I4
- GOTO (I6, id) I5
- GOTO (I7, ( ) I4
- GOTO (I7, id) I5
- GOTO (I8, ) I6
26Canonical Collections
- C(I0,I1,I2,I3,I4,I5,I6,I7,I8,I9,I10,I11)
- Â
- Follow(E) , ), Follow (T) ,
, ), Follow(F) , , ),
27Transition Diagram
28Algorithm to construct SLR Parsing
- 1-Construct a canonical collection
- C I0,... for Augmented grammar G
- From the graph create the SLR(0)
- for SHIFT and GOTO
- Â 2- Rows are the states
- Columns are the Terminal Symbols For SHIFT and
NonTerminal Symbols for GOTO
29Algorithm to construct SLR Parsing
- 3-Each item li corresponds to a state i for
Terminal symbol a and State I - If A ---gt?. aB ?li Goto (li,a) lj then
- Action li,a Shift (j)
- if A ---gt ?. ? li and for all as in follow (A)
then - Reduce A --gt ?
- But not A ? S'
- If (S'--gtS.) ? I0 then Set Action i, Accept.
30Algorithm to construct SLR Parsing
- 4- For all nonterminal symbol A and state I
- If GOTO(Ij,A) Ij then
- GOTOI,A j
- 5- All nonterminal entries are called Error
- 6- The initial state of the parser is the state
corresponding to the set of items including - S --gt .S
- If no conflict in creating the table then G is
SLR Grammar
31SLR Parser (Second Example)
- Example (I)
- S --gt S
- S --gt E
- E --gt E T
- E --gt T
- T --gt id
- T --gt (E)
I0 Closure S .S S --gt .S S --gt .E E --gt
.E T E --gt .T T --gt .id T --gt .(E)
32- GOTO I0, S I1 S --gt S.
- GOTO I0, E I2 S --gt E.
- E --gt E. T
- GOTO I0, T I3 E --gt T.
- GOTO I0,id I4 T --gt id.
- GOTO I0, ( I5 T --gt (.E)
- E --gt .E T
- E --gt .T GOTO I5, T I3
- T --gt .id GOTO I5,id I4
- T --gt .(E) GOTOI5,( I5
33- GOTO I2, I6 E --gt E .T
- T --gt .id GOTOI6,id I4
- T --gt .(E) GOTOI6,( I5
- GOTOI5,E I7 T --gt (E.)
- E ? E. T GOTO I7, I6
- GOTO I6,T I8 E --gt E T.
- GOTO I7,) I9 T --gt (E).
34Stack Input
35- Follow (S) -
- Follow (E) -, , )
- Follow (T) -, , )
36Action GoTo
37Canonical LR Parsing or LR(1)
- Introduction 0- S --gt S I0 S --gt .S1- S
--gt L R S --gt .L R2- S --gt R S --gt .R3- L
--gt R L --gt .R4- L --gt id L --gt. id5- R --gt
L R --gt .L - GOTOI0, S I1 S --gt S.GOTOI0, L I2
S --gt L. R R --gt L.GOTOI2, I6
S --gt L .R
38LR(0) Parse Table
State 2 Follow (R) , R5 State 2
GotoI2,I5 S5
39Canonical LR Parsing Tables
- LR(1) item
- A --gt ?.B, a where A --gt ? B is a production
and a is a terminal or the right endmarker - A--gt B1.B2 if B2 ? ? will not create additional
information. But if B2 ? it helps to make the
right choice - Here we have same production but different
lookahead symbols
40- LR(1) item, is the same as canonical collection
of sets of LR(0) item. - We need only to modify the functions
- Closure
- GOTO
- LR(1) Grammar
- I0
- S--gt .L R
- S --gt .R
- L --gt .id
- L --gt .R
- R --gt .L
41- LR(0) Closure (I)
- I
- A --gt ? . XB ?
- X --gt ? in G
- Add
- X --gt . ? to I
42- LR(1) Closure (I)
- I
- A --gt ? . XB, a ?
- For each X --gt ? in G
- For each b in first (Ba)
- Add
- X --gt . ?, b to I if it is not already in
43- GOTO I, X Closure(j) where
- JA --gt ? X. B, a
- A --gt ? . XB, a in I
44Example
- S --gt S
- S --gt CC
- C --gt aC
- C --gt d
- A --gt ? . XB, a
- For each X--gt ? in G
- And
- For each b ? First (Ba) Add X --gt . ?, b
45Canonical LR(1) Parsing Table (LR(1))
- 0- S --gt S
- 1- S --gt CC
- 2- C --gt aC
- 3- C --gt d
46Canonical LR(1) Parsing Table (LR(1))
- I0 S --gt .S,
- S --gt .CC,
- C --gt .aC, a/d
- C --gt .d, a/d
47Canonical LR(1) Parsing Table (LR(1))
- GOTOI0,S I1 S1 ---gt S.,
- GOTOI0,C I2 S ---gt C.C,
- C ---gt .aC,
- C ---gt .d,
- GOTOI0,a I3 C ---gt a.C,a/d
- C ---gt .aC,a/d
- C ---gt .d.a/d
- GOTOI0,d I4 C ---gt d.,a/d
48Canonical LR(1) Parsing Table (LR(1))
- GOTOI2,C I5 S ---gt CC.,
- GOTOI2,a I6 C ---gt a.C,
- C ---gt .aC,
- C ---gt .d,
- GOTOI2,d I7 C ---gt d.,
- GOTOI3,C I8 C ---gt aC.,a/d
- GOTOI3,a I3
- GOTOI3,d I4
- GOTOI6,C I9 C ---gt aC.,
- GOTOI6,a I6
- GOTOI6,d I7
49Transition Diagram
50Action GoTo
R2
51- State Input Action/GoTo
- 0 aadd
52LALR(1) Parser
- LALR(1) Lookahead Parser
- Practical
- Smaller Parse Table
- Most Programming Languages
- Merge the States
- A --gt ?. xB, a
- A --gt ? .xB, b
- To
- A --gt ? .xB, a/b
53Canonical LR(1) Parsing Table (LR(1))
- GOTOI0,S I1 S --gt S.,
- GOTOI0,C I2 S --gt C.C,
- C --gt .aC,
- C --gt .d,
- GOTOI0,a I3 C --gt a.C, a/d
- C --gt .aC, a/d
- C --gt .d, a/d
54- GOTOI0,d I4 C --gt d., a/d
- GOTOI2,C I5 S --gt CC.,
- GOTOI2,a I6 C --gt a.C,
- C ? .d,
- GOTOI2,d I7 C --gt d.,
- GOTOI3,C I8 C --gt aC., a/d
- GOTOI3,a I3
- GOTOI3,d I4
- GOTOI6,C I9 C --gt aC.,
- GOTOI6,a I6
- GOTOI6,d I7
- C --gt .aC,
55Transition Diagram (DFA)
56LALR Parsing Table
- Â C I0, I1, In
- Combine Ij with identical First Part (Core) and
different Lookahead symbols - Let C J0, J1,Jm be a new set of items
57LALR Parsing Table
- State I of the parser is corresponding to set Ji
- Action I,a Shift Ji
- If A --gt ?. A B, b ? Ji
- GOTO(Ji,a) Ji
- Action I,a Reduce
- A --gt ? if A --gt ?. , a ? Ji and A ? S
- Â Note that in case if
- A --gt ? . a/b/c//z
- the action is included in column a,b,z
- Action I, Accept
- IF S --gt S., ? Ji
58- Let J Ii, U I2, U IR
- GOTO(J,X) R where R is union of
- GOTO(I1,X)
- GOTO(I2,X)
- GOTO(IR,X)
- In case of no conflict, we have a LALR Parsing
Table
59LALR Parse Table
60LALR Parse Table (Revised)
61Constructing LALR Parse Table
1-Use a 2 dimensional array(or make two tables
one for action and one for GOTO)
Action Goto
- Shift
- Reduce
- 0 Accept
- 99 Error
622-Use Sparce Matrix Representation
Row Column Action
633-Pair Method
- State Number Number of elements, Pairs
Input Symbols
Actions
Action
2 1,3 2,4
A1
0
A2
1
1 3,0
2
3
3 1,-3 2,-3 3,-3
A3
4
A4
5
1 3,-1
6
3 1,-2 2,-2 3,-2
A5
64Pair Method (Array Action)
0
A1
1
A2
2
A1
3
A1
A3
4
A4
5
A5
6
65Algorithm for the Driver(PARSER)
- State lt-- State 1 Input lt-- a,
- While (Action ltgt Accept and Action ltgt Error)
- //Stack lt-- S0 Xi, S1,XmSm
- //Input lt-- ai, ai1,
- Â
- IF Sm is terminal
- Case Action Sm, ai of
- /Sn/ Action lt-- ShiftSn, ai)
- - /Rn/ Action ? Reducen
- 0 /Accept/ Action lt-- Accept
- 99 /Blank/ Action lt-- Error
- Else
- Action lt-- GOTOSm, nonterminal)
66Algorithm for the Driver(PARSER)
- Procedure Reduce(n)
- Repeat
- Pop(state)
- Pop(symbol)
- Until RHS is empty
- Push(LHS)
- Â
- Procedure Shift(S,a)
- Push(a)
- Push(S)
67Algorithm for the Driver(PARSER)
- Procedure GOTO(S, nonterminal)
- Push(S)
- Procedure ShiftTerminal
- // A --gt Bcd id Shift 7
- Push(Id)
- Push(7)
- Procedure ShiftNonterminal
- Push(State)