+ All Categories
Home > Documents > Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... ·...

Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... ·...

Date post: 22-Jul-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
155
Spring 2016 CSCI 565 - Compiler Design Pedro Diniz [email protected] Syntactic Analysis Implementing a Parser LR parsing tables Copyright 2016, Pedro C. Diniz, all rights reserved. Students enrolled in Compilers class at University of Southern California (USC) have explicit permission to make copies of these materials for their personal use.
Transcript
Page 1: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

Syntactic Analysis

Implementing a ParserLR parsing tables

Copyright 2016, Pedro C. Diniz, all rights reserved.Students enrolled in Compilers class at University of Southern California (USC)have explicit permission to make copies of these materials for their personal use.

Page 2: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 2

Outline

• Implementing a Parser • Shift-Reduce Parser Example• Why is it hard to build a Parser Engine?• LR(k) Parser Tables

Page 3: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 3

• Different Parsing Techniques– Each can handle some set of CFGs– Categorization of techniques

Implementing a Parser

Page 4: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 4

• Different Parsing Techniques– Each can handle some set of CFGs– Categorization of techniques

( )

Implementing a Parser

Page 5: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 5

• Different Parsing Techniques– Each can handle some set of CFGs– Categorization of techniques

– L - parse from left to right– R - parse from right to left

( )

Implementing a Parser

Page 6: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 6

• Different Parsing Techniques– Each can handle some set of CFGs– Categorization of techniques

– L - leftmost derivation– R - rightmost derivation

( )

Implementing a Parser

Page 7: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 7

• Different Parsing Techniques– Each can handle some set of CFGs– Categorization of techniques

– Number of lookahead characters

( )

Implementing a Parser

Page 8: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 8

• Different Parsing Techniques– Each can handle some set of CFGs– Categorization of techniques

– Examples: LL(0), LR(1)

( )

Implementing a Parser

Page 9: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 9

• Different Parsing Techniques– Each can handle some set of CFGs– Categorization of techniques

– Examples: LL(0), LR(1)

• We will be studying LR(k) parsers

( )L R k

Implementing a Parser

Page 10: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 10

• Implementing a Parser• Shift-Reduce Parser Example• Why is it hard to build a parser engine?• LR(k) parser tables• Constructing a LR(0) Parser Engine

Outline

Page 11: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 11

Why use a LR(k) parser• Can be construct to recognize a large class of CFGs

– virtually all programming language constructs

• Most general non-backtracking parsing method

• Can build a very efficient very parser engine

• Can detect a syntactic error as soon as it is possible to do so

Page 12: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 12

Let’s look at a Parser Implementation

• Workings of a LR(k) Parser• Parse from Left to Right • Rightmost Derivation

– Starts with the entire String– Ends with the Start Symbol

Page 13: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 13

Actions of a Shift-Reduce Parser

ParseTree

Page 14: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 14

Actions of a Shift-Reduce Parser

ParseTreeParseTree

Page 15: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 15

Actions of a Shift-Reduce Parser

ParseTreeParseTree

Page 16: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 16

Actions of a Shift-Reduce Parser

ParseTreeParseTree

Page 17: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 17

Actions of a Shift-Reduce Parser

ParseTreeParseTree

Page 18: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 18

Actions of a Shift-Reduce Parser

ParseTreeParseTree

Page 19: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 19

Actions of a Shift-Reduce Parser

ParseTreeParseTree

Page 20: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 20

Actions of a Shift-Reduce Parser

ParseTreeParseTree

Page 21: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 21

Actions of a Shift-Reduce Parser

ParseTree

Page 22: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 22

Actions of a Shift-Reduce Parser

ParseTree

Page 23: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 23

Actions of a Shift-Reduce Parser

ParseTree

Page 24: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 24

Actions of a Shift-Reduce Parser

ParseTree

Page 25: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 25

Actions of a Shift-Reduce Parser

• Cannot create a full sub parse tree • Need the look ahead information• Thus, keep some state

ParseTree

Page 26: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 26

Actions of a Shift-Reduce Parser

Cur

rent

Sta

teParseTree

