xref: /minix/external/bsd/byacc/dist/test/quote_calc.y (revision 84d9c625)
1 /*	$NetBSD: quote_calc.y,v 1.1.1.1 2013/04/06 14:45:30 christos Exp $	*/
2 
3 %{
4 # include <stdio.h>
5 # include <ctype.h>
6 
7 int regs[26];
8 int base;
9 
10 int yylex(void);
11 static void yyerror(const char *s);
12 
13 %}
14 
15 %start list
16 
17 %token OP_ADD "ADD"
18 %token OP_SUB "SUB"
19 %token OP_MUL "MUL"
20 %token OP_DIV "DIV"
21 %token OP_MOD "MOD"
22 %token OP_AND "AND"
23 
24 %token DIGIT LETTER
25 
26 %left '|'
27 %left '&'
28 %left '+' '-'
29 %left '*' '/' '%'
30 %left UMINUS   /* supplies precedence for unary minus */
31 
32 %% /* beginning of rules section */
33 
34 list  :  /* empty */
35       |  list stat '\n'
36       |  list error '\n'
37             {  yyerrok ; }
38       ;
39 
40 stat  :  expr
41             {  printf("%d\n",$1);}
42       |  LETTER '=' expr
43             {  regs[$1] = $3; }
44       ;
45 
46 expr  :  '(' expr ')'
47             {  $$ = $2; }
48       |  expr OP_ADD expr
49             {  $$ = $1 + $3; }
50       |  expr OP_SUB expr
51             {  $$ = $1 - $3; }
52       |  expr OP_MUL expr
53             {  $$ = $1 * $3; }
54       |  expr OP_DIV expr
55             {  $$ = $1 / $3; }
56       |  expr OP_MOD expr
57             {  $$ = $1 % $3; }
58       |  expr OP_AND expr
59             {  $$ = $1 & $3; }
60       |  expr '|' expr
61             {  $$ = $1 | $3; }
62       |  OP_SUB expr %prec UMINUS
63             {  $$ = - $2; }
64       |  LETTER
65             {  $$ = regs[$1]; }
66       |  number
67       ;
68 
69 number:  DIGIT
70          {  $$ = $1; base = ($1==0) ? 8 : 10; }
71       |  number DIGIT
72          {  $$ = base * $1 + $2; }
73       ;
74 
75 %% /* start of programs */
76 
77 int
78 main (void)
79 {
80     while(!feof(stdin)) {
81 	yyparse();
82     }
83     return 0;
84 }
85 
86 static void
yyerror(const char * s)87 yyerror(const char *s)
88 {
89     fprintf(stderr, "%s\n", s);
90 }
91 
92 int
yylex(void)93 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 */
98 
99     int c;
100 
101     while( (c=getchar()) == ' ' )   { /* skip blanks */ }
102 
103     /* c is now nonblank */
104 
105     if( islower( c )) {
106 	yylval = c - 'a';
107 	return ( LETTER );
108     }
109     if( isdigit( c )) {
110 	yylval = c - '0';
111 	return ( DIGIT );
112     }
113     return( c );
114 }
115