0

I am aware how we can evaluate an expression after converting into Polish Notations. However I would like to know how I can evaluate something like this:

If a < b Then a + b Else a - b

a + b happens in case condition a < b is True, otherwise, if False a - b is computed.

The grammar is not an issue here. Since I only need the algorithm to solve this problem. I am able evaluate boolean and algebraic expressions. But how can I go about solving the above problem?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Expert Novice
  • 1,943
  • 4
  • 22
  • 47
  • If you just need the algorithm you might as well drop the language tags. What aspect do you need help with ? Parsing, how to represent the switching statement on your stack, etc ? – Kindread Oct 17 '13 at 05:27
  • I am having issues in Tokenizing the Algebraic expression . – Expert Novice Oct 17 '13 at 09:01

4 Answers4

0

Do you need to assign a+b or a-b to something?

You can do this:

int c = a < b ? a+b : a-b;

Or

int sign = a < b ? 1 : -1; int c = a + (sign * b);

F.Z
  • 93
  • 6
0

Refer to LISP language for S-express: e.g

 (if (> a b)                  ; if-part
     (+ a b)                  ; then-part
   (- a b))                   ; else-part
Shijing Lv
  • 6,286
  • 1
  • 20
  • 12
0

Actually if you want evaluate just this simple if statement, toknize it and evaluate it, but if you want to evaluate somehow more complicated things, like nested if then else, if with experssions, multiple else, variable assignments, types, ... you need to use some parser, like LR parsers. You can use e.g Lex&Yacc to write a good parser for your own language. They support somehow complicated grammars. But if you want to know how does LR parser (or so) works, you should read into them, and see how they use their table to read tokens and parse them. e.g take a look at wiki page and see how does LR parser table works (it's something more than simple stack and is not easy to describe it here).

If your problem is just really parsing if statement, you can cheat from parser techniques, you can add empty thing after a < b, which means some action, and empty thing after else, which also means an action. When you parsed the condition, depending on correctness or wrongness you will run one of actions. By the way if you want to parse expressions inside if statement you need conditional stack, means something like SLR table.

Saeed Amiri
  • 22,252
  • 5
  • 45
  • 83
0

Basically, you need to build in support for a ternary operator. IE, where currently you pop an operator, and then wait for 2 sequential values before resolving it, you need to wait for 3 if your current operation is IF, and 2 for the other operations.

To handle the if statement, you can consider the if statement in terms of C++'s ternary operator. Which formats you want your grammar to support is up to you.

a < b ? a + b : a - b

You should be able to evaluate boolean operators on your stack the way you currently evaluate arithmetic operations, so a < b should be pushed as

< a b

The if can be represented by its own symbol on the stack, we can stick with '?'.

? < a b

and the 2 possible conditions to evaluate need to separated by another operator, might as well use ':'

? < a b : + a b - a b

So now when you pop '?', you see it is the operator that needs 3 values, so put it aside as you normally would, and continue to evaluate the stack until you have 3 values. The ':' operator should be a binary operator, that simply pushes both of its values back onto the stack.

Once you have 3 values on the stack, you evaluate ? as:

  • If the first value is 1, push the 2nd value, throw away the third.

  • If the first value is 0, throw away the 2nd and push the 3rd.

Kindread
  • 926
  • 4
  • 12