tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / byacc / dist / test / calc3.y
blob6cd597efbc094b243023ef76f74ed4c9da2bd87c
1 /* $NetBSD: calc3.y,v 1.1.1.4 2013/04/06 14:45:28 christos Exp $ */
3 %pure-parser
5 %parse-param { int regs[26] }
6 %parse-param { int *base }
8 %lex-param { int *base }
11 # include <stdio.h>
12 # include <ctype.h>
14 #ifdef YYBISON
15 #define YYSTYPE int
16 #define YYLEX_PARAM base
17 #define YYLEX_DECL() yylex(YYSTYPE *yylval, int *YYLEX_PARAM)
18 #define YYERROR_DECL() yyerror(int regs[26], int *base, const char *s)
19 int YYLEX_DECL();
20 static void YYERROR_DECL();
21 #endif
25 %start list
27 %token DIGIT LETTER
29 %left '|'
30 %left '&'
31 %left '+' '-'
32 %left '*' '/' '%'
33 %left UMINUS /* supplies precedence for unary minus */
35 %% /* beginning of rules section */
37 list : /* empty */
38 | list stat '\n'
39 | list error '\n'
40 { yyerrok ; }
43 stat : expr
44 { printf("%d\n",$1);}
45 | LETTER '=' expr
46 { regs[$1] = $3; }
49 expr : '(' expr ')'
50 { $$ = $2; }
51 | expr '+' expr
52 { $$ = $1 + $3; }
53 | expr '-' expr
54 { $$ = $1 - $3; }
55 | expr '*' expr
56 { $$ = $1 * $3; }
57 | expr '/' expr
58 { $$ = $1 / $3; }
59 | expr '%' expr
60 { $$ = $1 % $3; }
61 | expr '&' expr
62 { $$ = $1 & $3; }
63 | expr '|' expr
64 { $$ = $1 | $3; }
65 | '-' expr %prec UMINUS
66 { $$ = - $2; }
67 | LETTER
68 { $$ = regs[$1]; }
69 | number
72 number: DIGIT
73 { $$ = $1; (*base) = ($1==0) ? 8 : 10; }
74 | number DIGIT
75 { $$ = (*base) * $1 + $2; }
78 %% /* start of programs */
80 #ifdef YYBYACC
81 extern int YYLEX_DECL();
82 #endif
84 int
85 main (void)
87 int regs[26];
88 int base = 10;
90 while(!feof(stdin)) {
91 yyparse(regs, &base);
93 return 0;
96 static void
97 YYERROR_DECL()
99 fprintf(stderr, "%s\n", s);
103 YYLEX_DECL()
105 /* lexical analysis routine */
106 /* returns LETTER for a lower case letter, yylval = 0 through 25 */
107 /* return DIGIT for a digit, yylval = 0 through 9 */
108 /* all other characters are returned immediately */
110 int c;
112 while( (c=getchar()) == ' ' ) { /* skip blanks */ }
114 /* c is now nonblank */
116 if( islower( c )) {
117 *yylval = (c - 'a');
118 return ( LETTER );
120 if( isdigit( c )) {
121 *yylval = (c - '0') % (*base);
122 return ( DIGIT );
124 return( c );