CS 46B - Lecture 9
Pre-class reading
Mutual Recursions
Syntax Diagrams for Evaluating an Expression
Mutual Recursions
- An expression can broken down into a sequence of terms, separated by
+ or -
- Each term is broken down into a sequence of factors, separated by
* or /
- Each factor is either a parenthesized expression or a number
- The syntax trees represent which operations should be carried out first
Syntax Tree for Two Expressions
Mutually Recursive Methods
- In a mutual recursion, a set of cooperating methods calls each other
repeatedly
- To compute the value of an expression, implement 3 methods that call each
other recursively:
- getExpressionValue
- getTermValue
- getFactorValue
Lecture 9 Clicker Question 1
Why do we need both terms and factors?
- Factors are combined by multiplicative operators (* and
/), terms are combined by additive operators (+,
-).
- Terms are combined by multiplicative operators (* and
/), factors are combined by additive operators (+,
-).
- We need both so that multiplication can bind more strongly than addition.
- We need both so that we can have mutual recursion
The getExpressionValue Method
public int getExpressionValue()
{
int value = getTermValue();
boolean done = false;
while (!done)
{
String next = tokenizer.peekToken();
if ("+".equals(next) || "-".equals(next))
{
tokenizer.nextToken(); // Discard "+" or "-"
int value2 = getTermValue();
if ("+".equals(next)) value = value + value2;
else value = value - value2;
}
else
{
done = true;
}
}
return value;
}
The getTermValue Method
The getTermValue method calls getFactorValue in the same
way, multiplying or dividing the factor values
The getFactorValue Method
public int getFactorValue()
{
int value;
String next = tokenizer.peekToken();
if ("(".equals(next))
{
tokenizer.nextToken(); // Discard "("
value = getExpressionValue();
tokenizer.nextToken(); // Discard ")"
}
else
{
value = Integer.parseInt(tokenizer.nextToken());
}
return value;
}
Lecture 9 Clicker Question 2
What happens if you try to parse the illegal expression 3+4*)5?
Specifically, which method throws an exception?
- getExpressionValue
- getTermValue
- getFactorValue
- main
Using Mutual Recursions
To see the mutual recursion clearly, trace through the expression
(3+4)*5:
- getExpressionValue calls getTermValue
- getTermValue calls getFactorValue
- getFactorValue consumes the ( input
- getFactorValue calls getExpressionValue
- getExpressionValue returns eventually with the value
of 7, having consumed 3 + 4. This is the
recursive call.
- getFactorValue consumes the ) input
- getFactorValue returns 7
- getTermValue consumes the inputs * and 5
and returns 35
- getExpressionValue returns 35
Lecture 9 Clicker Question 3
Why does the expression parser use mutual recursion?
- To compute the value of an arithmetic expression more efficiently than
with a loop
- To make * and / bind stronger than + and -
- To handle both operators such as * and / and numbers such as 2 and 3
- To handle parenthesized expressions, such as 2+3*(4+5)