Rules
Previous: <Symbols=>Symbols> * Next: <Recursion=>Recursion> * Up: <Grammar File=>GrammarFil>

#Wrap on
{fH3}Syntax of Grammar Rules{f}

A Bison grammar rule has the following general form:

#Wrap off
#fCode
{fStrong}result{f}: {fStrong}components{f}
        ;
#f
#Wrap on

where {fStrong}result{f} is the nonterminal symbol that this rule describes
and {fStrong}components{f} are various terminal and nonterminal symbols that
are put together by this rule (\*Note <Symbols=>Symbols>).  

For example,

#Wrap off
#fCode
exp:      exp '+' exp
        ;
#f
#Wrap on

says that two groupings of type {fCode}exp{f}, with a {fEmphasis}+{f} token in between,
can be combined into a larger grouping of type {fCode}exp{f}.

Whitespace in rules is significant only to separate symbols.  You can add
extra whitespace as you wish.

Scattered among the components can be {fStrong}actions{f} that determine
the semantics of the rule.  An action looks like this:

#Wrap off
#fCode
\{{fStrong}C statements{f}\}
#f
#Wrap on

Usually there is only one action and it follows the components.
\*Note <Actions=>Actions>.

Multiple rules for the same {fStrong}result{f} can be written separately or can
be joined with the vertical-bar character {fEmphasis}|{f} as follows:

#Wrap off
#fCode
{fStrong}result{f}:   {fStrong}rule1-components{f}
        | {fStrong}rule2-components{f}
        
        ;
#f
#Wrap on

They are still considered distinct rules even when joined in this way.

If {fStrong}components{f} in a rule is empty, it means that {fStrong}result{f} can
match the empty string.  For example, here is how to define a
comma-separated sequence of zero or more {fCode}exp{f} groupings:

#Wrap off
#fCode
expseq:   \/\* empty \*\/
        | expseq1
        ;

expseq1:  exp
        | expseq1 ',' exp
        ;
#f
#Wrap on

It is customary to write a comment {fEmphasis}\/\* empty \*\/{f} in each rule
with no components.

