An operator precedence grammar is a kind of grammar for formal languages.

Technically, an operator precedence grammar is a context-free grammar that has the property (among others)

that no production has either an empty right-hand side or two adjacent nonterminals in its

right-hand side. These properties allow precedence relations to be

defined between the terminals of the grammar. A parser that exploits these relations is considerably simpler than more general-purpose parsers, such as LALR parsers. Operator-precedence parsers can be constructed for a large class of context-free grammars.

Precedence relations

Operator precedence grammars rely on the following three precedence relations between the terminals:

{| class="wikitable"

|-

! Relation

! Meaning

|-

| <math>a \lessdot b</math>

| yields precedence to

|-

| <math>a \doteq b</math>

| has the same precedence as

|-

| <math>a \gtrdot b</math>

| takes precedence over

|}

These operator precedence relations allow to delimit the handles in the right sentential forms: <math>\lessdot</math> marks the left end, <math>\doteq</math> appears in the interior of the handle, and <math>\gtrdot</math> marks the right end. Contrary to other shift-reduce parsers, all nonterminals are considered equal for the purpose of identifying handles.

The relations do not have the same properties as their un-dotted counterparts;

e.&nbsp;g. <math>a \doteq b</math> does not generally imply <math>b \doteq a</math>, and <math>b \gtrdot a</math> does not follow

from <math>a \lessdot b</math>. Furthermore, <math>a \doteq a</math> does not generally hold, and <math>a \gtrdot a</math> is possible.

Let us assume that between the terminals and there is always exactly one precedence relation. Suppose that $ is the end of the string.

Then for all terminals we define: <math>\$ \lessdot b</math> and <math>b \gtrdot \$</math>. If we remove all nonterminals and place the correct precedence relation:

<math>\lessdot</math>, <math>\doteq</math>, <math>\gtrdot</math> between the remaining terminals, there remain strings that can be analyzed by an easily developed bottom-up parser.

Example

For example, the following operator precedence relations can

be introduced for simple expressions:

:<math>\begin{array}{c|cccc}

& \mathrm{id}

& +

& *

& \$

\\

\hline

\mathrm{id}

&

& \gtrdot

& \gtrdot

& \gtrdot

\\

+

& \lessdot

& \gtrdot

& \lessdot

& \gtrdot

\\

& \lessdot

& \gtrdot

& \gtrdot

& \gtrdot

\\

\$

& \lessdot

& \lessdot

& \lessdot

&

\end{array}</math>

They follow from the following facts:

  • + has lower precedence than * (hence <math>+ \lessdot *</math> and <math>* \gtrdot +</math>).
  • Both + and * are left-associative (hence <math>+ \gtrdot +</math> and <math>* \gtrdot *</math>).

The input string

If $ is on the top of the stack and ip points to $ then return

else

Let a be the top terminal on the stack, and b the symbol pointed to by ip

if a <math>\lessdot</math> b or a <math>\doteq</math> b then

push b onto the stack

advance ip to the next input symbol

else if a <math>\gtrdot</math> b then

repeat

pop the stack

until the top stack terminal is related by <math>\lessdot</math> to the terminal most recently popped

else error()

end

Precedence functions

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.

They map terminal symbols to integers, and so the precedence relations between the symbols are implemented by numerical comparison:

must hold if <math>a \lessdot b</math> holds, etc.

Not every table of precedence relations has precedence functions, but in practice for most grammars such functions can be designed.

Algorithm for constructing precedence functions

The below algorithm is from Aho et al.:

  1. Create symbols and for each grammar terminal and for the end of string symbol;
  2. Partition the created symbols in groups so that and are in the same group if <math>a \doteq b</math> (there can be symbols in the same group even if their terminals are not connected by this relation);
  3. Create a directed graph whose nodes are the groups. For each pair of terminals do: place an edge from the group of to the group of if otherwise if <math>a \gtrdot b</math> place an edge from the group of to that of ;
  4. If the constructed graph has a cycle then no precedence functions exist. When there are no cycles, let be the length of the longest path from the group of and let be the length of the longest path from the group of .

Example

Consider the following table (repeated from above):

:<math>\begin{array}{c|cccc}

& \mathrm{id}

& +

& *

& \$

\\

\hline

\mathrm{id}

&

& \gtrdot

& \gtrdot

& \gtrdot

\\

+

& \lessdot

& \gtrdot

& \lessdot

& \gtrdot

\\

& \lessdot

& \gtrdot

& \gtrdot

& \gtrdot

\\

\$

& \lessdot

& \lessdot

& \lessdot

&

\end{array}</math>

Using the algorithm leads to the following graph:

gid

\

fid f*

\ /

g*

/

f+

| \

| g+

| |

g$ f$

from which we extract the following precedence functions from the maximum heights in the directed acyclic graph:

:{| class="wikitable"

|-

!

! id

! +

! *

! $

|-

! f

| 4

| 2

| 4

| 0

|-

! g

| 5

| 1

| 3

| 0

|}

Operator-precedence languages

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.

Operator-precedence languages enjoy many closure properties: union, intersection, complementation, concatenation, that enables efficient parallel parsing.

There are also characterizations based on an equivalent form of automata and monadic second-order logic.

Notes

References

Further reading

  • Nikolay Nikolaev: IS53011A Language Design and Implementation , Course notes for CIS&nbsp;324, 2010.