47 |
This file documents the Bison parser generator. |
This file documents the Bison parser generator. |
48 |
|
|
49 |
Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1995, 1998, 1999, |
Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1995, 1998, 1999, |
50 |
2000, 2001 |
2000, 2001, 2002 |
51 |
Free Software Foundation, Inc. |
Free Software Foundation, Inc. |
52 |
|
|
53 |
Permission is granted to make and distribute verbatim copies of |
Permission is granted to make and distribute verbatim copies of |
89 |
@page |
@page |
90 |
@vskip 0pt plus 1filll |
@vskip 0pt plus 1filll |
91 |
Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1995, 1998, |
Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1995, 1998, |
92 |
1999, 2000, 2001 |
1999, 2000, 2001, 2002 |
93 |
Free Software Foundation, Inc. |
Free Software Foundation, Inc. |
94 |
|
|
95 |
@sp 2 |
@sp 2 |
1083 |
represents a token type. The same text used in Bison rules to stand for |
represents a token type. The same text used in Bison rules to stand for |
1084 |
this token type is also a C expression for the numeric code for the type. |
this token type is also a C expression for the numeric code for the type. |
1085 |
This works in two ways. If the token type is a character literal, then its |
This works in two ways. If the token type is a character literal, then its |
1086 |
numeric code is the ASCII code for that character; you can use the same |
numeric code is that of the character; you can use the same |
1087 |
character literal in the lexical analyzer to express the number. If the |
character literal in the lexical analyzer to express the number. If the |
1088 |
token type is an identifier, that identifier is defined by Bison as a C |
token type is an identifier, that identifier is defined by Bison as a C |
1089 |
macro whose definition is the appropriate number. In this example, |
macro whose definition is the appropriate number. In this example, |
1104 |
@example |
@example |
1105 |
@group |
@group |
1106 |
/* Lexical analyzer returns a double floating point |
/* Lexical analyzer returns a double floating point |
1107 |
number on the stack and the token NUM, or the ASCII |
number on the stack and the token NUM, or the numeric code |
1108 |
character read if not a number. Skips all blanks |
of the character read if not a number. Skips all blanks |
1109 |
and tabs, returns 0 for EOF. */ |
and tabs, returns 0 for EOF. */ |
1110 |
|
|
1111 |
#include <ctype.h> |
#include <ctype.h> |
2148 |
|
|
2149 |
All the usual escape sequences used in character literals in C can be |
All the usual escape sequences used in character literals in C can be |
2150 |
used in Bison as well, but you must not use the null character as a |
used in Bison as well, but you must not use the null character as a |
2151 |
character literal because its ASCII code, zero, is the code @code{yylex} |
character literal because its numeric code, zero, is the code @code{yylex} |
2152 |
returns for end-of-input (@pxref{Calling Convention, ,Calling Convention |
returns for end-of-input (@pxref{Calling Convention, ,Calling Convention |
2153 |
for @code{yylex}}). |
for @code{yylex}}). |
2154 |
|
|
2189 |
The value returned by @code{yylex} is always one of the terminal symbols |
The value returned by @code{yylex} is always one of the terminal symbols |
2190 |
(or 0 for end-of-input). Whichever way you write the token type in the |
(or 0 for end-of-input). Whichever way you write the token type in the |
2191 |
grammar rules, you write it the same way in the definition of @code{yylex}. |
grammar rules, you write it the same way in the definition of @code{yylex}. |
2192 |
The numeric code for a character token type is simply the ASCII code for |
The numeric code for a character token type is simply the numeric code of |
2193 |
the character, so @code{yylex} can use the identical character constant to |
the character, so @code{yylex} can use the identical character constant to |
2194 |
generate the requisite code. Each named token type becomes a C macro in |
generate the requisite code. Each named token type becomes a C macro in |
2195 |
the parser file, so @code{yylex} can use the name to stand for the code. |
the parser file, so @code{yylex} can use the name to stand for the code. |
2202 |
into a separate header file @file{@var{name}.tab.h} which you can include |
into a separate header file @file{@var{name}.tab.h} which you can include |
2203 |
in the other source files that need it. @xref{Invocation, ,Invoking Bison}. |
in the other source files that need it. @xref{Invocation, ,Invoking Bison}. |
2204 |
|
|
2205 |
|
The @code{yylex} function must use the same character set and encoding |
2206 |
|
that was used by Bison. For example, if you run Bison in an |
2207 |
|
@sc{ascii} environment, but then compile and run the resulting program |
2208 |
|
in an environment that uses an incompatible character set like |
2209 |
|
@sc{ebcdic}, the resulting program will probably not work because the |
2210 |
|
tables generated by Bison will assume @sc{ascii} numeric values for |
2211 |
|
character tokens. Portable grammars should avoid non-@sc{ascii} |
2212 |
|
character tokens, as implementations in practice often use different |
2213 |
|
and incompatible extensions in this area. However, it is standard |
2214 |
|
practice for software distributions to contain C source files that |
2215 |
|
were generated by Bison in an @sc{ascii} environment, so installers on |
2216 |
|
platforms that are incompatible with @sc{ascii} must rebuild those |
2217 |
|
files before compiling them. |
2218 |
|
|
2219 |
The symbol @code{error} is a terminal symbol reserved for error recovery |
The symbol @code{error} is a terminal symbol reserved for error recovery |
2220 |
(@pxref{Error Recovery}); you shouldn't use it for any other purpose. |
(@pxref{Error Recovery}); you shouldn't use it for any other purpose. |
2221 |
In particular, @code{yylex} should never return this value. |
In particular, @code{yylex} should never return this value. |
2222 |
|
The default value of the error token is 256, so in the |
2223 |
|
unlikely event that you need to use a character token with numeric |
2224 |
|
value 256 you must reassign the error token's value with a |
2225 |
|
@code{%token} declaration. |
2226 |
|
|
2227 |
@node Rules |
@node Rules |
2228 |
@section Syntax of Grammar Rules |
@section Syntax of Grammar Rules |
2960 |
@noindent |
@noindent |
2961 |
It is generally best, however, to let Bison choose the numeric codes for |
It is generally best, however, to let Bison choose the numeric codes for |
2962 |
all token types. Bison will automatically select codes that don't conflict |
all token types. Bison will automatically select codes that don't conflict |
2963 |
with each other or with ASCII characters. |
with each other or with normal characters. |
2964 |
|
|
2965 |
In the event that the stack type is a union, you must augment the |
In the event that the stack type is a union, you must augment the |
2966 |
@code{%token} or other token declaration to include the data type |
@code{%token} or other token declaration to include the data type |