Page 27: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 27

Actions of a Shift-Reduce Parser

Stac

k

Current Symbol

StackParser A

ction

ParserEngine

Page 28: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 28

Actions of a Shift-Reduce Parser

• Shift– Shift the current element on top of the stack– Move the current input pointer

• Reduce– Apply a production– Top of the stack should match the RHS of a production– Remove those symbols from the stack– Add the LHS non-terminal to the stack

• Accept– End of stream reached &– Stack only has the start symbol

• Reject– End of stream reached but – stack has more than the start symbol

Page 29: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 29

Shift-Reduce Parser Example

* ( + num )numnum

Page 30: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 30

Shift-Reduce Parser Example

* ( + num )numnum

Page 31: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 31

Shift-Reduce Parser Example

* ( + num )numnum

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

Page 32: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 32

Shift-Reduce Parser Example

* ( + num )numnum

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

Page 33: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 33

Shift-Reduce Parser Example

* ( + num )num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

num

Page 34: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 34

Shift-Reduce Parser Example

* ( + num )numnum

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

num

SHIF

T

Page 35: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 35

Shift-Reduce Parser Example

* ( + num )numnum

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

num

Page 36: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 36

Shift-Reduce Parser Example

* ( + num )numnum

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

num

RED

UC

E

Page 37: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 37

Shift-Reduce Parser Example

* ( + num )num

<expr><expr> <expr><op> <op>

<expr>

<expr>

<expr>

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<expr>

RED

UC

E

Page 38: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 38

Shift-Reduce Parser Example

* ( + num )num

<expr><expr> <expr><op> <op>

<expr>

<expr>

<expr>

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<expr>

Page 39: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 39

Shift-Reduce Parser Example

( + num )num

<expr><expr> <expr><op> <op>

<expr>

<expr>

<expr>

num *

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

*<expr>

SHIF

T

Page 40: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 40

Shift-Reduce Parser Example

( + num )num

<expr><expr> <expr>

<op><op>

<expr>

<expr>

<expr>

num *

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

*<expr>

Page 41: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 41

Shift-Reduce Parser Example

( + num )num

<expr><expr> <expr><op> <op>

<expr>

<expr>

<expr>

num *

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

*<expr>

RED

UC

E

Page 42: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 42

Shift-Reduce Parser Example

( + num )num

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num *

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op><expr>

RED

UC

E

Page 43: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 43

(

Shift-Reduce Parser Example

+ num )num

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num *

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op><expr>

Page 44: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 44

Shift-Reduce Parser Example

+ num )num

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num * (

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr>

SHIF

T

Page 45: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 45

Shift-Reduce Parser Example

+ num )num

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num * (

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr>

Page 46: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 46

Shift-Reduce Parser Example

* + num )

<expr><expr> <expr>

<op><op>

<expr>

<expr>

<expr>

num ( num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

num

<expr>

SHIF

T

Page 47: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 47

Shift-Reduce Parser Example

* + num )

<expr><expr> <expr>

<op><op>

<expr>

<expr>

<expr>

num ( num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

num

<expr>

Page 48: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 48

Shift-Reduce Parser Example

* + num )

<expr><expr>

<op>

<expr>

<expr>

<expr>

num ( num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

num

<expr>

RED

UC

E

Page 49: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 49

Shift-Reduce Parser Example

* + num )

<expr> <expr><expr>

<op><op>

<expr>

<expr><expr>

num ( num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr>

<expr>

RED

UC

E

Page 50: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 50

+

Shift-Reduce Parser Example

* num )

<expr> <expr><expr>

<op><op>

<expr>

<expr>

<expr>

num ( num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr>

<expr>

Page 51: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 51

Shift-Reduce Parser Example

* num )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num +

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr>+

<expr>

SHIF

T

Page 52: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 52

Shift-Reduce Parser Example

* num )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num +

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr>+

<expr>

Page 53: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 53

Shift-Reduce Parser Example

* num )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num +

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr>+

<expr>

RED

UC

E

Page 54: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 54

Shift-Reduce Parser Example

* num )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num +

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr><op>

<expr>

RED

