payap university ics220 - data structures and algorithm analysis instructor: dr. ken cosh

60
Case Study: A Recursive Descent Interpreter Implementation in C++ (Source Code Courtesy of Dr. Adam Drozdek) Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh Analysis and Presentation by Rob Agle

Upload: fancy

Post on 22-Mar-2016

62 views

Category:

Documents


0 download

DESCRIPTION

Case Study: A Recursive Descent Interpreter Implementation in C ++ (Source Code Courtesy of Dr. Adam Drozdek ). Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh Analysis and Presentation by Rob Agle. First, let’s clear up some terminology…. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Case Study:A Recursive Descent Interpreter

Implementation in C++(Source Code Courtesy of Dr. Adam Drozdek)

Payap UniversityICS220 - Data Structures and Algorithm AnalysisInstructor: Dr. Ken CoshAnalysis and Presentation by Rob Agle

Page 2: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

First, let’s clear up some terminology…

Page 3: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Interpreter

Examples of “Interpreted” LanguagesPythonPearl

JavaScript

RubySmalltalk

Java

• In general, a compiler is a program that converts an entire program from high level source code into some lower level representation (assembly or machine code for example).

• An interpreter on the other hand, traditionally translates high level instructions and executes them on the fly (at runtime).

• The lines between these two concepts are blurring however…

Page 4: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Interpreter

For our purposes – we can simply say that our interpreter will be used to translate and execute

one instruction statement at a time.

Page 5: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Interpreter

For our purposes – we can simply say that our interpreter will be used to translate and execute

one instruction statement at a time.

Page 6: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Interpreter

For our purposes – we can simply say that our interpreter will be used to translate and execute

one instruction statement at a time.

Page 7: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Interpreter

Things our interpreter understands:Variable Names: Any alphanumeric string

Operators: + - / * =Commands: Print, Status, End

Page 8: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Recursive Descent

A process that allows us to descend – or “go down” to lower and lower levels of complexity via recursion, and then work “backwards” towards a solution once all the

pieces are in place.

Page 9: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Recursive Descent

A process that allows us to descend – or “go down” to lower and lower levels of complexity via recursion, and then work “backwards” towards a solution once all the

pieces are in place.

Page 10: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Recursive Descent

For Example:

var = 2*(3+5);

Page 11: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Recursive Descent

For Example:

var = 2*(3+5);

Page 12: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Recursive Descent

For Example:The statement: var = 2*(3+5); can be parsed and broken

down into its individual pieces using recursion.

Page 13: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Recursive Descent

For Example:The statement: var = 2*(3+5); can be parsed and broken

down into its individual pieces using recursion.

Page 14: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Recursive Descentvar = 2*(3+5);

Don’t worry about how, just imagine we magically use some combination of direct and indirect recursion

to break down the above statement into the following…

Page 15: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Recursive Descentvar = 2*(3+5);

Don’t worry about how, just imagine we magically use some combination of direct and indirect recursion

to break down the above statement into the following…

Page 16: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

var = 2 * ( 3 + 5 ) ;

Page 17: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

var = 2 * ( 3 + 5 ) ;ID

Page 18: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

var = 2 * ( 3 + 5 ) ;ID

Operator Operator Operator

Page 19: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

var = 2 * ( 3 + 5 ) ;ID

Operator

Factor

Operator Operator

Factor Factor

Page 20: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

var = 2 * ( 3 + 5 ) ;ID

Operator

Factor

Operator Operator

Factor Factor

Term Term

Page 21: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

var = 2 * ( 3 + 5 ) ;ID

Operator

Factor

Operator Operator

Factor Factor

Term Term

Expression

Expression

Page 22: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

var = 2 * ( 3 + 5 ) ;ID

Operator

Factor

Operator Operator

Factor Factor

Term Term

Expression

Expression

Page 23: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

What do we need (object wise) to accomplish this?

Data:• a list of all ID’s (variables)• An array of characters to store an input statement

Functionality:• A way to get the input statement from the user• A way to parse the input, get values for expressions (if any) and

its composite parts (terms, factors – if any) and perform indicated operations.• A few “black boxes”…

Page 24: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Data

Page 25: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Data:• a list of all ID’s (variables)• An array of characters to store an input statement

Functionality:• A way to get the input statement from the user• A way to parse the input, get values for expressions (if any) and

its composite parts (terms, factors – if any) and perform indicated operations.• A few “black boxes”…

What do we need (object wise) to accomplish this?

Page 26: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Functionality

Page 27: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

So – let’s go back to our concrete example and trace the program…

Page 28: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Trace: Input -> var = 2*(3+5);

Page 29: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Trace: Input -> var = 2*(3+5);

Page 30: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Page 31: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke =id =command =

getStatement()

Page 32: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke =id =command =

getStatement()

