Thread: Parser - need help!

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    1

    Unhappy 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.
    Can some one please help me out on this.
    Thanks in advance
    ~Thelma

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can some one please help me out on this.
    People who only post the requirements for a program get no help. Please read the announcements at the top of the forum before posting.
    My best code is written with the delete key.

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    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
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with Parser Generators - Functions
    By jason_m in forum C Programming
    Replies: 1
    Last Post: 09-09-2008, 09:38 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Help! Bad parser :/.
    By Blackroot in forum C++ Programming
    Replies: 13
    Last Post: 03-07-2006, 11:08 AM
  4. Parser Help
    By Barnzey in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2005, 12:10 PM
  5. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 09:54 AM