symbols: document the overhaul of symbol declarations

* doc/bison.texi (Symbol Decls): New.
This commit is contained in:
Akim Demaille
2018-12-16 17:02:49 +01:00
parent 8e5b1f40ae
commit 1099b8dc26
2 changed files with 75 additions and 9 deletions

26
NEWS
View File

@@ -159,8 +159,30 @@ GNU Bison NEWS
** Changes ** Changes
The C++ output now use noexcept and constexpr. Please, report missing *** Parsers in C++
annotations.
They now use noexcept and constexpr. Please, report missing annotations.
*** Symbol Declarations
The syntax of the variation directives to declare symbols was overhauled
for more consistency, and also better POSIX Yacc compliance (which, for
instance, allows "%type" without actually providing a type). The %nterm
directive, supported by Bison since its inception, is now documented and
officially supported.
The syntax is now as follows:
%token TAG? ( ID NUMBER? STRING? )+ ( TAG ( ID NUMBER? STRING? )+ )*
%left TAG? ( ID NUMBER?)+ ( TAG ( ID NUMBER? )+ )*
%type TAG? ( ID | CHAR | STRING )+ ( TAG ( ID | CHAR | STRING )+ )*
%nterm TAG? ID+ ( TAG ID+ )*
where TAG denotes a type tag such as <ival>, ID denotes an identifier
such as NUM, NUMBER a decimal or hexadecimal integer such as 300 or
0x12d, CHAR a character literal such as '+', and STRING a string
literal such as "number". The postfix quantifiers are ? (zero or
one), * (zero or more) and + (one or more).
* Noteworthy changes in release 3.2.2 (2018-11-21) [stable] * Noteworthy changes in release 3.2.2 (2018-11-21) [stable]

View File

