Open System Services Shell and Utilities Reference Manual (G06.29+, H06.08+, J06.03+)
User Commands (v - z) yacc(1)
calc The executable program file.
You can then run the program directly by entering:
calc
Then enter numbers and operators in calculator fashion. After you press <Return>, the
program displays the result of the operation. If you assign a value to a variable as fol-
lows, the cursor moves to the next line:
m=4 <Return>
_
You can then use the variable in calculations and it will have the value assigned to it:
m+5 <Return>
9
The Parser Source Code
The text that follows shows the contents of the file calc.y. This file has entries in all three of the
sections of a yacc grammar file: declarations, rules, and programs.
%{
#include <stdio.h>
int regs[26];
int base;
%}
%start list
%token DIGIT LETTER
%left ’|’
%left ’&’
%left ’+’ ’-’
%left ’*’ ’/’ ’%’
%left UMINUS /*supplies precedence for unary minus */
%% /*beginning of rules section */
list : /*empty */
| list stat ’\n’
| list error ’\n’
{ yyerrok; }
;
stat : expr
{ printf("%d\n",$1); }
| LETTER ’=’ expr
{ regs[$1] = $3; }
;
expr : ’(’ expr ’)’
{ $$ = $2; }
527188-021 Hewlett-Packard Company 10−41