tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / byacc / dist / test / code_calc.y
blob19a376f0f0364d9193e063ff289df7a4f0be136c
1 /* $NetBSD: code_calc.y,v 1.1.1.4 2013/04/06 14:45:27 christos Exp $ */
3 %{
4 # include <stdio.h>
5 # include <ctype.h>
7 int regs[26];
8 int base;
10 #ifdef YYBISON
11 int yylex(void);
12 static void yyerror(const char *s);
13 #endif
17 %start list
19 %token DIGIT LETTER
21 %left '|'
22 %left '&'
23 %left '+' '-'
24 %left '*' '/' '%'
25 %left UMINUS /* supplies precedence for unary minus */
27 %% /* beginning of rules section */
29 list : /* empty */
30 | list stat '\n'
31 | list error '\n'
32 { yyerrok ; }
35 stat : expr
36 { printf("%d\n",$1);}
37 | LETTER '=' expr
38 { regs[$1] = $3; }
41 expr : '(' expr ')'
42 { $$ = $2; }
43 | expr '+' expr
44 { $$ = $1 + $3; }
45 | expr '-' expr
46 { $$ = $1 - $3; }
47 | expr '*' expr
48 { $$ = $1 * $3; }
49 | expr '/' expr
50 { $$ = $1 / $3; }
51 | expr '%' expr
52 { $$ = $1 % $3; }
53 | expr '&' expr
54 { $$ = $1 & $3; }
55 | expr '|' expr
56 { $$ = $1 | $3; }
57 | '-' expr %prec UMINUS
58 { $$ = - $2; }
59 | LETTER
60 { $$ = regs[$1]; }
61 | number
64 number: DIGIT
65 { $$ = $1; base = ($1==0) ? 8 : 10; }
66 | number DIGIT
67 { $$ = base * $1 + $2; }
70 %% /* start of programs */
72 #ifdef YYBYACC
73 extern int YYLEX_DECL();
74 #endif
76 int
77 main (void)
79 while(!feof(stdin)) {
80 yyparse();
82 return 0;
85 static void
86 yyerror(const char *s)
88 fprintf(stderr, "%s\n", s);
91 int
92 yylex(void)
94 /* lexical analysis routine */
95 /* returns LETTER for a lower case letter, yylval = 0 through 25 */
96 /* return DIGIT for a digit, yylval = 0 through 9 */
97 /* all other characters are returned immediately */
99 int c;
101 while( (c=getchar()) == ' ' ) { /* skip blanks */ }
103 /* c is now nonblank */
105 if( islower( c )) {
106 yylval = c - 'a';
107 return ( LETTER );
109 if( isdigit( c )) {
110 yylval = c - '0';
111 return ( DIGIT );
113 return( c );