UC

E

Page 55: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 55

num

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num +

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr><op>

<expr>

Page 56: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 56

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr><op>num

<expr>

SHIF

T

Page 57: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 57

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr><op>num

<expr>

Page 58: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 58

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr><op>num

<expr>

RED

UC

E

Page 59: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 59

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr><op>

<expr>

<expr>

RED

UC

E

Page 60: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 60

Shift-Reduce Parser Example

* )

<expr> <op>

<expr>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr><op>

<expr>

<expr>

<op><expr> <expr>

Page 61: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 61

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr><op>

<expr>

<expr>

RED

UC

E

Page 62: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 62

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr>

<expr>

<expr>

RED

UC

E

Page 63: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 63

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr>

<expr>

Page 64: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 64

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr>)

<expr>

SHIF

T

Page 65: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 65

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op>(

<expr>)

<expr>

Page 66: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 66

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )

<expr> → - <expr><expr> → num

<op> → +<op> → -<op> → *

<op>(

<expr>)

<expr>

RED

UC

E

Page 67: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 67

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )

<expr> → - <expr><expr> → num

<op> → +<op> → -<op> → *

<op><expr>

<expr>

<expr>

<expr>

RED

UC

E

Page 68: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 68

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op><expr>

<expr>

Page 69: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 69

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *

<op><expr>

<expr>

RED

UC

E

Page 70: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 70

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *<expr>

<expr>

RED

UC

E

Page 71: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 71

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *<expr>

<expr>

Page 72: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 72

Shift-Reduce Parser Example

* )

<expr> <expr> <expr><op> <op>

<expr>

<expr>

