So that these unnecessary parentheses are recreated, the translator adds the parentheses sub-code of tokens. The above example expression is translated as A B C *')' +. The translator also allows for extra sets of parentheses; the expression A+((B*C)) is translated to A B C *')' ) +. A closing parentheses code is added when the previous token already has the parentheses sub-code. For a third pair of parentheses, the closing parentheses token gets the parentheses sub-code. And so on. At run-time, parentheses codes and sub-codes are ignored.
Implementation
A new parentheses recreate function was added, which calls the pop with parentheses function with true as the argument to add parentheses when popping the top string. The pop with parentheses was modified to optionally return the precedence value and unary operator flag from the item popped from the holding stack. The string with parentheses along with the precedence value and unary operator flag are then pushed back to the holding stack.In the recreate routine after processing an item in the RPN list, a check was added for the parentheses sub-code and if set, calls the parentheses recreate function. The parentheses recreate function was also added to the table entry of the closing parentheses code.
During the testing these changes, a problem was found in the translator where the parentheses sub-code was not being added to a closing parentheses token (it incorrectly added another closing parentheses token to the output list). This was due to translator check pending parentheses routine checking if the top item of the done stack (the last item added to the output list) to see if it already had a parentheses sub-code. It should have been checking the last item in the output list instead (the closing parentheses tokens are not pushed to the done stack) .
A few additional expressions were added to expression test #2 (parentheses tests) for testing extra parentheses. Both the expected translated and recreated results were updated. All of the expressions in test #2 are now being recreated correctly.
[commit 385cc5925d]
No comments:
Post a Comment
All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)