Operator-precedence grammar

Operator-precedence grammar

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[1]) 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.


Contents

Precedence Relations

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

Relation Meaning
a <• b a yields precedence to b
a =• b a has the same precedence as b
a •> b a takes precedence over b

These operator precedence relations allow to delimit the handles in the right sentential forms: <• marks the left end, =• appears in the interior of the handle, and •> 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 does not generally imply b =• a, and b •> a does not follow from a <• b. Furthermore, a =• a does not generally hold, and a •> 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 and b •> $. If we remove all nonterminals and place the correct precedence relation: <•, =•, •> 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:[4]

id + * $
id •> •> •>
+ <• •> <• •>
* <• •> •> •>
$ <• <• <•

They follow from the following facts:[5]

  • + has lower precedence than * (hence + <• * and * •> +).
  • Both + and * are left-associative (hence + •> + and * •> *).

The input string[4]

id1 + id2 * id3

after adding end markers and inserting precedence relations becomes

$ <• id1 •> + <• id2 •> * <• id3 •> $

Operator Precedence Parsing

Having precedence relations allows to identify handles as follows:[4]

  • scan the string from left until seeing •>
  • scan backwards (from right to left) over any =• until seeing <•
  • everything between the two relations <• and •>, including any intervening or surrounding nonterminals, forms the handle

It is generally not necessary to scan the entire sentential form to find the handle.

Operator Precedence Parsing Algorithm[6]

Initialize: Set ip to point to the first symbol of w$
Repeat:
  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 <• b or a =• b then
      push b onto the stack
      advance ip to the next input symbol
    else if a •> b then
      repeat
        pop the stack
      until the top stack terminal is related by <• 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.[7] They map terminal symbols to integers, and so the precedence relations between the symbols are implemented by numerical comparison: f(a) < g(b) must hold if a <• b holds, etc.

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

Algorithm for Constructing Precedence Functions[9]

  1. Create symbols fa and ga for each grammar terminal a and for the end of string symbol;
  2. Partition the created symbols in groups so that fa and gb are in the same group if a =• b (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, next for each pair (a,b) of terminals do: place an edge from the group of gb to the group of fa if a <• b, otherwise if a •> b place an edge from the group of fa to that of gb;
  4. If the constructed graph has a cycle then no precedence functions exist. When there are no cycles, let f(a) be the length of the longest path from the group of fa and let g(a) be the length of the longest path from the group of ga.

Example

Consider the following table (repeated from above):[10]

id + * $
id •> •> •>
+ <• •> <• •>
* <• •> •> •>
$ <• <• <•

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:

id + * $
f 4 2 4 0
g 5 1 3 0

Notes

  1. ^ ASU 1988, p. 203.
  2. ^ ASU 1988, pp. 203-204.
  3. ^ ASU 1988, pp. 205-206.
  4. ^ a b c ASU 1988, p. 205.
  5. ^ ASU 1988, p. 204.
  6. ^ ASU 1988, p. 206.
  7. ^ ASU 1988, pp. 208-209.
  8. ^ ASU 1988, p. 209.
  9. ^ ASU 1988, pp. 209-210.
  10. ^ ASU 1988, p. 210.

References

  • Aho, Alfred V., Sethi, Ravi, and Ullman, Jeffrey D. (1988). Compilers — Principles, Techniques, and Tools. Addison-Wesley.
  • Floyd, R. W. (1963). "Syntactic Analysis and Operator Precedence". Journal of the ACM 10: 316. doi:10.1145/321172.321179.  edit

External links


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Operator-precedence parser — An operator precedence parser is a bottom up parser that interprets an operator precedence grammar. For example, most calculators use operator precedence parsers to convert from the human readable infix notation with order of operations format… …   Wikipedia

  • Operator — may refer to: Contents 1 Music 2 Computers 3 Science and mathematics …   Wikipedia

  • Parser Grammar Engine — The Parser Grammar Engine (originally Parrot Grammar Engine) or PGE is a compiler and runtime for a Perl 6 rules for the Parrot virtual machine. [cite web | url=http://search.cpan.org/ ltoetsch/parrot 0.2.2/compilers/pge/README | title=Parrot… …   Wikipedia

  • Parsing expression grammar — A parsing expression grammar, or PEG, is a type of analytic formal grammar that describes a formal language in terms of a set of rules for recognizing strings in the language. A parsing expression grammar essentially represents a recursive… …   Wikipedia

  • Head-driven Phrase Structure Grammar — Die Head driven Phrase Structure Grammar (HPSG) ist eine Grammatiktheorie, die in den 1980er Jahren auf der Basis der Wiederbelebung der kontextfreien Phrasenstrukturgrammatiken als Generative Grammatiktheorie aus der Familie der… …   Deutsch Wikipedia

  • Operators in C and C++ — This is a list of operators in the C and C++ programming languages. All the operators listed exist in C++; the fourth column Included in C , dictates whether an operator is also present in C. Note that C does not support operator overloading.… …   Wikipedia

  • Logical connective — This article is about connectives in classical logic. For connectors in natural languages, see discourse connective. For connectives and operators in other logics, see logical constant. For other logical symbols, see table of logic symbols. In… …   Wikipedia

  • Parsing — In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a sequence of tokens to determine their grammatical structure with respect to a given (more or less) formal grammar.Parsing is also… …   Wikipedia

  • Bottom-up parsing — (also known as shift reduce parsing) is a strategy for analyzing unknown data relationships that attempts to identify the most fundamental units first, and then to infer higher order structures from them. It attempts to build trees upward toward… …   Wikipedia

  • C (programming language) — C The C Programming Language[1] (aka K R ) is the seminal book on C …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”