+ All Categories
Home > Documents > Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

Date post: 29-Mar-2015
Category:
Upload: abdul-wahid-khan
View: 1,142 times
Download: 1 times
Share this document with a friend
Description:
Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311
58
1 Expression Grammar Expression Grammar 1 Goal Expr 2 Expr Expr + Term 3 | Expr Term 4 | Term 5 Term Term Factor 6 | Term Factor 7 | Factor 8 Facto r num 9 | id 10 | ( Expr )
Transcript
Page 1: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

1

Expression GrammarExpression GrammarExpression GrammarExpression Grammar1 Goal → Expr2 Expr → Expr + Term3 | Expr – Term4 | Term5 Term → Term Factor6 | Term ∕ Factor7 | Factor8 Factor → num9 | id10

| ( Expr )

Page 2: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

2

The states of the bottom-up parser on input x – 2 y(tokenized as id – num * id) are

Page 3: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

3

word Stack Handle Action

1 id ► - none - shift

2 – id ► Factor → id,1 reduce

3 – Factor ► Term → Factor,1 reduce

4 – Term ► Expr → Term,1 reduce

5 – Expr ► - none - shift

6 num Expr – ► - none - shift

7 Expr – num ► Factor → num,3 reduce

8 Expr – Factor ► Term → Factor,3 reduce

Page 4: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

4

word Stack Handle Action

9 Expr – Term ► - none - shift

10

id Expr – Term ► - none - shift

11

$ Expr – Term id► Factor → id,5 reduce

12

$ Expr–Term Factor► Term→TermFactor,5

reduce

13

$ Expr – Term ► Expr → Expr – Term,3 reduce

14

$ Expr ► Goal → Expr,1 reduce

15

$ Goal - none - accept

Page 5: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

5

HandlesHandlesHandlesHandles The handle-finding

mechanism is the key to effiecient bottom-up parsing

As it process an input string, the parser must find and track all potential handles

Page 6: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

6

HandlesHandlesHandlesHandles The handle-finding

mechanism is the key to effiecient bottom-up parsing

As it process an input string, the parser must find and track all potential handles

Page 7: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

7

HandlesHandlesHandlesHandles For example, every legal

input eventually reduces the entire frontier to grammar’s goal symbol

Thus, Goal → Expr,1 is a potential handle at the start of every parse

Page 8: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

8

HandlesHandlesHandlesHandles For example, every legal

input eventually reduces the entire frontier to grammar’s goal symbol

Thus, Goal → Expr,1 is a potential handle at the start of every parse

Page 9: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

9

HandlesHandlesHandlesHandles As the parser builds a

derivation, it discovers other handles

At each step, the set of potential handles represent different suffixes that lead to a reduction.

Page 10: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

10

HandlesHandlesHandlesHandles As the parser builds a

derivation, it discovers other handles

At each step, the set of potential handles represent different suffixes that lead to a reduction.

Page 11: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

11

HandlesHandlesHandlesHandles Each potential handle

represent a string of grammar symbols that, if seen, would complete the right-hand side of some production.

Page 12: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

12

HandlesHandlesHandlesHandles

For the bottom-up parse of the expression grammar string, we can represent the potential handles that the shift-reduce parser should track

Page 13: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

13

HandlesHandlesHandlesHandlesUsing the placeholder to represent top of the stack, there are nine handles:

Page 14: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

14

Same

Handles

1 Factor → id

2 Term → Factor

3 Expr → Term

4 Factor → num

5 Term → Factor

6 Factor → id

7 Term → Term Factor

8 Expr → Expr – Term

9 Goal → Expr

Same

Page 15: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

15

HandlesHandlesHandlesHandles This notation shows that the

second and fifth handles are identical, as are first and sixth

It also create a way to represent the potential of discovering a handle in future.

Page 16: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

16

HandlesHandlesHandlesHandles This notation shows that the

second and fifth handles are identical, as are first and sixth

It also create a way to represent the potential of discovering a handle in future.

Page 17: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

17

HandlesHandlesHandlesHandlesConsider the parser’s state in step 6:

6 num Expr – ► - none - shift

7 Expr – num ► Factor → num,3 reduce

8 Expr – Factor ► Term → Factor,3 shift

9 Expr – Term ► - none - shift

10 id Expr – Term ► - none - shift

11 $ Expr – Term id► Factor → id,5 reduce

12 $ Expr–TermFactor►

Term→TermFactor,5

reduce

13 $ Expr – Term ► Expr→Expr–Term,3 reduce

Page 18: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

18

HandlesHandlesHandlesHandlesThe parser has recognized Expr –

6 num Expr – ► - none - shift

7 Expr – num ► Factor → num,3 reduce

8 Expr – Factor ► Term → Factor,3 shift

9 Expr – Term ► - none - shift

