Manual
Manual
Manual
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
80 Bison 2.3<br />
parser uses the same basic algorithm for parsing as an ordinary Bison parser, but behaves<br />
differently in cases where there is a shift-reduce conflict that has not been resolved by<br />
precedence rules (see Section 5.3 [Precedence], page 73) or a reduce-reduce conflict. When<br />
a GLR parser encounters such a situation, it effectively splits into a several parsers, one for<br />
each possible shift or reduction. These parsers then proceed as usual, consuming tokens<br />
in lock-step. Some of the stacks may encounter other conflicts and split further, with the<br />
result that instead of a sequence of states, a Bison GLR parsing stack is what is in effect a<br />
tree of states.<br />
In effect, each stack represents a guess as to what the proper parse is. Additional<br />
input may indicate that a guess was wrong, in which case the appropriate stack silently<br />
disappears. Otherwise, the semantics actions generated in each stack are saved, rather than<br />
being executed immediately. When a stack disappears, its saved semantic actions never get<br />
executed. When a reduction causes two stacks to become equivalent, their sets of semantic<br />
actions are both saved with the state that results from the reduction. We say that two<br />
stacks are equivalent when they both represent the same sequence of states, and each pair<br />
of corresponding states represents a grammar symbol that produces the same segment of<br />
the input token stream.<br />
Whenever the parser makes a transition from having multiple states to having one, it<br />
reverts to the normal LALR(1) parsing algorithm, after resolving and executing the saved-up<br />
actions. At this transition, some of the states on the stack will have semantic values that<br />
are sets (actually multisets) of possible actions. The parser tries to pick one of the actions<br />
by first finding one whose rule has the highest dynamic precedence, as set by the ‘%dprec’<br />
declaration. Otherwise, if the alternative actions are not ordered by precedence, but there<br />
the same merging function is declared for both rules by the ‘%merge’ declaration, Bison<br />
resolves and evaluates both and then calls the merge function on the result. Otherwise, it<br />
reports an ambiguity.<br />
It is possible to use a data structure for the GLR parsing tree that permits the processing<br />
of any LALR(1) grammar in linear time (in the size of the input), any unambiguous (not<br />
necessarily LALR(1)) grammar in quadratic worst-case time, and any general (possibly ambiguous)<br />
context-free grammar in cubic worst-case time. However, Bison currently uses a<br />
simpler data structure that requires time proportional to the length of the input times the<br />
maximum number of stacks required for any prefix of the input. Thus, really ambiguous<br />
or nondeterministic grammars can require exponential time and space to process. Such<br />
badly behaving examples, however, are not generally of practical interest. Usually, nondeterminism<br />
in a grammar is local—the parser is “in doubt” only for a few tokens at a time.<br />
Therefore, the current data structure should generally be adequate. On LALR(1) portions<br />
of a grammar, in particular, it is only slightly slower than with the default Bison parser.<br />
For a more detailed exposition of GLR parsers, please see: Elizabeth Scott,<br />
Adrian Johnstone and Shamsa Sadaf Hussain, Tomita-Style Generalised LR Parsers,<br />
Royal Holloway, University of London, Department of Computer Science, TR-00-12,<br />
http://www.cs.rhul.ac.uk/research/languages/publications/tomita_style_1.ps,<br />
(2000-12-24).