* doc/bison.texinfo: Formatting changes.

This commit is contained in:
Akim Demaille
2001-12-29 14:25:45 +00:00
parent 091e20bbee
commit 704a47c475
3 changed files with 134 additions and 97 deletions

View File

@@ -1,3 +1,7 @@
2001-12-29 Akim Demaille <akim@epita.fr>
* doc/bison.texinfo: Formatting changes.
2001-12-29 Akim Demaille <akim@epita.fr> 2001-12-29 Akim Demaille <akim@epita.fr>
Don't store the token defs in a muscle, just be ready to output it Don't store the token defs in a muscle, just be ready to output it

41
TODO
View File

@@ -2,20 +2,37 @@
* Prologue * Prologue
The %union is declared after the user C declarations. It can be The %union is declared after the user C declarations. It can be
a problem if YYSTYPE is decalred after the user part. [] a problem if YYSTYPE is declared after the user part. []
* --verbose Actually, the real problem seems that the %union ought to be output
Tell the truth about EOF. [] where it was defined. For instance, in gettext/intl/plural.y, we
have:
%{
...
#include "gettextP.h"
...
%}
%union {
unsigned long int num;
enum operator op;
struct expression *exp;
}
%{
...
static int yylex PARAMS ((YYSTYPE *lval, const char **pexp));
...
%}
Where the first part defines struct expression, the second uses it to
define YYSTYPE, and the last uses YYSTYPE. Only this order is valid.
* --graph * --graph
Show reductions. [] Show reductions. []
* tokendefs * Broken options ?
This muscle should not exist: the information it contains should be
available from the rest of bison. Once the information public, get
rid of it. []
* Broken options ?.
** %no-lines [ok] ** %no-lines [ok]
** %no-parser [] ** %no-parser []
** %pure-parser [] ** %pure-parser []
@@ -33,9 +50,6 @@ Must we keep %no-parser?
%token-table? %token-table?
*** New skeletons. [] *** New skeletons. []
* src/macrotab.[ch]
Removing warnings when compiling. (gcc-warnings). [ok]
* src/print_graph.c * src/print_graph.c
Find the best graph parameters. [] Find the best graph parameters. []
@@ -46,7 +60,6 @@ informations about ERROR_VERBOSE. []
skeleton muscles. [] skeleton muscles. []
%skeleton. [] %skeleton. []
* testsuite. * testsuite
** tests/reduce.at [ok]
** tests/pure-parser.at [] ** tests/pure-parser.at []
New tests. New tests.

View File

