[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 6/6] doc: api.value.type union.
From: |
Akim Demaille |
Subject: |
Re: [PATCH 6/6] doc: api.value.type union. |
Date: |
Mon, 25 Feb 2013 12:09:54 +0100 |
Le 23 févr. 2013 à 17:00, Akim Demaille <address@hidden> a écrit :
> * doc/bison.texi (Type Generation): New section.
I will squash the following patch, to demonstrate the use
of api.value.type=union in the doc. There is sill another
full example with %union, so I'm not removing anything here.
commit 12676d2f7739c3f5010840eae95d41a31b7bb93c
Author: Akim Demaille <address@hidden>
Date: Mon Feb 25 12:03:30 2013 +0100
squash! doc: api.value.type union.
* doc/bison.texi (Multi-function Calc): Convert to use
api.value.type=union.
diff --git a/doc/bison.texi b/doc/bison.texi
index f88b8d6..fd23963 100644
--- a/doc/bison.texi
+++ b/doc/bison.texi
@@ -2413,15 +2413,10 @@ Here are the C and Bison declarations for the
multi-function calculator.
address@hidden
@end group
address@hidden
-%union @{
- double val; /* For returning numbers. */
- symrec *tptr; /* For returning symbol-table pointers. */
address@hidden
address@hidden group
-%token <val> NUM /* Simple double precision number. */
-%token <tptr> VAR FNCT /* Variable and function. */
-%type <val> exp
+%define api.value.type union /* Generate YYSTYPE from these types: */
+%token <double> NUM /* Simple double precision number. */
+%token <symrec*> VAR FNCT /* Symbol table pointer: variable and function.
*/
+%type <double> exp
@group
%precedence '='
@@ -2436,23 +2431,23 @@ The above grammar introduces only two new features of
the Bison language.
These features allow semantic values to have various data types
(@pxref{Multiple Types, ,More Than One Value Type}).
-The @code{%union} declaration specifies the entire list of possible types;
-this is instead of defining @code{api.value.type}. The allowable types are now
-double-floats (for @code{exp} and @code{NUM}) and pointers to entries in
-the symbol table. @xref{Union Decl, ,The Union Declaration}.
-
-Since values can now have various types, it is necessary to associate a
-type with each grammar symbol whose semantic value is used. These symbols
-are @code{NUM}, @code{VAR}, @code{FNCT}, and @code{exp}. Their
-declarations are augmented with information about their data type (placed
-between angle brackets).
-
-The Bison construct @code{%type} is used for declaring nonterminal
-symbols, just as @code{%token} is used for declaring token types. We
-have not used @code{%type} before because nonterminal symbols are
-normally declared implicitly by the rules that define them. But
address@hidden must be declared explicitly so we can specify its value type.
address@hidden Decl, ,Nonterminal Symbols}.
+The special @code{union} value assigned to the @code{%define} variable
address@hidden specifies that the symbols are defined with their data
+types. Bison will generate an appropriate definition of @code{YYSTYPE} to
+store these values.
+
+Since values can now have various types, it is necessary to associate a type
+with each grammar symbol whose semantic value is used. These symbols are
address@hidden, @code{VAR}, @code{FNCT}, and @code{exp}. Their declarations are
+augmented with their data type (placed between angle brackets). For
+instance, values of @code{NUM} are stored in @code{double}.
+
+The Bison construct @code{%type} is used for declaring nonterminal symbols,
+just as @code{%token} is used for declaring token types. Previously we did
+not use @code{%type} before because nonterminal symbols are normally
+declared implicitly by the rules that define them. But @code{exp} must be
+declared explicitly so we can specify its value type. @xref{Type Decl,
+,Nonterminal Symbols}.
@node Mfcalc Rules
@subsection Grammar Rules for @code{mfcalc}
@@ -2676,11 +2671,18 @@ yylex (void)
if (c == '.' || isdigit (c))
@{
ungetc (c, stdin);
- scanf ("%lf", &yylval.val);
+ scanf ("%lf", &yylval.yytype_NUM);
return NUM;
@}
@end group
address@hidden example
+
address@hidden
+Bison generated a definition of @code{YYSTYPE} with a member named
address@hidden to store value of @code{NUM} symbols.
address@hidden file: mfcalc.y: 3
address@hidden
@group
/* Char starts an identifier => read the name. */
if (isalpha (c))
@@ -2722,7 +2724,7 @@ yylex (void)
s = getsym (symbuf);
if (s == 0)
s = putsym (symbuf, VAR);
- yylval.tptr = s;
+ *((symrec**) &yylval) = s;
return s->type;
@}
@@ -9624,7 +9626,7 @@ prologue:
/* Formatting semantic values. */
%printer @{ fprintf (yyoutput, "%s", $$->name); @} VAR;
%printer @{ fprintf (yyoutput, "%s()", $$->name); @} FNCT;
-%printer @{ fprintf (yyoutput, "%g", $$); @} <val>;
+%printer @{ fprintf (yyoutput, "%g", $$); @} <double>;
@end example
The @code{%define} directive instructs Bison to generate run-time trace
@@ -9637,8 +9639,8 @@ ill-named) @code{%verbose} directive.
The set of @code{%printer} directives demonstrates how to format the
semantic value in the traces. Note that the specification can be done
either on the symbol type (e.g., @code{VAR} or @code{FNCT}), or on the type
-tag: since @code{<val>} is the type for both @code{NUM} and @code{exp}, this
-printer will be used for them.
+tag: since @code{<double>} is the type for both @code{NUM} and @code{exp},
+this printer will be used for them.
Here is a sample of the information provided by run-time traces. The traces
are sent onto standard error.
@@ -9688,7 +9690,7 @@ Entering state 24
@noindent
The previous reduction demonstrates the @code{%printer} directive for
address@hidden<val>}: both the token @code{NUM} and the resulting nonterminal
address@hidden<double>}: both the token @code{NUM} and the resulting nonterminal
@code{exp} have @samp{1} as value.
@example
- [PATCH 5/6] doc: move the section about "%union" where types are discussed, (continued)
- [PATCH 5/6] doc: move the section about "%union" where types are discussed, Akim Demaille, 2013/02/23
- [PATCH 4/6] doc: deprecate #define YYSTYPE in favor of %define api.value.type, Akim Demaille, 2013/02/23
- [PATCH 1/6] api.value.type: implement proper support, check, and document, Akim Demaille, 2013/02/23
- Re: [PATCH 0/6] api.value.type support, Paul Eggert, 2013/02/23
- [PATCH 0/6] api.value.type support (was: rename variant), Akim Demaille, 2013/02/23
- [PATCH 4/6] doc: deprecate #define YYSTYPE in favor of %define api.value.type, Akim Demaille, 2013/02/23
- [PATCH 3/6] value type: accept "->" in type tags, Akim Demaille, 2013/02/23
- [PATCH 2/6] style: simplify the scanning of type tags, Akim Demaille, 2013/02/23
- [PATCH 6/6] doc: api.value.type union., Akim Demaille, 2013/02/23
- Re: [PATCH 6/6] doc: api.value.type union.,
Akim Demaille <=
- [PATCH 5/6] doc: move the section about "%union" where types are discussed, Akim Demaille, 2013/02/23
- [PATCH 1/6] api.value.type: implement proper support, check, and document, Akim Demaille, 2013/02/23
- Re: [PATCH 0/6] api.value.type support (was: rename variant), Joel E. Denny, 2013/02/24