home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 5
/
FreshFish_July-August1994.bin
/
bbs
/
gnu
/
bison-1.22-bin.lha
/
info
/
bison.info-3
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1994-07-11
|
51KB
|
982 lines
This is Info file bison.info, produced by Makeinfo-1.54 from the input
file /home/gd2/gnu/bison/bison.texinfo.
This file documents the Bison parser generator.
Copyright (C) 1988, 1989, 1990, 1991, 1992 Free Software Foundation,
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the sections entitled "GNU General Public License" and "Conditions
for Using Bison" are included exactly as in the original, and provided
that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the sections entitled "GNU General Public
License", "Conditions for Using Bison" and this permission notice may be
included in translations approved by the Free Software Foundation
instead of in the original English.
File: bison.info, Node: Mid-Rule Actions, Prev: Action Types, Up: Semantics
Actions in Mid-Rule
-------------------
Occasionally it is useful to put an action in the middle of a rule.
These actions are written just like usual end-of-rule actions, but they
are executed before the parser even recognizes the following components.
A mid-rule action may refer to the components preceding it using
`$N', but it may not refer to subsequent components because it is run
before they are parsed.
The mid-rule action itself counts as one of the components of the
rule. This makes a difference when there is another action later in
the same rule (and usually there is another at the end): you have to
count the actions along with the symbols when working out which number
N to use in `$N'.
The mid-rule action can also have a semantic value. The action can
set its value with an assignment to `$$', and actions later in the rule
can refer to the value using `$N'. Since there is no symbol to name
the action, there is no way to declare a data type for the value in
advance, so you must use the `$<...>' construct to specify a data type
each time you refer to this value.
There is no way to set the value of the entire rule with a mid-rule
action, because assignments to `$$' do not have that effect. The only
way to set the value for the entire rule is with an ordinary action at
the end of the rule.
Here is an example from a hypothetical compiler, handling a `let'
statement that looks like `let (VARIABLE) STATEMENT' and serves to
create a variable named VARIABLE temporarily for the duration of
STATEMENT. To parse this construct, we must put VARIABLE into the
symbol table while STATEMENT is parsed, then remove it afterward. Here
is how it is done:
stmt: LET '(' var ')'
{ $<context>$ = push_context ();
declare_variable ($3); }
stmt { $$ = $6;
pop_context ($<context>5); }
As soon as `let (VARIABLE)' has been recognized, the first action is
run. It saves a copy of the current semantic context (the list of
accessible variables) as its semantic value, using alternative
`context' in the data-type union. Then it calls `declare_variable' to
add the new variable to that list. Once the first action is finished,
the embedded statement `stmt' can be parsed. Note that the mid-rule
action is component number 5, so the `stmt' is component number 6.
After the embedded statement is parsed, its semantic value becomes
the value of the entire `let'-statement. Then the semantic value from
the earlier action is used to restore the prior list of variables. This
removes the temporary `let'-variable from the list so that it won't
appear to exist while the rest of the program is parsed.
Taking action before a rule is completely recognized often leads to
conflicts since the parser must commit to a parse in order to execute
the action. For example, the following two rules, without mid-rule
actions, can coexist in a working parser because the parser can shift
the open-brace token and look at what follows before deciding whether
there is a declaration or not:
compound: '{' declarations statements '}'
| '{' statements '}'
;
But when we add a mid-rule action as follows, the rules become
nonfunctional:
compound: { prepare_for_local_variables (); }
'{' declarations statements '}'
| '{' statements '}'
;
Now the parser is forced to decide whether to run the mid-rule action
when it has read no farther than the open-brace. In other words, it
must commit to using one rule or the other, without sufficient
information to do it correctly. (The open-brace token is what is called
the "look-ahead" token at this time, since the parser is still deciding
what to do about it. *Note Look-Ahead Tokens: Look-Ahead.)
You might think that you could correct the problem by putting
identical actions into the two rules, like this:
compound: { prepare_for_local_variables (); }
'{' declarations statements '}'
| { prepare_for_local_variables (); }
'{' statements '}'
;
But this does not help, because Bison does not realize that the two
actions are identical. (Bison never tries to understand the C code in
an action.)
If the grammar is such that a declaration can be distinguished from a
statement by the first token (which is true in C), then one solution
which does work is to put the action after the open-brace, like this:
compound: '{' { prepare_for_local_variables (); }
declarations statements '}'
| '{' statements '}'
;
Now the first token of the following declaration or statement, which
would in any case tell Bison which rule to use, can still do so.
Another solution is to bury the action inside a nonterminal symbol
which serves as a subroutine:
subroutine: /* empty */
{ prepare_for_local_variables (); }
;
compound: subroutine
'{' declarations statements '}'
| subroutine
'{' statements '}'
;
Now Bison can execute the action in the rule for `subroutine' without
deciding which rule for `compound' it will eventually use. Note that
the action is now at the end of its rule. Any mid-rule action can be
converted to an end-of-rule action in this way, and this is what Bison
actually does to implement mid-rule actions.
File: bison.info, Node: Declarations, Next: Multiple Parsers, Prev: Semantics, Up: Grammar File
Bison Declarations
==================
The "Bison declarations" section of a Bison grammar defines the
symbols used in formulating the grammar and the data types of semantic
values. *Note Symbols::.
All token type names (but not single-character literal tokens such as
`'+'' and `'*'') must be declared. Nonterminal symbols must be
declared if you need to specify which data type to use for the semantic
value (*note More Than One Value Type: Multiple Types.).
The first rule in the file also specifies the start symbol, by
default. If you want some other symbol to be the start symbol, you
must declare it explicitly (*note Languages and Context-Free Grammars:
Language and Grammar.).
* Menu:
* Token Decl:: Declaring terminal symbols.
* Precedence Decl:: Declaring terminals with precedence and associativity.
* Union Decl:: Declaring the set of all semantic value types.
* Type Decl:: Declaring the choice of type for a nonterminal symbol.
* Expect Decl:: Suppressing warnings about shift/reduce conflicts.
* Start Decl:: Specifying the start symbol.
* Pure Decl:: Requesting a reentrant parser.
* Decl Summary:: Table of all Bison declarations.
File: bison.info, Node: Token Decl, Next: Precedence Decl, Up: Declarations
Token Type Names
----------------
The basic way to declare a token type name (termin