num ( num + num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → - <expr>

<expr> → num<op> → +<op> → -<op> → *<expr>

<expr>

AC

CEP

T

Page 73: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 73

What does the Parser Engine do?• If the top symbols of the stack match an RHS

of a production do the reduction – Pop the RHS from the top of the stack– Push the LHS symbol onto the stack

• If no production is found do the shift– Push the current input into the stack

• If the input is empty– Accept if only the start symbol is on the stack– Reject otherwise Parser

Engine

Page 74: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 74

Outline• Implementing a Parser • Shift-Reduce Parser Example• Why is it Hard to build a Parser Engine?• LR(k) parser tables• Constructing a LR(0) Parser Engine

Page 75: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 75

What does the Parser Engine do?• If the Top Symbols of the Stack Match an RHS

of a Production do the Reduction – Pop the RHS from the top of the stack– Push the LHS symbol onto the stack

• If No Production is Found do the Shift– Push the current input into the stack

• If the Input is Empty– Accept if only the start symbol is on the stack– Reject otherwise Parser

Engine

Page 76: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 76

This is not that Simple!

• Many Choices of Reductions– Matches multiple RHS

• Choice between Shift and Reduce– Stack matches a RHS– But that may not be the right match– May need to shift and later find a different reduction

Page 77: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 77

Shift-Reduce Parser Example

<expr> → <expr> <op> <expr><expr> → ( <expr> )

<expr> → - <expr><expr> → num<op> → +<op> → -<op> → *

• Change in the Grammar

Page 78: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 78

Shift-Reduce Parser Example

<expr> → <expr> <op> <expr><expr> → ( <expr> )

<expr> → - <expr><expr> → num<op> → +<op> → -<op> → *

• Change in the Grammar

Page 79: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 79

Shift-Reduce Parser Example

<expr> → <expr> <op> <expr><expr> → ( <expr> )

<expr> → <expr> -<expr> → num<op> → +<op> → -<op> → *

• Change in the Grammar

Page 80: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 80

Shift-Reduce Parser Example

- numnum

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

Page 81: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 81

Shift-Reduce Parser Example

- num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

num

Page 82: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 82

num

Shift-Reduce Parser Example

- numnumnum

SHIF

T

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

Page 83: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 83

num

Shift-Reduce Parser Example

- num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum

Page 84: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 84

num

Shift-Reduce Parser Example

- num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum

RED

UC

E

Page 85: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 85

<expr>

Shift-Reduce Parser Example

- num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum

RED

UC

E

<expr>

Page 86: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 86

<expr>

Shift-Reduce Parser Example

- num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum

<expr>

Page 87: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 87

-<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<op><expr>

SHIF

T

Page 88: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 88

-<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<op><expr>

Page 89: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 89

-<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

We have a choice!!!<op><expr>

RED

UC

E

Page 90: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 90

-<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

But not the right thing to do!!<op><expr>

RED

UC

E

Page 91: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 91

<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

But not the right thing to do!!<expr>

<expr>

RED

UC

E

Page 92: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 92

<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

But not the right thing to do!!<expr>

<expr>

Page 93: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 93

num<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

But not the right thing to do!!<expr>

<expr>

SHIF

T

Page 94: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 94

num<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

But not the right thing to do!!<expr>

<expr>

Page 95: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 95

num<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

But not the right thing to do!!<expr>

<expr>

RED

UC

E

Page 96: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 96

<expr><expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

But not the right thing to do!!<expr>

<expr>

<expr>

RED

UC

E

Page 97: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 97

<expr><expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

But not the right thing to do!!<expr>

<expr>

<expr>

Page 98: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 98

<expr><expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

But not the right thing to do!!No more actions!!!

<expr>

<expr>

<expr>

ERR

OR

Page 99: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 99

Shift-Reduce Parser Example

numnumnum -

• But this is perfectly valid input for the grammar• We chose a wrong production • Lets see what is the right production

Page 100: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 100

-<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

We have a choice<op><expr>

RED

UC

E

The step before we went wrong

Page 101: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 101

-<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

Use the other production

<op><expr>

RED

UC

E

Page 102: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 102

<op><expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<expr>

RED

UC

E

<op> Use the other production

Page 103: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 103

<op><expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<expr> <op>

Page 104: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 104

num<op>

<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<expr> <op>

SHIF

T

Page 105: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 105

num<op>

<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<expr> <op>

Page 106: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 106

num<op>

<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<expr> <op>

RED

UC

E

Page 107: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 107

<expr><op>

<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<expr> <op>

RED

UC

E

<expr>

Page 108: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 108

<expr><op>

<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<expr> <op> <expr>

Page 109: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 109

<expr><op>

<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<expr> <op> <expr>

RED

UC

E

Page 110: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 110

<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<expr> <op> <expr>

RED

UC

E<expr>

Page 111: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 111

<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<expr> <op> <expr>

<expr>

Page 112: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 112

<expr>

Shift-Reduce Parser Example

num

<expr> → <expr> <op> <expr><expr> → ( <expr> )<expr> → <expr> -

<expr> → num<op> → +<op> → -<op> → *

numnum -

<expr> <op> <expr>

<expr>

AC

CEP

T

Page 113: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 113

This is not that Simple!

• Many Choices of Reductions– Matches Multiple RHS

• Choice between Shift and Reduce– Stack Matches a RHS– But that may not be the ‘right’ match– May need to Shift and later find a different Reduction

• Keep additional information

Page 114: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 114

Outline

• Implementing a Parser • Shift-Reduce Parser Example• Why is it hard to build a Parser Engine?• LR(k) parser tables• Constructing a LR(0) Parser Engine

Page 115: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 115

Constructing a LR(k) Parser

• We will construct few LR(k) parsers– LR(0), – SLR (or simple LR)– LR(1)

Page 116: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 116

Constructing a LR(k) Parser

• We will construct few LR(k) parsers– LR(0), – SLR (or simple LR)– LR(1)

• We followed the Parsing Actions• What is in the Parse Engine?

– Decision between shift and reduce– Decide on the “right” reduction

Page 117: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 117

Actions of a Shift-Reduce Parser

Stac

k

Current Symbol

StackParser A

ction

ParserEngine

Page 118: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 118

Constructing a LR(k) Parser• Create a DFA

– Encodes all the possible states that the parser can be in– DFA state transition occurs on terminals and non-terminals

• Create an Parser table – Stores what action should be taken for the current

state and current input character

• Maintain a Stack of States – in parallel with the stack of symbols

Page 119: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 119

LR(k) Parser Engine

Current Symbol

Parser Action

LR(k)ParserEngine

Sym

bol S

tack

Stat

e St

ack

ACTION GotoState ( ) $ X

s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)

