GLR parser

From Wikipedia, the free encyclopedia

In computer science, a GLR parser (Generalized LR parser) is an extension of an LR parser algorithm to handle nondeterministic and ambiguous grammars. First described in a 1986 paper by Masaru Tomita, it has also been referred to as a "parallel parser".

Though the algorithm has evolved since its original form, the principles have remained intact: Tomita's goal was to parse natural language text thoroughly and efficiently. Standard LR parsers cannot accommodate for the non-deterministic and ambiguous nature of natural language, and the GLR algorithm can.

[edit] Algorithm

Briefly, the GLR algorithm works in a manner similar to the LR parser algorithm, except that, given a particular grammar, a GLR parser will process all possible interpretations of a given input in a breadth-first search. On the front-end, a GLR parser generator converts an input grammar into parser tables, in a manner similar to an LR generator. However, where LR parse tables allow for only one state transition (given a state and an input token), GLR parse tables allow for multiple transitions. In effect, GLR allows for shift/reduce and reduce/reduce conflicts.

When a conflicting transition is encountered, the parse stack is forked into two or more parallel parse stacks, where the state corresponding to each possible transition is at the top. Then, the next input token is read and used to determine the next transition(s) for each of the "top" states -- and further forking can occur. If any given top state and input token do not result in at least one transition, then that "path" through the parse tables is invalid and can be discarded.

A crucial optimization allows sharing of common prefixes and suffixes of these stacks, which constrains the overall search space and memory usage required to parse input text. The complex structures that arise from this improvement make the stack more like a lattice of nodes.

[edit] Advantages

When implemented carefully, the GLR algorithm has the same time complexity as the CYK algorithm and Earley algorithm -- O(n3). However, GLR carries two additional important advantages:

  • The time required to run the algorithm is proportional to the degree of nondeterminism in the grammar -- on deterministic grammars the GLR algorithm runs in O(n) time (this is not true of the Earley and CYK algorithms)
  • The GLR algorithm is "on-line" -- that is, it consumes the input tokens in a specific order and performs as much work as possible after consuming each token.

Since most programming languages are deterministic or "almost deterministic", in practice the GLR algorithm often performs markedly better than others.