Download - Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh
![Page 1: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/2.jpg)
First, let’s clear up some terminology…
![Page 3: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/10.jpg)
Recursive Descent
For Example:
var = 2*(3+5);
![Page 11: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/11.jpg)
Recursive Descent
For Example:
var = 2*(3+5);
![Page 12: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/16.jpg)
var = 2 * ( 3 + 5 ) ;
![Page 17: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/17.jpg)
var = 2 * ( 3 + 5 ) ;ID
![Page 18: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/18.jpg)
var = 2 * ( 3 + 5 ) ;ID
Operator Operator Operator
![Page 19: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/24.jpg)
Data
![Page 25: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/26.jpg)
Functionality
![Page 27: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/28.jpg)
Trace: Input -> var = 2*(3+5);
![Page 29: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/29.jpg)
Trace: Input -> var = 2*(3+5);
![Page 30: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/30.jpg)
Trace: Input -> var = 2*(3+5);
statement.idList =
Statement.ch =
![Page 31: Payap University ICS220 - Data Structures and Algorithm Analysis Instructor: Dr. Ken Cosh](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/31.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/32.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/33.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/34.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/35.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/36.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/37.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/38.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/39.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/40.jpg)
*
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/41.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/42.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/43.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/44.jpg)
(
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/45.jpg)
(
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/46.jpg)
?
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/47.jpg)
?
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/48.jpg)
?
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/49.jpg)
?
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/50.jpg)
?
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/51.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/52.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/53.jpg)
+
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/54.jpg)
)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/55.jpg)
;
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/56.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/57.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/58.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/59.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062521/56816532550346895dd7b9a7/html5/thumbnails/60.jpg)
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.