Page 120: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 120

Parser Tables

• Look-up [top of state stack] [ input symbol] in the Parser Table

• Carry-out the Corresponding Action

ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accepts2 shift to s2 shift to s5 error goto s3s3 error shift to s4 errors4 reduce (2) reduce (2) reduce (2)s5 reduce (3) reduce (3) reduce (3)

Page 121: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 121

• Shift to sn– Push input token into the symbol stack– Push sn into state stack and move to state sn– Advance to next input symbol

ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accepts2 shift to s2 shift to s5 error goto s3s3 error shift to s4 errors4 reduce (2) reduce (2) reduce (2)s5 reduce (3) reduce (3) reduce (3)

Parser Tables

Page 122: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 122

• Reduce (n)– Pop both stacks as many times as the number of symbols on the RHS of rule n– Push LHS of production onto the symbol stack – Let t be top of state stack

– Lookup [top of the state stack t][top of symbol stack] in Goto section of table– Push that state (in Goto k) into state stack and move to that state

ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accepts2 shift to s2 shift to s5 error goto s3s3 error shift to s4 errors4 reduce (2) reduce (2) reduce (2)s5 reduce (3) reduce (3) reduce (3)

Parser Tables

Page 123: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 123

• Accept– Stop parsing and report success

ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accepts2 shift to s2 shift to s5 error goto s3s3 error shift to s4 errors4 reduce (2) reduce (2) reduce (2)s5 reduce (3) reduce (3) reduce (3)

Parser Tables

Page 124: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 124

• Error– Stop parsing and report failure

ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accepts2 shift to s2 shift to s5 error goto s3s3 error shift to s4 errors4 reduce (2) reduce (2) reduce (2)s5 reduce (3) reduce (3) reduce (3)

Parser Tables

Page 125: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 125

LR Example

• The grammar<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

Page 126: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 126

Question

• The grammar<S> → <X>$ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

• What is the language accepted by this CFG?

Page 127: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 127

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

Page 128: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 128

Parser Table in ActionAction Table

• The grammar<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

Page 129: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 129

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

Action Table

Page 130: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 130

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

Action Table

Page 131: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 131

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 132: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 132

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 133: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 133

(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

Action Table

Page 134: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 134

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 135: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 135

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

(s2(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 136: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 136

(s2(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

Action Table

Page 137: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 137

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

(s2(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 138: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 138

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

s5 )(s2(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 139: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 139

s5 )(s2(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

Action Table

Page 140: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 140

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

s5 )(s2(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 141: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 141

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

s5 )(s2(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 142: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 142

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

s5 )(s2

(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 143: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 143

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

X(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 144: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 144

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

X(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 145: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 145

s3

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

X(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 146: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 146

s3

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

X(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 147: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 147

s4 )s3

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

X(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 148: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 148

s4 )s3

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

X(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 149: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 149

s4 )s3

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

X(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 150: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 150

s4 )s3

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

X(s2

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 151: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 151

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

X

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 152: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 152

s1

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

X

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 153: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 153

s1

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

X

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $

Action Table

Page 154: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 154

s1

Action GotoState ( ) $

s0 goto s1

s1s2s3s4s5

goto s3

errorerrorerror

accept

error

errorerror

error

shift to s2

shift to s2

reduce (2)reduce (3)

reduce (2)reduce (3)

reduce (2)reduce (3)

shift to s5shift to s4

X

X

Parser Table in Action• The grammar

<S> → <X> $ (1)<X> → ( <X> ) (2)<X> → ( ) (3)

s0 $)( ( ) $ Accept

Action Table

Page 155: Syntactic Analysis - Information Sciences Institutepedro/Teaching/CSCI565-Spring16/Lectures/... · • Workings of a LR(k) Parser • Parse from Left to Right • Rightmost Derivation

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected] 155

Summary

• Parser Implementation• Shift-Reduce Parsing• Examples


Recommended