What sorts of algorithms would be used to do this (as in, this is a string, and I want to find the answer):
((5 + (3 + (7 * 2))) - (8 * 9)) / 72
Say someone wrote that in, how could I deal with so many nested parenthesis?
What sorts of algorithms would be used to do this (as in, this is a string, and I want to find the answer):
((5 + (3 + (7 * 2))) - (8 * 9)) / 72
Say someone wrote that in, how could I deal with so many nested parenthesis?
Or you can just do this in one line in R:
You could use either a state machine parser (yacc LALR, etc.), or a recursive descent parser.
The parser could emit RPN tokens to evaluate or compile later. Or, in an immediate interpreter implementation, a recursive descent parser could calculate subexpressions on the fly as it returns from the leaf tokens, and end up with the result.
You can use Shunting yard algorithm or Reverse Polish Notation, both of them are using stacks to handle this, wiki said it better than me.
From wiki,
James has provided a good answer. Wikipedia has a good article on this as well.
If (and I don't recommend this) you wanted to parse that expression directly, given that it seems orderly in that every set of parens has no more than one pair of operands, I think you could approach it like this:
parse to the first ")". Then parse back to the previous "(". Evaluate what's inside and replace the whole set with a value. Then repeat recursively until you are done.
So in this example, you would first parse "(7 * 2)" and replace it with 14. Then you would get (3 + 14) and replace it with 17. And so on.
You can do that with Regex or even .IndexOf and .Substring.
I'm going without benefit of checking my syntax here, but something like this:
You'll need to evaluate the resulting expression and loop this until the parens are exhausted and then evaluate that last part of the string.
I would use the tools that are available nearly everywhere.
I like lex/yacc because I know them but there are equivalents everywhere. So before you write complex code see if there are tools that can help you to make it simple (problems like this have been solved before so don;t re-invent the wheel).
So, using lex(flex)/yacc(bison) I would do:
e.l
e.y
Build
The above also handles normal operator precedence rules:
Not because of anything I did,but somebody smart worked this out ages ago and now you can get the grammar rules for expression parsing easily (Just google
C Grammer
and rip the bit you need out).If the expressions are known to be fully-parenthesized (that is, all possible parentheses are there), then this can easily be done using recursive-descent parsing. Essentially, each expression is either of the form
or of the form
These two cases can be distinguished by their first token, and so a simple recursive descent suffices. I've actually seen this exact problem given out as a way of testing recursive thinking in introductory programming classes.
If you don't necessarily have this guarantee, then some form of precedence parsing might be a good idea. Many of the other answers to this question discuss various flavors of algorithms for doing this.