@@ -676,12 +676,13 @@ the grammar rules---for example, to build identifiers and operators into
expressions. As it does this, it runs the actions for the grammar rules it expressions. As it does this, it runs the actions for the grammar rules it
uses. uses.
The tokens come from a function called the @dfn{lexical analyzer} that you The tokens come from a function called the @dfn{lexical analyzer} that
must supply in some fashion (such as by writing it in C). The Bison parser you must supply in some fashion (such as by writing it in C). The Bison
calls the lexical analyzer each time it wants a new token. It doesn't know parser calls the lexical analyzer each time it wants a new token. It
what is ``inside'' the tokens (though their semantic values may reflect doesn't know what is ``inside'' the tokens (though their semantic values
this). Typically the lexical analyzer makes the tokens by parsing may reflect this). Typically the lexical analyzer makes the tokens by
characters of text, but Bison does not depend on this. @xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}. parsing characters of text, but Bison does not depend on this.
@xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}.
The Bison parser file is C code which defines a function named The Bison parser file is C code which defines a function named
@code{yyparse} which implements that grammar. This function does not make @code{yyparse} which implements that grammar. This function does not make
@@ -722,15 +723,16 @@ to a working compiler or interpreter, has these parts:
@enumerate @enumerate
@item @item
Formally specify the grammar in a form recognized by Bison Formally specify the grammar in a form recognized by Bison
(@pxref{Grammar File, ,Bison Grammar Files}). For each grammatical rule in the language, (@pxref{Grammar File, ,Bison Grammar Files}). For each grammatical rule
describe the action that is to be taken when an instance of that rule in the language, describe the action that is to be taken when an
is recognized. The action is described by a sequence of C statements. instance of that rule is recognized. The action is described by a
sequence of C statements.
@item @item
Write a lexical analyzer to process input and pass tokens to the Write a lexical analyzer to process input and pass tokens to the parser.
parser. The lexical analyzer may be written by hand in C The lexical analyzer may be written by hand in C (@pxref{Lexical, ,The
(@pxref{Lexical, ,The Lexical Analyzer Function @code{yylex}}). It could also be produced using Lex, but the use Lexical Analyzer Function @code{yylex}}). It could also be produced
of Lex is not discussed in this manual. using Lex, but the use of Lex is not discussed in this manual.
@item @item
Write a controlling function that calls the Bison-produced parser. Write a controlling function that calls the Bison-produced parser.
@@ -884,9 +886,10 @@ which is a floating point number.
The @code{#include} directive is used to declare the exponentiation The @code{#include} directive is used to declare the exponentiation
function @code{pow}. function @code{pow}.
The second section, Bison declarations, provides information to Bison about The second section, Bison declarations, provides information to Bison
the token types (@pxref{Bison Declarations, ,The Bison Declarations Section}). Each terminal symbol that is about the token types (@pxref{Bison Declarations, ,The Bison
not a single-character literal must be declared here. (Single-character Declarations Section}). Each terminal symbol that is not a
single-character literal must be declared here. (Single-character
literals normally don't need to be declared.) In this example, all the literals normally don't need to be declared.) In this example, all the
arithmetic operators are designated by single-character literals, so the arithmetic operators are designated by single-character literals, so the
only terminal symbol that needs to be declared is @code{NUM}, the token only terminal symbol that needs to be declared is @code{NUM}, the token
@@ -1066,9 +1069,10 @@ The latter, however, is much more readable.
@cindex writing a lexical analyzer @cindex writing a lexical analyzer
@cindex lexical analyzer, writing @cindex lexical analyzer, writing
The lexical analyzer's job is low-level parsing: converting characters or The lexical analyzer's job is low-level parsing: converting characters
sequences of characters into tokens. The Bison parser gets its tokens by or sequences of characters into tokens. The Bison parser gets its
calling the lexical analyzer. @xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}. tokens by calling the lexical analyzer. @xref{Lexical, ,The Lexical
Analyzer Function @code{yylex}}.
Only a simple lexical analyzer is needed for the RPN calculator. This Only a simple lexical analyzer is needed for the RPN calculator. This
lexical analyzer skips blanks and tabs, then reads in numbers as lexical analyzer skips blanks and tabs, then reads in numbers as
@@ -1325,12 +1329,14 @@ Operator precedence is determined by the line ordering of the
declarations; the higher the line number of the declaration (lower on declarations; the higher the line number of the declaration (lower on
the page or screen), the higher the precedence. Hence, exponentiation the page or screen), the higher the precedence. Hence, exponentiation
has the highest precedence, unary minus (@code{NEG}) is next, followed has the highest precedence, unary minus (@code{NEG}) is next, followed
by @samp{*} and @samp{/}, and so on. @xref{Precedence, ,Operator Precedence}. by @samp{*} and @samp{/}, and so on. @xref{Precedence, ,Operator
Precedence}.
The other important new feature is the @code{%prec} in the grammar section The other important new feature is the @code{%prec} in the grammar
for the unary minus operator. The @code{%prec} simply instructs Bison that section for the unary minus operator. The @code{%prec} simply instructs
the rule @samp{| '-' exp} has the same precedence as @code{NEG}---in this Bison that the rule @samp{| '-' exp} has the same precedence as
case the next-to-highest. @xref{Contextual Precedence, ,Context-Dependent Precedence}. @code{NEG}---in this case the next-to-highest. @xref{Contextual
Precedence, ,Context-Dependent Precedence}.
Here is a sample run of @file{calc.y}: Here is a sample run of @file{calc.y}:
@@ -1683,11 +1689,12 @@ are @code{NUM}, @code{VAR}, @code{FNCT}, and @code{exp}. Their
declarations are augmented with information about their data type (placed declarations are augmented with information about their data type (placed
between angle brackets). between angle brackets).
The Bison construct @code{%type} is used for declaring nonterminal symbols, The Bison construct @code{%type} is used for declaring nonterminal
just as @code{%token} is used for declaring token types. We have not used symbols, just as @code{%token} is used for declaring token types. We
@code{%type} before because nonterminal symbols are normally declared have not used @code{%type} before because nonterminal symbols are
implicitly by the rules that define them. But @code{exp} must be declared normally declared implicitly by the rules that define them. But
explicitly so we can specify its value type. @xref{Type Decl, ,Nonterminal Symbols}. @code{exp} must be declared explicitly so we can specify its value type.
@xref{Type Decl, ,Nonterminal Symbols}.
@node Mfcalc Rules @node Mfcalc Rules
@subsection Grammar Rules for @code{mfcalc} @subsection Grammar Rules for @code{mfcalc}
@@ -1961,8 +1968,8 @@ yylex (void)
@end smallexample @end smallexample
This program is both powerful and flexible. You may easily add new This program is both powerful and flexible. You may easily add new
functions, and it is a simple job to modify this code to install predefined functions, and it is a simple job to modify this code to install
variables such as @code{pi} or @code{e} as well. predefined variables such as @code{pi} or @code{e} as well.
@node Exercises @node Exercises
@section Exercises @section Exercises
@@ -2425,7 +2432,8 @@ requires you to do two things:
@itemize @bullet @itemize @bullet
@item @item
Specify the entire collection of possible data types, with the Specify the entire collection of possible data types, with the
@code{%union} Bison declaration (@pxref{Union Decl, ,The Collection of Value Types}). @code{%union} Bison declaration (@pxref{Union Decl, ,The Collection of
Value Types}).
@item @item
Choose one of those types for each symbol (terminal or nonterminal) for Choose one of those types for each symbol (terminal or nonterminal) for
@@ -2447,10 +2455,11 @@ is to compute a semantic value for the grouping built by the rule from the
semantic values associated with tokens or smaller groupings. semantic values associated with tokens or smaller groupings.
An action consists of C statements surrounded by braces, much like a An action consists of C statements surrounded by braces, much like a
compound statement in C. It can be placed at any position in the rule; it compound statement in C. It can be placed at any position in the rule;
is executed at that position. Most rules have just one action at the end it is executed at that position. Most rules have just one action at the
of the rule, following all the components. Actions in the middle of a rule end of the rule, following all the components. Actions in the middle of
are tricky and used only for special purposes (@pxref{Mid-Rule Actions, ,Actions in Mid-Rule}). a rule are tricky and used only for special purposes (@pxref{Mid-Rule
Actions, ,Actions in Mid-Rule}).
The C code in an action can refer to the semantic values of the components The C code in an action can refer to the semantic values of the components
matched by the rule with the construct @code{$@var{n}}, which stands for matched by the rule with the construct @code{$@var{n}}, which stands for
@@ -2730,8 +2739,8 @@ especially symbol locations.
@c (terminal or not) ? @c (terminal or not) ?
The way locations are handled is defined by providing a data type, and actions The way locations are handled is defined by providing a data type, and
to take when rules are matched. actions to take when rules are matched.
@menu @menu
* Location Type:: Specifying a data type for locations. * Location Type:: Specifying a data type for locations.
@@ -2832,11 +2841,11 @@ exp: @dots{}
@subsection Default Action for Locations @subsection Default Action for Locations
@vindex YYLLOC_DEFAULT @vindex YYLLOC_DEFAULT
Actually, actions are not the best place to compute locations. Since locations Actually, actions are not the best place to compute locations. Since
are much more general than semantic values, there is room in the output parser locations are much more general than semantic values, there is room in
to redefine the default action to take for each rule. The the output parser to redefine the default action to take for each
@code{YYLLOC_DEFAULT} macro is called each time a rule is matched, before the rule. The @code{YYLLOC_DEFAULT} macro is invoked each time a rule is
associated action is run. matched, before the associated action is run.
Most of the time, this macro is general enough to suppress location Most of the time, this macro is general enough to suppress location
dedicated code from semantic actions. dedicated code from semantic actions.
@@ -2888,7 +2897,8 @@ value (@pxref{Multiple Types, ,More Than One Value Type}).
The first rule in the file also specifies the start symbol, by default. The first rule in the file also specifies the start symbol, by default.
If you want some other symbol to be the start symbol, you must declare If you want some other symbol to be the start symbol, you must declare
it explicitly (@pxref{Language and Grammar, ,Languages and Context-Free Grammars}). it explicitly (@pxref{Language and Grammar, ,Languages and Context-Free
Grammars}).
@menu @menu
* Token Decl:: Declaring terminal symbols. * Token Decl:: Declaring terminal symbols.
@@ -2937,7 +2947,8 @@ with each other or with ASCII characters.
In the event that the stack type is a union, you must augment the In the event that the stack type is a union, you must augment the
@code{%token} or other token declaration to include the data type @code{%token} or other token declaration to include the data type
alternative delimited by angle-brackets (@pxref{Multiple Types, ,More Than One Value Type}). alternative delimited by angle-brackets (@pxref{Multiple Types, ,More
Than One Value Type}).
For example: For example:
@@ -2984,7 +2995,8 @@ obtain the token type code number (@pxref{Calling Convention}).
Use the @code{%left}, @code{%right} or @code{%nonassoc} declaration to Use the @code{%left}, @code{%right} or @code{%nonassoc} declaration to
declare a token and specify its precedence and associativity, all at declare a token and specify its precedence and associativity, all at
once. These are called @dfn{precedence declarations}. once. These are called @dfn{precedence declarations}.
@xref{Precedence, ,Operator Precedence}, for general information on operator precedence. @xref{Precedence, ,Operator Precedence}, for general information on
operator precedence.
The syntax of a precedence declaration is the same as that of The syntax of a precedence declaration is the same as that of
@code{%token}: either @code{%token}: either
@@ -3071,11 +3083,12 @@ used. This is done with a @code{%type} declaration, like this:
@end example @end example
@noindent @noindent
Here @var{nonterminal} is the name of a nonterminal symbol, and @var{type} Here @var{nonterminal} is the name of a nonterminal symbol, and
is the name given in the @code{%union} to the alternative that you want @var{type} is the name given in the @code{%union} to the alternative
(@pxref{Union Decl, ,The Collection of Value Types}). You can give any number of nonterminal symbols in that you want (@pxref{Union Decl, ,The Collection of Value Types}). You
the same @code{%type} declaration, if they have the same value type. Use can give any number of nonterminal symbols in the same @code{%type}
spaces to separate the symbol names. declaration, if they have the same value type. Use spaces to separate
the symbol names.
You can also declare the value type of a terminal symbol. To do this, You can also declare the value type of a terminal symbol. To do this,
use the same @code{<@var{type}>} construction in a declaration for the use the same @code{<@var{type}>} construction in a declaration for the
@@ -3378,10 +3391,10 @@ language with the same program? Then you need to avoid a name conflict
between different definitions of @code{yyparse}, @code{yylval}, and so on. between different definitions of @code{yyparse}, @code{yylval}, and so on.
The easy way to do this is to use the option @samp{-p @var{prefix}} The easy way to do this is to use the option @samp{-p @var{prefix}}
(@pxref{Invocation, ,Invoking Bison}). This renames the interface functions and (@pxref{Invocation, ,Invoking Bison}). This renames the interface
variables of the Bison parser to start with @var{prefix} instead of functions and variables of the Bison parser to start with @var{prefix}
@samp{yy}. You can use this to give each parser distinct names that do instead of @samp{yy}. You can use this to give each parser distinct
not conflict. names that do not conflict.
The precise list of symbols renamed is @code{yyparse}, @code{yylex}, The precise list of symbols renamed is @code{yyparse}, @code{yylex},
@code{yyerror}, @code{yynerrs}, @code{yylval}, @code{yychar} and @code{yyerror}, @code{yynerrs}, @code{yylval}, @code{yychar} and
@@ -3575,9 +3588,10 @@ Thus, if the type is @code{int} (the default), you might write this in
@end example @end example
When you are using multiple data types, @code{yylval}'s type is a union When you are using multiple data types, @code{yylval}'s type is a union
made from the @code{%union} declaration (@pxref{Union Decl, ,The Collection of Value Types}). So when made from the @code{%union} declaration (@pxref{Union Decl, ,The
you store a token's value, you must use the proper member of the union. Collection of Value Types}). So when you store a token's value, you
If the @code{%union} declaration looks like this: must use the proper member of the union. If the @code{%union}
declaration looks like this:
@example @example
@group @group
@@ -3786,8 +3800,8 @@ immediately return 1.
@vindex yynerrs @vindex yynerrs
The variable @code{yynerrs} contains the number of syntax errors The variable @code{yynerrs} contains the number of syntax errors
encountered so far. Normally this variable is global; but if you encountered so far. Normally this variable is global; but if you
request a pure parser (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}) then it is a local variable request a pure parser (@pxref{Pure Decl, ,A Pure (Reentrant) Parser})
which only the actions can access. then it is a local variable which only the actions can access.
@node Action Features @node Action Features
@section Special Features for Use in Actions @section Special Features for Use in Actions
@@ -3808,7 +3822,8 @@ Acts like a variable that contains the semantic value for the
@item $<@var{typealt}>$ @item $<@var{typealt}>$
Like @code{$$} but specifies alternative @var{typealt} in the union Like @code{$$} but specifies alternative @var{typealt} in the union
specified by the @code{%union} declaration. @xref{Action Types, ,Data Types of Values in Actions}. specified by the @code{%union} declaration. @xref{Action Types, ,Data
Types of Values in Actions}.
@item $<@var{typealt}>@var{n} @item $<@var{typealt}>@var{n}
Like @code{$@var{n}} but specifies alternative @var{typealt} in the Like @code{$@var{n}} but specifies alternative @var{typealt} in the
@@ -4237,18 +4252,19 @@ and therefore are represented by names, not character literals.)
The first effect of the precedence declarations is to assign precedence The first effect of the precedence declarations is to assign precedence
levels to the terminal symbols declared. The second effect is to assign levels to the terminal symbols declared. The second effect is to assign
precedence levels to certain rules: each rule gets its precedence from the precedence levels to certain rules: each rule gets its precedence from
last terminal symbol mentioned in the components. (You can also specify the last terminal symbol mentioned in the components. (You can also
explicitly the precedence of a rule. @xref{Contextual Precedence, ,Context-Dependent Precedence}.) specify explicitly the precedence of a rule. @xref{Contextual
Precedence, ,Context-Dependent Precedence}.)
Finally, the resolution of conflicts works by comparing the Finally, the resolution of conflicts works by comparing the precedence
precedence of the rule being considered with that of the of the rule being considered with that of the look-ahead token. If the
look-ahead token. If the token's precedence is higher, the token's precedence is higher, the choice is to shift. If the rule's
choice is to shift. If the rule's precedence is higher, the precedence is higher, the choice is to reduce. If they have equal
choice is to reduce. If they have equal precedence, the choice precedence, the choice is made based on the associativity of that
is made based on the associativity of that precedence level. The precedence level. The verbose output file made by @samp{-v}
verbose output file made by @samp{-v} (@pxref{Invocation, ,Invoking Bison}) says (@pxref{Invocation, ,Invoking Bison}) says how each conflict was
how each conflict was resolved. resolved.
Not all rules and not all tokens have precedence. If either the rule or Not all rules and not all tokens have precedence. If either the rule or
the look-ahead token has no precedence, then the default is to shift. the look-ahead token has no precedence, then the default is to shift.
@@ -4966,13 +4982,14 @@ of the state stack afterward.
@end itemize @end itemize
To make sense of this information, it helps to refer to the listing file To make sense of this information, it helps to refer to the listing file
produced by the Bison @samp{-v} option (@pxref{Invocation, ,Invoking Bison}). This file produced by the Bison @samp{-v} option (@pxref{Invocation, ,Invoking
shows the meaning of each state in terms of positions in various rules, and Bison}). This file shows the meaning of each state in terms of
also what each state will do with each possible input token. As you read positions in various rules, and also what each state will do with each
the successive trace messages, you can see that the parser is functioning possible input token. As you read the successive trace messages, you
according to its specification in the listing file. Eventually you will can see that the parser is functioning according to its specification in
arrive at the place where something undesirable happens, and you will see the listing file. Eventually you will arrive at the place where
which parts of the grammar are to blame. something undesirable happens, and you will see which parts of the
grammar are to blame.
The parser file is a C program and you can use C debuggers on it, but it's The parser file is a C program and you can use C debuggers on it, but it's
not easy to interpret what it is doing. The parser function is a not easy to interpret what it is doing. The parser function is a
@@ -5378,8 +5395,9 @@ containing an error message. @xref{Error Reporting, ,The Error
Reporting Function @code{yyerror}}. Reporting Function @code{yyerror}}.
@item yylex @item yylex
User-supplied lexical analyzer function, called with no arguments User-supplied lexical analyzer function, called with no arguments to get
to get the next token. @xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}. the next token. @xref{Lexical, ,The Lexical Analyzer Function
@code{yylex}}.
@item yylval @item yylval
External variable in which @code{yylex} should place the semantic External variable in which @code{yylex} should place the semantic
@@ -5455,7 +5473,8 @@ Bison declaration to assign right associativity to token(s).
@xref{Precedence Decl, ,Operator Precedence}. @xref{Precedence Decl, ,Operator Precedence}.
@item %start @item %start
Bison declaration to specify the start symbol. @xref{Start Decl, ,The Start-Symbol}. Bison declaration to specify the start symbol. @xref{Start Decl, ,The
Start-Symbol}.
@item %token @item %token
Bison declaration to declare token(s) without specifying precedence. Bison declaration to declare token(s) without specifying precedence.
@@ -5466,7 +5485,8 @@ Bison declaration to include a token name table in the parser file.
@xref{Decl Summary}. @xref{Decl Summary}.
@item %type @item %type
Bison declaration to declare nonterminals. @xref{Type Decl, ,Nonterminal Symbols}. Bison declaration to declare nonterminals. @xref{Type Decl,
,Nonterminal Symbols}.
@item %union @item %union
Bison declaration to specify several possible data types for semantic Bison declaration to specify several possible data types for semantic