Compellingly Beautiful Software
Products Programming Contact
"Evil, nasty, tricksy Hobbitses!"
- Gollum
People who use the multi-pass parsing algorithm often disagree on the expected output of the following little Java program. That alone is reason to worry about using that algorithm when thinking about your programs, but let's ignore that for the moment. Most will say that it generates 9. It doesn't: it generates 5.
public class AuntSallyThe few multi-pass practitioners who correctly say this generates 5 will tell you that, in fact, they evaluate all functions first, from left to right, regardless of parentheses. But their explanation becomes difficult and confusing in more general cases than this one. Even allowing for this, the multi-pass algorithm is not safe in C++, where even operators, such as + and *, can be redefined.
Some people say one shouldn't write code like this at all. But in many realistic programs, code with side effects can vary from hard-to-avoid to quite desirable. Good object-oriented code is often characterized by subterranean side effects. At any rate, the single-pass parsing algorithm is very simple once you get used to it, and it always gives the right result.