Upload
shina
View
20
Download
0
Embed Size (px)
DESCRIPTION
Compiler 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. Bottom- Up Parsing ( Shift and Reduce ). 1- S ---> aAcBe 2- A ---> Ab 3- A ---> b 4- B ---> d - PowerPoint PPT Presentation
Citation preview
Chapter 7: Bottom-Up Parser 1
Compiler 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
Chapter 7: Bottom-Up Parser 2
Bottom- Up Parsing (Shift and Reduce)
1- S ---> aAcBe
2- A ---> Ab
3- A ---> b
4- B ---> d
Input abbcde
Chapter 7: Bottom-Up Parser 3
Derivation: RMD
LMD
1 4 2 3
S --> aAcBe --> aAcde --> aAbcde --> abbcde
1 2 3 4
S --> aAcBe --> aAbcBe --> abbcBe --> abbcde
Chapter 7: Bottom-Up Parser 4
Derivation Tree:
abbcde
Chapter 7: Bottom-Up Parser 5
Handle: S ==> A ==> B A---> B
Implementation:
•Shift: Push(NextInputSymbol), Advance•Reduce: Pop(Handle), Push(LHS)•Accept•Error:
Definition of Handles
Chapter 7: Bottom-Up Parser 6
Stack Implementation of Shift-Reduce Parser:Input Stack Handle Action
abbcde$ Δ Shift
bbcde$ Δa Shift
bcde$ Δab b Reduce
bcde$ ΔaA Shift
cde$ ΔaAb Ab Reduce
cde$ ΔaA Shift
de$ ΔaAc Shift
e$ ΔaAcd d Reduce
e$ ΔaAcB Shift
$ ΔaAcBe aAcBe Reduce
$ ΔS Accept
Chapter 7: Bottom-Up Parser 7
There 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 ---> E+E
Chapter 7: Bottom-Up Parser 8
Example:
<S> ---> While <EXP> do<EXP> ---> <EXP> = <EXP><EXP> ---> <EXP> >= <EXP><EXP> ---> <EXP> <= <EXP><EXP> ---> id
WHY?
Chapter 7: Bottom-Up Parser 9
Bottom-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
Chapter 7: Bottom-Up Parser 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
Chapter 7: Bottom-Up Parser 11
Implementing the Parser as a Finite State Machine
Chapter 7: Bottom-Up Parser 12
Stack:
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
Chapter 7: Bottom-Up Parser 13
Example:
1- E ---> E+T
2- E ---> T
3- T ---> T*F
4- T ---> F
5- F ---> (E)
6- F ---> id
Chapter 7: Bottom-Up Parser 14
id + * ( ) $ E T F
0 S5 S4 1 2 3
1 S6 Accept
2 R2 S7 R2 R2
3 R4 R4 R4 R4
4 S5 S4 8 2 3
5 R6 R6 R6 R6
6 S5 S4 9 3
7 S5 S4 10
8 S6 S11
9 R1 S7 R1 R1
10 R3 R3 R3 R3
11 R5 R5 R5 R5
State Action Goto
Chapter 7: Bottom-Up Parser 15
STACK INPUT ACTION
1 0 id*id+id$ S5
2 0,id,5 *id+id$ R6
3 0,F *id+id$ 3
4 0,F,3 *id+id$ R4
5 0,T *id+id$ 2
6 0,T,2 *id+id$ S7
7 0,T,2,*,7 id+id$ S5
8 0,2,*,7,id,5 +id$ R6
9 0,T,2,*,7,F,10 +id$ R6
10 0,T,2 +id$ R2
11 0,E,1 +id$ S6
12 0,E,1,+,6 id$ s5
13 0,E,1,+,6,id,5 $ R6
14 0,E,1,+,6,F,3 $ R4
15 0,E,1,+,6,T,9 $ R1
16 0,E,1 $ Accept
Chapter 7: Bottom-Up Parser 16
SLR Parse Table
LR (0) "Item": Original Productions of a grammar with a dot(.) at a given place in RHS
Example:X ---> ABC X ---> .ABC X ---> A.BC X ---> AB.C X ---> ABC.
Chapter 7: Bottom-Up Parser 17
IF X ---> Produce one item:
X ---> .
SLR Table: Augmented grammar S'---> S Functions
• Closure• Goto
Chapter 7: Bottom-Up Parser 18
S ---> a. S ---> a.X S ---> 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 ---> .B is in closure (I) and
B ---> is a production, then add the item
B --->. to I (if not already in)
Chapter 7: Bottom-Up Parser 19
Example:
E’ --> E
E --> E + T
E --> T
T --> T * F
Closure of (I): E’ --> .E
E --> .E + T
E --> .T
T --> .T * F
Chapter 7: Bottom-Up Parser 20
GoTo:
Goto [I,X] = closure of [A ---> X. ] such that
A ---> .X I
Def: I0 closure [S' ---> .S] C = {I0,I1,...In} set of canonical collection
of items for grammar G with starting symbol S
Chapter 7: Bottom-Up Parser 21
Example: If I= E' ---> E.
E ---> E. + T Then Goto [I, +] is:
E ---> E + .T T ---> .T * F T ---> .F F ---> .(E) F ---> .id
Chapter 7: Bottom-Up Parser 22
I0=CLOSURE (E’ -->.E) I0=E’ --> .E
E --> .E+T
E --> .T
T --> .T*F
T --> .F
F -->.(E)
F --> .id
0. E’ --> E1. E --> E + T2. E --> T3. T --> T * F4. T --> F5. F --> (E)6. F --> id
Example 1
Chapter 7: Bottom-Up Parser 23
GOTO(I0,E) =I1
E’ --> E.E --> E.+T
GOTO(I0,T) =I2
E --> T.T --> T.*F
GOTO(I0,F) = I3
T --> F.
GOTO(I0,( ) = I4
F --> (.E)E --> .E+TE --> .TT --> .T*FT --> .FF --> .(E)F --> .id
Example 1
Chapter 7: Bottom-Up Parser 24
GOTO(I0,id) = I5
F --> id.
GOTO(I1,+) = I6
E --> E +.TT --> .T*FT --> .FF --> .(E)F --> .id
GOTO(I2,*) = I7
T --> T*.F
F --> .(E)
F --> .id
GOTO (I4,E) = I8
F --> (E.)E --> E.+T
Example 1
Chapter 7: Bottom-Up Parser 25
GOTO(I6,T) = I9
E --> E+T.T --> T.*F
GOTO(I7,F) = I10
T --> T*F.
GOTO(I8,) ) = I11
F --> (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
Example 1
Chapter 7: Bottom-Up Parser 26
C=(I0,I1,I2,I3,I4,I5,I6,I7,I8,I9,I10,I11)
Follow(E) = { +, ), $ }
Follow (T) = { *, +, ), $ } Follow(F)= { *, +, ), $ }
Canonical Collections
Chapter 7: Bottom-Up Parser 27
Transition Diagram
Chapter 7: Bottom-Up Parser 28
Algorithm to construct SLR Parsing:
1-Construct a canonical collectionC = {I0,...} for Augmented grammar G‘
From the graph create the SLR(0)- for SHIFT and GOTO
2- Rows are the statesColumns are the Terminal Symbols For SHIFT
and NonTerminal Symbols for GOTO
Chapter 7: Bottom-Up Parser 29
Algorithm to construct SLR Parsing:
3-Each item li corresponds to a state i for Terminal symbol a and State I If [A --->. aB] li & Goto (li,a) = lj then
Action [li,a] = Shift (j) if [A ---> .] li and for all a’s in follow (A) then
Reduce A -->
But not A S'
If (S'-->S.) I0 then Set Action [i,$] = Accept.
Chapter 7: Bottom-Up Parser 30
4- For all nonterminal symbol A and state I If GOTO(Ij,A) = Ij then
GOTO[I,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’ --> .S]
If no conflict in creating the table then G is SLR Grammar
Algorithm to construct SLR Parsing:
Chapter 7: Bottom-Up Parser 31
SLR Parser (Second Example)
Example (I) S’ --> S S --> E E --> E + T E --> T T --> id T --> (E)
I0 = Closure [S’ .S]
S’ --> .SS --> .EE --> .E + TE --> .TT --> .idT --> .(E)
Chapter 7: Bottom-Up Parser 32
GOTO [I0, S] = I1 = S’ --> S. GOTO [I0, E] = I2 = S --> E.
•E --> E. + T GOTO [I0, T] = I3 = E --> T. GOTO [I0,id] = I4 = T --> id. GOTO [I0, (] = I5 = T --> (.E)
E --> .E + T E --> .T GOTO [I5, T] = I3 T --> .id GOTO [I5,id] = I4 T --> .(E) GOTO[I5,(] = I5
Chapter 7: Bottom-Up Parser 33
GOTO [I2, +] = I6 = E --> E + .T T --> .id GOTO[I6,id] = I4 T --> .(E) GOTO[I6,(] = I5
GOTO[I5,E] = I7 = T --> (E.) E E. + T GOTO [I7,+] = I6
GOTO [I6,T] = I8 = E --> E + T. GOTO [I7,)] = I9 T --> (E).
Chapter 7: Bottom-Up Parser 34
Stack Input
0 (id + id ) -|
0(5 id + id )-|
0(5id4 +id)-| R5
0(5T3 +id)-| R4
0(5E7 +id)-| S6
0(5E7+6 Id)-| S4
0(5E7+6id4 )-| R5
0(5E7+6T8 )-| R3
0(5E7 )-| S9
0(5E7)9 -| R6
0T3 -| R4
0E2 -| R2
0S1 -| Accept
Chapter 7: Bottom-Up Parser 35
Follow (S) = -| Follow (E) = -|, +, ) Follow (T) = -|, +, )
Chapter 7: Bottom-Up Parser 36
Action GoTo
State id ( ) + -| S E T
0 S4 S5 1 2 3
1 Acc
2 S6 R2
3 R4 R4 R4
4 R5 R5 R5
5 S4 S5 7 3
6 S4 S5 8
7 S9 S6
8 R3 R3 R3
9 R6 R6 R6
Chapter 7: Bottom-Up Parser 37
Canonical LR Parsing or LR(1)
Introduction: 0- S’ --> S I0 = S’ --> .S1- S --> L = R S --> .L = R2- S --> R S --> .R3- L --> * R L --> .*R4- L --> id L -->. id5- R --> L R --> .L
GOTO[I0, S] = I1 = [S’ --> S.]GOTO[I0, L] = I2 = [S --> L. = R]
R --> L.GOTO[I2, =] = I6 = [S --> L = .R]
Chapter 7: Bottom-Up Parser 38
= id0
1
2 R5/S6
State 2: Follow (R) = {=, } R5State 2: Goto[I2,=]=I5 S5
LR(0) Parse Table
Chapter 7: Bottom-Up Parser 39
Canonical LR Parsing Tables:
LR(1) item: [A --> .B, a] where A --> B is a production and a is a
terminal or the right endmarker $
A--> 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
Chapter 7: Bottom-Up Parser 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) GrammarI0 : S--> .L = R S --> .R L --> .id L --> .*R R --> .L
Chapter 7: Bottom-Up Parser 41
LR(0) Closure (I) = I A --> . XB |
X --> in G
Add
[X --> . ] to I
Chapter 7: Bottom-Up Parser 42
LR(1) Closure (I) = I A --> . XB, a |
For each X --> in G
For each b in first (Ba)
Add
[X --> . , b ] to I [if it is not already in ]
Chapter 7: Bottom-Up Parser 43
GOTO[ I, X] = Closure(j) where: J=[A --> X. B, a ]
[A --> . XB, a ] in I
Chapter 7: Bottom-Up Parser 44
Example:
S’ --> SS --> CCC --> aCC --> d
[A --> . XB, a]For each X--> in GAnd For each b First (Ba) Add
[X --> . , b]
Chapter 7: Bottom-Up Parser 45
Canonical LR(1) Parsing Table: (LR(1))
0- S’ --> S 1- S --> CC 2- C --> aC 3- C --> d
Chapter 7: Bottom-Up Parser 46
Canonical LR(1) Parsing Table: (LR(1))
I0= S’ --> .S, $
S --> .CC, $
C --> .aC, a/d
C --> .d, a/d
Chapter 7: Bottom-Up Parser 47
Canonical LR(1) Parsing Table: (LR(1))
GOTO[I0,S] = I1 = S1 ---> S.,$
GOTO[I0,C] = I2 = S ---> C.C,$
C ---> .aC,$ C ---> .d,$
GOTO[I0,a] = I3 = C ---> a.C,a/d
C ---> .aC,a/d C ---> .d.a/d
GOTO[I0,d] = I4 = C ---> d.,a/d
Chapter 7: Bottom-Up Parser 48
Canonical LR(1) Parsing Table: (LR(1))
GOTO[I2,C] = I5 = S ---> CC.,$ GOTO[I2,a] = I6 = C ---> a.C,$ C ---> .aC,$ C ---> .d,$ GOTO[I2,d] = I7 = C ---> d.,$ GOTO[I3,C] = I8 = C ---> aC.,a/d GOTO[I3,a] = I3
GOTO[I3,d] = I4
GOTO[I6,C] = I9 = C ---> aC.,$ GOTO[I6,a] = I6
GOTO[I6,d] = I7
Chapter 7: Bottom-Up Parser 49
Transition Diagram
Chapter 7: Bottom-Up Parser 50
Action GoTo a d $ S C
0 S3 S4 1 2
1 Acc
2 S6 S7 5
3 S3 S4 8
4 R3 R3
5 R1
6 S6 S7 9
7 R3
8 R2 R2
9 R2
Chapter 7: Bottom-Up Parser 51
State $Input Action/GoTo0 aadd$
Chapter 7: Bottom-Up Parser 52
LALR(1) Parser
LALR(1) = Lookahead Parser Practical Smaller Parse Table Most Programming Languages
• Merge the States: • [A --> . xB, a]• [A --> .xB, b]
To• [A --> .xB, a/b]
Chapter 7: Bottom-Up Parser 53
Canonical LR(1) Parsing Table: (LR(1))
GOTO[I0,S] = I1 = S’ --> S., $ GOTO[I0,C] = I2 = S --> C.C, $
C --> .aC, $C --> .d, $
GOTO[I0,a] = I3 = C --> a.C, a/dC --> .aC, a/dC --> .d, a/d
Chapter 7: Bottom-Up Parser 54
GOTO[I0,d] = I4 = C --> d., a/d GOTO[I2,C] = I5 = S --> CC., $ GOTO[I2,a] = I6 = C --> a.C, $
C .d, $ GOTO[I2,d] = I7 = C --> d., $ GOTO[I3,C] = I8 = C --> aC., a/d GOTO[I3,a] = I3 GOTO[I3,d] = I4 GOTO[I6,C] = I9 = C --> aC., $ GOTO[I6,a] = I6 GOTO[I6,d] = I7
C --> .aC, $
Chapter 7: Bottom-Up Parser 55
Transition Diagram (DFA)
Chapter 7: Bottom-Up Parser 56
LALR Parsing Table:
1. C = {I0, I1, …In} 2. Combine Ij with identical First Part
(Core) and different Lookahead symbols 3. Let C’ {J0, J1,…Jm} be a new set of items
Chapter 7: Bottom-Up Parser 57
LALR Parsing Table:
4. State I of the parser is corresponding to set J i a. Action [I,a] = Shift Ji
If [A --> . A B, b] Ji
GOTO(Ji,a) = Ji
b. Action [I,a] = Reduce A --> if A --> . , a] Ji and A S’ Note: that in case if[A --> . a/b/c/…/z]the action is included in column a,b,…z
c. Action [I,$] = Accept a. IF [S’ --> S.,$] Ji
Chapter 7: Bottom-Up Parser 58
5. 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
Chapter 7: Bottom-Up Parser 59
LALR Parse Table
a d $ S C
0 S36 S47 1 2
1 Acc
2 S36 S47 5
36 S36 S47 89
47 R3 R3 R3
5 R1
89 R2 R2 R2
Chapter 7: Bottom-Up Parser 60
LALR Parse Table (Revised)
a d $ S C
0 S3 S4 1 2
1 Acc
2 S3 S4 5
3 S3 S4 6
4 R3 R3 R3
5 R1
6 R2 R2 R2
Chapter 7: Bottom-Up Parser 61
1-Use a 2 dimensional array(or make two tables – one for action and one for GOTO)
Constructing LALR Parse Table
a d $ S C
0 3 4 99 1 2
1 99 99 0 99 99
2 3 4 99 99 5
3 3 4 99 99 6
4 -3 -3 -3 99 99
5 99 99 -1 99 99
6 -2 -2 -2 99 99
Action Goto
+ Shift- Reduce0 Accept99 Error
Chapter 7: Bottom-Up Parser 62
2-Use Sparce Matrix Representation
0 a 3
0 d 4
0 S 1
0 C 2
1 $ 0
2 a 3
2 d 4
2 C 5
3 a 3
4 d 4
3 C 5
4 a -3
4 d -3
4 $ -3
5 $ -1
6 a -2
6 d -2
6 $ -2
Row Column Action
Chapter 7: Bottom-Up Parser 63
3-Pair Method State Number = Number of elements, Pairs
Input Symbols Actions
3
0
2
1
4
5
6
2 1,3 2,4 A1
Action
1 3,0 A2
3 1,-3 2,-3 3,-3 A3
1 3,-1 A4
3 1,-2 2,-2 3,-2 A5
Chapter 7: Bottom-Up Parser 64
Pair Method (Array Action)
3
1
2
0
4
5
6
A1
A2
A1
A1
A5
A4
A3
Chapter 7: Bottom-Up Parser 65
Algorithm for the Driver(PARSER)
State <-- State 1; Input <-- a, While (Action <> Accept and Action <> Error)
//Stack <-- S0 Xi, S1,…XmSm
//Input <-- ai, ai+1, … $
IF {Sm is terminal}
Case Action [Sm, ai] of+ /*Sn*/: Action <-- Shift{Sn, ai)- /*Rn*/: Action Reduce{n}0 /*Accept:/*: Action <-- Accept99 /*Blank*/: Action <-- Error
Else Action <-- GOTO{Sm, nonterminal)
Chapter 7: Bottom-Up Parser 66
Procedure Reduce(n)Repeat
Pop(state)Pop(symbol)
Until RHS is emptyPush(LHS)
Procedure Shift(S,a)
Push(a)Push(S)
Algorithm for the Driver(PARSER)
Chapter 7: Bottom-Up Parser 67
Procedure GOTO(S, nonterminal)Push(S)
Procedure ShiftTerminal// A --> Bcd id Shift 7
Push(Id)
Push(7)
Procedure ShiftNonterminalPush(State)
Algorithm for the Driver(PARSER)