Java Reference
In-Depth Information
In a mutual recursion, a set of cooperating methods calls each other repeatedly.
We will develop a program that can compute the values of arithmetic expressions
such as
3+4*5
(3+4)*5
1-(2-(3-(4-5)))
Computing such an expression is complicated by the fact that * and / bind more
strongly than + and Ċ , and that parentheses can be used to group subexpressions.
Figure 3 shows a set of syntax diagrams that describes the syntax of these
expres sions. To see how the syntax diagrams work, consider the expression 3+4*5 .
When you enter the expression syntax diagram, the arrow points directly to term,
giving you no alternative but to enter the term syntax diagram. The arrow points to
factor, again giving you no choice. You enter the factor diagram, and now you have
two choices: to follow the top branch or the bottom branch. Because the first input
token is the number 3 and not a (, you must follow the bottom branch. You accept the
input token because it matches the number. Follow the arrow out of number to the
end of factor. Just like in a method call, you now back up, returning to the end of the
factor element of the term diagram. Now you have another choiceȌto loop back in
the term diagram, or to exit. The next input token is a + , and it matches neither the *
or the / that would be required to loop back. So you exit, returning to expression.
Again, you have a choice, to loop back or to exit. Now the + matches one of the
choices in the loop. Accept the + in the input and move back to the term element.
611
612
Search WWH ::




Custom Search