This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / intl / plural.y
blob99849e53202b289f05789092c808cdc16107535d
1 %{
2 /* Expression parsing for plural form selection.
3 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4 Written by Ulrich Drepper <drepper@cygnus.com>, 2000.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published
8 by the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 USA. */
21 /* The bison generated parser uses alloca. AIX 3 forces us to put this
22 declaration at the beginning of the file. The declaration in bison's
23 skeleton file comes too late. This must come before <config.h>
24 because <config.h> may include arbitrary system headers. */
25 #if defined _AIX && !defined __GNUC__
26 #pragma alloca
27 #endif
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include "plural-exp.h"
37 /* The main function generated by the parser is called __gettextparse,
38 but we want it to be called PLURAL_PARSE. */
39 #ifndef _LIBC
40 # define __gettextparse PLURAL_PARSE
41 #endif
43 #define YYLEX_PARAM &((struct parse_args *) arg)->cp
44 #define YYPARSE_PARAM arg
46 %pure_parser
47 %expect 7
49 %union {
50 unsigned long int num;
51 enum operator op;
52 struct expression *exp;
56 /* Prototypes for local functions. */
58 #ifndef __GNUC__
59 #define inline /* nothing: don't inline if the compiler is not GCC */
60 #endif
62 static struct expression *new_exp PARAMS ((int nargs, enum operator op,
63 struct expression * const *args));
64 static inline struct expression *new_exp_0 PARAMS ((enum operator op));
65 static inline struct expression *new_exp_1 PARAMS ((enum operator op,
66 struct expression *right));
67 static struct expression *new_exp_2 PARAMS ((enum operator op,
68 struct expression *left,
69 struct expression *right));
70 static inline struct expression *new_exp_3 PARAMS ((enum operator op,
71 struct expression *bexp,
72 struct expression *tbranch,
73 struct expression *fbranch));
74 static int yylex PARAMS ((YYSTYPE *lval, const char **pexp));
75 static void yyerror PARAMS ((const char *str));
77 /* Allocation of expressions. */
79 static struct expression *
80 new_exp (nargs, op, args)
81 int nargs;
82 enum operator op;
83 struct expression * const *args;
85 int i;
86 struct expression *newp;
88 /* If any of the argument could not be malloc'ed, just return NULL. */
89 for (i = nargs - 1; i >= 0; i--)
90 if (args[i] == NULL)
91 goto fail;
93 /* Allocate a new expression. */
94 newp = (struct expression *) malloc (sizeof (*newp));
95 if (newp != NULL)
97 newp->nargs = nargs;
98 newp->operation = op;
99 for (i = nargs - 1; i >= 0; i--)
100 newp->val.args[i] = args[i];
101 return newp;
104 fail:
105 for (i = nargs - 1; i >= 0; i--)
106 FREE_EXPRESSION (args[i]);
108 return NULL;
111 static inline struct expression *
112 new_exp_0 (op)
113 enum operator op;
115 return new_exp (0, op, NULL);
118 static inline struct expression *
119 new_exp_1 (op, right)
120 enum operator op;
121 struct expression *right;
123 struct expression *args[1];
125 args[0] = right;
126 return new_exp (1, op, args);
129 static struct expression *
130 new_exp_2 (op, left, right)
131 enum operator op;
132 struct expression *left;
133 struct expression *right;
135 struct expression *args[2];
137 args[0] = left;
138 args[1] = right;
139 return new_exp (2, op, args);
142 static inline struct expression *
143 new_exp_3 (op, bexp, tbranch, fbranch)
144 enum operator op;
145 struct expression *bexp;
146 struct expression *tbranch;
147 struct expression *fbranch;
149 struct expression *args[3];
151 args[0] = bexp;
152 args[1] = tbranch;
153 args[2] = fbranch;
154 return new_exp (3, op, args);
159 /* This declares that all operators have the same associativity and the
160 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
161 There is no unary minus and no bitwise operators.
162 Operators with the same syntactic behaviour have been merged into a single
163 token, to save space in the array generated by bison. */
164 %right '?' /* ? */
165 %left '|' /* || */
166 %left '&' /* && */
167 %left EQUOP2 /* == != */
168 %left CMPOP2 /* < > <= >= */
169 %left ADDOP2 /* + - */
170 %left MULOP2 /* * / % */
171 %right '!' /* ! */
173 %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
174 %token <num> NUMBER
175 %type <exp> exp
179 start: exp
181 if ($1 == NULL)
182 YYABORT;
183 ((struct parse_args *) arg)->res = $1;
187 exp: exp '?' exp ':' exp
189 $$ = new_exp_3 (qmop, $1, $3, $5);
191 | exp '|' exp
193 $$ = new_exp_2 (lor, $1, $3);
195 | exp '&' exp
197 $$ = new_exp_2 (land, $1, $3);
199 | exp EQUOP2 exp
201 $$ = new_exp_2 ($2, $1, $3);
203 | exp CMPOP2 exp
205 $$ = new_exp_2 ($2, $1, $3);
207 | exp ADDOP2 exp
209 $$ = new_exp_2 ($2, $1, $3);
211 | exp MULOP2 exp
213 $$ = new_exp_2 ($2, $1, $3);
215 | '!' exp
217 $$ = new_exp_1 (lnot, $2);
219 | 'n'
221 $$ = new_exp_0 (var);
223 | NUMBER
225 if (($$ = new_exp_0 (num)) != NULL)
226 $$->val.num = $1;
228 | '(' exp ')'
230 $$ = $2;
236 void
237 internal_function
238 FREE_EXPRESSION (exp)
239 struct expression *exp;
241 if (exp == NULL)
242 return;
244 /* Handle the recursive case. */
245 switch (exp->nargs)
247 case 3:
248 FREE_EXPRESSION (exp->val.args[2]);
249 /* FALLTHROUGH */
250 case 2:
251 FREE_EXPRESSION (exp->val.args[1]);
252 /* FALLTHROUGH */
253 case 1:
254 FREE_EXPRESSION (exp->val.args[0]);
255 /* FALLTHROUGH */
256 default:
257 break;
260 free (exp);
264 static int
265 yylex (lval, pexp)
266 YYSTYPE *lval;
267 const char **pexp;
269 const char *exp = *pexp;
270 int result;
272 while (1)
274 if (exp[0] == '\0')
276 *pexp = exp;
277 return YYEOF;
280 if (exp[0] != ' ' && exp[0] != '\t')
281 break;
283 ++exp;
286 result = *exp++;
287 switch (result)
289 case '0': case '1': case '2': case '3': case '4':
290 case '5': case '6': case '7': case '8': case '9':
292 unsigned long int n = result - '0';
293 while (exp[0] >= '0' && exp[0] <= '9')
295 n *= 10;
296 n += exp[0] - '0';
297 ++exp;
299 lval->num = n;
300 result = NUMBER;
302 break;
304 case '=':
305 if (exp[0] == '=')
307 ++exp;
308 lval->op = equal;
309 result = EQUOP2;
311 else
312 result = YYERRCODE;
313 break;
315 case '!':
316 if (exp[0] == '=')
318 ++exp;
319 lval->op = not_equal;
320 result = EQUOP2;
322 break;
324 case '&':
325 case '|':
326 if (exp[0] == result)
327 ++exp;
328 else
329 result = YYERRCODE;
330 break;
332 case '<':
333 if (exp[0] == '=')
335 ++exp;
336 lval->op = less_or_equal;
338 else
339 lval->op = less_than;
340 result = CMPOP2;
341 break;
343 case '>':
344 if (exp[0] == '=')
346 ++exp;
347 lval->op = greater_or_equal;
349 else
350 lval->op = greater_than;
351 result = CMPOP2;
352 break;
354 case '*':
355 lval->op = mult;
356 result = MULOP2;
357 break;
359 case '/':
360 lval->op = divide;
361 result = MULOP2;
362 break;
364 case '%':
365 lval->op = module;
366 result = MULOP2;
367 break;
369 case '+':
370 lval->op = plus;
371 result = ADDOP2;
372 break;
374 case '-':
375 lval->op = minus;
376 result = ADDOP2;
377 break;
379 case 'n':
380 case '?':
381 case ':':
382 case '(':
383 case ')':
384 /* Nothing, just return the character. */
385 break;
387 case ';':
388 case '\n':
389 case '\0':
390 /* Be safe and let the user call this function again. */
391 --exp;
392 result = YYEOF;
393 break;
395 default:
396 result = YYERRCODE;
397 #if YYDEBUG != 0
398 --exp;
399 #endif
400 break;
403 *pexp = exp;
405 return result;
409 static void
410 yyerror (str)
411 const char *str;
413 /* Do nothing. We don't print error messages here. */