@@ -239,6 +239,7 @@ Bison Declarations
* Token Decl:: Declaring terminal symbols. * Token Decl:: Declaring terminal symbols.
* Precedence Decl:: Declaring terminals with precedence and associativity. * Precedence Decl:: Declaring terminals with precedence and associativity.
* Type Decl:: Declaring the choice of type for a nonterminal symbol. * Type Decl:: Declaring the choice of type for a nonterminal symbol.
* Symbol Decls:: Summary of the Syntax of Symbol Declarations.
* Initial Action Decl:: Code run before parsing starts. * Initial Action Decl:: Code run before parsing starts.
* Destructor Decl:: Declaring how symbols are freed. * Destructor Decl:: Declaring how symbols are freed.
* Printer Decl:: Declaring how symbol values are displayed. * Printer Decl:: Declaring how symbol values are displayed.
@@ -4757,6 +4758,7 @@ and Context-Free Grammars}).
* Token Decl:: Declaring terminal symbols. * Token Decl:: Declaring terminal symbols.
* Precedence Decl:: Declaring terminals with precedence and associativity. * Precedence Decl:: Declaring terminals with precedence and associativity.
* Type Decl:: Declaring the choice of type for a nonterminal symbol. * Type Decl:: Declaring the choice of type for a nonterminal symbol.
* Symbol Decls:: Summary of the Syntax of Symbol Declarations.
* Initial Action Decl:: Code run before parsing starts. * Initial Action Decl:: Code run before parsing starts.
* Destructor Decl:: Declaring how symbols are freed. * Destructor Decl:: Declaring how symbols are freed.
* Printer Decl:: Declaring how symbol values are displayed. * Printer Decl:: Declaring how symbol values are displayed.
@@ -4804,7 +4806,7 @@ The basic way to declare a token type name (terminal symbol) is as follows:
%token @var{name} %token @var{name}
@end example @end example
Bison will convert this into a @code{#define} directive in the parser, so Bison will convert this into a definition in the parser, so
that the function @code{yylex} (if it is in this file) can use the name that the function @code{yylex} (if it is in this file) can use the name
@var{name} to stand for this token type's code. @var{name} to stand for this token type's code.
@@ -4945,6 +4947,7 @@ a separate token. For example:
@subsection Nonterminal Symbols @subsection Nonterminal Symbols
@cindex declaring value types, nonterminals @cindex declaring value types, nonterminals
@cindex value types, nonterminals, declaring @cindex value types, nonterminals, declaring
@findex %nterm
@findex %type @findex %type
@noindent @noindent
@@ -4963,9 +4966,40 @@ is the name given in the @code{%union} to the alternative that you want
nonterminal symbols in the same @code{%type} declaration, if they have the nonterminal symbols in the same @code{%type} declaration, if they have the
same value type. Use spaces to separate the symbol names. same value type. Use spaces to separate the symbol names.
You can also declare the value type of a terminal symbol. To do this, use While POSIX Yacc allows @code{%type} only for nonterminals, Bison accepts
the same @code{<@var{type}>} construction in a declaration for the terminal that this directive be also applied to terminal symbols. To declare
symbol. All kinds of token declarations allow @code{<@var{type}>}. exclusively nonterminal symbols, use @code{%nterm}:
@example
%nterm <@var{type}> @var{nonterminal}@dots{}
@end example
@node Symbol Decls
@subsection Syntax of Symbol Declarations
@findex %left
@findex %nterm
@findex %token
@findex %type
The syntax of the various directives to declare symbols is as follows.
@example
%token @var{tag}? ( @var{id} @var{number}? @var{string}? )+ ( @var{tag} ( @var{id} @var{number}? @var{string}? )+ )*
%left @var{tag}? ( @var{id} @var{number}?)+ ( @var{tag} ( @var{id} @var{number}? )+ )*
%type @var{tag}? ( @var{id} | @var{char} | @var{string} )+ ( @var{tag} ( @var{id} | @var{char} | @var{string} )+ )*
%nterm @var{tag}? @var{id}+ ( @var{tag} @var{id}+ )*
@end example
@noindent
where @var{tag} denotes a type tag such as @samp{<ival>}, @var{id} denotes
an identifier such as @samp{NUM}, @var{number} a decimal or hexadecimal
integer such as @samp{300} or @samp{0x12d}, @var{char} a character literal
such as @samp{'+'}, and @var{string} a string literal such as
@samp{"number"}. The postfix quantifiers are @samp{?} (zero or one),
@samp{*} (zero or more) and @samp{+} (one or more).
The directives @code{%precedence}, @code{%right} and @code{%nonassoc} behave
like @code{%left}.
@node Initial Action Decl @node Initial Action Decl
@subsection Performing Actions before Parsing @subsection Performing Actions before Parsing
@@ -5522,9 +5556,14 @@ Assign a precedence to rules lacking an explicit @code{%prec} modifier
@end deffn @end deffn
@end ifset @end ifset
@deffn {Directive} %nterm
Declare the type of semantic values for a nonterminal symbol (@pxref{Type
Decl, ,Nonterminal Symbols}).
@end deffn
@deffn {Directive} %type @deffn {Directive} %type
Declare the type of semantic values for a nonterminal symbol Declare the type of semantic values for a symbol (@pxref{Type Decl,
(@pxref{Type Decl, ,Nonterminal Symbols}). ,Nonterminal Symbols}).
@end deffn @end deffn
@deffn {Directive} %start @deffn {Directive} %start
@@ -13439,6 +13478,11 @@ Bison declaration to assign precedence and nonassociativity to token(s).
@xref{Precedence Decl, ,Operator Precedence}. @xref{Precedence Decl, ,Operator Precedence}.
@end deffn @end deffn
@deffn {Directive} %nterm
Bison declaration to declare nonterminals. @xref{Type Decl, ,Nonterminal
Symbols}.
@end deffn
@deffn {Directive} %output "@var{file}" @deffn {Directive} %output "@var{file}"
Bison declaration to set the name of the parser implementation file. Bison declaration to set the name of the parser implementation file.
@xref{Decl Summary}. @xref{Decl Summary}.
@@ -13502,7 +13546,7 @@ implementation file. @xref{Decl Summary}.
@end deffn @end deffn
@deffn {Directive} %type @deffn {Directive} %type
Bison declaration to declare nonterminals. @xref{Type Decl, Bison declaration to declare symbol value types. @xref{Type Decl,
,Nonterminal Symbols}. ,Nonterminal Symbols}.
@end deffn @end deffn