OperatorPrecedenceParseDoc.tioga
Spreitzer.pa, July 8, 1985 9:55:22 pm PDT
OperatorPrecedenceParse
CEDAR 6.0 — FOR INTERNAL XEROX USE ONLY
OperatorPrecedenceParse
Mike Spreitzer
© Copyright 1985 Xerox Corporation. All rights reserved.
Abstract: OperatorPrecedenceParse is an operator-precedence parser.
Created by: Mike Spreitzer
Maintained by: Mike Spreitzer <Spreitzer.pa>
Keywords: Operator Precedence, Parse
XEROX  Xerox Corporation
   Palo Alto Research Center
   3333 Coyote Hill Road
   Palo Alto, California 94304

For Internal Xerox Use Only
1. OperatorPrecedenceParse
This parser takes as input a stream of tokens (provided by the client), and returns a parse tree. The tree is entirely managed by the client; the parser knows only that tree nodes are REFs. The client supplies the semantic routines which are called on each reduction.
This parser takes pains to preserve byte pointers into the source stream. Each node in the parse tree can have a SourceRange, which indicates the beginning and edning byte position of the range of source covered by that node's subtree.
Each token has a left precedence and a right precedence (leftPrec and rightPrec, in the token's class). A precedence describes how hard the token wants to combine with the expression on that side. Thus, when both left and right precedences are 0, the token is an argument; otherwise it is an operator (infix if both are non-zero, posfitx if right is 0, prefix if left is 0). When there is an operator on both sides of an argument, the reduction that is wanted most strongly is done. When the competing precedences are the same, the reduction involves all the operators with that precedence. Here are some examples of input token sequences, with left and right precedences noted, and bracketed according to the reductions done:
{{0A0 20*20 {0B0 30!0} 20*20 {0sin25 0q0}} 10+10 0C0}
{0A0 20*20 {0(5 0B0 10+10 0C0 5)0}}
{0Fn0 40[5 {0x0 7,7 0y0} 5]0}
{0IF8 0b0 8THEN8 {0x0 10-10 0z0} 8ELSE9 {0y0 10+10 0z0}}
The parser knows at each step whether it expects the next token to have a left precedence of 0. If it expects a left precedence of 0, but the token given has a non-zero left precedence, all is not necessarily lost. That token may (via the leftFix in its class) specify an argument to be inserted before it in the input stream to fix the problem. This makes it easy to parse languages accepted by grammars like
...
Call => Subject [ ArgList ]
Call => Subject [ ]
...