34 |
@end ifinfo |
@end ifinfo |
35 |
@comment %**end of header |
@comment %**end of header |
36 |
|
|
37 |
@ifinfo |
@copying |
|
@format |
|
|
START-INFO-DIR-ENTRY |
|
|
* bison: (bison). GNU Project parser generator (yacc replacement). |
|
|
END-INFO-DIR-ENTRY |
|
|
@end format |
|
|
@end ifinfo |
|
38 |
|
|
39 |
@ifinfo |
This manual is for GNU Bison (version @value{VERSION}, @value{UPDATED}), |
40 |
This file documents the Bison parser generator. |
the GNU parser generator. |
41 |
|
|
42 |
Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1995, 1998, 1999, |
Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1995, 1998, |
43 |
2000, 2001, 2002 |
1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
44 |
Free Software Foundation, Inc. |
|
45 |
|
@quotation |
46 |
Permission is granted to make and distribute verbatim copies of |
Permission is granted to copy, distribute and/or modify this document |
47 |
this manual provided the copyright notice and this permission notice |
under the terms of the GNU Free Documentation License, Version 1.1 or |
48 |
are preserved on all copies. |
any later version published by the Free Software Foundation; with no |
49 |
|
Invariant Sections, with the Front-Cover texts being ``A GNU Manual,'' |
50 |
@ignore |
and with the Back-Cover Texts as in (a) below. A copy of the |
51 |
Permission is granted to process this file through Tex and print the |
license is included in the section entitled ``GNU Free Documentation |
52 |
results, provided the printed document carries copying permission |
License.'' |
53 |
notice identical to this one except for the removal of this paragraph |
|
54 |
(this paragraph not being relevant to the printed manual). |
(a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify |
55 |
|
this GNU Manual, like GNU software. Copies published by the Free |
56 |
@end ignore |
Software Foundation raise funds for GNU development.'' |
57 |
Permission is granted to copy and distribute modified versions of this |
@end quotation |
58 |
manual under the conditions for verbatim copying, provided also that the |
@end copying |
59 |
sections entitled ``GNU General Public License'' and ``Conditions for |
|
60 |
Using Bison'' are included exactly as in the original, and provided that |
@dircategory GNU programming tools |
61 |
the entire resulting derived work is distributed under the terms of a |
@direntry |
62 |
permission notice identical to this one. |
* bison: (bison). GNU parser generator (yacc replacement). |
63 |
|
@end direntry |
|
Permission is granted to copy and distribute translations of this manual |
|
|
into another language, under the above conditions for modified versions, |
|
|
except that the sections entitled ``GNU General Public License'', |
|
|
``Conditions for Using Bison'' and this permission notice may be |
|
|
included in translations approved by the Free Software Foundation |
|
|
instead of in the original English. |
|
|
@end ifinfo |
|
64 |
|
|
65 |
@ifset shorttitlepage-enabled |
@ifset shorttitlepage-enabled |
66 |
@shorttitlepage Bison |
@shorttitlepage Bison |
74 |
|
|
75 |
@page |
@page |
76 |
@vskip 0pt plus 1filll |
@vskip 0pt plus 1filll |
77 |
Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1995, 1998, |
@insertcopying |
|
1999, 2000, 2001, 2002 |
|
|
Free Software Foundation, Inc. |
|
|
|
|
78 |
@sp 2 |
@sp 2 |
79 |
Published by the Free Software Foundation @* |
Published by the Free Software Foundation @* |
80 |
59 Temple Place, Suite 330 @* |
59 Temple Place, Suite 330 @* |
81 |
Boston, MA 02111-1307 USA @* |
Boston, MA 02111-1307 USA @* |
82 |
Printed copies are available from the Free Software Foundation.@* |
Printed copies are available from the Free Software Foundation.@* |
83 |
ISBN 1-882114-44-2 |
ISBN 1-882114-44-2 |
|
|
|
|
Permission is granted to make and distribute verbatim copies of |
|
|
this manual provided the copyright notice and this permission notice |
|
|
are preserved on all copies. |
|
|
|
|
|
@ignore |
|
|
Permission is granted to process this file through TeX and print the |
|
|
results, provided the printed document carries copying permission |
|
|
notice identical to this one except for the removal of this paragraph |
|
|
(this paragraph not being relevant to the printed manual). |
|
|
|
|
|
@end ignore |
|
|
Permission is granted to copy and distribute modified versions of this |
|
|
manual under the conditions for verbatim copying, provided also that the |
|
|
sections entitled ``GNU General Public License'' and ``Conditions for |
|
|
Using Bison'' are included exactly as in the original, and provided that |
|
|
the entire resulting derived work is distributed under the terms of a |
|
|
permission notice identical to this one. |
|
|
|
|
|
Permission is granted to copy and distribute translations of this manual |
|
|
into another language, under the above conditions for modified versions, |
|
|
except that the sections entitled ``GNU General Public License'', |
|
|
``Conditions for Using Bison'' and this permission notice may be |
|
|
included in translations approved by the Free Software Foundation |
|
|
instead of in the original English. |
|
84 |
@sp 2 |
@sp 2 |
85 |
Cover art by Etienne Suvasa. |
Cover art by Etienne Suvasa. |
86 |
@end titlepage |
@end titlepage |
90 |
@ifnottex |
@ifnottex |
91 |
@node Top |
@node Top |
92 |
@top Bison |
@top Bison |
93 |
|
@insertcopying |
|
This manual documents version @value{VERSION} of Bison, updated |
|
|
@value{UPDATED}. |
|
94 |
@end ifnottex |
@end ifnottex |
95 |
|
|
96 |
@menu |
@menu |
395 |
@cindex ambiguous grammars |
@cindex ambiguous grammars |
396 |
@cindex non-deterministic parsing |
@cindex non-deterministic parsing |
397 |
Parsers for LALR(1) grammars are @dfn{deterministic}, meaning roughly that |
Parsers for LALR(1) grammars are @dfn{deterministic}, meaning roughly that |
398 |
the next grammar rule to apply at any point in the input is uniquely |
the next grammar rule to apply at any point in the input is uniquely |
399 |
determined by the preceding input and a fixed, finite portion (called |
determined by the preceding input and a fixed, finite portion (called |
400 |
a @dfn{look-ahead}) of the remaining input. |
a @dfn{look-ahead}) of the remaining input. |
401 |
A context-free grammar can be @dfn{ambiguous}, meaning that |
A context-free grammar can be @dfn{ambiguous}, meaning that |
402 |
there are multiple ways to apply the grammar rules to get the some inputs. |
there are multiple ways to apply the grammar rules to get the some inputs. |
403 |
Even unambiguous grammars can be @dfn{non-deterministic}, meaning that no |
Even unambiguous grammars can be @dfn{non-deterministic}, meaning that no |
404 |
fixed look-ahead always suffices to determine the next grammar rule to apply. |
fixed look-ahead always suffices to determine the next grammar rule to apply. |
405 |
With the proper declarations, Bison is also able to parse these more general |
With the proper declarations, Bison is also able to parse these more general |
406 |
context-free grammars, using a technique known as GLR parsing (for |
context-free grammars, using a technique known as GLR parsing (for |
407 |
Generalized LR). Bison's GLR parsers are able to handle any context-free |
Generalized LR). Bison's GLR parsers are able to handle any context-free |
408 |
grammar for which the number of possible parses of any given string |
grammar for which the number of possible parses of any given string |
409 |
is finite. |
is finite. |
410 |
|
|
411 |
@cindex symbols (abstract) |
@cindex symbols (abstract) |
412 |
@cindex token |
@cindex token |
659 |
user-defined function on the resulting values to produce an arbitrary |
user-defined function on the resulting values to produce an arbitrary |
660 |
merged result. |
merged result. |
661 |
|
|
662 |
Let's consider an example, vastly simplified from C++. |
Let's consider an example, vastly simplified from C++. |
663 |
|
|
664 |
@example |
@example |
665 |
%@{ |
%@{ |
675 |
|
|
676 |
%% |
%% |
677 |
|
|
678 |
prog : |
prog : |
679 |
| prog stmt @{ printf ("\n"); @} |
| prog stmt @{ printf ("\n"); @} |
680 |
; |
; |
681 |
|
|
684 |
; |
; |
685 |
|
|
686 |
expr : ID @{ printf ("%s ", $$); @} |
expr : ID @{ printf ("%s ", $$); @} |
687 |
| TYPENAME '(' expr ')' |
| TYPENAME '(' expr ')' |
688 |
@{ printf ("%s <cast> ", $1); @} |
@{ printf ("%s <cast> ", $1); @} |
689 |
| expr '+' expr @{ printf ("+ "); @} |
| expr '+' expr @{ printf ("+ "); @} |
690 |
| expr '=' expr @{ printf ("= "); @} |
| expr '=' expr @{ printf ("= "); @} |
691 |
; |
; |
692 |
|
|
693 |
decl : TYPENAME declarator ';' |
decl : TYPENAME declarator ';' |
694 |
@{ printf ("%s <declare> ", $1); @} |
@{ printf ("%s <declare> ", $1); @} |
695 |
| TYPENAME declarator '=' expr ';' |
| TYPENAME declarator '=' expr ';' |
696 |
@{ printf ("%s <init-declare> ", $1); @} |
@{ printf ("%s <init-declare> ", $1); @} |
713 |
parses as either an @code{expr} or a @code{stmt} |
parses as either an @code{expr} or a @code{stmt} |
714 |
(assuming that @samp{T} is recognized as a TYPENAME and @samp{x} as an ID). |
(assuming that @samp{T} is recognized as a TYPENAME and @samp{x} as an ID). |
715 |
Bison detects this as a reduce/reduce conflict between the rules |
Bison detects this as a reduce/reduce conflict between the rules |
716 |
@code{expr : ID} and @code{declarator : ID}, which it cannot resolve at the |
@code{expr : ID} and @code{declarator : ID}, which it cannot resolve at the |
717 |
time it encounters @code{x} in the example above. The two @code{%dprec} |
time it encounters @code{x} in the example above. The two @code{%dprec} |
718 |
declarations, however, give precedence to interpreting the example as a |
declarations, however, give precedence to interpreting the example as a |
719 |
@code{decl}, which implies that @code{x} is a declarator. |
@code{decl}, which implies that @code{x} is a declarator. |
720 |
The parser therefore prints |
The parser therefore prints |
721 |
|
|
722 |
@example |
@example |
723 |
"x" y z + T <init-declare> |
"x" y z + T <init-declare> |
724 |
@end example |
@end example |
725 |
|
|
726 |
Consider a different input string for this parser: |
Consider a different input string for this parser: |
740 |
then vanishes when it sees @code{+}, and the parser prints |
then vanishes when it sees @code{+}, and the parser prints |
741 |
|
|
742 |
@example |
@example |
743 |
x T <cast> y + |
x T <cast> y + |
744 |
@end example |
@end example |
745 |
|
|
746 |
Suppose that instead of resolving the ambiguity, you wanted to see all |
Suppose that instead of resolving the ambiguity, you wanted to see all |
783 |
as both an @code{expr} and a @code{decl}, and print |
as both an @code{expr} and a @code{decl}, and print |
784 |
|
|
785 |
@example |
@example |
786 |
"x" y z + T <init-declare> x T <cast> y z + = <OR> |
"x" y z + T <init-declare> x T <cast> y z + = <OR> |
787 |
@end example |
@end example |
788 |
|
|
789 |
|
|
4794 |
; |
; |
4795 |
@end example |
@end example |
4796 |
|
|
4797 |
@node Generalized LR Parsing |
@node Generalized LR Parsing |
4798 |
@section Generalized LR (GLR) Parsing |
@section Generalized LR (GLR) Parsing |
4799 |
@cindex GLR parsing |
@cindex GLR parsing |
4800 |
@cindex generalized LR (GLR) parsing |
@cindex generalized LR (GLR) parsing |
4801 |
@cindex ambiguous grammars |
@cindex ambiguous grammars |
4802 |
@cindex non-deterministic parsing |
@cindex non-deterministic parsing |
4803 |
|
|
4804 |
Bison produces @emph{deterministic} parsers that choose uniquely |
Bison produces @emph{deterministic} parsers that choose uniquely |
4805 |
when to reduce and which reduction to apply |
when to reduce and which reduction to apply |
4806 |
based on a summary of the preceding input and on one extra token of lookahead. |
based on a summary of the preceding input and on one extra token of lookahead. |
4807 |
As a result, normal Bison handles a proper subset of the family of |
As a result, normal Bison handles a proper subset of the family of |
4808 |
context-free languages. |
context-free languages. |
4809 |
Ambiguous grammars, since they have strings with more than one possible |
Ambiguous grammars, since they have strings with more than one possible |
4810 |
sequence of reductions cannot have deterministic parsers in this sense. |
sequence of reductions cannot have deterministic parsers in this sense. |
4811 |
The same is true of languages that require more than one symbol of |
The same is true of languages that require more than one symbol of |
4812 |
lookahead, since the parser lacks the information necessary to make a |
lookahead, since the parser lacks the information necessary to make a |
4813 |
decision at the point it must be made in a shift-reduce parser. |
decision at the point it must be made in a shift-reduce parser. |
4814 |
Finally, as previously mentioned (@pxref{Mystery Conflicts}), |
Finally, as previously mentioned (@pxref{Mystery Conflicts}), |
4815 |
there are languages where Bison's particular choice of how to |
there are languages where Bison's particular choice of how to |
4816 |
summarize the input seen so far loses necessary information. |
summarize the input seen so far loses necessary information. |
4817 |
|
|
4820 |
Generalized LR (or GLR). A Bison GLR parser uses the same basic |
Generalized LR (or GLR). A Bison GLR parser uses the same basic |
4821 |
algorithm for parsing as an ordinary Bison parser, but behaves |
algorithm for parsing as an ordinary Bison parser, but behaves |
4822 |
differently in cases where there is a shift-reduce conflict that has not |
differently in cases where there is a shift-reduce conflict that has not |
4823 |
been resolved by precedence rules (@pxref{Precedence}) or a |
been resolved by precedence rules (@pxref{Precedence}) or a |
4824 |
reduce-reduce conflict. When a GLR parser encounters such a situation, it |
reduce-reduce conflict. When a GLR parser encounters such a situation, it |
4825 |
effectively @emph{splits} into a several parsers, one for each possible |
effectively @emph{splits} into a several parsers, one for each possible |
4826 |
shift or reduction. These parsers then proceed as usual, consuming |
shift or reduction. These parsers then proceed as usual, consuming |
4827 |
tokens in lock-step. Some of the stacks may encounter other conflicts |
tokens in lock-step. Some of the stacks may encounter other conflicts |
4828 |
and split further, with the result that instead of a sequence of states, |
and split further, with the result that instead of a sequence of states, |
4829 |
a Bison GLR parsing stack is what is in effect a tree of states. |
a Bison GLR parsing stack is what is in effect a tree of states. |
4830 |
|
|
4831 |
In effect, each stack represents a guess as to what the proper parse |
In effect, each stack represents a guess as to what the proper parse |
4832 |
is. Additional input may indicate that a guess was wrong, in which case |
is. Additional input may indicate that a guess was wrong, in which case |
4833 |
the appropriate stack silently disappears. Otherwise, the semantics |
the appropriate stack silently disappears. Otherwise, the semantics |
4834 |
actions generated in each stack are saved, rather than being executed |
actions generated in each stack are saved, rather than being executed |
4835 |
immediately. When a stack disappears, its saved semantic actions never |
immediately. When a stack disappears, its saved semantic actions never |
4836 |
get executed. When a reduction causes two stacks to become equivalent, |
get executed. When a reduction causes two stacks to become equivalent, |
4837 |
their sets of semantic actions are both saved with the state that |
their sets of semantic actions are both saved with the state that |
4838 |
results from the reduction. We say that two stacks are equivalent |
results from the reduction. We say that two stacks are equivalent |
4839 |
when they both represent the same sequence of states, |
when they both represent the same sequence of states, |
4840 |
and each pair of corresponding states represents a |
and each pair of corresponding states represents a |
4841 |
grammar symbol that produces the same segment of the input token |
grammar symbol that produces the same segment of the input token |
4842 |
stream. |
stream. |
4848 |
values that are sets (actually multisets) of possible actions. The |
values that are sets (actually multisets) of possible actions. The |
4849 |
parser tries to pick one of the actions by first finding one whose rule |
parser tries to pick one of the actions by first finding one whose rule |
4850 |
has the highest dynamic precedence, as set by the @samp{%dprec} |
has the highest dynamic precedence, as set by the @samp{%dprec} |
4851 |
declaration. Otherwise, if the alternative actions are not ordered by |
declaration. Otherwise, if the alternative actions are not ordered by |
4852 |
precedence, but there the same merging function is declared for both |
precedence, but there the same merging function is declared for both |
4853 |
rules by the @samp{%merge} declaration, |
rules by the @samp{%merge} declaration, |
4854 |
Bison resolves and evaluates both and then calls the merge function on |
Bison resolves and evaluates both and then calls the merge function on |
4855 |
the result. Otherwise, it reports an ambiguity. |
the result. Otherwise, it reports an ambiguity. |
4856 |
|
|
4857 |
It is possible to use a data structure for the GLR parsing tree that |
It is possible to use a data structure for the GLR parsing tree that |
4858 |
permits the processing of any LALR(1) grammar in linear time (in the |
permits the processing of any LALR(1) grammar in linear time (in the |
4859 |
size of the input), any unambiguous (not necessarily LALR(1)) grammar in |
size of the input), any unambiguous (not necessarily LALR(1)) grammar in |
4860 |
quadratic worst-case time, and any general (possibly ambiguous) |
quadratic worst-case time, and any general (possibly ambiguous) |
4861 |
context-free grammar in cubic worst-case time. However, Bison currently |
context-free grammar in cubic worst-case time. However, Bison currently |
4862 |
uses a simpler data structure that requires time proportional to the |
uses a simpler data structure that requires time proportional to the |
4863 |
length of the input times the maximum number of stacks required for any |
length of the input times the maximum number of stacks required for any |
6158 |
Bison declaration to create a header file meant for the scanner. |
Bison declaration to create a header file meant for the scanner. |
6159 |
@xref{Decl Summary}. |
@xref{Decl Summary}. |
6160 |
|
|
6161 |
@item %dprec |
@item %dprec |
6162 |
Bison declaration to assign a precedence to a rule that is used at parse |
Bison declaration to assign a precedence to a rule that is used at parse |
6163 |
time to resolve reduce/reduce conflicts. @xref{GLR Parsers}. |
time to resolve reduce/reduce conflicts. @xref{GLR Parsers}. |
6164 |
|
|
6183 |
|
|
6184 |
@item %merge |
@item %merge |
6185 |
Bison declaration to assign a merging function to a rule. If there is a |
Bison declaration to assign a merging function to a rule. If there is a |
6186 |
reduce/reduce conflict with a rule having the same merging function, the |
reduce/reduce conflict with a rule having the same merging function, the |
6187 |
function is applied to the two semantic values to get a single result. |
function is applied to the two semantic values to get a single result. |
6188 |
@xref{GLR Parsers}. |
@xref{GLR Parsers}. |
6189 |
|
|
6301 |
|
|
6302 |
@item Generalized LR (GLR) |
@item Generalized LR (GLR) |
6303 |
A parsing algorithm that can handle all context-free grammars, including those |
A parsing algorithm that can handle all context-free grammars, including those |
6304 |
that are not LALR(1). It resolves situations that Bison's usual LALR(1) |
that are not LALR(1). It resolves situations that Bison's usual LALR(1) |
6305 |
algorithm cannot by effectively splitting off multiple parsers, trying all |
algorithm cannot by effectively splitting off multiple parsers, trying all |
6306 |
possible parsers, and discarding those that fail in the light of additional |
possible parsers, and discarding those that fail in the light of additional |
6307 |
right context. @xref{Generalized LR Parsing, ,Generalized LR Parsing}. |
right context. @xref{Generalized LR Parsing, ,Generalized LR Parsing}. |