Rpcalc Rules
Previous: <Rpcalc Decls=>RpcalcDecm> * Next: <Rpcalc Lexer=>RpcalcLexf> * Up: <RPN Calc=>RPNCalc>

#Wrap on
{fH4}Grammar Rules for {fCode}rpcalc{f}{f}

Here are the grammar rules for the reverse polish notation calculator.

#Wrap off
#fCode
input:    \/\* empty \*\/
        | input line
;

line:     '\\n'
        | exp '\\n'  \{ printf ("\\t%.10g\\n", $1); \}
;

exp:      NUM             \{ $$ = $1;         \}
        | exp exp '+'     \{ $$ = $1 + $2;    \}
        | exp exp '-'     \{ $$ = $1 - $2;    \}
        | exp exp '\*'     \{ $$ = $1 \* $2;    \}
        | exp exp '\/'     \{ $$ = $1 \/ $2;    \}
      \/\* Exponentiation \*\/
        | exp exp '^'     \{ $$ = pow ($1, $2); \}
      \/\* Unary minus    \*\/
        | exp 'n'         \{ $$ = -$1;        \}
;
%%
#f
#Wrap on

The groupings of the rpcalc ``language'' defined here are the expression
(given the name {fCode}exp{f}), the line of input ({fCode}line{f}), and the
complete input transcript ({fCode}input{f}).  Each of these nonterminal
symbols has several alternate rules, joined by the {fEmphasis}|{f} punctuator
which is read as ``or''.  The following sections explain what these rules
mean.

The semantics of the language is determined by the actions taken when a
grouping is recognized.  The actions are the C code that appears inside
braces.  \*Note <Actions=>Actions>.

You must specify these actions in C, but Bison provides the means for
passing semantic values between the rules.  In each action, the
pseudo-variable {fCode}$${f} stands for the semantic value for the grouping
that the rule is going to construct.  Assigning a value to {fCode}$${f} is the
main job of most actions.  The semantic values of the components of the
rule are referred to as {fCode}$1{f}, {fCode}$2{f}, and so on.

#Wrap off
<Rpcalc Input=>RpcalcInpu>:      
<Rpcalc Line=>RpcalcLine>:       
<Rpcalc Expr=>RpcalcExpr>:       
#Wrap on

