Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / byacc / dist / test / calc.y
blob9eb03e2114114a26cdddea14c0949d77e4c5c774
1 /* $NetBSD$ */
3 %{
4 # include <stdio.h>
5 # include <ctype.h>
7 int regs[26];
8 int base;
12 %start list
14 %token DIGIT LETTER
16 %left '|'
17 %left '&'
18 %left '+' '-'
19 %left '*' '/' '%'
20 %left UMINUS /* supplies precedence for unary minus */
22 %% /* beginning of rules section */
24 list : /* empty */
25 | list stat '\n'
26 | list error '\n'
27 { yyerrok ; }
30 stat : expr
31 { printf("%d\n",$1);}
32 | LETTER '=' expr
33 { regs[$1] = $3; }
36 expr : '(' expr ')'
37 { $$ = $2; }
38 | expr '+' expr
39 { $$ = $1 + $3; }
40 | expr '-' expr
41 { $$ = $1 - $3; }
42 | expr '*' expr
43 { $$ = $1 * $3; }
44 | expr '/' expr
45 { $$ = $1 / $3; }
46 | expr '%' expr
47 { $$ = $1 % $3; }
48 | expr '&' expr
49 { $$ = $1 & $3; }
50 | expr '|' expr
51 { $$ = $1 | $3; }
52 | '-' expr %prec UMINUS
53 { $$ = - $2; }
54 | LETTER
55 { $$ = regs[$1]; }
56 | number
59 number: DIGIT
60 { $$ = $1; base = ($1==0) ? 8 : 10; }
61 | number DIGIT
62 { $$ = base * $1 + $2; }
65 %% /* start of programs */
67 main ()
70 while(!feof(stdin)) {
71 yyparse();
75 yyerror(char *s)
77 fprintf(stderr, "%s\n", s);
81 yylex() { /* lexical analysis routine */
82 /* returns LETTER for a lower case letter, yylval = 0 through 25 */
83 /* return DIGIT for a digit, yylval = 0 through 9 */
84 /* all other characters are returned immediately */
86 int c;
88 while( (c=getchar()) == ' ' ) { /* skip blanks */ }
90 /* c is now nonblank */
92 if( islower( c )) {
93 yylval = c - 'a';
94 return ( LETTER );
96 if( isdigit( c )) {
97 yylval = c - '0';
98 return ( DIGIT );
100 return( c );