1

I am new to Python and am trying to write a calculator program. I have been trying to do the following but with no success, so please point me in the right direction:

I would like to input an equation as a user, for example: f(t) = 2x^5 + 8

the program should recognize the different parts of a string and in this case make a variable f(t) and assign 2x^5 + 8 to it.

Though, if I input an equation followed by an equals sign, for example 2x^5 + 8 =

the program will instead just output the answer.

I am not asking how to code for the math-logic of solving the equation, just how to get the program to recognize the different parts of a string and make decisions accordingly.

I am sorry I don't have any code to show as an attempt as I'm not sure how to go about this and am looking for a bit of help to get started.

Thank you.

Serac
  • 11
  • 3
  • This help: http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python? Try to post an attempt at coding it if you can. – MikeiLL Sep 04 '14 at 02:17
  • 1
    What do you mean by "make a variable f(t)"? Does that mean you want to later be able to write `f(t)` and get that equation back? Or write `f(2)` and get that equation with `2` substituted for `t`? (Of course there is no `t` in the equation in the first place…) Or what? Also, what is "the answer" supposed to be for `2x^5 + 8 =`? That's not an equation. Do you want it to assume `0` on the right side and solve for that? Or something different? – abarnert Sep 04 '14 at 02:26
  • 1
    Anyway, the answer to this is that you need to write a parser. If your syntax is actually a perfect subset of Python's, you can use Python's parser, by calling `ast.parse` and then walking the resulting AST. Otherwise, you probably want to use a parser library like `pyparsing` (which has some great examples), or write a grammar and feed it to a parser-generator tool. – abarnert Sep 04 '14 at 02:28
  • @ abarnert, getting the code to solve for say f(2) is a plan for later. At the moment I want to be able to write f(t) and get that equation back later. Thanks – Serac Sep 04 '14 at 02:32
  • Ok, I will do some research about writing a parser and grammar. Thank you. – Serac Sep 04 '14 at 02:36
  • I hope you are new to python and not new to programming, because this type of program can get hairy really fast if you dont know what your doing. – Nick Humrich Sep 04 '14 at 21:50

1 Answers1

0

For a little bit of context: The problem you're describing is more generally known as parsing, and it can get rather complicated, depending on the grammar. The grammar is the description of the language; the language, in your case, is the set of all valid formulas for your calculator.

The first recommended step, even before you start coding, is to formalize your grammar. This is mainly for your own benefit, as it will make the programming easier. A well established way to do this is to describe the grammar using EBNF, and there exist tools like PLY for Python that you can use to generate parsers for such languages.

Let's try a simplified version of your calculator grammar:

digit := "0" | "1"                  # our numbers are in binary
number := digit | number digit      # these numbers are all nonnegative
variable := "x" | "y"               # we recognize two variable names
operator := "+" | "-"               # we could have more operators
expression := number | variable | "(" expression operator expression ")"
definition := variable "=" expression 
evaluation := expression "="

Note that there are multiple problems with this grammar. For example:

  1. What about whitespace?
  2. What about negative numbers?
  3. What do you do about inputs like x = x (this is a valid definition)?

The first two are probably problems with the grammar itself, while the last one might need to be handled at a later stage (is the language perhaps context sensitive?).

But anyway, given such a grammar a tool like PLY can generate a parser for you, but leaving it up to you to handle any additional logic (like x = x). First, however, I'd suggest you try to implement it on your own. One idea is to write a so called Top Down Parser using recursion.

vindvaki
  • 534
  • 2
  • 7