Update.
[glibc.git] / intl / plural.y
blob3345d15eebb139f3430e8e00501bdbf02e892a9a
1 %{
2 /* Expression parsing for plural form selection.
3 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Written by Ulrich Drepper <drepper@cygnus.com>, 2000.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <stdlib.h>
27 #include "gettextP.h"
29 /* Names for the libintl functions are a problem. They must not clash
30 with existing names and they should follow ANSI C. But this source
31 code is also used in GNU C Library where the names have a __
32 prefix. So we have to make a difference here. */
33 #ifdef _LIBC
34 # define FREE_EXPRESSION __gettext_free_exp
35 #else
36 # define FREE_EXPRESSION gettext_free_exp__
37 # define __gettextparse gettextparse__
38 #endif
40 #define YYLEX_PARAM &((struct parse_args *) arg)->cp
41 #define YYPARSE_PARAM arg
43 %pure_parser
44 %expect 10
46 %union {
47 unsigned long int num;
48 enum operator op;
49 struct expression *exp;
53 /* Prototypes for local functions. */
54 static struct expression *new_exp PARAMS ((int nargs, enum operator op,
55 struct expression * const *args));
56 static inline struct expression *new_exp_0 PARAMS ((enum operator op));
57 static inline struct expression *new_exp_1 PARAMS ((enum operator op,
58 struct expression *right));
59 static struct expression *new_exp_2 PARAMS ((enum operator op,
60 struct expression *left,
61 struct expression *right));
62 static inline struct expression *new_exp_3 PARAMS ((enum operator op,
63 struct expression *bexp,
64 struct expression *tbranch,
65 struct expression *fbranch));
66 static int yylex PARAMS ((YYSTYPE *lval, const char **pexp));
67 static void yyerror PARAMS ((const char *str));
69 /* Allocation of expressions. */
71 static struct expression *
72 new_exp (nargs, op, args)
73 int nargs;
74 enum operator op;
75 struct expression * const *args;
77 int i;
78 struct expression *newp;
80 /* If any of the argument could not be malloc'ed, just return NULL. */
81 for (i = nargs - 1; i >= 0; i--)
82 if (args[i] == NULL)
83 goto fail;
85 /* Allocate a new expression. */
86 newp = (struct expression *) malloc (sizeof (*newp));
87 if (newp != NULL)
89 newp->nargs = nargs;
90 newp->operation = op;
91 for (i = nargs - 1; i >= 0; i--)
92 newp->val.args[i] = args[i];
93 return newp;
96 fail:
97 for (i = nargs - 1; i >= 0; i--)
98 FREE_EXPRESSION (args[i]);
100 return NULL;
103 static inline struct expression *
104 new_exp_0 (op)
105 enum operator op;
107 return new_exp (0, op, NULL);
110 static inline struct expression *
111 new_exp_1 (op, right)
112 enum operator op;
113 struct expression *right;
115 struct expression *args[1];
117 args[0] = right;
118 return new_exp (1, op, args);
121 static struct expression *
122 new_exp_2 (op, left, right)
123 enum operator op;
124 struct expression *left;
125 struct expression *right;
127 struct expression *args[2];
129 args[0] = left;
130 args[1] = right;
131 return new_exp (2, op, args);
134 static inline struct expression *
135 new_exp_3 (op, bexp, tbranch, fbranch)
136 enum operator op;
137 struct expression *bexp;
138 struct expression *tbranch;
139 struct expression *fbranch;
141 struct expression *args[3];
143 args[0] = bexp;
144 args[1] = tbranch;
145 args[2] = fbranch;
146 return new_exp (3, op, args);
151 /* This declares that all operators have the same associativity and the
152 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
153 There is no unary minus and no bitwise operators.
154 Operators with the same syntactic behaviour have been merged into a single
155 token, to save space in the array generated by bison. */
156 %right '?' /* ? */
157 %left '|' /* || */
158 %left '&' /* && */
159 %left EQUOP2 /* == != */
160 %left CMPOP2 /* < > <= >= */
161 %left ADDOP2 /* + - */
162 %left MULOP2 /* * / % */
163 %right '!' /* ! */
165 %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
166 %token <num> NUMBER
167 %type <exp> exp
171 start: exp
173 if ($1 == NULL)
174 YYABORT;
175 ((struct parse_args *) arg)->res = $1;
179 exp: exp '?' exp ':' exp
181 $$ = new_exp_3 (qmop, $1, $3, $5);
183 | exp '|' exp
185 $$ = new_exp_2 (lor, $1, $3);
187 | exp '&' exp
189 $$ = new_exp_2 (land, $1, $3);
191 | exp EQUOP2 exp
193 $$ = new_exp_2 ($2, $1, $3);
195 | exp CMPOP2 exp
197 $$ = new_exp_2 ($2, $1, $3);
199 | exp ADDOP2 exp
201 $$ = new_exp_2 ($2, $1, $3);
203 | exp MULOP2 exp
205 $$ = new_exp_2 ($2, $1, $3);
207 | '!' exp
209 $$ = new_exp_1 (lnot, $2);
211 | 'n'
213 $$ = new_exp_0 (var);
215 | NUMBER
217 if (($$ = new_exp_0 (num)) != NULL)
218 $$->val.num = $1;
220 | '(' exp ')'
222 $$ = $2;
228 void
229 internal_function
230 FREE_EXPRESSION (exp)
231 struct expression *exp;
233 if (exp == NULL)
234 return;
236 /* Handle the recursive case. */
237 switch (exp->nargs)
239 case 3:
240 FREE_EXPRESSION (exp->val.args[2]);
241 /* FALLTHROUGH */
242 case 2:
243 FREE_EXPRESSION (exp->val.args[1]);
244 /* FALLTHROUGH */
245 case 1:
246 FREE_EXPRESSION (exp->val.args[0]);
247 /* FALLTHROUGH */
248 default:
249 break;
252 free (exp);
256 static int
257 yylex (lval, pexp)
258 YYSTYPE *lval;
259 const char **pexp;
261 const char *exp = *pexp;
262 int result;
264 while (1)
266 if (exp[0] == '\0')
268 *pexp = exp;
269 return YYEOF;
272 if (exp[0] != ' ' && exp[0] != '\t')
273 break;
275 ++exp;
278 result = *exp++;
279 switch (result)
281 case '0': case '1': case '2': case '3': case '4':
282 case '5': case '6': case '7': case '8': case '9':
284 unsigned long int n = result - '0';
285 while (exp[0] >= '0' && exp[0] <= '9')
287 n *= 10;
288 n += exp[0] - '0';
289 ++exp;
291 lval->num = n;
292 result = NUMBER;
294 break;
296 case '=':
297 if (exp[0] == '=')
299 ++exp;
300 lval->op = equal;
301 result = EQUOP2;
303 else
304 result = YYERRCODE;
305 break;
307 case '!':
308 if (exp[0] == '=')
310 ++exp;
311 lval->op = not_equal;
312 result = EQUOP2;
314 break;
316 case '&':
317 case '|':
318 if (exp[0] == result)
319 ++exp;
320 else
321 result = YYERRCODE;
322 break;
324 case '<':
325 if (exp[0] == '=')
327 ++exp;
328 lval->op = less_or_equal;
330 else
331 lval->op = less_than;
332 result = CMPOP2;
333 break;
335 case '>':
336 if (exp[0] == '=')
338 ++exp;
339 lval->op = greater_or_equal;
341 else
342 lval->op = greater_than;
343 result = CMPOP2;
344 break;
346 case '*':
347 lval->op = mult;
348 result = MULOP2;
349 break;
351 case '/':
352 lval->op = divide;
353 result = MULOP2;
354 break;
356 case '%':
357 lval->op = module;
358 result = MULOP2;
359 break;
361 case '+':
362 lval->op = plus;
363 result = ADDOP2;
364 break;
366 case '-':
367 lval->op = minus;
368 result = ADDOP2;
369 break;
371 case 'n':
372 case '?':
373 case ':':
374 case '(':
375 case ')':
376 /* Nothing, just return the character. */
377 break;
379 case ';':
380 case '\n':
381 case '\0':
382 /* Be safe and let the user call this function again. */
383 --exp;
384 result = YYEOF;
385 break;
387 default:
388 result = YYERRCODE;
389 #if YYDEBUG != 0
390 --exp;
391 #endif
392 break;
395 *pexp = exp;
397 return result;
401 static void
402 yyerror (str)
403 const char *str;
405 /* Do nothing. We don't print error messages here. */