Upload
kristina-daniel
View
214
Download
2
Embed Size (px)
Citation preview
Mutual Recursion
• A set of methods calls each other cooperatively and repeatedly
• Expression is a term, or sum or difference of terms
• Term is a factor, or a product or quotient of factors
• Factor is a number, or an expression enclosed in parenthesis
Using Mutual Recursion
• Problem: to compute the value of arithmetic expressions such as
3 + 4 * 5(3 + 4) * 51 - (2 - (3 - (4 - 5)))
Syntax Diagram for Evaluating and Expression
Syntax Tree for Two Expressions
Evaluator
• ExpressionTokenizer: breaks input string into tokens (numbers, operators, parenthesis)
• nextToken returns string• peekToken: look at next token without
consuming it • (3 * 4) + 6: See “+” without consuming
it.
Mutually Recursive Methods
• Implement 3 methods that call each other recursively o getExpressionValue: calls getTermValue, is next token + or -, if so call get TermValue again and adds or subtracts
o getTermValue: calls getFactorValueo getFactorValue: number or getExpressionValue
File Evaluator.java 01: /**02: A class that can compute the value of an arithmetic expression.03: */04: public class Evaluator05: {06: /**07: Constructs an evaluator.08: @param anExpression a string containing the expression09: to be evaluated.10: */11: public Evaluator(String anExpression)12: {13: tokenizer = new ExpressionTokenizer(anExpression);14: }15: 16: /**17: Evaluates the expression.
18: @return the value of the expression.19: */20: public int getExpressionValue()21: {22: int value = getTermValue();23: boolean done = false;24: while (!done)25: {26: String next = tokenizer.peekToken();27: if ("+".equals(next) || "-".equals(next))28: {29: tokenizer.nextToken();30: int value2 = getTermValue();31: if ("+".equals(next)) value = value + value2;32: else value = value - value2;33: }34: else done = true;35: }36: return value;37: }
38: 39: /**40: Evaluates the next term found in the expression.41: @return the value of the term.42: */43: public int getTermValue()44: {45: int value = getFactorValue();46: boolean done = false;47: while (!done)48: {49: String next = tokenizer.peekToken();50: if ("*".equals(next) || "/".equals(next))51: {52: tokenizer.nextToken();53: int value2 = getFactorValue();54: if ("*".equals(next)) value = value * value2;55: else value = value / value2;56: }57: else done = true;
58: }
59: return value;
60: }
61:
62: /**
63: Evaluates the next factor found in the expression.
64: @return the value of the factor.
65: */
66: public int getFactorValue()
67: {
68: int value;
69: String next = tokenizer.peekToken();
70: if ("(".equals(next))
71: {
72: tokenizer.nextToken();
73: value = getExpressionValue();
74: next = tokenizer.nextToken(); // read ")"
75: }
76: else
77: value = Integer.parseInt(tokenizer.nextToken());
78: return value;
79: }
80:
81: private ExpressionTokenizer tokenizer;
82: }
File ExpressionTokenizer.java01: /**
02: This class breaks up a string describing an expression03: into tokens: numbers, parentheses, and operators04: */05: public class ExpressionTokenizer06: {07: /**08: Constructs a tokenizer.09: @param anInput the string to tokenize10: */11: public ExpressionTokenizer(String anInput)12: {13: input = anInput;14: start = 0;15: end = 0;16: nextToken();17: }
18: 19: /**20: Peeks at the next token without consuming it.21: @return the next token or null if there are no more tokens22: */23: public String peekToken()24: {25: if (start >= input.length()) return null;26: else return input.substring(start, end); 27: }28: 29: /**30: Gets the next token and moves the tokenizer to the31: following token.32: @return the next token or null if there are no more tokens33: */34: public String nextToken()35: {36: String r = peekToken();37: start = end;
38: if (start >= input.length()) return r;39: if (Character.isDigit(input.charAt(start)))40: {41: end = start + 1;42: while (end < input.length() && Character.isDigit(input.charAt(end)))43: end++;44: }45: else46: end = start + 1;47: return r; 48: }49: 50: private String input;51: private int start;52: private int end;53: }
File EvaluatorTest.java 01: import javax.swing.JOptionPane;02: 03: /**04: This program tests the expression evaluator.05: */06: public class EvaluatorTest07: {08: public static void main(String[] args)09: {10: String input = JOptionPane.showInputDialog("Enter an expression:");11: Evaluator e = new Evaluator(input);12: int value = e.getExpressionValue();13: System.out.println(input + "=" + value);14: System.exit(0);15: }16: }