varVAR

var

Page 33: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

=var

Page 34: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

=

t =

R term()

?

Page 35: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

=

t =

R term()

?

R factor()

f = ?

Page 36: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

=

t =

R term()

?

R factor()

f = ?

var = minus = id =

1.0 1.0

Page 37: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

statement.idList =

Statement.ch = =2

R expression()

Trace: Input -> var = 2*(3+5);

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = ?

var = minus = id =

1.0 1.0

Page 38: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

2

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = ?

var = minus = id =

1.0 1.0

Page 39: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

2

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = ?

var = minus = id =

1.0 1.0

Page 40: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

*

statement.idList =

Statement.ch =

1.0 2.0

2

R expression()

Trace: Input -> var = 2*(3+5);

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = ?

var = minus = id =

1.0

Page 41: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

statement.idList =

Statement.ch =

2

2.0

*

R expression()

Trace: Input -> var = 2*(3+5);

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = ?

var = minus = id =

1.0

Page 42: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

statement.idList =

Statement.ch =

2

R expression()

Trace: Input -> var = 2*(3+5);

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

f =

*

R factor()

* ?

Page 43: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

*

t =

R term()

?

R factor()

f = 2

var = minus = id =

1.0 1.0

* ?

Page 44: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

(

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

*

t =

R term()

?

R factor()

f = 2

var = minus = id =

1.0 1.0

* ?

Page 45: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

(

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = 2

var = minus = id =

1.0 1.0

* ?

Page 46: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

?

R expression()

(

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = 2

var = minus = id =

1.0 1.0

* ?

Page 47: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

?

R expression()

(

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = 2

var = minus = id =

1.0

* ?

Here, we have our first recursive function call…

Page 48: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

?

R expression()

(

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = 2

var = minus = id =

1.0

* ?

This conveniently allows us to naturally follow mathematical

precedence …

Page 49: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

?

R expression()

(

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = 2

var = minus = id =

1.0

* ?

A series of additional indirect recursive

calls will determine the value of (3+5)…

R term()

Page 50: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

?

R expression()

(

R expression()

Trace: Input -> var = 2*(3+5);

statement.idList =

Statement.ch =

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = 2

var = minus = id =

1.0

* ?

A series of additional indirect recursive

calls will determine the value of (3+5)…

R term()R factor()

Page 51: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

3

statement.idList =

Statement.ch =

?

R expression()

(

R expression()

Trace: Input -> var = 2*(3+5);

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = 2

var = minus = id =

1.0

* ?

A series of additional indirect recursive

calls will determine the value of (3+5)…

R term()R factor()

Page 52: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

R factor()+

statement.idList =

Statement.ch = 3

?

R expression()

R expression()

Trace: Input -> var = 2*(3+5);

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = 2

var = minus = id =

1.0

* ?

A series of additional indirect recursive

calls will determine the value of (3+5)…

R term()

Page 53: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

+

statement.idList =

Statement.ch =

?

R expression()

R expression()

Trace: Input -> var = 2*(3+5);

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = 2

var = minus = id =

1.0

* ?

Page 54: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

)

statement.idList =

Statement.ch =

8

+

?

R expression()

R expression()

Trace: Input -> var = 2*(3+5);

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

R factor()

f = 2

var = minus = id =

1.0

* ?

We now see the same chain of recursive calls to term and factor…

Which eventually sets t = 8 in expression(). This value will be returned to factor…

Page 55: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

;

statement.idList =

Statement.ch =

R factor()

* 82 * ?

)

8

R expression()

Trace: Input -> var = 2*(3+5);

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

f =

var = minus = id =

1.0 • factor() can now return 8 to

term()

Page 56: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

16

statement.idList =

Statement.ch =

R expression()

Trace: Input -> var = 2*(3+5);

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t =

R term()

?

f = 2 * 8

• factor() can now return 8 to term()

• term() can return 2*8 =16 to expression()

;

Page 57: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

16

statement.idList =

Statement.ch =

R expression()

Trace: Input -> var = 2*(3+5);

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

?

t = 16

• factor() can now return 8 to term()

• term() can return 2*8 =16 to expression()

• expression() also returns 16 to getStatement…

;

Page 58: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

16

statement.idList =

Statement.ch =

Trace: Input -> var = 2*(3+5);

Runti

me

Stac

ke = id =command =

getStatement()

varVAR

;

var => 16

Page 59: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Control is returned to the main function, where once again getStatement will be called…

Page 60: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh

Final Thoughts

For the sake of time – a lot of the non-recursive functions were overlooked and treated as black boxes.

Tracing your own input through the functions carefully will leave you with a solid understanding of recursion.

Recursive descent was once a popular way to build a parser. These days more complex parsers can be built

by parser generators. For more information (and a solid headache), google: LR parsers.