Operator precedence grammars rely on the following three precedence relations between the terminals:2
These operator precedence relations allow to delimit the handles in the right sentential forms: ⋖ {\displaystyle \lessdot } marks the left end, ≐ {\displaystyle \doteq } appears in the interior of the handle, and ⋗ {\displaystyle \gtrdot } marks the right end. Contrary to other shift-reduce parsers, all nonterminals are considered equal for the purpose of identifying handles.3 The relations do not have the same properties as their un-dotted counterparts; e. g. a ≐ b {\displaystyle a\doteq b} does not generally imply b ≐ a {\displaystyle b\doteq a} , and b ⋗ a {\displaystyle b\gtrdot a} does not follow from a ⋖ b {\displaystyle a\lessdot b} . Furthermore, a ≐ a {\displaystyle a\doteq a} does not generally hold, and a ⋗ a {\displaystyle a\gtrdot a} is possible.
Let us assume that between the terminals ai and ai+1 there is always exactly one precedence relation. Suppose that $ is the end of the string. Then for all terminals b we define: $ ⋖ b {\displaystyle \$\lessdot b} and b ⋗ $ {\displaystyle b\gtrdot \$} . If we remove all nonterminals and place the correct precedence relation: ⋖ {\displaystyle \lessdot } , ≐ {\displaystyle \doteq } , ⋗ {\displaystyle \gtrdot } between the remaining terminals, there remain strings that can be analyzed by an easily developed bottom-up parser.
For example, the following operator precedence relations can be introduced for simple expressions:4
They follow from the following facts:5
The input string6
after adding end markers and inserting precedence relations becomes
Having precedence relations allows to identify handles as follows:7
It is generally not necessary to scan the entire sentential form to find the handle.
The algorithm below is from Aho et al.:8
An operator precedence parser usually does not store the precedence table with the relations, which can get rather large. Instead, precedence functions f and g are defined.9 They map terminal symbols to integers, and so the precedence relations between the symbols are implemented by numerical comparison: f ( a ) < g ( b ) {\displaystyle f(a)<g(b)} must hold if a ⋖ b {\displaystyle a\lessdot b} holds, etc.
Not every table of precedence relations has precedence functions, but in practice for most grammars such functions can be designed.10
The below algorithm is from Aho et al.:11
Consider the following table (repeated from above):12
Using the algorithm leads to the following graph:
from which we extract the following precedence functions from the maximum heights in the directed acyclic graph:
The class of languages described by operator-precedence grammars, i.e., operator-precedence languages, is strictly contained in the class of deterministic context-free languages, and strictly contains visibly pushdown languages.13
Operator-precedence languages enjoy many closure properties: union, intersection, complementation,14 concatenation,15 and they are the largest known class closed under all these operations and for which the emptiness problem is decidable. Another peculiar feature of operator-precedence languages is their local parsability,16 that enables efficient parallel parsing.
There are also characterizations based on an equivalent form of automata and monadic second-order logic.17
Aho, Sethi & Ullman 1988, p. 203 - Aho, Alfred V.; Sethi, Ravi; Ullman, Jeffrey D. (1988). Compilers — Principles, Techniques, and Tools. Addison-Wesley. ↩
Aho, Sethi & Ullman 1988, pp. 203–204 - Aho, Alfred V.; Sethi, Ravi; Ullman, Jeffrey D. (1988). Compilers — Principles, Techniques, and Tools. Addison-Wesley. ↩
Aho, Sethi & Ullman 1988, pp. 205–206 - Aho, Alfred V.; Sethi, Ravi; Ullman, Jeffrey D. (1988). Compilers — Principles, Techniques, and Tools. Addison-Wesley. ↩
Aho, Sethi & Ullman 1988, p. 205 - Aho, Alfred V.; Sethi, Ravi; Ullman, Jeffrey D. (1988). Compilers — Principles, Techniques, and Tools. Addison-Wesley. ↩
Aho, Sethi & Ullman 1988, p. 204 - Aho, Alfred V.; Sethi, Ravi; Ullman, Jeffrey D. (1988). Compilers — Principles, Techniques, and Tools. Addison-Wesley. ↩
Aho, Sethi & Ullman 1988, p. 206 - Aho, Alfred V.; Sethi, Ravi; Ullman, Jeffrey D. (1988). Compilers — Principles, Techniques, and Tools. Addison-Wesley. ↩
Aho, Sethi & Ullman 1988, pp. 208–209 - Aho, Alfred V.; Sethi, Ravi; Ullman, Jeffrey D. (1988). Compilers — Principles, Techniques, and Tools. Addison-Wesley. ↩
Aho, Sethi & Ullman 1988, p. 209 - Aho, Alfred V.; Sethi, Ravi; Ullman, Jeffrey D. (1988). Compilers — Principles, Techniques, and Tools. Addison-Wesley. ↩
Aho, Sethi & Ullman 1988, pp. 209–210 - Aho, Alfred V.; Sethi, Ravi; Ullman, Jeffrey D. (1988). Compilers — Principles, Techniques, and Tools. Addison-Wesley. ↩
Aho, Sethi & Ullman 1988, p. 210 - Aho, Alfred V.; Sethi, Ravi; Ullman, Jeffrey D. (1988). Compilers — Principles, Techniques, and Tools. Addison-Wesley. ↩
Crespi Reghizzi & Mandrioli 2012 - Crespi Reghizzi, Stefano; Mandrioli, Dino (2012). "Operator precedence and the visibly pushdown property". Journal of Computer and System Sciences. 78 (6): 1837–1867. doi:10.1016/j.jcss.2011.12.006. https://doi.org/10.1016%2Fj.jcss.2011.12.006 ↩
Crespi Reghizzi, Mandrioli & Martin 1978 - Crespi Reghizzi, Stefano; Mandrioli, Dino; Martin, David F. (1978). "Algebraic Properties of Operator Precedence Languages". Information and Control. 37 (2): 115–133. doi:10.1016/S0019-9958(78)90474-6. https://doi.org/10.1016%2FS0019-9958%2878%2990474-6 ↩
Barenghi et al. 2015 - Barenghi, Alessandro; Crespi Reghizzi, Stefano; Mandrioli, Dino; Panella, Federica; Pradella, Matteo (2015). "Parallel parsing made practical". Science of Computer Programming. 112 (3): 245–249. doi:10.1016/j.scico.2015.09.002. hdl:11311/971391. https://doi.org/10.1016%2Fj.scico.2015.09.002 ↩
Lonati et al. 2015 - Lonati, Violetta; Mandrioli, Dino; Panella, Federica; Pradella, Matteo (2015). "Operator Precedence Languages: Their Automata-Theoretic and Logic Characterization". SIAM Journal on Computing. 44 (4): 1026–1088. doi:10.1137/140978818. hdl:2434/352809. https://doi.org/10.1137%2F140978818 ↩