10 id Expr – Term ► - none - shift

11 $ Expr – Term id► Factor → id,5 reduce

12 $ Expr–TermFactor►

Term→TermFactor,5

reduce

13 $ Expr – Term ► Expr→Expr–Term,3 reduce

It needs to recognize, eventually, a Term before it can reduce this part of the frontier

Page 19: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

19

HandlesHandlesHandlesHandles Using the stack-relative

notation, we can represent the parser’s state as Expr → Expr – Term

The parser has already recognized an Expr and a –

Page 20: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

20

HandlesHandlesHandlesHandles Using the stack-relative

notation, we can represent the parser’s state as Expr → Expr – Term

The parser has already recognized an Expr and a –

Page 21: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

21

HandlesHandlesHandlesHandles If the parser reaches a state

where it shifts a Term on top of Expr and –, it will complete the handleExpr → Expr – Term

Page 22: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

22

HandlesHandlesHandlesHandles How many potential handles

must the parser recognize?

The right-hand side of each production can have a placeholder at its start, at its end and between any two consecutive symbols

Page 23: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

23

HandlesHandlesHandlesHandles How many potential handles

must the parser recognize?

The right-hand side of each production can have a placeholder at its start, at its end and between any two consecutive symbols

Page 24: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

24

HandlesHandlesHandlesHandlesExpr → Expr –TermExpr → Expr – TermExpr → Expr – TermExpr → Expr – Term

if the right-hand side has k symbols, it has k +1 placeholder positions

Page 25: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

25

HandlesHandlesHandlesHandles The number of potential handles

for the grammar is simply the sum of the lengths of the right-hand side of all the productions

The number of complete handles is simply the number of productions

Page 26: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

26

HandlesHandlesHandlesHandles The number of potential handles

for the grammar is simply the sum of the lengths of the right-hand side of all the productions

The number of complete handles is simply the number of productions

Page 27: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

27

HandlesHandlesHandlesHandles These two facts lead to the

critical insight behind LR parsers:

A given grammar generates a finite set of handles (and potential handles) that the parser must recognize

Page 28: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

28

HandlesHandlesHandlesHandles These two facts lead to the

critical insight behind LR parsers:

A given grammar generates a finite set of handles (and potential handles) that the parser must recognize

Page 29: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

29

LookaheadLookaheadLookaheadLookahead However, it is not a simple

matter of putting the placeholder in right-hand side to generate handles

The parser needs to recognize the correct handle by different right contexts

Page 30: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

30

LookaheadLookaheadLookaheadLookahead However, it is not a simple

matter of putting the placeholder in right-hand side to generate handles

The parser needs to recognize the correct handle by different right contexts

Page 31: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

31

word Stack Handle Action

9 Expr – Term ► - none - shift

10

id Expr – Term ► - none - shift

11

$ Expr – Term id► Factor → id,5 reduce

12

$ Expr–Term Factor►

Term→TermFactor,5 reduce

13

$ Expr – Term ► Expr → Expr – Term,3 reduce

14

$ Expr ► Goal → Expr,1 reduce

15

$ Goal - none - accept

Consider the parser’s action at step 9.The frontier is Expr – Term, suggesting a handle Expr → Expr – Term However, the parser decides to extend the frontier by shifting on to the stack rather than reducing frontier to Expr

Page 32: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

32

LookaheadLookaheadLookaheadLookahead Clearly, this the correct move

for the parser

No potential handle contains Expr followed by

Page 33: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

33

LookaheadLookaheadLookaheadLookahead Clearly, this the correct move

for the parser

No potential handle contains Expr followed by

Page 34: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

34

LookaheadLookaheadLookaheadLookahead At step 9, the set of potential

handles is

Expr → Expr – Term

Term →Term Factor

Term →Term / Factor

Page 35: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

35

LookaheadLookaheadLookaheadLookahead The next input symbol clearly

matches the second choice

Expr → Expr – Term

Term →Term Factor

Term →Term / Factor

Page 36: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

36

LookaheadLookaheadLookaheadLookahead The parser needs a basis for

deciding between first (reduce) and second (shift) choices:

Expr → Expr – Term

Term →Term Factor

Page 37: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

37

LookaheadLookaheadLookaheadLookahead This requires more context

than the parser has in the frontier (stack)

Page 38: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

38

LookaheadLookaheadLookaheadLookahead To choose between reducing

and shifting, the parser must recognize which symbols can occur to the right of Expr and Term in valid phrases

Page 39: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

39

LR(1) ParsersLR(1) ParsersLR(1) ParsersLR(1) Parsers The LR(1) parsers can

recognize precisely those languages in which one-symbol lookahead suffices to determine whether to shift or reduce.

Page 40: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

40

LR(1) ParsersLR(1) ParsersLR(1) ParsersLR(1) Parsers The LR(1) construction

