|Minimizing parentheses in a postfix->infix converter output email@example.com (1991-04-13)|
|Re: Minimizing parentheses in a postfix->infix converter output firstname.lastname@example.org (1991-04-14)|
|Re: Minimizing parentheses in a postfix->infix converter output email@example.com (1991-04-14)|
|Re: Minimizing parentheses in a postfix->infix converter output firstname.lastname@example.org (1991-04-15)|
|From:||email@example.com (Robert Firth)|
|Organization:||Software Engineering Institute, Pittsburgh, PA|
|Date:||14 Apr 91 21:13:13 GMT|
In article <9104130521.AA00996@fiu.edu> firstname.lastname@example.org (ACMFIU) writes:
>I have a postfix->infix converter and need to output the least number of
>parentheses as possible. The resulting infix expression is kept in a
>binary tree and I just traverse the tree outputting the left/right
>parentheses at each level.
Easy. You need to know the priority of the operators (obviously),
and, before emittinga subtree that isn't a leaf, you compare the
priority of its operator to that of the parent node.
If the sub has a higher priority, no parens.
If the sub has a lowe priority, parens always.
If the sub has the same priority, then you use parens around the
right subtree for a left-associative operator, and the left subtree
for a right-associative operator.
ab*c+ => a*b+c
ab+c* => (a+b)*c
abc-- => a-(b-c)
Hope that helps.
Return to the
Search the comp.compilers archives again.