[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
FYI: Both: Protect YYSTYPE
From: |
Akim Demaille |
Subject: |
FYI: Both: Protect YYSTYPE |
Date: |
05 Nov 2001 10:12:11 +0100 |
User-agent: |
Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Artificial Intelligence) |
I think it'd be cool if the header could always be included in the
parser.
I realized a few shortcomings with the current headers: for instance
we don't define yydebug, nor yypatch etc. We should IMHO. Also, the
support for --prefix-name is lacking. But we can't just issue some
#define yyparse fooparse, because that is still a global definition of
yyparse. We really need to issue a proto of fooparse.
This is to be addressed in the trunk.
Index: ChangeLog
from Akim Demaille <address@hidden>
* src/reader.c (parse_union_decl): Output yystype/YYSTYPE as we do
with yyltype/YYLTYPE. This allows inclusion of the generated
header within the parser if the compiler, such as GGC, accepts
multiple equivalent #defines.
Index: doc/bison.texinfo
--- doc/bison.texinfo Sun, 04 Nov 2001 17:13:49 +0100 akim
+++ doc/bison.texinfo Sun, 04 Nov 2001 20:13:25 +0100 akim
@@ -855,11 +855,12 @@ @node Rpcalc Decls
preprocessor directives.
The @code{#define} directive defines the macro @code{YYSTYPE}, thus
-specifying the C data type for semantic values of both tokens and groupings
-(@pxref{Value Type, ,Data Types of Semantic Values}). The Bison parser will
use whatever type
address@hidden is defined as; if you don't define it, @code{int} is the
-default. Because we specify @code{double}, each token and each expression
-has an associated value, which is a floating point number.
+specifying the C data type for semantic values of both tokens and
+groupings (@pxref{Value Type, ,Data Types of Semantic Values}). The
+Bison parser will use whatever type @code{YYSTYPE} is defined as; if you
+don't define it, @code{int} is the default. Because we specify
address@hidden, each token and each expression has an associated value,
+which is a floating point number.
The @code{#include} directive is used to declare the exponentiation
function @code{pow}.
@@ -1066,10 +1067,11 @@ @node Rpcalc Lexer
macro whose definition is the appropriate number. In this example,
therefore, @code{NUM} becomes a macro for @code{yylex} to use.
-The semantic value of the token (if it has one) is stored into the global
-variable @code{yylval}, which is where the Bison parser will look for it.
-(The C data type of @code{yylval} is @code{YYSTYPE}, which was defined
-at the beginning of the grammar; @pxref{Rpcalc Decls, ,Declarations for
@code{rpcalc}}.)
+The semantic value of the token (if it has one) is stored into the
+global variable @code{yylval}, which is where the Bison parser will look
+for it. (The C data type of @code{yylval} is @code{YYSTYPE}, which was
+defined at the beginning of the grammar; @pxref{Rpcalc Decls,
+,Declarations for @code{rpcalc}}.)
A token type code of zero is returned if the end-of-file is encountered.
(Bison recognizes any nonpositive value as indicating the end of the
@@ -2374,7 +2376,8 @@ @node Value Type
In a simple program it may be sufficient to use the same data type for
the semantic values of all language constructs. This was true in the
-RPN and infix calculator examples (@pxref{RPN Calc, ,Reverse Polish Notation
Calculator}).
+RPN and infix calculator examples (@pxref{RPN Calc, ,Reverse Polish
+Notation Calculator}).
Bison's default is to use type @code{int} for all semantic values. To
specify some other type, define @code{YYSTYPE} as a macro, like this:
Index: src/reader.c
--- src/reader.c Sun, 04 Nov 2001 17:13:49 +0100 akim
+++ src/reader.c Sun, 04 Nov 2001 20:35:07 +0100 akim
@@ -711,6 +711,13 @@
{
int c;
int count = 0;
+ const char *prologue = "\
+#ifndef YYSTYPE\n\
+typedef union";
+ const char *epilogue = "\
+ yystype;\n\
+# define YYSTYPE yystype\n\
+#endif\n";
if (typed)
complain (_("multiple %s declarations"), "%union");
@@ -723,9 +730,9 @@
else
obstack_1grow (&attrs_obstack, '\n');
- obstack_sgrow (&attrs_obstack, "typedef union");
+ obstack_sgrow (&attrs_obstack, prologue);
if (defines_flag)
- obstack_sgrow (&defines_obstack, "typedef union");
+ obstack_sgrow (&defines_obstack, prologue);
c = getc (finput);
@@ -760,9 +767,9 @@
count--;
if (count <= 0)
{
- obstack_sgrow (&attrs_obstack, " YYSTYPE;\n");
+ obstack_sgrow (&attrs_obstack, epilogue);
if (defines_flag)
- obstack_sgrow (&defines_obstack, " YYSTYPE;\n");
+ obstack_sgrow (&defines_obstack, epilogue);
/* JF don't choke on trailing semi */
c = skip_white_space ();
if (c != ';')
@@ -1955,7 +1962,6 @@
/* Read the declaration section. Copy %{ ... %} groups to
TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
etc. found there. */
- obstack_1grow (&table_obstack, '\n');
obstack_fgrow3 (&table_obstack, "\
/* %s, made from %s\n\
by GNU bison %s. */\n\
- FYI: Both: Protect YYSTYPE,
Akim Demaille <=