c++regexexpression-evaluation

Expression Evaluation in C++


I'm writing some excel-like C++ console app for homework. My app should be able to accept formulas for it's cells, for example it should evaluate something like this:

Sum(tablename\fieldname[recordnumber], fieldname[recordnumber], ...)

tablename\fieldname[recordnumber] points to a cell in another table, 
fieldname[recordnumber] points to a cell in current table

or

Sin(fieldname[recordnumber])

or

anotherfieldname[recordnumber]

or

"10" // (simply a number)

something like that. functions are Sum, Ave, Sin, Cos, Tan, Cot, Mul, Div, Pow, Log (10), Ln, Mod

It's pathetic, I know, but it's my homework :'(

So does anyone know a trick to evaluate something like this?


Solution

  • Ok, nice homework question by the way.

    It really depends on how heavy you want this to be. You can create a full expression parser (which is fun but also time consuming).

    In order to do that, you need to describe the full grammar and write a frontend (have a look at lex and yacc or flexx and bison.

    But as I see your question you can limit yourself to three subcases:

    • a simple value
    • a lookup (possibly to an other table)
    • a function which inputs are lookups

    I think a little OO design can helps you out here.

    I'm not sure if you have to deal with real time refresh and circular dependency checks. Else they can be tricky too.