xref: /freebsd/contrib/byacc/test/calc2.y (revision ab0b9f6b3073e6c4d1dfbf07444d7db67a189a96)
1 %parse-param { int regs[26] }
2 %parse-param { int *base }
3 
4 %lex-param { int *base }
5 
6 %{
7 # include <stdio.h>
8 # include <ctype.h>
9 
10 #ifdef YYBISON
11 #define YYLEX_PARAM base
12 #define YYLEX_DECL() yylex(int *YYLEX_PARAM)
13 #define YYERROR_DECL() yyerror(int regs[26], int *base, const char *s)
14 int YYLEX_DECL();
15 static void YYERROR_DECL();
16 #endif
17 
18 %}
19 
20 %start list
21 
22 %token DIGIT LETTER
23 
24 %left '|'
25 %left '&'
26 %left '+' '-'
27 %left '*' '/' '%'
28 %left UMINUS   /* supplies precedence for unary minus */
29 
30 %% /* beginning of rules section */
31 
32 list  :  /* empty */
33       |  list stat '\n'
34       |  list error '\n'
35             {  yyerrok ; }
36       ;
37 
38 stat  :  expr
39             {  printf("%d\n",$1);}
40       |  LETTER '=' expr
41             {  regs[$1] = $3; }
42       ;
43 
44 expr  :  '(' expr ')'
45             {  $$ = $2; }
46       |  expr '+' expr
47             {  $$ = $1 + $3; }
48       |  expr '-' expr
49             {  $$ = $1 - $3; }
50       |  expr '*' expr
51             {  $$ = $1 * $3; }
52       |  expr '/' expr
53             {  $$ = $1 / $3; }
54       |  expr '%' expr
55             {  $$ = $1 % $3; }
56       |  expr '&' expr
57             {  $$ = $1 & $3; }
58       |  expr '|' expr
59             {  $$ = $1 | $3; }
60       |  '-' expr %prec UMINUS
61             {  $$ = - $2; }
62       |  LETTER
63             {  $$ = regs[$1]; }
64       |  number
65       ;
66 
67 number:  DIGIT
68          {  $$ = $1; (*base) = ($1==0) ? 8 : 10; }
69       |  number DIGIT
70          {  $$ = (*base) * $1 + $2; }
71       ;
72 
73 %% /* start of programs */
74 
75 #ifdef YYBYACC
76 extern int YYLEX_DECL();
77 #endif
78 
79 int
80 main (void)
81 {
82     int regs[26];
83     int base = 10;
84 
85     while(!feof(stdin)) {
86 	yyparse(regs, &base);
87     }
88     return 0;
89 }
90 
91 static void
92 YYERROR_DECL()
93 {
94     fprintf(stderr, "%s\n", s);
95 }
96 
97 int
98 YYLEX_DECL()
99 {
100 	/* lexical analysis routine */
101 	/* returns LETTER for a lower case letter, yylval = 0 through 25 */
102 	/* return DIGIT for a digit, yylval = 0 through 9 */
103 	/* all other characters are returned immediately */
104 
105     int c;
106 
107     while( (c=getchar()) == ' ' )   { /* skip blanks */ }
108 
109     /* c is now nonblank */
110 
111     if( islower( c )) {
112 	yylval = c - 'a';
113 	return ( LETTER );
114     }
115     if( isdigit( c )) {
116 	yylval = (c - '0') % (*base);
117 	return ( DIGIT );
118     }
119     return( c );
120 }
121