Lectures 7 and 8. Parsing (syntax analysis) Wei Le 2015.9 Bottom - - PowerPoint PPT Presentation

lectures 7 and 8 parsing syntax analysis
SMART_READER_LITE
LIVE PREVIEW

Lectures 7 and 8. Parsing (syntax analysis) Wei Le 2015.9 Bottom - - PowerPoint PPT Presentation

Lectures 7 and 8. Parsing (syntax analysis) Wei Le 2015.9 Bottom Up Parsing Recognize many programming languages, used in practice More general, more powerful parser: less restrictions on grammars, e.g., can handle left-recursive grammar


slide-1
SLIDE 1

Lectures 7 and 8. Parsing (syntax analysis)

Wei Le 2015.9

slide-2
SLIDE 2

Bottom Up Parsing

◮ Recognize many programming languages, used in practice ◮ More general, more powerful parser: less restrictions on grammars,

e.g., can handle left-recursive grammar

◮ no backtracking ◮ LR parsing: L – tokens are read from the left to right, R – rightmost

derivation

◮ LR parsing reduces a string to the start symbol by inverting

productions

slide-3
SLIDE 3

LR Parsing

slide-4
SLIDE 4

LR Parsing: An Example

slide-5
SLIDE 5

LR Parsing: An Example

slide-6
SLIDE 6

LR Parsing: An Example

slide-7
SLIDE 7

LR Parsing: An Example

slide-8
SLIDE 8

LR Parsing: An Example

slide-9
SLIDE 9

LR Parsing: An Example

slide-10
SLIDE 10

LR Parsing: Important Factor #1

slide-11
SLIDE 11

LR Parsing: Where Reduction Happens

slide-12
SLIDE 12

LR Parsing: Split Notation

slide-13
SLIDE 13

LR Parsing: Shift and Reduce

slide-14
SLIDE 14

Shift and Reduce Example

slide-15
SLIDE 15

Shift and Reduce Example

slide-16
SLIDE 16

Shift and Reduce Example

slide-17
SLIDE 17

Shift and Reduce Example

slide-18
SLIDE 18

Shift and Reduce Example

slide-19
SLIDE 19

Shift and Reduce Example

slide-20
SLIDE 20

Shift and Reduce Example

slide-21
SLIDE 21

Shift and Reduce Example

slide-22
SLIDE 22

Shift and Reduce Example

slide-23
SLIDE 23

Shift and Reduce Example

slide-24
SLIDE 24

Shift and Reduce Example

slide-25
SLIDE 25

The stack

slide-26
SLIDE 26

Key issues: when to shift or reduce

slide-27
SLIDE 27

Key issues: when to shift or reduce

slide-28
SLIDE 28

LR Parsing Algorithm

slide-29
SLIDE 29

Parsing Table

slide-30
SLIDE 30

Parsing Table

slide-31
SLIDE 31

Table-Driven Bottom Up Parsing

slide-32
SLIDE 32

LR Parsing Algorithm

slide-33
SLIDE 33

Review: Key Ideas of LR Parsing

slide-34
SLIDE 34

Review: Key Ideas of LR Parsing

slide-35
SLIDE 35

How to Decide When to Shift or Reduce

slide-36
SLIDE 36

How to Construct the Parsing Table

Run stack on DFA: search which context matches the stack?

slide-37
SLIDE 37

LR(1) Item Defines the State (or Context)

slide-38
SLIDE 38

Notation Clarification

slide-39
SLIDE 39

LR(1) Items Are Used to Define DFA

slide-40
SLIDE 40

LR(1) Items Are Used to Define DFA: An Example

slide-41
SLIDE 41

LR(1) Items Are Used to Define DFA: An Example

slide-42
SLIDE 42

How To Determine a set of LR(1) Items for the State?

slide-43
SLIDE 43

Closure Operation: An Example

slide-44
SLIDE 44

Closure Operation: An Example

slide-45
SLIDE 45

Closure Operation: An Example

slide-46
SLIDE 46

LR(1) Items Are Used to Define DFA: An Example

slide-47
SLIDE 47

LR(1) Items Are Used to Define DFA: Transitions

slide-48
SLIDE 48

Automatically Generating Parsing Tables

slide-49
SLIDE 49

Shift and Reduce Conflict

slide-50
SLIDE 50

Shift and Reduce Conflict

slide-51
SLIDE 51

Shift and Reduce Conflict

slide-52
SLIDE 52

Shift and Reduce Conflict

slide-53
SLIDE 53

Reduce Reduce Conflict

slide-54
SLIDE 54

Reduce Reduce Conflict

slide-55
SLIDE 55

Reduce Reduce Conflict

slide-56
SLIDE 56

Motivation of LALR

slide-57
SLIDE 57

Motivation of LALR

slide-58
SLIDE 58

Motivation of LALR

slide-59
SLIDE 59

LALR

slide-60
SLIDE 60

LALR

slide-61
SLIDE 61

LALR: Example

slide-62
SLIDE 62

LALR

slide-63
SLIDE 63

LALR

slide-64
SLIDE 64

Parsing: A Summary

◮ Parse tree: how to derive a string from the grammar ◮ Context free grammars ◮ Algorithms: Recursive Decedent Parsers, LL (1), LR (1), LALR (1) ◮ General approach: table-driven parsing (the stack, streams of

tokens, general parsing algorithm), using table to guide each step of building parsing trees

◮ Challenge: Building parsing table