algorithm builds a handle-recognizing DFA

The parsing algorithm uses this DFA to recognize handles and potential handles on the parse stack

Page 41: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

41

LR(1) ParsersLR(1) ParsersLR(1) ParsersLR(1) Parsers The LR(1) construction

algorithm builds a handle-recognizing DFA

The parsing algorithm uses this DFA to recognize handles and potential handles on the parse stack

Page 42: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

42

0

10

1

2 3 4

76 5

11

8 9

Reduce:E → int on ),+

int

E

E

+ (

E

)

+

int

int(

+

)

Reduce:E → int on $,+

accept on $

Reduce:E → E+(E) on ),+

Reduce:E → E+(E) on ),+

Page 43: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

43

Parsing DFAParsing DFAParsing DFAParsing DFA►int + (int) + (int) $

shift 1

int ► + (int) + (int) $

reduce E → int

E ► + (int) + (int) $

shift 3 times

E + (int ►) + (int) $

reduce E → int

E + (E ►) + (int) $ shift

E + (E) ► + (int) $ red E → E+(E)

Page 44: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

44

Parsing DFAParsing DFAParsing DFAParsing DFA

How can the parser remember the state the DFA goes into on a symbol?

Page 45: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

45

Parsing DFAParsing DFAParsing DFAParsing DFA

Store the DFA state in the stack along with the symbol

Initial entry in the stack will be ‘<dummy,0>’.

Page 46: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

46

◘ is dummy symbol

◘ 0 ►int + (int)+(int) $

shift 1

◘ 0 int 1 ►+(int)+(int)$

reduce E → int

◘ 0 E 2 ►+(int)+(int)$

shift 3 times

.. ... ... ... ....int

stack

0

1

Page 47: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

47

◘ 0 ►int + (int)+(int) $

shift 1

◘ 0 int 1 ►+(int)+(int)$

reduce E → int

◘ 0 E 2 ►+(int)+(int)$

shift 3 times

.. ... ... ... ....int

stack

0

1

E 2

Page 48: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

48

Parsing DFAParsing DFAParsing DFAParsing DFA

How is next state decided upon a reduce?

◘ 0 ►int + (int)+(int) $

shift 1

◘ 0 int 1 ►+(int)+(int)$

reduce E → int

◘ 0 E 2 ►+(int)+(int)$

shift 3 times

.. ... ... ... ....

Page 49: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

49

Representing DFARepresenting DFARepresenting DFARepresenting DFA Parsers represent DFA as a

2D table

Rows correspond to DFA states

Columns correspond to terminals and non-terminals

Page 50: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

50

Representing DFARepresenting DFARepresenting DFARepresenting DFA Parsers represent DFA as a

2D table

Rows correspond to DFA states

Columns correspond to terminals and non-terminals

Page 51: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

51

Representing DFARepresenting DFARepresenting DFARepresenting DFA Parsers represent DFA as a

2D table

Rows correspond to DFA states

Columns correspond to terminals and non-terminals

Page 52: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

52

Representing DFARepresenting DFARepresenting DFARepresenting DFA

columns are split into:

those for terminals: action table

those for non-terminals: goto table

Page 53: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

53

3

7

6E → int on ),+

(

E

)

int

E → E+(E) on ),+

4

5

state int + ( ) $ E...

3 s4

4 s5 g6

5 r2 r2

6 s8 s7

7 r1 r1

...

action goto

Page 54: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

54

Parsing DFAParsing DFAParsing DFAParsing DFA

The state comes from the goto table

◘ 0 ►int + (int)+(int) $

shift 1

◘ 0 int 1 ►+(int)+(int)$

reduce E → int

◘ 0 E 2 ►+(int)+(int)$ shift 3 times

.. ... ... ... ....

Page 55: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

55

Building LR(1) TablesBuilding LR(1) TablesBuilding LR(1) TablesBuilding LR(1) Tables To construct Action and Goto

tables, the LR(1) parser generator builds a model of handle-recognizing DFA

The model is used to fill in the tables

Page 56: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

56

Building LR(1) TablesBuilding LR(1) TablesBuilding LR(1) TablesBuilding LR(1) Tables To construct Action and Goto

tables, the LR(1) parser generator builds a model of handle-recognizing DFA

The model is used to fill in the tables

Page 57: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

57

LR(1) ItemsLR(1) ItemsLR(1) ItemsLR(1) Items The LR(1)-Table construction

needs a concrete representation for the handles and their associated lookahead symbols.

We call this representation an LR(1) item.

Page 58: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

58

LR(1) ItemsLR(1) ItemsLR(1) ItemsLR(1) Items The LR(1)-Table construction

needs a concrete representation for the handles and their associated lookahead symbols.

We call this representation an LR(1) item.


Recommended