16394

Postfix to infix with unary/binary operators

Question:

I am trying to make a converter from postfix to infix notation and need some help. There is already <a href="https://stackoverflow.com/questions/2431863/infix-to-postfix-and-unary-binary-operators" rel="nofollow">a question about infix-to-postfix conversion</a>, which gives an example I am failing to convert back. (Note: a minus sign is missing there!)

The following is the output of my converter, where the 1st "column" is postfix input, the 2nd is my infix output, and the 3rd is what I probably should get(?):

2 - = - 2 =? - 2 true 1 + 2 + = + 1 + 2 =? + 1 + 2 true 1 + 2 + + = + (+ 1 + 2) =? + 1 + + 2 false 1 + 2 + + 3 - - 4 - - = - (- (+ (+ 1 + 2) - 3) - 4) =? + 1 + + 2 - - 3 - - 4 false

Do you think that it is possible to solve this problem, or the last two lines are actually converted correctly? How would you write algorithm to solve this problem?

Please, assume that more operators (not only + and -) can be set as both unary and binary, where unary operators have higher precedence than binary ones.

<h3>References</h3> <ol><li><a href="http://www.rubyquiz.com/quiz148.html" rel="nofollow">Ruby Quiz #148: Postfix to Infix</a>, also via <a href="http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/1fc1b43a91b6ceed" rel="nofollow">Google Groups</a></li> <li><a href="http://en.literateprograms.org/Shunting_yard_algorithm_(Perl)" rel="nofollow">Shunting-yard algorithm (C, Python, Perl) with unary operator support</a> on LiteratePrograms</li> </ol>

Answer1:

Postfix notation does not have the concept of precedence, as the operands for any operator are always the top N values on the stack (which are then replaced by the result of the operator.

One problem with postfix notation is that it does not cope well with operator symbols that can refer to different operators depending on the number of operands (such as -, which can denote either unary or binary minus). The only way out of that is to ensure each operator has a unique symbol representing it.

Recommend

  • pandas looking at next row and swapping values
  • Reversal of string.contains In python, pandas
  • How to delete row based on row above? Python Pandas
  • Setting 1 or 0 to new Pandas column conditionally
  • How do I get the absolute value of an integer without using Math.abs?
  • Why can === have some odd behavior in Javascript?
  • What do `def +@` and `def -@` mean?
  • pandas data frame removing the first row of every numbers
  • TypeError: bad operand type for unary ~: float
  • SQL Server: What does 1 ++ 2 mean?
  • Exceptions - meaning of '&' sign [duplicate]
  • Array-pointer arithmetic - legal and undefined behaviour [closed]
  • Using an openmp pragma inside #define [duplicate]
  • Operator override - when to use friend?
  • Rails foreign key column name convention for a unary many-many relation
  • finding values in pandas series - Python3
  • ISO C and signed literal constants
  • Single line Lambda and Run-time exceptions - Not compiling?
  • Is there a way to stop this default insert in rails
  • AngularJS - ngBind and Bootstrap Switch
  • python struct.pack(): pack multiple datas in a list or a tuple
  • Is a .txt file created in VB different than one I'd randomly create?
  • Telegram bot API - Inline bot getting Error 400 while trying to answer inline query
  • Create function that can pass a parameter without making a new component
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • How can we prepend rows to a react native list-view?
  • Default parameter as generic type
  • How can I sort a a table with VBA with given text condition?
  • All Classes Conforming to Protocol Inherit Default Implementation
  • R - Combining Columns to String Based on Logical Match
  • Read text file and split every line in MSBuild
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • Return words with double consecutive letters
  • how to add data labels for bar graph in matlab
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory
  • Converting MP3 duration time