www.delorie.com/gnu/docs/bison/bison_46.html   search  
 
Buy the book!


Bison 1.875

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3 Syntax of Grammar Rules

A Bison grammar rule has the following general form:

 
result: components...
        ;

where result is the nonterminal symbol that this rule describes, and components are various terminal and nonterminal symbols that are put together by this rule (see section 3.2 Symbols, Terminal and Nonterminal).

For example,

 
exp:      exp '+' exp
        ;

says that two groupings of type exp, with a `+' token in between, can be combined into a larger grouping of type exp.

White space in rules is significant only to separate symbols. You can add extra white space as you wish.

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

 
{C statements}

Usually there is only one action and it follows the components. See section 3.5.3 Actions.

Multiple rules for the same result can be written separately or can be joined with the vertical-bar character `|' as follows:

 
result:   rule1-components...
        | rule2-components...
        ...
        ;

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

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

 
expseq:   /* empty */
        | expseq1
        ;

expseq1:  exp
        | expseq1 ',' exp
        ;

It is customary to write a comment `/* empty */' in each rule with no components.


  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003