/[bison]/bison/doc/bison.texinfo
ViewVC logotype

Diff of /bison/doc/bison.texinfo

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.38 by marc, Thu Sep 20 18:24:27 2001 UTC revision 1.39 by akim, Thu Oct 4 14:55:20 2001 UTC
# Line 129  Cover art by Etienne Suvasa. Line 129  Cover art by Etienne Suvasa.
129    
130  @contents  @contents
131    
132  @node Top, Introduction, (dir), (dir)  @ifnottex
133    @node Top
134  @ifinfo  @top Bison
135  This manual documents version @value{VERSION} of Bison.  
136  @end ifinfo  This manual documents version @value{VERSION} of Bison, updated
137    @value{UPDATED}.
138    @end ifnottex
139    
140  @menu  @menu
141  * Introduction::  * Introduction::
# Line 159  Reference sections: Line 161  Reference sections:
161  * Copying This Manual::  License for copying this manual.  * Copying This Manual::  License for copying this manual.
162  * Index::             Cross-references to the text.  * Index::             Cross-references to the text.
163    
164   --- The Detailed Node Listing ---  @detailmenu --- The Detailed Node Listing ---
165    
166  The Concepts of Bison  The Concepts of Bison
167    
# Line 182  Examples Line 184  Examples
184  * Infix Calc::        Infix (algebraic) notation calculator.  * Infix Calc::        Infix (algebraic) notation calculator.
185                          Operator precedence is introduced.                          Operator precedence is introduced.
186  * Simple Error Recovery::  Continuing after syntax errors.  * Simple Error Recovery::  Continuing after syntax errors.
187    * Location Tracking Calc:: Demonstrating the use of @@@var{n} and @@$.
188  * Multi-function Calc::    Calculator with memory and trig functions.  * Multi-function Calc::    Calculator with memory and trig functions.
189                          It uses multiple data-types for semantic values.                          It uses multiple data-types for semantic values.
190  * Exercises::         Ideas for improving the multi-function calculator.  * Exercises::         Ideas for improving the multi-function calculator.
# Line 202  Grammar Rules for @code{rpcalc} Line 205  Grammar Rules for @code{rpcalc}
205  * Rpcalc Line::  * Rpcalc Line::
206  * Rpcalc Expr::  * Rpcalc Expr::
207    
208    Location Tracking Calculator: @code{ltcalc}
209    
210    * Decls: Ltcalc Decls.  Bison and C declarations for ltcalc.
211    * Rules: Ltcalc Rules.  Grammar rules for ltcalc, with explanations.
212    * Lexer: Ltcalc Lexer.  The lexical analyzer.
213    
214  Multi-Function Calculator: @code{mfcalc}  Multi-Function Calculator: @code{mfcalc}
215    
216  * Decl: Mfcalc Decl.      Bison declarations for multi-function calculator.  * Decl: Mfcalc Decl.      Bison declarations for multi-function calculator.
# Line 301  Copying This Manual Line 310  Copying This Manual
310    
311  * GNU Free Documentation License::  License for copying this manual.  * GNU Free Documentation License::  License for copying this manual.
312    
313    @end detailmenu
314  @end menu  @end menu
315    
316  @node Introduction, Conditions, Top, Top  @node Introduction
317  @unnumbered Introduction  @unnumbered Introduction
318  @cindex introduction  @cindex introduction
319    
# Line 329  multi-character string literals and othe Line 339  multi-character string literals and othe
339    
340  This edition corresponds to version @value{VERSION} of Bison.  This edition corresponds to version @value{VERSION} of Bison.
341    
342  @node Conditions, Copying, Introduction, Top  @node Conditions
343  @unnumbered Conditions for Using Bison  @unnumbered Conditions for Using Bison
344    
345  As of Bison version 1.24, we have changed the distribution terms for  As of Bison version 1.24, we have changed the distribution terms for
# Line 359  using the other GNU tools. Line 369  using the other GNU tools.
369    
370  @include gpl.texi  @include gpl.texi
371    
372  @node Concepts, Examples, Copying, Top  @node Concepts
373  @chapter The Concepts of Bison  @chapter The Concepts of Bison
374    
375  This chapter introduces many of the basic concepts without which the  This chapter introduces many of the basic concepts without which the
# Line 381  use Bison or Yacc, we suggest you start Line 391  use Bison or Yacc, we suggest you start
391  * Grammar Layout::    Overall structure of a Bison grammar file.  * Grammar Layout::    Overall structure of a Bison grammar file.
392  @end menu  @end menu
393    
394  @node Language and Grammar, Grammar in Bison,  , Concepts  @node Language and Grammar
395  @section Languages and Context-Free Grammars  @section Languages and Context-Free Grammars
396    
397  @cindex context-free grammar  @cindex context-free grammar
# Line 490  the grammar's start symbol.  If we use a Line 500  the grammar's start symbol.  If we use a
500  must be a `sequence of definitions and declarations'.  If not, the parser  must be a `sequence of definitions and declarations'.  If not, the parser
501  reports a syntax error.  reports a syntax error.
502    
503  @node Grammar in Bison, Semantic Values, Language and Grammar, Concepts  @node Grammar in Bison
504  @section From Formal Rules to Bison Input  @section From Formal Rules to Bison Input
505  @cindex Bison grammar  @cindex Bison grammar
506  @cindex grammar, Bison  @cindex grammar, Bison
# Line 535  stmt:   RETURN expr ';' Line 545  stmt:   RETURN expr ';'
545  @noindent  @noindent
546  @xref{Rules, ,Syntax of Grammar Rules}.  @xref{Rules, ,Syntax of Grammar Rules}.
547    
548  @node Semantic Values, Semantic Actions, Grammar in Bison, Concepts  @node Semantic Values
549  @section Semantic Values  @section Semantic Values
550  @cindex semantic value  @cindex semantic value
551  @cindex value, semantic  @cindex value, semantic
# Line 577  semantic value that is a number.  In a c Line 587  semantic value that is a number.  In a c
587  language, an expression typically has a semantic value that is a tree  language, an expression typically has a semantic value that is a tree
588  structure describing the meaning of the expression.  structure describing the meaning of the expression.
589    
590  @node Semantic Actions, Locations Overview, Semantic Values, Concepts  @node Semantic Actions
591  @section Semantic Actions  @section Semantic Actions
592  @cindex semantic actions  @cindex semantic actions
593  @cindex actions, semantic  @cindex actions, semantic
# Line 608  expr: expr '+' expr   @{ $$ = $1 + $3; @ Line 618  expr: expr '+' expr   @{ $$ = $1 + $3; @
618  The action says how to produce the semantic value of the sum expression  The action says how to produce the semantic value of the sum expression
619  from the values of the two subexpressions.  from the values of the two subexpressions.
620    
621  @node Locations Overview, Bison Parser, Semantic Actions, Concepts  @node Locations Overview
622  @section Locations  @section Locations
623  @cindex location  @cindex location
624  @cindex textual position  @cindex textual position
# Line 637  $} should be formed. Wh Line 647  $} should be formed. Wh
647  grouping, the default behavior of the output parser is to take the beginning  grouping, the default behavior of the output parser is to take the beginning
648  of the first symbol, and the end of the last symbol.  of the first symbol, and the end of the last symbol.
649    
650  @node Bison Parser, Stages, Locations Overview, Concepts  @node Bison Parser
651  @section Bison Output: the Parser File  @section Bison Output: the Parser File
652  @cindex Bison parser  @cindex Bison parser
653  @cindex Bison utility  @cindex Bison utility
# Line 682  Therefore, you should avoid using C iden Line 692  Therefore, you should avoid using C iden
692  or @samp{YY} in the Bison grammar file except for the ones defined in  or @samp{YY} in the Bison grammar file except for the ones defined in
693  this manual.  this manual.
694    
695  @node Stages, Grammar Layout, Bison Parser, Concepts  @node Stages
696  @section Stages in Using Bison  @section Stages in Using Bison
697  @cindex stages in using Bison  @cindex stages in using Bison
698  @cindex using Bison  @cindex using Bison
# Line 724  Compile the code output by Bison, as wel Line 734  Compile the code output by Bison, as wel
734  Link the object files to produce the finished product.  Link the object files to produce the finished product.
735  @end enumerate  @end enumerate
736    
737  @node Grammar Layout,  , Stages, Concepts  @node Grammar Layout
738  @section The Overall Layout of a Bison Grammar  @section The Overall Layout of a Bison Grammar
739  @cindex grammar file  @cindex grammar file
740  @cindex file format  @cindex file format
# Line 751  general form of a Bison grammar file is Line 761  general form of a Bison grammar file is
761  The @samp{%%}, @samp{%@{} and @samp{%@}} are punctuation that appears  The @samp{%%}, @samp{%@{} and @samp{%@}} are punctuation that appears
762  in every Bison grammar file to separate the sections.  in every Bison grammar file to separate the sections.
763    
764  The prologue may define types and variables used in the actions. You can  The prologue may define types and variables used in the actions. You can
765  also use preprocessor commands to define macros used there, and use  also use preprocessor commands to define macros used there, and use
766  @code{#include} to include header files that do any of these things.  @code{#include} to include header files that do any of these things.
767    
768  The Bison declarations declare the names of the terminal and nonterminal  The Bison declarations declare the names of the terminal and nonterminal
# Line 762  semantic values of various symbols. Line 772  semantic values of various symbols.
772  The grammar rules define how to construct each nonterminal symbol from its  The grammar rules define how to construct each nonterminal symbol from its
773  parts.  parts.
774    
775  The epilogue can contain any code you want to use. Often the definition of  The epilogue can contain any code you want to use. Often the definition of
776  the lexical analyzer @code{yylex} goes here, plus subroutines called by the  the lexical analyzer @code{yylex} goes here, plus subroutines called by the
777  actions in the grammar rules.  In a simple program, all the rest of the  actions in the grammar rules.  In a simple program, all the rest of the
778  program can go here.  program can go here.
779    
780  @node Examples, Grammar File, Concepts, Top  @node Examples
781  @chapter Examples  @chapter Examples
782  @cindex simple examples  @cindex simple examples
783  @cindex examples, simple  @cindex examples, simple
# Line 791  to try them. Line 801  to try them.
801  * Infix Calc::        Infix (algebraic) notation calculator.  * Infix Calc::        Infix (algebraic) notation calculator.
802                          Operator precedence is introduced.                          Operator precedence is introduced.
803  * Simple Error Recovery::  Continuing after syntax errors.  * Simple Error Recovery::  Continuing after syntax errors.
804    * Location Tracking Calc:: Demonstrating the use of @@@var{n} and @@$.
805  * Multi-function Calc::  Calculator with memory and trig functions.  * Multi-function Calc::  Calculator with memory and trig functions.
806                             It uses multiple data-types for semantic values.                             It uses multiple data-types for semantic values.
807  * Exercises::         Ideas for improving the multi-function calculator.  * Exercises::         Ideas for improving the multi-function calculator.
808  @end menu  @end menu
809    
810  @node RPN Calc, Infix Calc,  , Examples  @node RPN Calc
811  @section Reverse Polish Notation Calculator  @section Reverse Polish Notation Calculator
812  @cindex reverse polish notation  @cindex reverse polish notation
813  @cindex polish notation calculator  @cindex polish notation calculator
# Line 821  The source code for this calculator is n Line 832  The source code for this calculator is n
832  * Comp: Rpcalc Compile. Run the C compiler on the output code.  * Comp: Rpcalc Compile. Run the C compiler on the output code.
833  @end menu  @end menu
834    
835  @node Rpcalc Decls, Rpcalc Rules,  , RPN Calc  @node Rpcalc Decls
836  @subsection Declarations for @code{rpcalc}  @subsection Declarations for @code{rpcalc}
837    
838  Here are the C and Bison declarations for the reverse polish notation  Here are the C and Bison declarations for the reverse polish notation
# Line 861  arithmetic operators are designated by s Line 872  arithmetic operators are designated by s
872  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
873  type for numeric constants.  type for numeric constants.
874    
875  @node Rpcalc Rules, Rpcalc Lexer, Rpcalc Decls, RPN Calc  @node Rpcalc Rules
876  @subsection Grammar Rules for @code{rpcalc}  @subsection Grammar Rules for @code{rpcalc}
877    
878  Here are the grammar rules for the reverse polish notation calculator.  Here are the grammar rules for the reverse polish notation calculator.
# Line 912  rule are referred to as @code{$1}, @code Line 923  rule are referred to as @code{$1}, @code
923  * Rpcalc Expr::  * Rpcalc Expr::
924  @end menu  @end menu
925    
926  @node Rpcalc Input, Rpcalc Line,  , Rpcalc Rules  @node Rpcalc Input
927  @subsubsection Explanation of @code{input}  @subsubsection Explanation of @code{input}
928    
929  Consider the definition of @code{input}:  Consider the definition of @code{input}:
# Line 946  The parser function @code{yyparse} conti Line 957  The parser function @code{yyparse} conti
957  grammatical error is seen or the lexical analyzer says there are no more  grammatical error is seen or the lexical analyzer says there are no more
958  input tokens; we will arrange for the latter to happen at end of file.  input tokens; we will arrange for the latter to happen at end of file.
959    
960  @node Rpcalc Line, Rpcalc Expr, Rpcalc Input, Rpcalc Rules  @node Rpcalc Line
961  @subsubsection Explanation of @code{line}  @subsubsection Explanation of @code{line}
962    
963  Now consider the definition of @code{line}:  Now consider the definition of @code{line}:
# Line 971  uninitialized (its value will be unpredi Line 982  uninitialized (its value will be unpredi
982  that value were ever used, but we don't use it: once rpcalc has printed the  that value were ever used, but we don't use it: once rpcalc has printed the
983  value of the user's input line, that value is no longer needed.  value of the user's input line, that value is no longer needed.
984    
985  @node Rpcalc Expr,  , Rpcalc Line, Rpcalc Rules  @node Rpcalc Expr
986  @subsubsection Explanation of @code{expr}  @subsubsection Explanation of @code{expr}
987    
988  The @code{exp} grouping has several rules, one for each kind of expression.  The @code{exp} grouping has several rules, one for each kind of expression.
# Line 1030  exp:      NUM Line 1041  exp:      NUM
1041  @noindent  @noindent
1042  The latter, however, is much more readable.  The latter, however, is much more readable.
1043    
1044  @node Rpcalc Lexer, Rpcalc Main, Rpcalc Rules, RPN Calc  @node Rpcalc Lexer
1045  @subsection The @code{rpcalc} Lexical Analyzer  @subsection The @code{rpcalc} Lexical Analyzer
1046  @cindex writing a lexical analyzer  @cindex writing a lexical analyzer
1047  @cindex lexical analyzer, writing  @cindex lexical analyzer, writing
# Line 1105  yylex (void) Line 1116  yylex (void)
1116  @end group  @end group
1117  @end example  @end example
1118    
1119  @node Rpcalc Main, Rpcalc Error, Rpcalc Lexer, RPN Calc  @node Rpcalc Main
1120  @subsection The Controlling Function  @subsection The Controlling Function
1121  @cindex controlling function  @cindex controlling function
1122  @cindex main function in simple example  @cindex main function in simple example
# Line 1124  main (void) Line 1135  main (void)
1135  @end group  @end group
1136  @end example  @end example
1137    
1138  @node Rpcalc Error, Rpcalc Gen, Rpcalc Main, RPN Calc  @node Rpcalc Error
1139  @subsection The Error Reporting Routine  @subsection The Error Reporting Routine
1140  @cindex error reporting routine  @cindex error reporting routine
1141    
# Line 1153  have not written any error rules in this Line 1164  have not written any error rules in this
1164  cause the calculator program to exit.  This is not clean behavior for a  cause the calculator program to exit.  This is not clean behavior for a
1165  real calculator, but it is adequate for the first example.  real calculator, but it is adequate for the first example.
1166    
1167  @node Rpcalc Gen, Rpcalc Compile, Rpcalc Error, RPN Calc  @node Rpcalc Gen
1168  @subsection Running Bison to Make the Parser  @subsection Running Bison to Make the Parser
1169  @cindex running Bison (introduction)  @cindex running Bison (introduction)
1170    
# Line 1161  Before running Bison to produce a parser Line 1172  Before running Bison to produce a parser
1172  arrange all the source code in one or more source files.  For such a  arrange all the source code in one or more source files.  For such a
1173  simple example, the easiest thing is to put everything in one file.  The  simple example, the easiest thing is to put everything in one file.  The
1174  definitions of @code{yylex}, @code{yyerror} and @code{main} go at the  definitions of @code{yylex}, @code{yyerror} and @code{main} go at the
1175  end, in the epilogue of the file  end, in the epilogue of the file
1176  (@pxref{Grammar Layout, ,The Overall Layout of a Bison Grammar}).  (@pxref{Grammar Layout, ,The Overall Layout of a Bison Grammar}).
1177    
1178  For a large project, you would probably have several source files, and use  For a large project, you would probably have several source files, and use
# Line 1182  Bison contains the source code for @code Line 1193  Bison contains the source code for @code
1193  functions in the input file (@code{yylex}, @code{yyerror} and @code{main})  functions in the input file (@code{yylex}, @code{yyerror} and @code{main})
1194  are copied verbatim to the output.  are copied verbatim to the output.
1195    
1196  @node Rpcalc Compile,  , Rpcalc Gen, RPN Calc  @node Rpcalc Compile
1197  @subsection Compiling the Parser File  @subsection Compiling the Parser File
1198  @cindex compiling the parser  @cindex compiling the parser
1199    
# Line 1227  example session using @code{rpcalc}. Line 1238  example session using @code{rpcalc}.
1238  %  %
1239  @end example  @end example
1240    
1241  @node Infix Calc, Simple Error Recovery, RPN Calc, Examples  @node Infix Calc
1242  @section Infix Notation Calculator: @code{calc}  @section Infix Notation Calculator: @code{calc}
1243  @cindex infix notation calculator  @cindex infix notation calculator
1244  @cindex @code{calc}  @cindex @code{calc}
# Line 1313  Here is a sample run of @file{calc.y}: Line 1324  Here is a sample run of @file{calc.y}:
1324  9  9
1325  @end example  @end example
1326    
1327  @node Simple Error Recovery, Multi-function Calc, Infix Calc, Examples  @node Simple Error Recovery
1328  @section Simple Error Recovery  @section Simple Error Recovery
1329  @cindex error recovery, simple  @cindex error recovery, simple
1330    
# Line 1355  input lines; it would also have to disca Line 1366  input lines; it would also have to disca
1366  input.  We won't discuss this issue further because it is not specific to  input.  We won't discuss this issue further because it is not specific to
1367  Bison programs.  Bison programs.
1368    
1369  @node Multi-function Calc, Exercises, Simple Error Recovery, Examples  @node Location Tracking Calc
1370    @section Location Tracking Calculator: @code{ltcalc}
1371    @cindex location tracking calculator
1372    @cindex @code{ltcalc}
1373    @cindex calculator, location tracking
1374    
1375    This example extends the infix notation calculator with location tracking.
1376    This feature will be used to improve error reporting, and provide better
1377    error messages.
1378    
1379    For the sake of clarity, we will switch for this example to an integer
1380    calculator, since most of the work needed to use locations will be done
1381    in the lexical analyser.
1382    
1383    @menu
1384    * Decls: Ltcalc Decls.  Bison and C declarations for ltcalc.
1385    * Rules: Ltcalc Rules.  Grammar rules for ltcalc, with explanations.
1386    * Lexer: Ltcalc Lexer.  The lexical analyzer.
1387    @end menu
1388    
1389    @node Ltcalc Decls
1390    @subsection Declarations for @code{ltcalc}
1391    
1392    The C and Bison declarations for the location tracking calculator are the same
1393    as the declarations for the infix notation calculator.
1394    
1395    @example
1396    /* Location tracking calculator.  */
1397    
1398    %@{
1399    #define YYSTYPE int
1400    #include <math.h>
1401    %@}
1402    
1403    /* Bison declarations.  */
1404    %token NUM
1405    
1406    %left '-' '+'
1407    %left '*' '/'
1408    %left NEG
1409    %right '^'
1410    
1411    %% /* Grammar follows */
1412    @end example
1413    
1414    In the code above, there are no declarations specific to locations.  Defining
1415    a data type for storing locations is not needed: we will use the type provided
1416    by default (@pxref{Location Type, ,Data Types of Locations}), which is a four
1417    member structure with the following integer fields: @code{first_line},
1418    @code{first_column}, @code{last_line} and @code{last_column}.
1419    
1420    @node Ltcalc Rules
1421    @subsection Grammar Rules for @code{ltcalc}
1422    
1423    Whether you choose to handle locations or not has no effect on the syntax of
1424    your language.  Therefore, grammar rules for this example will be very close to
1425    those of the previous example: we will only modify them to benefit from the new
1426    informations we will have.
1427    
1428    Here, we will use locations to report divisions by zero, and locate the wrong
1429    expressions or subexpressions.
1430    
1431    @example
1432    @group
1433    input   : /* empty */
1434            | input line
1435    ;
1436    @end group
1437    
1438    @group
1439    line    : '\n'
1440            | exp '\n' @{ printf ("%d\n", $1); @}
1441    ;
1442    @end group
1443    
1444    @group
1445    exp     : NUM           @{ $$ = $1; @}
1446            | exp '+' exp   @{ $$ = $1 + $3; @}
1447            | exp '-' exp   @{ $$ = $1 - $3; @}
1448            | exp '*' exp   @{ $$ = $1 * $3; @}
1449    @end group
1450            | exp '/' exp
1451    @group
1452                @{
1453                  if ($3)
1454                    $$ = $1 / $3;
1455                  else
1456                    @{
1457                      $$ = 1;
1458                      printf("Division by zero, l%d,c%d-l%d,c%d",
1459                             @@3.first_line, @@3.first_column,
1460                             @@3.last_line, @@3.last_column);
1461                    @}
1462                @}
1463    @end group
1464    @group
1465            | '-' exp %preg NEG     @{ $$ = -$2; @}
1466            | exp '^' exp           @{ $$ = pow ($1, $3); @}
1467            | '(' exp ')'           @{ $$ = $2; @}
1468    @end group
1469    @end example
1470    
1471    This code shows how to reach locations inside of semantic actions, by
1472    using the pseudo-variables @code{@@@var{n}} for rule components, and the
1473    pseudo-variable @code{@@$} for groupings.
1474    
1475    In this example, we never assign a value to @code{@@$}, because the
1476    output parser can do this automatically.  By default, before executing
1477    the C code of each action, @code{@@$} is set to range from the beginning
1478    of @code{@@1} to the end of @code{@@@var{n}}, for a rule with @var{n}
1479    components.
1480    
1481    Of course, this behavior can be redefined (@pxref{Location Default
1482    Action, , Default Action for Locations}), and for very specific rules,
1483    @code{@@$} can be computed by hand.
1484    
1485    @node Ltcalc Lexer
1486    @subsection The @code{ltcalc} Lexical Analyzer.
1487    
1488    Until now, we relied on Bison's defaults to enable location tracking. The next
1489    step is to rewrite the lexical analyser, and make it able to feed the parser
1490    with locations of tokens, as he already does for semantic values.
1491    
1492    To do so, we must take into account every single character of the input text,
1493    to avoid the computed locations of being fuzzy or wrong:
1494    
1495    @example
1496    @group
1497    int
1498    yylex (void)
1499    @{
1500      int c;
1501    
1502      /* skip white space */
1503      while ((c = getchar ()) == ' ' || c == '\t')
1504        ++yylloc.last_column;
1505    
1506      /* step */
1507      yylloc.first_line = yylloc.last_line;
1508      yylloc.first_column = yylloc.last_column;
1509    @end group
1510    
1511    @group
1512      /* process numbers */
1513      if (isdigit (c))
1514        @{
1515          yylval = c - '0';
1516          ++yylloc.last_column;
1517          while (isdigit (c = getchar ()))
1518            @{
1519              ++yylloc.last_column;
1520              yylval = yylval * 10 + c - '0';
1521            @}
1522          ungetc (c, stdin);
1523          return NUM;
1524        @}
1525    @end group
1526    
1527      /* return end-of-file */
1528      if (c == EOF)
1529        return 0;
1530    
1531      /* return single chars and update location */
1532      if (c == '\n')
1533        @{
1534          ++yylloc.last_line;
1535          yylloc.last_column = 0;
1536        @}
1537      else
1538        ++yylloc.last_column;
1539      return c;
1540    @}
1541    @end example
1542    
1543    Basically, the lexical analyzer does the same processing as before: it skips
1544    blanks and tabs, and reads numbers or single-character tokens.  In addition
1545    to this, it updates the @code{yylloc} global variable (of type @code{YYLTYPE}),
1546    where the location of tokens is stored.
1547    
1548    Now, each time this function returns a token, the parser has it's number as
1549    well as it's semantic value, and it's position in the text. The last needed
1550    change is to initialize @code{yylloc}, for example in the controlling
1551    function:
1552    
1553    @example
1554    int
1555    main (void)
1556    @{
1557      yylloc.first_line = yylloc.last_line = 1;
1558      yylloc.first_column = yylloc.last_column = 0;
1559      return yyparse ();
1560    @}
1561    @end example
1562    
1563    Remember that computing locations is not a matter of syntax.  Every character
1564    must be associated to a location update, whether it is in valid input, in
1565    comments, in literal strings, and so on...
1566    
1567    @node Multi-function Calc
1568  @section Multi-Function Calculator: @code{mfcalc}  @section Multi-Function Calculator: @code{mfcalc}
1569  @cindex multi-function calculator  @cindex multi-function calculator
1570  @cindex @code{mfcalc}  @cindex @code{mfcalc}
# Line 1407  Note that multiple assignment and nested Line 1616  Note that multiple assignment and nested
1616  * Symtab: Mfcalc Symtab.  Symbol table management subroutines.  * Symtab: Mfcalc Symtab.  Symbol table management subroutines.
1617  @end menu  @end menu
1618    
1619  @node Mfcalc Decl, Mfcalc Rules,  , Multi-function Calc  @node Mfcalc Decl
1620  @subsection Declarations for @code{mfcalc}  @subsection Declarations for @code{mfcalc}
1621    
1622  Here are the C and Bison declarations for the multi-function calculator.  Here are the C and Bison declarations for the multi-function calculator.
# Line 1458  just as @code{%token} is used for declar Line 1667  just as @code{%token} is used for declar
1667  implicitly by the rules that define them.  But @code{exp} must be declared  implicitly by the rules that define them.  But @code{exp} must be declared
1668  explicitly so we can specify its value type.  @xref{Type Decl, ,Nonterminal Symbols}.  explicitly so we can specify its value type.  @xref{Type Decl, ,Nonterminal Symbols}.
1669    
1670  @node Mfcalc Rules, Mfcalc Symtab, Mfcalc Decl, Multi-function Calc  @node Mfcalc Rules
1671  @subsection Grammar Rules for @code{mfcalc}  @subsection Grammar Rules for @code{mfcalc}
1672    
1673  Here are the grammar rules for the multi-function calculator.  Here are the grammar rules for the multi-function calculator.
# Line 1492  exp:      NUM                @{ $$ = $1; Line 1701  exp:      NUM                @{ $$ = $1;
1701  %%  %%
1702  @end smallexample  @end smallexample
1703    
1704  @node Mfcalc Symtab,  , Mfcalc Rules, Multi-function Calc  @node Mfcalc Symtab
1705  @subsection The @code{mfcalc} Symbol Table  @subsection The @code{mfcalc} Symbol Table
1706  @cindex symbol table example  @cindex symbol table example
1707    
# Line 1733  This program is both powerful and flexib Line 1942  This program is both powerful and flexib
1942  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 predefined
1943  variables such as @code{pi} or @code{e} as well.  variables such as @code{pi} or @code{e} as well.
1944    
1945  @node Exercises,  , Multi-function Calc, Examples  @node Exercises
1946  @section Exercises  @section Exercises
1947  @cindex exercises  @cindex exercises
1948    
# Line 1751  Make the program report an error if the Line 1960  Make the program report an error if the
1960  uninitialized variable in any way except to store a value in it.  uninitialized variable in any way except to store a value in it.
1961  @end enumerate  @end enumerate
1962    
1963  @node Grammar File, Interface, Examples, Top  @node Grammar File
1964  @chapter Bison Grammar Files  @chapter Bison Grammar Files
1965    
1966  Bison takes as input a context-free grammar specification and produces a  Bison takes as input a context-free grammar specification and produces a
# Line 1771  The Bison grammar input file conventiona Line 1980  The Bison grammar input file conventiona
1980  * Multiple Parsers::  Putting more than one Bison parser in one program.  * Multiple Parsers::  Putting more than one Bison parser in one program.
1981  @end menu  @end menu
1982    
1983  @node Grammar Outline, Symbols,  , Grammar File  @node Grammar Outline
1984  @section Outline of a Bison Grammar  @section Outline of a Bison Grammar
1985    
1986  A Bison grammar file has four main sections, shown here with the  A Bison grammar file has four main sections, shown here with the
# Line 1814  that they precede the definition of @cod Line 2023  that they precede the definition of @cod
2023  need any C declarations, you may omit the @samp{%@{} and @samp{%@}}  need any C declarations, you may omit the @samp{%@{} and @samp{%@}}
2024  delimiters that bracket this section.  delimiters that bracket this section.
2025    
2026  @node Bison Declarations, Grammar Rules, Prologue, Grammar Outline  @node Bison Declarations
2027  @subsection The Bison Declarations Section  @subsection The Bison Declarations Section
2028  @cindex Bison declarations (introduction)  @cindex Bison declarations (introduction)
2029  @cindex declarations, Bison (introduction)  @cindex declarations, Bison (introduction)
# Line 1824  terminal and nonterminal symbols, specif Line 2033  terminal and nonterminal symbols, specif
2033  In some simple grammars you may not need any declarations.  In some simple grammars you may not need any declarations.
2034  @xref{Declarations, ,Bison Declarations}.  @xref{Declarations, ,Bison Declarations}.
2035    
2036  @node Grammar Rules, Epilogue, Bison Declarations, Grammar Outline  @node Grammar Rules
2037  @subsection The Grammar Rules Section  @subsection The Grammar Rules Section
2038  @cindex grammar rules section  @cindex grammar rules section
2039  @cindex rules section for grammar  @cindex rules section for grammar
# Line 1842  if it is the first thing in the file. Line 2051  if it is the first thing in the file.
2051  @cindex epilogue  @cindex epilogue
2052  @cindex C code, section for additional  @cindex C code, section for additional
2053    
2054  The @var{epilogue} is copied verbatim to the end of the parser file, just as  The @var{epilogue} is copied verbatim to the end of the parser file, just as
2055  the @var{prologue} is copied to the beginning.  This is the most convenient  the @var{prologue} is copied to the beginning.  This is the most convenient
2056  place to put anything that you want to have in the parser file but which need  place to put anything that you want to have in the parser file but which need
2057  not come before the definition of @code{yyparse}.  For example, the  not come before the definition of @code{yyparse}.  For example, the
2058  definitions of @code{yylex} and @code{yyerror} often go here.    definitions of @code{yylex} and @code{yyerror} often go here.
2059  @xref{Interface, ,Parser C-Language Interface}.  @xref{Interface, ,Parser C-Language Interface}.
2060    
2061  If the last section is empty, you may omit the @samp{%%} that separates it  If the last section is empty, you may omit the @samp{%%} that separates it
# Line 1857  with @samp{yy} and many macros whose nam Line 2066  with @samp{yy} and many macros whose nam
2066  good idea to avoid using any such names (except those documented in this  good idea to avoid using any such names (except those documented in this
2067  manual) in the epilogue of the grammar file.  manual) in the epilogue of the grammar file.
2068    
2069  @node Symbols, Rules, Grammar Outline, Grammar File  @node Symbols
2070  @section Symbols, Terminal and Nonterminal  @section Symbols, Terminal and Nonterminal
2071  @cindex nonterminal symbol  @cindex nonterminal symbol
2072  @cindex terminal symbol  @cindex terminal symbol
# Line 1969  The symbol @code{error} is a terminal sy Line 2178  The symbol @code{error} is a terminal sy
2178  (@pxref{Error Recovery}); you shouldn't use it for any other purpose.  (@pxref{Error Recovery}); you shouldn't use it for any other purpose.
2179  In particular, @code{yylex} should never return this value.  In particular, @code{yylex} should never return this value.
2180    
2181  @node Rules, Recursion, Symbols, Grammar File  @node Rules
2182  @section Syntax of Grammar Rules  @section Syntax of Grammar Rules
2183  @cindex rule syntax  @cindex rule syntax
2184  @cindex grammar rule syntax  @cindex grammar rule syntax
# Line 2064  expseq1:  exp Line 2273  expseq1:  exp
2273  It is customary to write a comment @samp{/* empty */} in each rule  It is customary to write a comment @samp{/* empty */} in each rule
2274  with no components.  with no components.
2275    
2276  @node Recursion, Semantics, Rules, Grammar File  @node Recursion
2277  @section Recursive Rules  @section Recursive Rules
2278  @cindex recursive rule  @cindex recursive rule
2279    
# Line 2133  primary:  constant Line 2342  primary:  constant
2342  defines two mutually-recursive nonterminals, since each refers to the  defines two mutually-recursive nonterminals, since each refers to the
2343  other.  other.
2344    
2345  @node Semantics, Locations, Recursion, Grammar File  @node Semantics
2346  @section Defining Language Semantics  @section Defining Language Semantics
2347  @cindex defining language semantics  @cindex defining language semantics
2348  @cindex language semantics, defining  @cindex language semantics, defining
# Line 2157  the numbers associated with @var{x} and Line 2366  the numbers associated with @var{x} and
2366                          action in the middle of a rule.                          action in the middle of a rule.
2367  @end menu  @end menu
2368    
2369  @node Value Type, Multiple Types,  , Semantics  @node Value Type
2370  @subsection Data Types of Semantic Values  @subsection Data Types of Semantic Values
2371  @cindex semantic value type  @cindex semantic value type
2372  @cindex value type, semantic  @cindex value type, semantic
# Line 2176  specify some other type, define @code{YY Line 2385  specify some other type, define @code{YY
2385  @end example  @end example
2386    
2387  @noindent  @noindent
2388  This macro definition must go in the prologue of the grammar file  This macro definition must go in the prologue of the grammar file
2389  (@pxref{Grammar Outline, ,Outline of a Bison Grammar}).  (@pxref{Grammar Outline, ,Outline of a Bison Grammar}).
2390    
2391  @node Multiple Types, Actions, Value Type, Semantics  @node Multiple Types
2392  @subsection More Than One Value Type  @subsection More Than One Value Type
2393    
2394  In most programs, you will need different data types for different kinds  In most programs, you will need different data types for different kinds
# Line 2203  and for groupings with the @code{%type} Line 2412  and for groupings with the @code{%type}
2412  Decl, ,Nonterminal Symbols}).  Decl, ,Nonterminal Symbols}).
2413  @end itemize  @end itemize
2414    
2415  @node Actions, Action Types, Multiple Types, Semantics  @node Actions
2416  @subsection Actions  @subsection Actions
2417  @cindex action  @cindex action
2418  @vindex $$  @vindex $$
# Line 2279  As long as @code{bar} is used only in th Line 2488  As long as @code{bar} is used only in th
2488  always refers to the @code{expr} which precedes @code{bar} in the  always refers to the @code{expr} which precedes @code{bar} in the
2489  definition of @code{foo}.  definition of @code{foo}.
2490    
2491  @node Action Types, Mid-Rule Actions, Actions, Semantics  @node Action Types
2492  @subsection Data Types of Values in Actions  @subsection Data Types of Values in Actions
2493  @cindex action data types  @cindex action data types
2494  @cindex data types in actions  @cindex data types in actions
# Line 2324  reference.  For example, if you have def Line 2533  reference.  For example, if you have def
2533  then you can write @code{$<itype>1} to refer to the first subunit of the  then you can write @code{$<itype>1} to refer to the first subunit of the
2534  rule as an integer, or @code{$<dtype>1} to refer to it as a double.  rule as an integer, or @code{$<dtype>1} to refer to it as a double.
2535    
2536  @node Mid-Rule Actions,  , Action Types, Semantics  @node Mid-Rule Actions
2537  @subsection Actions in Mid-Rule  @subsection Actions in Mid-Rule
2538  @cindex actions in mid-rule  @cindex actions in mid-rule
2539  @cindex mid-rule actions  @cindex mid-rule actions
# Line 2486  the action is now at the end of its rule Line 2695  the action is now at the end of its rule
2695  converted to an end-of-rule action in this way, and this is what Bison  converted to an end-of-rule action in this way, and this is what Bison
2696  actually does to implement mid-rule actions.  actually does to implement mid-rule actions.
2697    
2698  @node Locations, Declarations, Semantics, Grammar File  @node Locations
2699  @section Tracking Locations  @section Tracking Locations
2700  @cindex location  @cindex location
2701  @cindex textual position  @cindex textual position
# Line 2507  to take when rules are matched. Line 2716  to take when rules are matched.
2716  * Location Default Action::     Defining a general way to compute locations.  * Location Default Action::     Defining a general way to compute locations.
2717  @end menu  @end menu
2718    
2719  @node Location Type, Actions and Locations,  , Locations  @node Location Type
2720  @subsection Data Type of Locations  @subsection Data Type of Locations
2721  @cindex data type of locations  @cindex data type of locations
2722  @cindex default location type  @cindex default location type
# Line 2529  struct Line 2738  struct
2738  @}  @}
2739  @end example  @end example
2740    
2741  @node Actions and Locations, Location Default Action, Location Type, Locations  @node Actions and Locations
2742  @subsection Actions and Locations  @subsection Actions and Locations
2743  @cindex location actions  @cindex location actions
2744  @cindex actions, location  @cindex actions, location
# Line 2596  exp:    @dots{} Line 2805  exp:    @dots{}
2805  @end group  @end group
2806  @end example  @end example
2807    
2808  @node Location Default Action,  , Actions and Locations, Locations  @node Location Default Action
2809  @subsection Default Action for Locations  @subsection Default Action for Locations
2810  @vindex YYLLOC_DEFAULT  @vindex YYLLOC_DEFAULT
2811    
# Line 2640  For consistency with semantic actions, v Line 2849  For consistency with semantic actions, v
2849  range from 1 to @var{n}.  range from 1 to @var{n}.
2850  @end itemize  @end itemize
2851    
2852  @node Declarations, Multiple Parsers, Locations, Grammar File  @node Declarations
2853  @section Bison Declarations  @section Bison Declarations
2854  @cindex declarations, Bison  @cindex declarations, Bison
2855  @cindex Bison declarations  @cindex Bison declarations
# Line 2669  it explicitly (@pxref{Language and Gramm Line 2878  it explicitly (@pxref{Language and Gramm
2878  * Decl Summary::      Table of all Bison declarations.  * Decl Summary::      Table of all Bison declarations.
2879  @end menu  @end menu
2880    
2881  @node Token Decl, Precedence Decl,  , Declarations  @node Token Decl
2882  @subsection Token Type Names  @subsection Token Type Names
2883  @cindex declaring token type names  @cindex declaring token type names
2884  @cindex token type names, declaring  @cindex token type names, declaring
# Line 2743  interchangeably in further declarations Line 2952  interchangeably in further declarations
2952  @code{yylex} function can use the token name or the literal string to  @code{yylex} function can use the token name or the literal string to
2953  obtain the token type code number (@pxref{Calling Convention}).  obtain the token type code number (@pxref{Calling Convention}).
2954    
2955  @node Precedence Decl, Union Decl, Token Decl, Declarations  @node Precedence Decl
2956  @subsection Operator Precedence  @subsection Operator Precedence
2957  @cindex precedence declarations  @cindex precedence declarations
2958  @cindex declaring operator precedence  @cindex declaring operator precedence
# Line 2792  When two tokens declared in different pr Line 3001  When two tokens declared in different pr
3001  the one declared later has the higher precedence and is grouped first.  the one declared later has the higher precedence and is grouped first.
3002  @end itemize  @end itemize
3003    
3004  @node Union Decl, Type Decl, Precedence Decl, Declarations  @node Union Decl
3005  @subsection The Collection of Value Types  @subsection The Collection of Value Types
3006  @cindex declaring value types  @cindex declaring value types
3007  @cindex value types, declaring  @cindex value types, declaring
# Line 2823  for a terminal or nonterminal symbol (@p Line 3032  for a terminal or nonterminal symbol (@p
3032  Note that, unlike making a @code{union} declaration in C, you do not write  Note that, unlike making a @code{union} declaration in C, you do not write
3033  a semicolon after the closing brace.  a semicolon after the closing brace.
3034    
3035  @node Type Decl, Expect Decl, Union Decl, Declarations  @node Type Decl
3036  @subsection Nonterminal Symbols  @subsection Nonterminal Symbols
3037  @cindex declaring value types, nonterminals  @cindex declaring value types, nonterminals
3038  @cindex value types, nonterminals, declaring  @cindex value types, nonterminals, declaring
# Line 2850  use the same @code{<@var{type}>} constru Line 3059  use the same @code{<@var{type}>} constru
3059  terminal symbol.  All kinds of token declarations allow  terminal symbol.  All kinds of token declarations allow
3060  @code{<@var{type}>}.  @code{<@var{type}>}.
3061    
3062  @node Expect Decl, Start Decl, Type Decl, Declarations  @node Expect Decl
3063  @subsection Suppressing Conflict Warnings  @subsection Suppressing Conflict Warnings
3064  @cindex suppressing conflict warnings  @cindex suppressing conflict warnings
3065  @cindex preventing warnings about conflicts  @cindex preventing warnings about conflicts
# Line 2898  Now Bison will stop annoying you about t Line 3107  Now Bison will stop annoying you about t
3107  it will warn you again if changes in the grammar result in additional  it will warn you again if changes in the grammar result in additional
3108  conflicts.  conflicts.
3109    
3110  @node Start Decl, Pure Decl, Expect Decl, Declarations  @node Start Decl
3111  @subsection The Start-Symbol  @subsection The Start-Symbol
3112  @cindex declaring the start symbol  @cindex declaring the start symbol
3113  @cindex start symbol, declaring  @cindex start symbol, declaring
# Line 2913  may override this restriction with the @ Line 3122  may override this restriction with the @
3122  %start @var{symbol}  %start @var{symbol}
3123  @end example  @end example
3124    
3125  @node Pure Decl, Decl Summary, Start Decl, Declarations  @node Pure Decl
3126  @subsection A Pure (Reentrant) Parser  @subsection A Pure (Reentrant) Parser
3127  @cindex reentrant parser  @cindex reentrant parser
3128  @cindex pure parser  @cindex pure parser
# Line 2953  Whether the parser is pure has nothing t Line 3162  Whether the parser is pure has nothing t
3162  You can generate either a pure parser or a nonreentrant parser from any  You can generate either a pure parser or a nonreentrant parser from any
3163  valid grammar.  valid grammar.
3164    
3165  @node Decl Summary,  , Pure Decl, Declarations  @node Decl Summary
3166  @subsection Bison Declaration Summary  @subsection Bison Declaration Summary
3167  @cindex Bison declaration summary  @cindex Bison declaration summary
3168  @cindex declaration summary  @cindex declaration summary
# Line 3110  The number of parser states (@pxref{Pars Line 3319  The number of parser states (@pxref{Pars
3319  @end table  @end table
3320  @end table  @end table
3321    
3322  @node Multiple Parsers,, Declarations, Grammar File  @node Multiple Parsers
3323  @section Multiple Parsers in the Same Program  @section Multiple Parsers in the Same Program
3324    
3325  Most programs that use Bison parse only one language and therefore contain  Most programs that use Bison parse only one language and therefore contain
# Line 3140  of the parser source file, defining @cod Line 3349  of the parser source file, defining @cod
3349  @code{@var{prefix}parse}, and so on.  This effectively substitutes one  @code{@var{prefix}parse}, and so on.  This effectively substitutes one
3350  name for the other in the entire parser file.  name for the other in the entire parser file.
3351    
3352  @node Interface, Algorithm, Grammar File, Top  @node Interface
3353  @chapter Parser C-Language Interface  @chapter Parser C-Language Interface
3354  @cindex C-language interface  @cindex C-language interface
3355  @cindex interface  @cindex interface
# Line 3162  in the grammar file, you are likely to r Line 3371  in the grammar file, you are likely to r
3371  * Action Features::   Special features for use in actions.  * Action Features::   Special features for use in actions.
3372  @end menu  @end menu
3373    
3374  @node Parser Function, Lexical,  , Interface  @node Parser Function
3375  @section The Parser Function @code{yyparse}  @section The Parser Function @code{yyparse}
3376  @findex yyparse  @findex yyparse
3377    
# Line 3190  Return immediately with value 0 (to repo Line 3399  Return immediately with value 0 (to repo
3399  Return immediately with value 1 (to report failure).  Return immediately with value 1 (to report failure).
3400  @end table  @end table
3401    
3402  @node Lexical, Error Reporting, Parser Function, Interface  @node Lexical
3403  @section The Lexical Analyzer Function @code{yylex}  @section The Lexical Analyzer Function @code{yylex}
3404  @findex yylex  @findex yylex
3405  @cindex lexical analyzer  @cindex lexical analyzer
# Line 3219  that need it.  @xref{Invocation, ,Invoki Line 3428  that need it.  @xref{Invocation, ,Invoki
3428                          in a pure parser (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}).                          in a pure parser (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}).
3429  @end menu  @end menu
3430    
3431  @node Calling Convention, Token Values,  , Lexical  @node Calling Convention
3432  @subsection Calling Convention for @code{yylex}  @subsection Calling Convention for @code{yylex}
3433    
3434  The value that @code{yylex} returns must be the numeric code for the type  The value that @code{yylex} returns must be the numeric code for the type
# Line 3296  The @code{yytname} table is generated on Line 3505  The @code{yytname} table is generated on
3505  @code{%token_table} declaration.  @xref{Decl Summary}.  @code{%token_table} declaration.  @xref{Decl Summary}.
3506  @end itemize  @end itemize
3507    
3508  @node Token Values, Token Positions, Calling Convention, Lexical  @node Token Values
3509  @subsection Semantic Values of Tokens  @subsection Semantic Values of Tokens
3510    
3511  @vindex yylval  @vindex yylval
# Line 3342  then the code in @code{yylex} might look Line 3551  then the code in @code{yylex} might look
3551  @end group  @end group
3552  @end example  @end example
3553    
3554  @node Token Positions, Pure Calling, Token Values, Lexical  @node Token Positions
3555  @subsection Textual Positions of Tokens  @subsection Textual Positions of Tokens
3556    
3557  @vindex yylloc  @vindex yylloc
# Line 3363  feature makes the parser noticeably slow Line 3572  feature makes the parser noticeably slow
3572  @tindex YYLTYPE  @tindex YYLTYPE
3573  The data type of @code{yylloc} has the name @code{YYLTYPE}.  The data type of @code{yylloc} has the name @code{YYLTYPE}.
3574    
3575  @node Pure Calling,  , Token Positions, Lexical  @node Pure Calling
3576  @subsection Calling Conventions for Pure Parsers  @subsection Calling Conventions for Pure Parsers
3577    
3578  When you use the Bison declaration @code{%pure_parser} to request a  When you use the Bison declaration @code{%pure_parser} to request a
# Line 3470  You can use @samp{%pure_parser} to reque Line 3679  You can use @samp{%pure_parser} to reque
3679  also using @code{YYPARSE_PARAM}.  Then you should call @code{yyparse}  also using @code{YYPARSE_PARAM}.  Then you should call @code{yyparse}
3680  with no arguments, as usual.  with no arguments, as usual.
3681    
3682  @node Error Reporting, Action Features, Lexical, Interface  @node Error Reporting
3683  @section The Error Reporting Function @code{yyerror}  @section The Error Reporting Function @code{yyerror}
3684  @cindex error reporting function  @cindex error reporting function
3685  @findex yyerror  @findex yyerror
# Line 3530  encountered so far.  Normally this varia Line 3739  encountered so far.  Normally this varia
3739  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}) then it is a local variable
3740  which only the actions can access.  which only the actions can access.
3741    
3742  @node Action Features,  , Error Reporting, Interface  @node Action Features
3743  @section Special Features for Use in Actions  @section Special Features for Use in Actions
3744  @cindex summary, action features  @cindex summary, action features
3745  @cindex action features summary  @cindex action features summary
# Line 3644  Tracking Locations}. Line 3853  Tracking Locations}.
3853    
3854  @end table  @end table
3855    
3856  @node Algorithm, Error Recovery, Interface, Top  @node Algorithm
3857  @chapter The Bison Parser Algorithm  @chapter The Bison Parser Algorithm
3858  @cindex Bison parser algorithm  @cindex Bison parser algorithm
3859  @cindex algorithm of parser  @cindex algorithm of parser
# Line 3711  This kind of parser is known in the lite Line 3920  This kind of parser is known in the lite
3920  * Stack Overflow::    What happens when stack gets full.  How to avoid it.  * Stack Overflow::    What happens when stack gets full.  How to avoid it.
3921  @end menu  @end menu
3922    
3923  @node Look-Ahead, Shift/Reduce,  , Algorithm  @node Look-Ahead
3924  @section Look-Ahead Tokens  @section Look-Ahead Tokens
3925  @cindex look-ahead token  @cindex look-ahead token
3926    
# Line 3766  doing so would produce on the stack the Line 3975  doing so would produce on the stack the
3975  The current look-ahead token is stored in the variable @code{yychar}.  The current look-ahead token is stored in the variable @code{yychar}.
3976  @xref{Action Features, ,Special Features for Use in Actions}.  @xref{Action Features, ,Special Features for Use in Actions}.
3977    
3978  @node Shift/Reduce, Precedence, Look-Ahead, Algorithm  @node Shift/Reduce
3979  @section Shift/Reduce Conflicts  @section Shift/Reduce Conflicts
3980  @cindex conflicts  @cindex conflicts
3981  @cindex shift/reduce conflicts  @cindex shift/reduce conflicts
# Line 3862  expr:     variable Line 4071  expr:     variable
4071          ;          ;
4072  @end example  @end example
4073    
4074  @node Precedence, Contextual Precedence, Shift/Reduce, Algorithm  @node Precedence
4075  @section Operator Precedence  @section Operator Precedence
4076  @cindex operator precedence  @cindex operator precedence
4077  @cindex precedence of operators  @cindex precedence of operators
# Line 3879  shift and when to reduce. Line 4088  shift and when to reduce.
4088  * How Precedence::    How they work.  * How Precedence::    How they work.
4089  @end menu  @end menu
4090    
4091  @node Why Precedence, Using Precedence,  , Precedence  @node Why Precedence
4092  @subsection When Precedence is Needed  @subsection When Precedence is Needed
4093    
4094  Consider the following ambiguous grammar fragment (ambiguous because the  Consider the following ambiguous grammar fragment (ambiguous because the
# Line 3926  matter of whether the parser chooses to Line 4135  matter of whether the parser chooses to
4135  contains @w{@samp{1 - 2}} and the look-ahead token is @samp{-}: shifting  contains @w{@samp{1 - 2}} and the look-ahead token is @samp{-}: shifting
4136  makes right-associativity.  makes right-associativity.
4137    
4138  @node Using Precedence, Precedence Examples, Why Precedence, Precedence  @node Using Precedence
4139  @subsection Specifying Operator Precedence  @subsection Specifying Operator Precedence
4140  @findex %left  @findex %left
4141  @findex %right  @findex %right
# Line 3947  order in which they are declared.  The f Line 4156  order in which they are declared.  The f
4156  precedence is lowest, the next such declaration declares the operators  precedence is lowest, the next such declaration declares the operators
4157  whose precedence is a little higher, and so on.  whose precedence is a little higher, and so on.
4158    
4159  @node Precedence Examples, How Precedence, Using Precedence, Precedence  @node Precedence Examples
4160  @subsection Precedence Examples  @subsection Precedence Examples
4161    
4162  In our example, we would want the following declarations:  In our example, we would want the following declarations:
# Line 3973  declared with @code{'-'}: Line 4182  declared with @code{'-'}:
4182  and so on.  We assume that these tokens are more than one character long  and so on.  We assume that these tokens are more than one character long
4183  and therefore are represented by names, not character literals.)  and therefore are represented by names, not character literals.)
4184    
4185  @node How Precedence,  , Precedence Examples, Precedence  @node How Precedence
4186  @subsection How Precedence Works  @subsection How Precedence Works
4187    
4188  The first effect of the precedence declarations is to assign precedence  The first effect of the precedence declarations is to assign precedence
# Line 3994  how each conflict was resolved. Line 4203  how each conflict was resolved.
4203  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
4204  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.
4205    
4206  @node Contextual Precedence, Parser States, Precedence, Algorithm  @node Contextual Precedence
4207  @section Context-Dependent Precedence  @section Context-Dependent Precedence
4208  @cindex context-dependent precedence  @cindex context-dependent precedence
4209  @cindex unary operator precedence  @cindex unary operator precedence
# Line 4052  exp:    @dots{} Line 4261  exp:    @dots{}
4261  @end group  @end group
4262  @end example  @end example
4263    
4264  @node Parser States, Reduce/Reduce, Contextual Precedence, Algorithm  @node Parser States
4265  @section Parser States  @section Parser States
4266  @cindex finite-state machine  @cindex finite-state machine
4267  @cindex parser state  @cindex parser state
# Line 4078  There is one other alternative: the tabl Line 4287  There is one other alternative: the tabl
4287  is erroneous in the current state.  This causes error processing to begin  is erroneous in the current state.  This causes error processing to begin
4288  (@pxref{Error Recovery}).  (@pxref{Error Recovery}).
4289    
4290  @node Reduce/Reduce, Mystery Conflicts, Parser States, Algorithm  @node Reduce/Reduce
4291  @section Reduce/Reduce Conflicts  @section Reduce/Reduce Conflicts
4292  @cindex reduce/reduce conflict  @cindex reduce/reduce conflict
4293  @cindex conflicts, reduce/reduce  @cindex conflicts, reduce/reduce
# Line 4193  redirects:redirect Line 4402  redirects:redirect
4402          ;          ;
4403  @end example  @end example
4404    
4405  @node Mystery Conflicts, Stack Overflow, Reduce/Reduce, Algorithm  @node Mystery Conflicts
4406  @section Mysterious Reduce/Reduce Conflicts  @section Mysterious Reduce/Reduce Conflicts
4407    
4408  Sometimes reduce/reduce conflicts can occur that don't look warranted.  Sometimes reduce/reduce conflicts can occur that don't look warranted.
# Line 4301  return_spec: Line 4510  return_spec:
4510          ;          ;
4511  @end example  @end example
4512    
4513  @node Stack Overflow,  , Mystery Conflicts, Algorithm  @node Stack Overflow
4514  @section Stack Overflow, and How to Avoid It  @section Stack Overflow, and How to Avoid It
4515  @cindex stack overflow  @cindex stack overflow
4516  @cindex parser stack overflow  @cindex parser stack overflow
# Line 4335  You can control how much stack is alloca Line 4544  You can control how much stack is alloca
4544  macro @code{YYINITDEPTH}.  This value too must be a compile-time  macro @code{YYINITDEPTH}.  This value too must be a compile-time
4545  constant integer.  The default is 200.  constant integer.  The default is 200.
4546    
4547  @node Error Recovery, Context Dependency, Algorithm, Top  @node Error Recovery
4548  @chapter Error Recovery  @chapter Error Recovery
4549  @cindex error recovery  @cindex error recovery
4550  @cindex recovery from errors  @cindex recovery from errors
# Line 4453  value 1 when the parser is recovering fr Line 4662  value 1 when the parser is recovering fr
4662  rest of the time.  A value of 1 indicates that error messages are  rest of the time.  A value of 1 indicates that error messages are
4663  currently suppressed for new syntax errors.  currently suppressed for new syntax errors.
4664    
4665  @node Context Dependency, Debugging, Error Recovery, Top  @node Context Dependency
4666  @chapter Handling Context Dependencies  @chapter Handling Context Dependencies
4667    
4668  The Bison paradigm is to parse tokens first, then group them into larger  The Bison paradigm is to parse tokens first, then group them into larger
# Line 4472  languages. Line 4681  languages.
4681  (Actually, ``kludge'' means any technique that gets its job done but is  (Actually, ``kludge'' means any technique that gets its job done but is
4682  neither clean nor robust.)  neither clean nor robust.)
4683    
4684  @node Semantic Tokens, Lexical Tie-ins,  , Context Dependency  @node Semantic Tokens
4685  @section Semantic Info in Token Types  @section Semantic Info in Token Types
4686    
4687  The C language has a context dependency: the way an identifier is used  The C language has a context dependency: the way an identifier is used
# Line 4547  here the information is global, and is u Line 4756  here the information is global, and is u
4756  program.  A true lexical tie-in has a special-purpose flag controlled by  program.  A true lexical tie-in has a special-purpose flag controlled by
4757  the syntactic context.  the syntactic context.
4758    
4759  @node Lexical Tie-ins, Tie-in Recovery, Semantic Tokens, Context Dependency  @node Lexical Tie-ins
4760  @section Lexical Tie-ins  @section Lexical Tie-ins
4761  @cindex lexical tie-in  @cindex lexical tie-in
4762    
# Line 4596  Here we assume that @code{yylex} looks a Line 4805  Here we assume that @code{yylex} looks a
4805  it is nonzero, all integers are parsed in hexadecimal, and tokens starting  it is nonzero, all integers are parsed in hexadecimal, and tokens starting
4806  with letters are parsed as integers if possible.  with letters are parsed as integers if possible.
4807    
4808  The declaration of @code{hexflag} shown in the prologue of the parser file  The declaration of @code{hexflag} shown in the prologue of the parser file
4809  is needed to make it accessible to the actions (@pxref{Prologue, ,The Prologue}).    is needed to make it accessible to the actions (@pxref{Prologue, ,The Prologue}).
4810  You must also write the code in @code{yylex} to obey the flag.  You must also write the code in @code{yylex} to obey the flag.
4811    
4812  @node Tie-in Recovery,  , Lexical Tie-ins, Context Dependency  @node Tie-in Recovery
4813  @section Lexical Tie-ins and Error Recovery  @section Lexical Tie-ins and Error Recovery
4814    
4815  Lexical tie-ins make strict demands on any error recovery rules you have.  Lexical tie-ins make strict demands on any error recovery rules you have.
# Line 4655  make sure your error recovery rules are Line 4864  make sure your error recovery rules are
4864  be such that you can be sure that it always will, or always won't, have to  be such that you can be sure that it always will, or always won't, have to
4865  clear the flag.  clear the flag.
4866    
4867  @node Debugging, Invocation, Context Dependency, Top  @node Debugging
4868  @chapter Debugging Your Parser  @chapter Debugging Your Parser
4869  @findex YYDEBUG  @findex YYDEBUG
4870  @findex yydebug  @findex yydebug
# Line 4666  If a Bison grammar compiles properly but Line 4875  If a Bison grammar compiles properly but
4875  runs, the @code{yydebug} parser-trace feature can help you figure out why.  runs, the @code{yydebug} parser-trace feature can help you figure out why.
4876    
4877  To enable compilation of trace facilities, you must define the macro  To enable compilation of trace facilities, you must define the macro
4878  @code{YYDEBUG} when you compile the parser.  You could use @samp{-DYYDEBUG=1}  @code{YYDEBUG} when you compile the parser.  You could use @samp{-DYYDEBUG=1}
4879  as a compiler option or you could put @samp{#define YYDEBUG 1} in the prologue  as a compiler option or you could put @samp{#define YYDEBUG 1} in the prologue
4880  of the grammar file (@pxref{Prologue, , The Prologue}). Alternatively, use the  of the grammar file (@pxref{Prologue, , The Prologue}). Alternatively, use the
4881  @samp{-t} option when you run Bison (@pxref{Invocation, ,Invoking Bison}).    @samp{-t} option when you run Bison (@pxref{Invocation, ,Invoking Bison}).
4882  We always define @code{YYDEBUG} so that debugging is always possible.  We always define @code{YYDEBUG} so that debugging is always possible.
4883    
4884  The trace facility uses @code{stderr}, so you must add  The trace facility uses @code{stderr}, so you must add
4885  @w{@code{#include <stdio.h>}} to the prologue unless it is already there.  @w{@code{#include <stdio.h>}} to the prologue unless it is already there.
4886    
4887  Once you have compiled the program with trace facilities, the way to  Once you have compiled the program with trace facilities, the way to
# Line 4736  yyprint (FILE *file, int type, YYSTYPE v Line 4945  yyprint (FILE *file, int type, YYSTYPE v
4945  @}  @}
4946  @end smallexample  @end smallexample
4947    
4948  @node Invocation, Table of Symbols, Debugging, Top  @node Invocation
4949  @chapter Invoking Bison  @chapter Invoking Bison
4950  @cindex invoking Bison  @cindex invoking Bison
4951  @cindex Bison invocation  @cindex Bison invocation
# Line 4782  will produce @file{output.c++} and @file Line 4991  will produce @file{output.c++} and @file
4991  * VMS Invocation::    Bison command syntax on VMS.  * VMS Invocation::    Bison command syntax on VMS.
4992  @end menu  @end menu
4993    
4994  @node Bison Options, Environment Variables,  , Invocation  @node Bison Options
4995  @section Bison Options  @section Bison Options
4996    
4997  Bison supports both traditional single-letter options and mnemonic long  Bison supports both traditional single-letter options and mnemonic long
# Line 4875  Adjust the output: Line 5084  Adjust the output:
5084    
5085  @table @option  @table @option
5086  @item -d  @item -d
 @itemx --defines  
5087  Pretend that @code{%verbose} was specified, i.e., write an extra output  Pretend that @code{%verbose} was specified, i.e., write an extra output
5088  file containing macro definitions for the token type names defined in  file containing macro definitions for the token type names defined in
5089  the grammar and the semantic value type @code{YYSTYPE}, as well as a few  the grammar and the semantic value type @code{YYSTYPE}, as well as a few
5090  @code{extern} variable declarations.  @xref{Decl Summary}.  @code{extern} variable declarations.  @xref{Decl Summary}.
5091    
5092    @item --defines=@var{defines-file}
5093    The behaviour of @var{--defines} is the same than @samp{-d}. The only
5094    difference is that it has an optionnal argument which is the name of
5095    the output filename.
5096    
5097  @item -b @var{file-prefix}  @item -b @var{file-prefix}
5098  @itemx --file-prefix=@var{prefix}  @itemx --file-prefix=@var{prefix}
5099  Specify a prefix to use for all Bison output file names.  The names are  Specify a prefix to use for all Bison output file names.  The names are
# Line 4898  Specify the name @var{outfile} for the p Line 5111  Specify the name @var{outfile} for the p
5111    
5112  The other output files' names are constructed from @var{outfile}  The other output files' names are constructed from @var{outfile}
5113  as described under the @samp{-v} and @samp{-d} options.  as described under the @samp{-v} and @samp{-d} options.
5114    
5115    @item -g
5116    Output a VCG definition of the LALR(1) grammar automaton computed by
5117    Bison. If the grammar file is @file{foo.y}, the VCG output file will
5118    be @file{foo.vcg}.
5119    
5120    @item --graph=@var{graph-file}
5121    The behaviour of @var{--graph} is the same than @samp{-g}. The only
5122    difference is that it has an optionnal argument which is the name of
5123    the output graph filename.
5124  @end table  @end table
5125    
5126  @node Environment Variables, Option Cross Key, Bison Options, Invocation  @node Environment Variables
5127  @section Environment Variables  @section Environment Variables
5128  @cindex environment variables  @cindex environment variables
5129  @cindex BISON_HAIRY  @cindex BISON_HAIRY
# Line 4925  also be specified or overridden in a sim Line 5148  also be specified or overridden in a sim
5148    
5149  @end table  @end table
5150    
5151  @node Option Cross Key, VMS Invocation, Environment Variables, Invocation  @node Option Cross Key
5152  @section Option Cross Key  @section Option Cross Key
5153    
5154  Here is a list of options, alphabetized by long option, to help you find  Here is a list of options, alphabetized by long option, to help you find
# Line 4939  the corresponding short option. Line 5162  the corresponding short option.
5162  \line{ --defines \leaderfill -d}  \line{ --defines \leaderfill -d}
5163  \line{ --file-prefix \leaderfill -b}  \line{ --file-prefix \leaderfill -b}
5164  \line{ --fixed-output-files \leaderfill -y}  \line{ --fixed-output-files \leaderfill -y}
5165    \line{ --graph \leaderfill -g}
5166  \line{ --help \leaderfill -h}  \line{ --help \leaderfill -h}
5167  \line{ --name-prefix \leaderfill -p}  \line{ --name-prefix \leaderfill -p}
5168  \line{ --no-lines \leaderfill -l}  \line{ --no-lines \leaderfill -l}
# Line 4954  the corresponding short option. Line 5178  the corresponding short option.
5178  @ifinfo  @ifinfo
5179  @example  @example
5180  --debug                               -t  --debug                               -t
5181  --defines                             -d  --defines=@var{defines-file}          -d
5182  --file-prefix=@var{prefix}                  -b @var{file-prefix}  --file-prefix=@var{prefix}                  -b @var{file-prefix}
5183  --fixed-output-files --yacc           -y  --fixed-output-files --yacc           -y
5184    --graph=@var{graph-file}              -d
5185  --help                                -h  --help                                -h
5186  --name-prefix=@var{prefix}                  -p @var{name-prefix}  --name-prefix=@var{prefix}                  -p @var{name-prefix}
5187  --no-lines                            -l  --no-lines                            -l
# Line 4968  the corresponding short option. Line 5193  the corresponding short option.
5193  @end example  @end example
5194  @end ifinfo  @end ifinfo
5195    
5196  @node VMS Invocation,  , Option Cross Key, Invocation  @node VMS Invocation
5197  @section Invoking Bison under VMS  @section Invoking Bison under VMS
5198  @cindex invoking Bison under VMS  @cindex invoking Bison under VMS
5199  @cindex VMS  @cindex VMS
# Line 4996  The VMS file system does not permit file Line 5221  The VMS file system does not permit file
5221  @file{foo.tab.c}.  In the above example, the output file  @file{foo.tab.c}.  In the above example, the output file
5222  would instead be named @file{foo_tab.c}.  would instead be named @file{foo_tab.c}.
5223    
5224  @node Table of Symbols, Glossary, Invocation, Top  @node Table of Symbols
5225  @appendix Bison Symbols  @appendix Bison Symbols
5226  @cindex Bison symbols, table of  @cindex Bison symbols, table of
5227  @cindex symbols in Bison, table of  @cindex symbols in Bison, table of
# Line 5196  Bison declarations section or the epilog Line 5421  Bison declarations section or the epilog
5421    
5422  @item %@{ %@}  @item %@{ %@}
5423  All code listed between @samp{%@{} and @samp{%@}} is copied directly to  All code listed between @samp{%@{} and @samp{%@}} is copied directly to
5424  the output file uninterpreted.  Such code forms the prologue of the input  the output file uninterpreted.  Such code forms the prologue of the input
5425  file.  @xref{Grammar Outline, ,Outline of a Bison  file.  @xref{Grammar Outline, ,Outline of a Bison
5426  Grammar}.  Grammar}.
5427    
# Line 5215  Separates alternate rules for the same r Line 5440  Separates alternate rules for the same r
5440  @xref{Rules, ,Syntax of Grammar Rules}.  @xref{Rules, ,Syntax of Grammar Rules}.
5441  @end table  @end table
5442    
5443  @node Glossary, Copying This Manual, Table of Symbols, Top  @node Glossary
5444  @appendix Glossary  @appendix Glossary
5445  @cindex glossary  @cindex glossary
5446    
# Line 5376  grammatically indivisible.  The piece of Line 5601  grammatically indivisible.  The piece of
5601  @xref{Language and Grammar, ,Languages and Context-Free Grammars}.  @xref{Language and Grammar, ,Languages and Context-Free Grammars}.
5602  @end table  @end table
5603    
5604  @node Copying This Manual, Index, Glossary, Top  @node Copying This Manual
5605  @appendix Copying This Manual  @appendix Copying This Manual
5606    
5607  @menu  @menu
# Line 5385  grammatically indivisible.  The piece of Line 5610  grammatically indivisible.  The piece of
5610    
5611  @include fdl.texi  @include fdl.texi
5612    
5613  @node Index,  , Copying This Manual, Top  @node Index
5614  @unnumbered Index  @unnumbered Index
5615    
5616  @printindex cp  @printindex cp

Legend:
Removed from v.1.38  
changed lines
  Added in v.1.39

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26