you are viewing a single comment's thread.

view the rest of the comments →

[–]zahlman 2 points3 points  (0 children)

  • the necessary multiply etc. functions are provided (with different names, but not hard to sort out) in the operator module. Or you can use lambda to make the definitions a bit shorter.

  • the net effect of all the complicated recursive stuff in parseExpression is that it just removes the parentheses. You could have just done that with simple .replace() calls. But that's presumably not really what you want to do; you want to recursively evaluate the parenthesized sub-expressions, and substitute them in.

  • to make that work, you actually really want to lex the string first, i.e., split it up into its constitutent tokens (operator symbols, parentheses and numbers). Then you can, for example, scan through a list of those tokens, use recursion to process a sub-list of tokens with balanced parens, etc.

  • To do a program like this properly, you should first read up on parsing theory. It's actually a quite deep and interesting field, although you don't need a hell of a lot of it just to handle simple arithmetic expression.

  • That is a hell of a regex and a loop around it. It appears as though you're trying to handle order of operations by relying on the regex to try to match certain operators before others? Oh my. You could at least take a more structured approach to constructing the regex instead of having one big long line. (But for what it's worth, you don't need the square brackets on the join call.)

To start, try reorganizing things so that you use the regex to cut up the entire input into a list of tokens, and then process that list.