Compiler Designs and Constructions - PowerPoint PPT Presentation

1 / 67
About This Presentation
Title:

Compiler Designs and Constructions

Description:

There are Two (2) types of Bottom-Up Parsers: 1-Operator Precedence Parser: ... Chapter 7: Bottom-Up Parser. 11. Implementing the Parser as a Finite State Machine ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 68
Provided by: scie6
Category:

less

Transcript and Presenter's Notes

Title: Compiler Designs and Constructions


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

2
Bottom- Up Parsing (Shift and Reduce)
  • 1- S ---gt aAcBe
  • 2- A ---gt Ab
  • 3- A ---gt b
  • 4- B ---gt d
  • Input abbcde

3
Derivation
  • RMD
  • LMD

4
Derivation Tree
abbcde
5
Definition of Handles
Handle S gt ?A ? gt ?B? A---gt B
  • Implementation
  • Shift Push(NextInputSymbol), Advance
  • Reduce Pop(Handle), Push(LHS)
  • Accept
  • Error

6
Stack Implementation of Shift-Reduce 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 ---gt EE

8
Example
  • ltSgt ---gt While ltEXPgt doltEXPgt ---gt ltEXPgt
    ltEXPgtltEXPgt ---gt ltEXPgt gt ltEXPgtltEXPgt ---gt ltEXPgt
    lt ltEXPgtltEXPgt ---gt id
  • WHY?

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

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

11
Implementing the Parser as a Finite State Machine
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

13
Example
  • 1- E ---gt ET
  • 2- E ---gt T
  • 3- T ---gt TF
  • 4- T ---gt F
  • 5- F ---gt (E)
  • 6- F ---gt id

14
State
Action
Goto
15
(No Transcript)
16
SLR 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)

19
Example
  • 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

20
GoTo
  • 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

21
Example
  • 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

22
Example 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
23
Example 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

24
Example 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

25
Example 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

26
Canonical Collections
  • C(I0,I1,I2,I3,I4,I5,I6,I7,I8,I9,I10,I11)
  •  
  • Follow(E) , ), Follow (T) ,
    , ), Follow(F) , , ),

27
Transition Diagram
28
Algorithm 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

29
Algorithm 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.

30
Algorithm 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

31
SLR 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).

34
Stack Input
35
  • Follow (S) -
  • Follow (E) -, , )
  • Follow (T) -, , )

36
Action GoTo
37
Canonical 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

38
LR(0) Parse Table
State 2 Follow (R) , R5 State 2
GotoI2,I5 S5
39
Canonical 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

44
Example
  • 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

45
Canonical LR(1) Parsing Table (LR(1))
  • 0- S --gt S
  • 1- S --gt CC
  • 2- C --gt aC
  • 3- C --gt d

46
Canonical LR(1) Parsing Table (LR(1))
  • I0 S --gt .S,
  • S --gt .CC,
  • C --gt .aC, a/d
  • C --gt .d, a/d

47
Canonical 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

48
Canonical 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

49
Transition Diagram
50
Action GoTo
R2
51
  • State Input Action/GoTo
  • 0 aadd

52
LALR(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

53
Canonical 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,

55
Transition Diagram (DFA)
56
LALR 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

57
LALR 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

59
LALR Parse Table
60
LALR Parse Table (Revised)
61
Constructing 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

62
2-Use Sparce Matrix Representation
Row Column Action
63
3-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
64
Pair Method (Array Action)
0
A1
1
A2
2
A1
3
A1
A3
4
A4
5
A5
6
65
Algorithm 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)

66
Algorithm 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)

67
Algorithm 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)
Write a Comment
User Comments (0)
About PowerShow.com