Changes to let it build with bison-3.0.
[elinks.git] / src / intl / gettext / plural.y
blob9303f172557cbf8cf8aad08ecbaf11400fca5eda
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
7 it under the terms of the GNU General Public License as published by
8 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* The bison generated parser uses alloca. AIX 3 forces us to put this
21 declaration at the beginning of the file. The declaration in bison's
22 skeleton file comes too late. This must come before <config.h>
23 because <config.h> may include arbitrary system headers. */
24 #if defined _AIX && !defined __GNUC__
25 #pragma alloca
26 #endif
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
32 #include <stdlib.h>
33 #include "intl/gettext/gettextP.h"
37 %parse-param {struct parse_args *arg}
38 %lex-param {struct parse_args *arg}
39 %define api.pure full
40 %expect 7
42 %union {
43 unsigned long int num;
44 enum operator op;
45 struct expression *exp;
49 /* Prototypes for local functions. */
50 static struct expression *new_exp(int nargs, enum operator op,
51 struct expression * const *args);
52 static inline struct expression *new_exp_0(enum operator op);
53 static inline struct expression *new_exp_1(enum operator op,
54 struct expression *right);
55 static struct expression *new_exp_2(enum operator op,
56 struct expression *left,
57 struct expression *right);
58 static inline struct expression *new_exp_3(enum operator op,
59 struct expression *bexp,
60 struct expression *tbranch,
61 struct expression *fbranch);
62 static int yylex(YYSTYPE *lval, struct parse_args *arg);
63 static void yyerror(struct parse_args *arg, const unsigned char *str);
65 /* Allocation of expressions. */
67 static struct expression *
68 new_exp(int nargs, enum operator op, struct expression * const *args)
70 int i;
71 struct expression *newp;
73 /* If any of the argument could not be malloc'ed, just return NULL. */
74 for (i = nargs - 1; i >= 0; i--)
75 if (args[i] == NULL)
76 goto fail;
78 /* Allocate a new expression. */
79 newp = (struct expression *) malloc (sizeof (*newp));
80 if (newp != NULL)
82 newp->nargs = nargs;
83 newp->operation = op;
84 for (i = nargs - 1; i >= 0; i--)
85 newp->val.args[i] = args[i];
86 return newp;
89 fail:
90 for (i = nargs - 1; i >= 0; i--)
91 gettext_free_exp__ (args[i]);
93 return NULL;
96 static inline struct expression *
97 new_exp_0(enum operator op)
99 return new_exp (0, op, NULL);
102 static inline struct expression *
103 new_exp_1(enum operator op, struct expression *right)
105 struct expression *args[1];
107 args[0] = right;
108 return new_exp (1, op, args);
111 static struct expression *
112 new_exp_2(enum operator op, struct expression *left, struct expression *right)
114 struct expression *args[2];
116 args[0] = left;
117 args[1] = right;
118 return new_exp (2, op, args);
121 static inline struct expression *
122 new_exp_3(enum operator op, struct expression *bexp, struct expression *tbranch,
123 struct expression *fbranch)
125 struct expression *args[3];
127 args[0] = bexp;
128 args[1] = tbranch;
129 args[2] = fbranch;
130 return new_exp (3, op, args);
135 /* This declares that all operators have the same associativity and the
136 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
137 There is no unary minus and no bitwise operators.
138 Operators with the same syntactic behaviour have been merged into a single
139 token, to save space in the array generated by bison. */
140 %right '?' /* ? */
141 %left '|' /* || */
142 %left '&' /* && */
143 %left EQUOP2 /* == != */
144 %left CMPOP2 /* < > <= >= */
145 %left ADDOP2 /* + - */
146 %left MULOP2 /* * / % */
147 %right '!' /* ! */
149 %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
150 %token <num> NUMBER
151 %type <exp> exp
155 start: exp
157 if ($1 == NULL)
158 YYABORT;
159 ((struct parse_args *) arg)->res = $1;
163 exp: exp '?' exp ':' exp
165 $$ = new_exp_3 (qmop, $1, $3, $5);
167 | exp '|' exp
169 $$ = new_exp_2 (lor, $1, $3);
171 | exp '&' exp
173 $$ = new_exp_2 (land, $1, $3);
175 | exp EQUOP2 exp
177 $$ = new_exp_2 ($2, $1, $3);
179 | exp CMPOP2 exp
181 $$ = new_exp_2 ($2, $1, $3);
183 | exp ADDOP2 exp
185 $$ = new_exp_2 ($2, $1, $3);
187 | exp MULOP2 exp
189 $$ = new_exp_2 ($2, $1, $3);
191 | '!' exp
193 $$ = new_exp_1 (lnot, $2);
195 | 'n'
197 $$ = new_exp_0 (var);
199 | NUMBER
201 if (($$ = new_exp_0 (num)) != NULL)
202 $$->val.num = $1;
204 | '(' exp ')'
206 $$ = $2;
212 void
213 gettext_free_exp__(struct expression *exp)
215 if (exp == NULL)
216 return;
218 /* Handle the recursive case. */
219 switch (exp->nargs)
221 case 3:
222 gettext_free_exp__ (exp->val.args[2]);
223 /* FALLTHROUGH */
224 case 2:
225 gettext_free_exp__ (exp->val.args[1]);
226 /* FALLTHROUGH */
227 case 1:
228 gettext_free_exp__ (exp->val.args[0]);
229 /* FALLTHROUGH */
230 default:
231 break;
234 free (exp);
238 static int
239 yylex(YYSTYPE *lval, struct parse_args *arg)
241 const unsigned char *exp = arg->cp;
242 int result;
244 while (1)
246 if (exp[0] == '\0')
248 arg->cp = exp;
249 return YYEOF;
252 if (exp[0] != ' ' && exp[0] != '\t')
253 break;
255 ++exp;
258 result = *exp++;
259 switch (result)
261 case '0': case '1': case '2': case '3': case '4':
262 case '5': case '6': case '7': case '8': case '9':
264 unsigned long int n = result - '0';
265 while (exp[0] >= '0' && exp[0] <= '9')
267 n *= 10;
268 n += exp[0] - '0';
269 ++exp;
271 lval->num = n;
272 result = NUMBER;
274 break;
276 case '=':
277 if (exp[0] == '=')
279 ++exp;
280 lval->op = equal;
281 result = EQUOP2;
283 else
284 result = YYERRCODE;
285 break;
287 case '!':
288 if (exp[0] == '=')
290 ++exp;
291 lval->op = not_equal;
292 result = EQUOP2;
294 break;
296 case '&':
297 case '|':
298 if (exp[0] == result)
299 ++exp;
300 else
301 result = YYERRCODE;
302 break;
304 case '<':
305 if (exp[0] == '=')
307 ++exp;
308 lval->op = less_or_equal;
310 else
311 lval->op = less_than;
312 result = CMPOP2;
313 break;
315 case '>':
316 if (exp[0] == '=')
318 ++exp;
319 lval->op = greater_or_equal;
321 else
322 lval->op = greater_than;
323 result = CMPOP2;
324 break;
326 case '*':
327 lval->op = mult;
328 result = MULOP2;
329 break;
331 case '/':
332 lval->op = divide;
333 result = MULOP2;
334 break;
336 case '%':
337 lval->op = module;
338 result = MULOP2;
339 break;
341 case '+':
342 lval->op = plus;
343 result = ADDOP2;
344 break;
346 case '-':
347 lval->op = minus;
348 result = ADDOP2;
349 break;
351 case 'n':
352 case '?':
353 case ':':
354 case '(':
355 case ')':
356 /* Nothing, just return the character. */
357 break;
359 case ';':
360 case '\n':
361 case '\0':
362 /* Be safe and let the user call this function again. */
363 --exp;
364 result = YYEOF;
365 break;
367 default:
368 result = YYERRCODE;
369 #if YYDEBUG != 0
370 --exp;
371 #endif
372 break;
375 arg->cp = exp;
377 return result;
381 static void
382 yyerror(struct parse_args *arg, const unsigned char *str)
384 /* Do nothing. We don't print error messages here. */