Abstract:
The present invention describes a method for a one-pass parsing algorithm for generation of a Polish string that computationally defines the maximal possible parallel execution of a general class of arithmetic expressions using one operator stack and two operand stacks. This invention relaxes the assumption that in a processor, only one operation can be performed at any given time.

Description:
BACKGROUND OF THE INVENTION 
     1. Field of the Invention 
     This invention is applicable to the arithmetic-logic unit (ALU) or that functional part of a digital computer that carries out arithmetic and logic operations on operands. More particularly the invention is directed to a parsing technique for arranging the input information received by the ALU, which consists of operands, operation codes, and format codes. 
     2. Description of Prior Art 
     In the process of compilation into machine executable code of a program written in a high-level language, the procedure for examining arithmetic expressions and determining operator precedence, the order of execution of the operators, is often referred to as the arithmetic scan. Since syntactically correct arithmetic expressions are well formed in that they possess regular properties related to the operands and the operators, many specialized parsing or scanning techniques have been developed. 
     Although it is algebraically well defined, an arithmetic expression is not computationally well defined unless the precise order of arithmetic operations is specified. The specification of ordering can be exhibited by a computation tree, by a fully parenthesized statement or by a Polish form such as Polish prefix notation. 
     One possible, but impractical, technique is to require the programmer to write arithmetic expressions in fully parenthesized notation (i.e., parentheses must be placed around each pair of operands and its associated operator) to obviate the need for knowledge about the relationships between operators in determining the order in which the operations are to be performed. 
     Most commonly used are transformational systems, which convert the normal infix form (i.e., the form in which the operator is placed between its operands) to a Polish form, in which there exist no parentheses and the order of execution of the operators is specified by their positioning. Such a system is needed because of the difficulty of associating operands with operators in infix notation. 
     Polish notation was originally developed in the prefix form, in which the operators precede the operands. 
     The postfix or suffix form, also known as reverse Polish notation or RPN, which is logically equivalent to the prefix form, has been widely used in computing. Many compilers first transform an arithmetic expression from its ordinary or infix form into RPN, so that its evaluation can be done in a single left-to-right scan. 
     The usual approach for parsing arithmetic expressions is to make a one pass scan over the string of characters, operands and operations, to transform them into a Polish form by using single operand stack and operator stacks. An implicit assumption is made that these Polish strings are to be operated upon by a computing system that permits only one arithmetic operation to be performed at any given time. For example, the arithmetic expression 
     
         &#34;Z=A+B+C+D&#34; 
    
     would be transformed into the Polish prefix string 
     
         &#34;Z←+++ABCD&#34; 
    
     which defines the computation tree 10 shown in FIG. 1. 
     The operator precedence is not pre determined and no operational subgroupings or associations are pre defined. Hence, this parsed expression may only be operated upon sequentially, e.g., by a one processor. 
     SUMMARY 
     The present invention is directed to a method and an apparatus for an efficient one-pass parsing algorithm that exhibits in a compiler suitable Polish prefix form the maximal degree of computational parallelism within an arithmetic expression. Multiple processing units then may simultaneously operate upon the parsed expression, while one of the processing units parses the next arithmetic expression. 
     A general parsing algorithm of the present invention would relax the assumption that in a processor only one operation can be performed at any given time. By using one operator stack and two operand stacks the present invention produces a one-pass parsing algorithm for the generation of a Polish string that computationally defines the maximal possible parallel execution of a general class of arithmetic expressions. 
     For purposes of this disclosure, operands are taken left-to-right within an operand grouping and the associated operations are taken from right-to-left. The expression 
     
         Z=A+B+C+D 
    
     would then be transformed into 
     
         Z←++AB+CD 
    
     which defines the computation tree with maximal parallelism. Hence, expressions +AB and +CD can be evaluated simultaneously by independent processors. 
    
    
     BRIEF DESCRIPTION OF DRAWINGS 
     FIG. 1 is a tree diagram of equation &#34;Z=A+B+C+D&#34;; 
     FIG. 2 is a tree diagram of an alternative interpretation of equation &#34;Z=A+B+C+D&#34;; 
     FIG. 3 shows a flowchart of the character scanning and processing phase of the present invention; and 
     FIG. 4 shows a flowchart of the stack processing phase of the present invention. 
     FIG. 5 shows changing contents of memory stacks, as equation &#34;Z=A+B+C+D;&#34; of Example 1 is parsed; 
     FIGS. 6a-6b show changing contents of memory stacks, as equation &#34;Z=((A-B)*C-(D-E)*F/((G-H)-I-(j-K)-L);&#34; of Example 2 is parsed; 
    
    
     DETAILED DESCRIPTION OF THE INVENTION 
     FIG. 2 shows the computation tree 10 defined by the expression 
     
         Z=A+B+C+D 
    
     which, for maximal parallelism, is transformed by the algorithm of the present invention into 
     
         Z←++AB+CD 
    
     The following criteria are followed by the algorithm of the present invention. 
     1. The arithmetic expression is well-formed, i.e., it is syntactically legal, and all operations are defined. In a well-formed arithmetic expression the commutative and non-commutative operations are recognizable and unary and binary symbols are distinguishable, e.g., unary minus is distinguishable from binary minus. An assumption is made that precedence is established for all the operations, e.g., multiplication has higher precedence than addition. 
     2. Three last in, first out (LIFO) stacks are available. These stacks are respectively, the low order operand stack (LOS), the high order operand stack (HOS) and the operator stack (RS). 
     3. The arithmetic expression consists of a string of characters classified into the following categories: 
     (a) variable or operand, 
     (b) operation, 
     (c) left parenthesis, &#34;(&#34;, 
     (d) right parenthesis, &#34;)&#34;, 
     (e) end-of-string delimiter, &#34;;&#34;. 
     4. The two variables or operands, which produce a new variable through the action of an operation, are collectively termed a variable pair. Each variable is tagged with a unique precedence index, the low order variable in the variable pair is tagged with the lowest precedence index and the high order variable is tagged with the highest precedence index. 
     5. The characters in the string are sequentially scanned from left to right by a sequence counter (pointer) whose value or sequence number indexes the characters in the string. 
     The Parsing Algorithm 
     The algorithm of the present invention consists of three phases: the initialization phase, the character scanning and processing phase, and the stack processing phase. At the initialization phase, the pointer is set to point to the first character in the arithmetic expression string, i.e., it is set to one, and the variable precedence index is set to one and the stacks are emptied. 
     FIG. 3 shows the character scanning and processing phase 12 of the present invention. The character in the string position given by the pointer is scanned by the &#34;scan a character&#34; module 1 and processed according to its category by the &#34;Character Processing&#34; module 3. As defined above in the criteria (3), a character group consists of the variable or operand, the operation, the left parenthesis, the right parenthesis and the end-of-string delimiter. 
     If the character is a variable, the control is passed to the &#34;Variable Processing&#34; module 8. The &#34;Variable Processing&#34; module 8 tags the character with the current precedence index and places it into the low order operand stack. The precedence index is then incremented, the character pointer is advanced and the next character is scanned by the &#34;Character Scanning&#34; module 2. 
     If the character is an operation, the &#34;Character Processing&#34; module 3 calls the &#34;Operation Processing&#34; module 10. The character is tested to determine if it is unary. If this is the case, an appropriate indicator is set by module 11, the character pointer is advanced and the next character is scanned by the &#34;Character Scanning&#34; module 2. 
     If the character is not a unary operation, two tests 12, 13 are performed to determine whether the scanned operation has higher precedence than the top element in the operator stack, and whether the previous scanned character is not a right parenthesis. Failure of either test 12, 13 results in the operation character being placed into the operator stack by module 15, the character pointer is then advanced and the next character is scanned by the &#34;Character Scanning&#34; module 2. 
     Passage of either test 12, 13 results in testing of the top element in the operator stack, by module 14, to determine if it is a commutative or a non-commutative operation. If the operation is non-commutative, then the character pointer is advanced and the next character is scanned by the &#34;Character Scanning&#34; module 2. 
     If the operation is commutative, a determination is made, by module 17, whether there are two variables in the low order operand stack and whether the top two operations in the operator stack have the same precedence. Satisfaction of these conditions invokes the stack processing procedure shown in FIG. 4. Then, the character pointer is advanced and the next character is scanned by the &#34;Character Scanning&#34; module 2. 
     If the character is a left parenthesis, &#34;(&#34;, then module 4 enters a special parenthesis character into both the high order operand stack and the low order operand stack, the character pointer is advanced and the next character is scanned by the &#34;Character Scanning&#34; module 2. If the character is a right parenthesis, &#34;)&#34;, then module 5 repeatedly invokes the stack processing procedure to process the scanned equation within a set of parentheses. The special left parenthesis characters are then deleted from both the high order operand stack and the low order operand stack. Module 6 then determines whether the expression within the parenthesis is operated upon by a unary operation. That unary operation is then prefixed to the expression located on the stack. The character pointer is advanced and the next character is scanned by the &#34;Character Scanning&#34; module 2. 
     Finally, if the character is an end-of-string delimiter, &#34;;&#34;, then module 9 invokes the stack processing procedure shown in FIG. 4. The stack processing continues until both the operator stack and the low order operand stack are empty. That void condition implies that the expression has been completely processed and the Polish string form exhibiting maximal parallelism is to be found in the high order operand stack. 
     Stack Processing 
     The function of the stack processing procedure phase is to adjoin the proper pair of variables with the appropriate operation, and to place the resulting variables in the high order operand stack with its correct precedence index. Shown in FIG. 4 is the flow diagram 14 of the stack processing procedure. Test 1 determines, if there is more then one element in each of the operand stacks. If there is more then one element, then test 2 is performed to determine if the top two elements in the operator stack are of equal precedence. 
     If there is more then one element in each of the operand stacks or if the top two elements in the operator stack are not of equal precedence, module 4 forms a new variable by joining the elements with the highest precedence tags from the high order operand stack and the low order operand stack and the element from the top of the operator stack. 
     If the top two elements in the operator stack are of equal precedence, then test 3 determines which operand stack has the element with the highest precedence tag. Module 5 then forms a new variable by joining together the top two elements from the operand stack with the highest precedence element and the element from the top of the operator stack. 
     In all cases, the operation is placed first, the low order variable is placed in the second position, and the high order variable is placed in the third position of any newly formed variable. Module 6 tags the new variable with the precedence index of the low order variable and places it into the high order operand stack. Test 7 determines what character triggered the stack processing. The possibilities are a semicolon, a right parenthesis and 2 consecutive operators of the same precedence as shown by module 17 (FIG. 3). The stack processing is then repeated or terminated based on the criteria of test 7. 
     To facilitate understanding, Example 1 shown in FIG. 5 illustrates the operation of the algorithm of the present invention in a step by step scanning and stack processing phases of equation 
     
         &#34;Z=A+B+C+D;&#34;. 
    
     In the initialization phase the pointer is set to scan the first variable of the equation. 
     In step 1, the character &#34;A&#34; is scanned at the pointer. Variable module 8 (FIG. 3) gives it the precedence index 1 and places it in LOS 20. The precedence index is incremented and the pointer is set to scan the next variable. 
     In step 2, the character &#34;+&#34; is scanned at the pointer. Operation module 10 (FIG. 3) determines that it is not a unary operation. Test 12 (FIG. 3) determines that there are no other operators present on RS 30 hence, the present operator has higher precedence. Test 13 (FIG. 3) determines that the previous character is not &#34;)&#34;. Module 15 (FIG. 3) places the character in RS 30 and the pointer is set to scan the next variable. 
     In step 3, the character &#34;B&#34; is scanned at the pointer. Variable module 8 (FIG. 3) gives it the precedence index 2 and places it in LOS 20. The precedence index is incremented and the pointer is set to scan the next variable. 
     In step 4, the character &#34;+&#34; is scanned at the pointer. Operation module 10 (FIG. 3) determines that it is not a unary operation. Test 12 (FIG. 3) determines that the present operator is of equal precedence with the operator present on RS 30. Test 13 (FIG. 3) determines that the previous character is not &#34;)&#34;. Module 15 (FIG. 3) places the character in RS 30 and the pointer is set to scan the next variable. 
     In step 5, the &#34;C&#34; character is scanned at the pointer. Variable module 8 (FIG. 3) gives it the precedence index 3 and places it in LOS 20. The precedence index is incremented and the pointer is set to scan the next variable. 
     In step 6, the character &#34;+&#34; is scanned at the pointer. Operation module 10 (FIG. 3) determines that it is not a unary operation. Test 12 (FIG. 3) determines that the present operator has equal precedence to the other operators present on RS 30. Test 13 (FIG. 3) determines that the previous character is not &#34;)&#34;. Module 15 (FIG. 3) places the character in RS 30 and the pointer is set to scan the next variable. 
     In step 7, the character &#34;D&#34; is scanned at the pointer. Variable module 8 (FIG. 3) gives it the precedence index 4 and places it in LOS 20. The precedence index is incremented and the pointer is set to scan the next variable. 
     In step 8, the character &#34;;&#34; is scanned at the pointer, the &#34;;&#34; module 9 (FIG. 3) invokes stack processing. Test 1 determines if there is more then one element in LOS 20, however HOS 10 is empty. Thus, module 4 joins the top two elements in LOS 20 to one another and to the top element of RS 30. Module 6 prefixes the result with the lower precedence number of the variable pair and places the result in HOS 10. Test 7 determines that the scanned character is &#34;;&#34; and that RS 30 is not empty, hence stack processing is repeated. 
     In step 9, Test 1 determines that LOS 20 and HOS 10 are not empty. Test 2 determines that the top two elements in RS 30 are of equal precedence. Test 3 selects LOS 20 because LOS 20 has entries with higher precedence than HOS 10. Thus module 5 joins the top two elements in LOS 20 to one another and to the top element of RS 30. Module 6 prefixes the result with the lower precedence number of the variable pair and places the result in HOS 10. Test 7 determines that the scanned character is &#34;;&#34; and that RS 30 is not empty, hence stack processing is repeated. In step 10, Test 1 determines that LOS 20 is empty however HOS 10 is not. Thus module 4 joins the top two elements in HOS 10 to one another and to the top element of RS 30. Module 6 prefixes the result with the lower precedence number of the variable pair and places the result in HOS 10. Test 7 determines that the scanned character is &#34;;&#34; and that RS 30 is empty, hence stack processing terminated. 
     Example 2 shown in FIGS. 6a, 6b illustrates the operation of the algorithm of the present invention in a step by step scanning and stack processing phases of equation 
     
         &#34;Z=((A-B)*C-(D-E)*F)/((G-H)*I-(J-K)*L);&#34;. 
    
     Steps similar to the ones described above should be utilized to transform that equation into a Polish notation form designed for maximal parallelism. 
     While the invention has been particularly shown and described with respect to illustrative and preferred embodiments thereof, it will be understood by those skilled in the art that the foregoing and other changes in form and details may be made therein without departing from the spirit and scope of the invention that should be limited only by the scope of the appended claims.