# Parser - need help!

• 04-05-2004
thelma
Parser - need help!
Hello everyone,
I need some help to writer this parser. The program should run in a loop that repeatedly asks the user for an arithmetic assignment and outputs its value. The program prompts the user for an assignment. The user might input
foo = 450*(bar/baz)
where bar and baz are previous variables. The program will first evaluate the expression in the right side of the assignment, and assign the result to the variable on the left side.
The expressions may consist of numbers (which may be integers or decimal numbers), previously defined variables, basic arithmetic operators and functions. A function consists of a function name immediately followed by the list of parameters given in parentheses. The parser should recognize and evaluate the functions sin, cos, abs and max, where the first three functions take a single parameter, and the max function takes arbitrary many parameters, separated by commas.
An example run of the program could look as follows (the user input follows the prompt >>>, the program output is shown here in italics):
>>>x = 1*2*3*4*5-5*-4*-3*-2*-1
0
>>>y = 1.11 + 2.22 + -3.33 * 4.4444444
-11.47
>>>z = max(x,y,x*y,x+y,x/y)
0
>>>quitealongvariablename = sin(cos(sin(cos(2))))
0.7952
>>>quitealongvariablename = quitealongvariablename + 1
1.7952
(The precision of your outputs may differ from these depending on how many decimals you choose to output.
Constraints:
The parser may do all the computations using the double type. There is no need to specifically handle overflows and numerical inaccuracy errors.
The input can be trusted to be a syntactically correct expression. However, the input can contain spaces wherever they are allowed.
If an expression contains a variable that has not been defined yet, its value is 0.
Multiplication is always used explicitly. For example, the expression x(y+z) is syntactically incorrect, and is given in full as x*(y+z).Similarly, ab is a single variable whereas a*b is the product of two variables.
The parser should give multiplication and division operations a higher precedence than addition and subtraction. The associativity of the same-precedence operations may be arbitrary, though. (Especially both addition and subtraction may be right-associative, which would, for example, make 1-2+3 equal –4 instead of 2.)
Variable names shall only contain the lowercase characters a,…,z and cannot be more than 100 characters long. The function names cannot be used as variable names, although a variable name can begin with a function name.
I can't used any packages or libraries other than those that are needed for user interaction, string handling and the mathematical functions sin and cos.
~Thelma
• 04-05-2004
Prelude
People who only post the requirements for a program get no help. Please read the announcements at the top of the forum before posting.
• 04-05-2004
sean345
I wrote a parser which does something similiar, but does not include things like functions. You can take a look at it and see how it works at: http://blackfrog.pagemac.com/programming/Libraries.php

It's the evaluate.lib library. I have learned a lot about parsers since then so it is not the most efficient or well written, but it should show you one way to write a parser.

- Sean