Hint added.
[AROS.git] / workbench / c / evalParser.y
blobeea09993ae6bef69002119aca142339303ca7364
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Bison file for the Eval grammar.
6 Lang: English
7 */
9 %{
10 #define YYSTYPE int
12 #include <ctype.h>
13 #include <math.h>
14 #include <string.h>
15 #include <stdio.h>
17 char *text;
19 extern int yylval;
20 static void yyerror(char *s);
21 static int intPow(int x, int y);
22 static int yylex();
24 int g_result;
28 %token NUM
29 %left 'l' 'r'
30 %left 'e'
31 %left '|'
32 %left 'x'
33 %left '&'
34 %left '-' '+'
35 %left '*' '/' '%'
36 %left NEG '~'
37 %right '^'
42 val: expr
44 g_result = $1;
48 expr: NUM { $$ = $1; }
49 | expr 'l' expr { $$ = $1 << $3; }
50 | expr 'r' expr { $$ = $1 >> $3; }
51 | expr 'e' expr { $$ = ($1 & $3) | (~$1 & ~$3); }
52 | expr '|' expr { $$ = $1 | $3; }
53 | expr 'x' expr { $$ = $1 ^ $3; }
54 | expr '&' expr { $$ = $1 & $3; }
55 | expr '+' expr { $$ = $1 + $3; }
56 | expr '-' expr { $$ = $1 - $3; }
57 | expr '*' expr { $$ = $1 * $3; }
58 | expr '/' expr { $$ = $1 / $3; }
59 | expr '%' expr { $$ = $1 % $3; }
60 | '-' expr %prec NEG { $$ = -$2; }
61 | '~' expr { $$ = ~$2; }
62 | expr '^' expr { $$ = intPow($1, $3); }
63 | '(' expr ')' { $$ = $2; }
68 static int yylex()
70 int c;
74 c = *text++;
75 } while (c == ' ' || c == '\t');
77 if (c == '0')
79 switch (*text)
81 case 'x':
82 sscanf(text + 1, "%x", &yylval);
84 text++;
86 while ((*text >= '0' && *text <= '9') ||
87 (*text >= 'a' && *text <= 'f') ||
88 (*text >= 'A' && *text <= 'F'))
90 text++;
93 return NUM;
95 case '0':
96 case '1':
97 case '2':
98 case '3':
99 case '4':
100 case '5':
101 case '6':
102 case '7':
103 sscanf(text - 1, "%o", &yylval);
105 while ((*text >= '0' && *text <= '7'))
107 text++;
110 return NUM;
112 case '8':
113 case '9':
114 /* Skip 08... and 09... constructions and let them be ordinary
115 decimal stuff */
116 break;
118 default:
119 /* It was just the constant 0 */
120 yylval = 0;
122 return NUM;
127 if (c == '#')
129 int d = *text++;
131 if (d == 'x')
133 sscanf(text, "%x", &yylval);
135 while ((*text >= '0' && *text <= '9') ||
136 (*text >= 'a' && *text <= 'f') ||
137 (*text >= 'A' && *text <= 'F'))
139 text++;
142 return NUM;
144 else if (isdigit(d))
146 sscanf(text - 1, "%o", &yylval);
148 while ((*text >= '0' && *text <= '7'))
150 text++;
153 return NUM;
155 else
157 yyerror("Lexer error");
162 if (c == '\'')
164 yylval = *text++;
166 return NUM;
169 if (isdigit(c))
171 sscanf(text - 1, "%i", &yylval);
173 while (isdigit(*text))
175 text++;
178 return NUM;
181 if (isalpha(c))
183 int i = 1;
184 char *textCopy = text - 1;
186 while (isalpha(text[i]))
188 i++;
191 text += i;
193 if (strncasecmp(textCopy, "m", i) == 0 ||
194 strncmp(textCopy, "mod", i) == 0)
196 return '%';
199 if (strncmp(textCopy, "xor", i) == 0 ||
200 strncasecmp(textCopy, "x", i) == 0)
202 return 'x';
205 if (strncmp(textCopy, "eqv", i) == 0 ||
206 strncasecmp(textCopy, "e", i) == 0)
208 return 'e';
211 if (strncmp(textCopy, "lsh", i) == 0 ||
212 strncasecmp(textCopy, "l", i) == 0)
214 return 'l';
217 if (strncmp(textCopy, "rsh", i) == 0 ||
218 strncasecmp(textCopy, "r", i) == 0)
220 return 'r';
223 yyerror("Lexing error");
226 return c;
230 static int intPow(int x, int y)
232 int result = 1;
234 while (y > 0)
236 result *= x;
237 y--;
240 return result;
244 static void yyerror(char *s)
246 printf("%s\n", s);
250 #if 0
252 int main()
255 text = "(1 lsh 4) mod 2";
257 if (yyparse() == 1)
259 printf("Parse error\n");
261 else
263 printf("The answer is %i\n", g_result);
266 return 0;
269 #endif