views:

467

answers:

1

I have a piece of code that converts an infix expression to an expression tree in memory. This works just fine. There's just one small trouble. I just connect work out how to involve the unary operators correctly (the right associative ones).

With the following infix expression :

+1 + +2 - -3 - -4

I would expect an RPN of:

1+2++3-4--

Yet, none of the online infix-post converters I can find handle this example in the way I would expect. Does anyone have a clear explanation of handling right associative operators, specifically the binary ones that can be mistaken for the unary ones?

Edit/Clarification: I would like to know how to deal with the unary operators during the translation from infix to postfix. Ie: recognizing the same '-' character for example as being unary instead of binary operator and thus a different precedence. I would think of using a state machine perhaps with two states but ...?

+1  A: 

Well, you need to determine if a given operator is binary/unary during the parsing stage. Once you do that, when you create the RPN, you can tag the operators as taking 2 or 1 arguments.

You could try using the Shunting Yard algorithm to do the parsing (and creation of RPN at the same time), which was designed to work with unary operators too.

In any case, if all you care about is unary + or -, you could just insert a 0 with brackets when you see a + or - that appears 'unexpectedly'.

For instance

+1 + +2 - -3 - -4

You should be able to make a pass through it and convert to

(0+1) + (0+2) - (0-3) - (0-4)

Now you don't need to worry about the unary + or - and can probably forget about tagging the operators with the number of arguments they take.

Moron
Yes, I had implemented the shunting yard algoritm but it, as you say, requires that you have already identified the - and the + as either unary +, - or the binary variations. I ended up using some changed parsing code to beter decide what I am dealing with. I do not want to change the actual expression because I need to write back the parsed expression again. Still, I have some troubles deciding when to write back unary operators to the RPN output. I'll have another look at the algorythm.
Jaapjan
@Jaapjan. RPN evaluation needs to know how many arguments an operator takes. The proper way to do this would be with a grammar which can tell when + is unary and when it is binary etc. btw, why does it matter if you include zeroes in the written back expression? Since you have it tagged as self-learning, it seems like that should be irrelevant.
Moron
It is self-learning because I am trying to learn how to read expressions in my source code using a program. If my program writes back 'cleaned' expressions then I prefer not to have extra zero's in them. Unfortunately it also needs to deal with variable number of parameters to functions. Hence trying to figure out how others deal with this situation. I presume for example -1--1 would become 1 neg 1 neg minus in this instance. But I do not find any clear way to deal with nested expressions with variable length functions in them... mmm.
Jaapjan
@Jaapjan: The wiki for the shunting yard algo has a link to the original paper by Dijkstra which apparently also deals with functions and parameters. Of course, you could do this using standard parsing techniques. Have you heard of the 'dragon' book? Recursive Descent Parsing might be a good option for learning about parsing.
Moron
I accepted your answer. And yes, it does deal with parameters but only when the amount of parameters is known beforehand. I had a prototype earlier that used recursive decent parsing instead of the shunting yard but SY proved, then at least, easier to work with. I have some thoughts still and will pursue the dragon book for some external thoughts from other people. I have accepted your answer, thank you for the time spend.
Jaapjan