Use @kbd to denote user input.
(Language and Grammar): ANSIfy the example.
Adjust its layout for info/notinfo.
(Location Tracking Calc): Output error messages to stderr.
Output locations in a more GNUtically correct way.
Fix a couple of Englishos.
Adjust @group/@end group pairs.
+2001-11-12 Akim Demaille <akim@epita.fr>
+
+ * doc/bison.texinfo: Use `$' as shell prompt, not `%'.
+ Use @kbd to denote user input.
+ (Language and Grammar): ANSIfy the example.
+ Adjust its layout for info/notinfo.
+ (Location Tracking Calc): Output error messages to stderr.
+ Output locations in a more GNUtically correct way.
+ Fix a couple of Englishos.
+ Adjust @group/@end group pairs.
+
2001-11-12 Akim Demaille <akim@epita.fr>
%expext was not functioning at all.
\1f
Indirect:
bison.info-1: 1313
-bison.info-2: 50690
-bison.info-3: 100580
-bison.info-4: 147602
-bison.info-5: 197472
+bison.info-2: 50632
+bison.info-3: 100395
+bison.info-4: 147417
+bison.info-5: 197287
\1f
Tag Table:
(Indirect)
Node: Copying\7f11707
Node: Concepts\7f30910
Node: Language and Grammar\7f31989
-Node: Grammar in Bison\7f37005
-Node: Semantic Values\7f38929
-Node: Semantic Actions\7f41030
-Node: Locations Overview\7f42219
-Node: Bison Parser\7f43666
-Node: Stages\7f45978
-Node: Grammar Layout\7f47261
-Node: Examples\7f48518
-Node: RPN Calc\7f49716
-Node: Rpcalc Decls\7f50690
-Node: Rpcalc Rules\7f52277
-Node: Rpcalc Input\7f54077
-Node: Rpcalc Line\7f55538
-Node: Rpcalc Expr\7f56653
-Node: Rpcalc Lexer\7f58598
-Node: Rpcalc Main\7f61170
-Node: Rpcalc Error\7f61568
-Node: Rpcalc Gen\7f62576
-Node: Rpcalc Compile\7f63725
-Node: Infix Calc\7f64600
-Node: Simple Error Recovery\7f67307
-Node: Location Tracking Calc\7f69196
-Node: Ltcalc Decls\7f69926
-Node: Ltcalc Rules\7f70835
-Node: Ltcalc Lexer\7f72896
-Node: Multi-function Calc\7f75234
-Node: Mfcalc Decl\7f76801
-Node: Mfcalc Rules\7f78824
-Node: Mfcalc Symtab\7f80204
-Node: Exercises\7f86577
-Node: Grammar File\7f87083
-Node: Grammar Outline\7f87931
-Node: C Declarations\7f88665
-Node: Bison Declarations\7f89245
-Node: Grammar Rules\7f89657
-Node: C Code\7f90117
-Node: Symbols\7f91047
-Node: Rules\7f96128
-Node: Recursion\7f97767
-Node: Semantics\7f99486
-Node: Value Type\7f100580
-Node: Multiple Types\7f101252
-Node: Actions\7f102269
-Node: Action Types\7f105054
-Node: Mid-Rule Actions\7f106357
-Node: Locations\7f111927
-Node: Location Type\7f112575
-Node: Actions and Locations\7f113133
-Node: Location Default Action\7f115289
-Node: Declarations\7f116752
-Node: Token Decl\7f118071
-Node: Precedence Decl\7f120084
-Node: Union Decl\7f121635
-Node: Type Decl\7f122479
-Node: Expect Decl\7f123385
-Node: Start Decl\7f124953
-Node: Pure Decl\7f125331
-Node: Decl Summary\7f127008
-Node: Multiple Parsers\7f133136
-Node: Interface\7f134630
-Node: Parser Function\7f135502
-Node: Lexical\7f136337
-Node: Calling Convention\7f137743
-Node: Token Values\7f140514
-Node: Token Positions\7f141663
-Node: Pure Calling\7f142548
-Node: Error Reporting\7f145480
-Node: Action Features\7f147602
-Node: Algorithm\7f150897
-Node: Look-Ahead\7f153190
-Node: Shift/Reduce\7f155322
-Node: Precedence\7f158234
-Node: Why Precedence\7f158885
-Node: Using Precedence\7f160750
-Node: Precedence Examples\7f161718
-Node: How Precedence\7f162419
-Node: Contextual Precedence\7f163568
-Node: Parser States\7f165359
-Node: Reduce/Reduce\7f166602
-Node: Mystery Conflicts\7f170163
-Node: Stack Overflow\7f173549
-Node: Error Recovery\7f174922
-Node: Context Dependency\7f180058
-Node: Semantic Tokens\7f180906
-Node: Lexical Tie-ins\7f183923
-Node: Tie-in Recovery\7f185471
-Node: Debugging\7f187643
-Node: Invocation\7f190944
-Node: Bison Options\7f192196
-Node: Environment Variables\7f195746
-Node: Option Cross Key\7f196594
-Node: VMS Invocation\7f197472
-Node: Table of Symbols\7f198256
-Node: Glossary\7f206224
-Node: Copying This Manual\7f212528
-Node: GNU Free Documentation License\7f212737
-Node: Index\7f232602
+Node: Grammar in Bison\7f36947
+Node: Semantic Values\7f38871
+Node: Semantic Actions\7f40972
+Node: Locations Overview\7f42161
+Node: Bison Parser\7f43608
+Node: Stages\7f45920
+Node: Grammar Layout\7f47203
+Node: Examples\7f48460
+Node: RPN Calc\7f49658
+Node: Rpcalc Decls\7f50632
+Node: Rpcalc Rules\7f52219
+Node: Rpcalc Input\7f54019
+Node: Rpcalc Line\7f55480
+Node: Rpcalc Expr\7f56595
+Node: Rpcalc Lexer\7f58540
+Node: Rpcalc Main\7f61112
+Node: Rpcalc Error\7f61510
+Node: Rpcalc Gen\7f62518
+Node: Rpcalc Compile\7f63667
+Node: Infix Calc\7f64542
+Node: Simple Error Recovery\7f67249
+Node: Location Tracking Calc\7f69138
+Node: Ltcalc Decls\7f69820
+Node: Ltcalc Rules\7f70713
+Node: Ltcalc Lexer\7f72723
+Node: Multi-function Calc\7f75049
+Node: Mfcalc Decl\7f76616
+Node: Mfcalc Rules\7f78639
+Node: Mfcalc Symtab\7f80019
+Node: Exercises\7f86392
+Node: Grammar File\7f86898
+Node: Grammar Outline\7f87746
+Node: C Declarations\7f88480
+Node: Bison Declarations\7f89060
+Node: Grammar Rules\7f89472
+Node: C Code\7f89932
+Node: Symbols\7f90862
+Node: Rules\7f95943
+Node: Recursion\7f97582
+Node: Semantics\7f99301
+Node: Value Type\7f100395
+Node: Multiple Types\7f101067
+Node: Actions\7f102084
+Node: Action Types\7f104869
+Node: Mid-Rule Actions\7f106172
+Node: Locations\7f111742
+Node: Location Type\7f112390
+Node: Actions and Locations\7f112948
+Node: Location Default Action\7f115104
+Node: Declarations\7f116567
+Node: Token Decl\7f117886
+Node: Precedence Decl\7f119899
+Node: Union Decl\7f121450
+Node: Type Decl\7f122294
+Node: Expect Decl\7f123200
+Node: Start Decl\7f124768
+Node: Pure Decl\7f125146
+Node: Decl Summary\7f126823
+Node: Multiple Parsers\7f132951
+Node: Interface\7f134445
+Node: Parser Function\7f135317
+Node: Lexical\7f136152
+Node: Calling Convention\7f137558
+Node: Token Values\7f140329
+Node: Token Positions\7f141478
+Node: Pure Calling\7f142363
+Node: Error Reporting\7f145295
+Node: Action Features\7f147417
+Node: Algorithm\7f150712
+Node: Look-Ahead\7f153005
+Node: Shift/Reduce\7f155137
+Node: Precedence\7f158049
+Node: Why Precedence\7f158700
+Node: Using Precedence\7f160565
+Node: Precedence Examples\7f161533
+Node: How Precedence\7f162234
+Node: Contextual Precedence\7f163383
+Node: Parser States\7f165174
+Node: Reduce/Reduce\7f166417
+Node: Mystery Conflicts\7f169978
+Node: Stack Overflow\7f173364
+Node: Error Recovery\7f174737
+Node: Context Dependency\7f179873
+Node: Semantic Tokens\7f180721
+Node: Lexical Tie-ins\7f183738
+Node: Tie-in Recovery\7f185286
+Node: Debugging\7f187458
+Node: Invocation\7f190759
+Node: Bison Options\7f192011
+Node: Environment Variables\7f195561
+Node: Option Cross Key\7f196409
+Node: VMS Invocation\7f197287
+Node: Table of Symbols\7f198071
+Node: Glossary\7f206039
+Node: Copying This Manual\7f212343
+Node: GNU Free Documentation License\7f212552
+Node: Index\7f232417
\1f
End Tag Table
Here is a simple C function subdivided into tokens:
int /* keyword `int' */
- square (x) /* identifier, open-paren, */
- /* identifier, close-paren */
- int x; /* keyword `int', identifier, semicolon */
+ square (int x) /* identifier, open-paren, identifier,
+ identifier, close-paren */
{ /* open-brace */
- return x * x; /* keyword `return', identifier, */
- /* asterisk, identifier, semicolon */
+ return x * x; /* keyword `return', identifier, asterisk,
+ identifier, semicolon */
} /* close-brace */
The syntactic groupings of C include the expression, the statement,
Here is how to compile and run the parser file:
# List files in current directory.
- % ls
+ $ ls
rpcalc.tab.c rpcalc.y
# Compile the Bison parser.
# `-lm' tells compiler to search math library for `pow'.
- % cc rpcalc.tab.c -lm -o rpcalc
+ $ cc rpcalc.tab.c -lm -o rpcalc
# List files again.
- % ls
+ $ ls
rpcalc rpcalc.tab.c rpcalc.y
The file `rpcalc' now contains the executable code. Here is an
example session using `rpcalc'.
- % rpcalc
+ $ rpcalc
4 9 +
13
3 7 + 3 4 5 *+-
3 4 ^ Exponentiation
81
^D End-of-file indicator
- %
+ $
\1f
File: bison.info, Node: Infix Calc, Next: Simple Error Recovery, Prev: RPN Calc, Up: Examples
Here is a sample run of `calc.y':
- % calc
+ $ calc
4 + 4.5 - (34/(8*3+-3))
6.880952381
-56 + 2
======================================
This example extends the infix notation calculator with location
-tracking. This feature will be used to improve error reporting, and
-provide better error messages.
-
- For the sake of clarity, we will switch for this example to an
-integer calculator, since most of the work needed to use locations will
-be done in the lexical analyser.
+tracking. This feature will be used to improve the error messages. For
+the sake of clarity, this example is a simple integer calculator, since
+most of the work needed to use locations will be done in the lexical
+analyser.
* Menu:
Declarations for `ltcalc'
-------------------------
- The C and Bison declarations for the location tracking calculator
-are the same as the declarations for the infix notation calculator.
+ The C and Bison declarations for the location tracking calculator are
+the same as the declarations for the infix notation calculator.
/* Location tracking calculator. */
%% /* Grammar follows */
- In the code above, there are no declarations specific to locations.
-Defining a data type for storing locations is not needed: we will use
-the type provided by default (*note Data Types of Locations: Location
-Type.), which is a four member structure with the following integer
-fields: `first_line', `first_column', `last_line' and `last_column'.
+Note there are no declarations specific to locations. Defining a data
+type for storing locations is not needed: we will use the type provided
+by default (*note Data Types of Locations: Location Type.), which is a
+four member structure with the following integer fields: `first_line',
+`first_column', `last_line' and `last_column'.
\1f
File: bison.info, Node: Ltcalc Rules, Next: Ltcalc Lexer, Prev: Ltcalc Decls, Up: Location Tracking Calc
Grammar Rules for `ltcalc'
--------------------------
- Whether you choose to handle locations or not has no effect on the
-syntax of your language. Therefore, grammar rules for this example
-will be very close to those of the previous example: we will only
-modify them to benefit from the new informations we will have.
+ Whether handling locations or not has no effect on the syntax of your
+language. Therefore, grammar rules for this example will be very close
+to those of the previous example: we will only modify them to benefit
+from the new information.
Here, we will use locations to report divisions by zero, and locate
the wrong expressions or subexpressions.
else
{
$$ = 1;
- printf("Division by zero, l%d,c%d-l%d,c%d",
- @3.first_line, @3.first_column,
- @3.last_line, @3.last_column);
+ fprintf (stderr, "%d.%d-%d.%d: division by zero",
+ @3.first_line, @3.first_column,
+ @3.last_line, @3.last_column);
}
}
| '-' exp %preg NEG { $$ = -$2; }
using the pseudo-variables `@N' for rule components, and the
pseudo-variable `@$' for groupings.
- In this example, we never assign a value to `@$', because the output
-parser can do this automatically. By default, before executing the C
-code of each action, `@$' is set to range from the beginning of `@1' to
-the end of `@N', for a rule with N components.
-
- Of course, this behavior can be redefined (*note Default Action for
-Locations: Location Default Action.), and for very specific rules, `@$'
-can be computed by hand.
+ We don't need to assign a value to `@$': the output parser does it
+automatically. By default, before executing the C code of each action,
+`@$' is set to range from the beginning of `@1' to the end of `@N', for
+a rule with N components. This behavior can be redefined (*note
+Default Action for Locations: Location Default Action.), and for very
+specific rules, `@$' can be computed by hand.
\1f
File: bison.info, Node: Ltcalc Lexer, Prev: Ltcalc Rules, Up: Location Tracking Calc
Until now, we relied on Bison's defaults to enable location
tracking. The next step is to rewrite the lexical analyser, and make it
-able to feed the parser with locations of tokens, as he already does
-for semantic values.
+able to feed the parser with the token locations, as it already does for
+semantic values.
- To do so, we must take into account every single character of the
+ To this end, we must take into account every single character of the
input text, to avoid the computed locations of being fuzzy or wrong:
int
return c;
}
- Basically, the lexical analyzer does the same processing as before:
-it skips blanks and tabs, and reads numbers or single-character tokens.
-In addition to this, it updates the `yylloc' global variable (of type
-`YYLTYPE'), where the location of tokens is stored.
+ Basically, the lexical analyzer performs the same processing as
+before: it skips blanks and tabs, and reads numbers or single-character
+tokens. In addition, it updates `yylloc', the global variable (of type
+`YYLTYPE') containing the token's location.
- Now, each time this function returns a token, the parser has it's
-number as well as it's semantic value, and it's position in the text.
-The last needed change is to initialize `yylloc', for example in the
+ Now, each time this function returns a token, the parser has its
+number as well as its semantic value, and its location in the text. The
+last needed change is to initialize `yylloc', for example in the
controlling function:
int
Remember that computing locations is not a matter of syntax. Every
character must be associated to a location update, whether it is in
-valid input, in comments, in literal strings, and so on...
+valid input, in comments, in literal strings, and so on.
\1f
File: bison.info, Node: Multi-function Calc, Next: Exercises, Prev: Location Tracking Calc, Up: Examples
to create named variables, store values in them, and use them later.
Here is a sample session with the multi-function calculator:
- % mfcalc
+ $ mfcalc
pi = 3.141592653589
3.1415926536
sin(pi)
0.8329091229
exp(ln(beta1))
2.3000000000
- %
+ $
Note that multiple assignment and nested function calls are
permitted.
Here is a simple C function subdivided into tokens:
+@ifinfo
@example
int /* @r{keyword `int'} */
-square (x) /* @r{identifier, open-paren,} */
- /* @r{identifier, close-paren} */
- int x; /* @r{keyword `int', identifier, semicolon} */
+square (int x) /* @r{identifier, open-paren, identifier,}
+ @r{identifier, close-paren} */
@{ /* @r{open-brace} */
- return x * x; /* @r{keyword `return', identifier,} */
- /* @r{asterisk, identifier, semicolon} */
+ return x * x; /* @r{keyword `return', identifier, asterisk,
+ identifier, semicolon} */
@} /* @r{close-brace} */
@end example
+@end ifinfo
+@ifnotinfo
+@example
+int /* @r{keyword `int'} */
+square (int x) /* @r{identifier, open-paren, identifier, identifier, close-paren} */
+@{ /* @r{open-brace} */
+ return x * x; /* @r{keyword `return', identifier, asterisk, identifier, semicolon} */
+@} /* @r{close-brace} */
+@end example
+@end ifnotinfo
The syntactic groupings of C include the expression, the statement, the
declaration, and the function definition. These are represented in the
@example
@group
# @r{List files in current directory.}
-% ls
+$ @kbd{ls}
rpcalc.tab.c rpcalc.y
@end group
@group
# @r{Compile the Bison parser.}
# @r{@samp{-lm} tells compiler to search math library for @code{pow}.}
-% cc rpcalc.tab.c -lm -o rpcalc
+$ @kbd{cc rpcalc.tab.c -lm -o rpcalc}
@end group
@group
# @r{List files again.}
-% ls
+$ @kbd{ls}
rpcalc rpcalc.tab.c rpcalc.y
@end group
@end example
example session using @code{rpcalc}.
@example
-% rpcalc
-4 9 +
+$ @kbd{rpcalc}
+@kbd{4 9 +}
13
-3 7 + 3 4 5 *+-
+@kbd{3 7 + 3 4 5 *+-}
-13
-3 7 + 3 4 5 * + - n @r{Note the unary minus, @samp{n}}
+@kbd{3 7 + 3 4 5 * + - n} @r{Note the unary minus, @samp{n}}
13
-5 6 / 4 n +
+@kbd{5 6 / 4 n +}
-3.166666667
-3 4 ^ @r{Exponentiation}
+@kbd{3 4 ^} @r{Exponentiation}
81
-^D @r{End-of-file indicator}
-%
+@kbd{^D} @r{End-of-file indicator}
+$
@end example
@node Infix Calc
@need 500
@example
-% calc
-4 + 4.5 - (34/(8*3+-3))
+$ @kbd{calc}
+@kbd{4 + 4.5 - (34/(8*3+-3))}
6.880952381
--56 + 2
+@kbd{-56 + 2}
-54
-3 ^ 2
+@kbd{3 ^ 2}
9
@end example
@cindex @code{ltcalc}
@cindex calculator, location tracking
-This example extends the infix notation calculator with location tracking.
-This feature will be used to improve error reporting, and provide better
-error messages.
-
-For the sake of clarity, we will switch for this example to an integer
-calculator, since most of the work needed to use locations will be done
-in the lexical analyser.
+This example extends the infix notation calculator with location
+tracking. This feature will be used to improve the error messages. For
+the sake of clarity, this example is a simple integer calculator, since
+most of the work needed to use locations will be done in the lexical
+analyser.
@menu
* Decls: Ltcalc Decls. Bison and C declarations for ltcalc.
@node Ltcalc Decls
@subsection Declarations for @code{ltcalc}
-The C and Bison declarations for the location tracking calculator are the same
-as the declarations for the infix notation calculator.
+The C and Bison declarations for the location tracking calculator are
+the same as the declarations for the infix notation calculator.
@example
/* Location tracking calculator. */
%% /* Grammar follows */
@end example
-In the code above, there are no declarations specific to locations. Defining
-a data type for storing locations is not needed: we will use the type provided
-by default (@pxref{Location Type, ,Data Types of Locations}), which is a four
-member structure with the following integer fields: @code{first_line},
-@code{first_column}, @code{last_line} and @code{last_column}.
+@noindent
+Note there are no declarations specific to locations. Defining a data
+type for storing locations is not needed: we will use the type provided
+by default (@pxref{Location Type, ,Data Types of Locations}), which is a
+four member structure with the following integer fields:
+@code{first_line}, @code{first_column}, @code{last_line} and
+@code{last_column}.
@node Ltcalc Rules
@subsection Grammar Rules for @code{ltcalc}
-Whether you choose to handle locations or not has no effect on the syntax of
-your language. Therefore, grammar rules for this example will be very close to
-those of the previous example: we will only modify them to benefit from the new
-informations we will have.
+Whether handling locations or not has no effect on the syntax of your
+language. Therefore, grammar rules for this example will be very close
+to those of the previous example: we will only modify them to benefit
+from the new information.
-Here, we will use locations to report divisions by zero, and locate the wrong
-expressions or subexpressions.
+Here, we will use locations to report divisions by zero, and locate the
+wrong expressions or subexpressions.
@example
@group
| exp '-' exp @{ $$ = $1 - $3; @}
| exp '*' exp @{ $$ = $1 * $3; @}
@end group
- | exp '/' exp
@group
+ | exp '/' exp
@{
if ($3)
$$ = $1 / $3;
else
@{
$$ = 1;
- printf("Division by zero, l%d,c%d-l%d,c%d",
- @@3.first_line, @@3.first_column,
- @@3.last_line, @@3.last_column);
+ fprintf (stderr, "%d.%d-%d.%d: division by zero",
+ @@3.first_line, @@3.first_column,
+ @@3.last_line, @@3.last_column);
@}
@}
@end group
using the pseudo-variables @code{@@@var{n}} for rule components, and the
pseudo-variable @code{@@$} for groupings.
-In this example, we never assign a value to @code{@@$}, because the
-output parser can do this automatically. By default, before executing
-the C code of each action, @code{@@$} is set to range from the beginning
-of @code{@@1} to the end of @code{@@@var{n}}, for a rule with @var{n}
-components.
-
-Of course, this behavior can be redefined (@pxref{Location Default
-Action, , Default Action for Locations}), and for very specific rules,
-@code{@@$} can be computed by hand.
+We don't need to assign a value to @code{@@$}: the output parser does it
+automatically. By default, before executing the C code of each action,
+@code{@@$} is set to range from the beginning of @code{@@1} to the end
+of @code{@@@var{n}}, for a rule with @var{n} components. This behavior
+can be redefined (@pxref{Location Default Action, , Default Action for
+Locations}), and for very specific rules, @code{@@$} can be computed by
+hand.
@node Ltcalc Lexer
@subsection The @code{ltcalc} Lexical Analyzer.
-Until now, we relied on Bison's defaults to enable location tracking. The next
-step is to rewrite the lexical analyser, and make it able to feed the parser
-with locations of tokens, as he already does for semantic values.
+Until now, we relied on Bison's defaults to enable location
+tracking. The next step is to rewrite the lexical analyser, and make it
+able to feed the parser with the token locations, as it already does for
+semantic values.
-To do so, we must take into account every single character of the input text,
-to avoid the computed locations of being fuzzy or wrong:
+To this end, we must take into account every single character of the
+input text, to avoid the computed locations of being fuzzy or wrong:
@example
@group
@}
@end example
-Basically, the lexical analyzer does the same processing as before: it skips
-blanks and tabs, and reads numbers or single-character tokens. In addition
-to this, it updates the @code{yylloc} global variable (of type @code{YYLTYPE}),
-where the location of tokens is stored.
+Basically, the lexical analyzer performs the same processing as before:
+it skips blanks and tabs, and reads numbers or single-character tokens.
+In addition, it updates @code{yylloc}, the global variable (of type
+@code{YYLTYPE}) containing the token's location.
-Now, each time this function returns a token, the parser has it's number as
-well as it's semantic value, and it's position in the text. The last needed
-change is to initialize @code{yylloc}, for example in the controlling
-function:
+Now, each time this function returns a token, the parser has its number
+as well as its semantic value, and its location in the text. The last
+needed change is to initialize @code{yylloc}, for example in the
+controlling function:
@example
+@group
int
main (void)
@{
yylloc.first_column = yylloc.last_column = 0;
return yyparse ();
@}
+@end group
@end example
-Remember that computing locations is not a matter of syntax. Every character
-must be associated to a location update, whether it is in valid input, in
-comments, in literal strings, and so on...
+Remember that computing locations is not a matter of syntax. Every
+character must be associated to a location update, whether it is in
+valid input, in comments, in literal strings, and so on.
@node Multi-function Calc
@section Multi-Function Calculator: @code{mfcalc}
Here is a sample session with the multi-function calculator:
@example
-% mfcalc
-pi = 3.141592653589
+$ @kbd{mfcalc}
+@kbd{pi = 3.141592653589}
3.1415926536
-sin(pi)
+@kbd{sin(pi)}
0.0000000000
-alpha = beta1 = 2.3
+@kbd{alpha = beta1 = 2.3}
2.3000000000
-alpha
+@kbd{alpha}
2.3000000000
-ln(alpha)
+@kbd{ln(alpha)}
0.8329091229
-exp(ln(beta1))
+@kbd{exp(ln(beta1))}
2.3000000000
-%
+$
@end example
Note that multiple assignment and nested function calls are permitted.