Thursday, March 25, 2010

Translator – Parentheses (Implementation)

While the previous several posts seem to suggest that the implementation of parentheses will be rather involved, this is not the case.  First the unary code field of the opening parentheses needs to be set (to the opening parentheses code) so that it will be processed correctly.

The precedence of the parentheses operators need to be set in the Table. The precedence of Null is zero and the precedence of EOL was set to 4. The precedence of the closing parentheses needs to the same as EOL – both perform the same hold stack emptying functionality. The precedence of the opening parentheses needs to be between Null and EOL/opening parentheses and therefore will be set to 2 so that neither EOL or an closing parentheses will remove it automatically during the emptying of the hold stack (of higher precedence).

The difference between processing a closing parentheses and an EOL is that a closing parentheses expects to find an opening parentheses on top of the hold stack after it is emptied and an EOL expects the null token at the bottom of the hold stack. If these conditions are not the case, the  appropriate missing parentheses error occurs.

For the dummy token checks, two additional variables are needed in the Translator class. There needs to be a pending parentheses token pointer. This pointer is initialized to Null (indicating no parentheses pending). When a closing parentheses token is processed, the token (which is normally not needed in the RPN list) is put into this pending pointer instead of being deleted. However, before setting the pending token pointer to the closing parentheses token, if it is already set (occurs with double or more parentheses), then that token needs to be added to the output list first.

There also needs to be a variable to hold the precedence of the last operator added to the output list during the processing of a closing parentheses. This variable will be initialized to a high precedence (say 127), to handle the case when there is just an operand (no operators) inside the parentheses to always add the dummy token. Since this precedence will be higher than that of any operator, the dummy parentheses token will always be added.

The dummy token check needs to be added to two locations in the add token routine, one before adding an operand to the output list and one before adding an operator to the output list (which is in the add operator routine). When a dummy token is not added to the output list, the token in the pending token pointer needs to be deleted. For both cases, the pending token pointer needs to be set to Null (unless it is going to be set to a second or more parentheses token).

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.)