1*404b540aSrobert %{
2*404b540aSrobert /* Expression parsing for plural form selection.
3*404b540aSrobert Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4*404b540aSrobert Written by Ulrich Drepper <drepper@cygnus.com>, 2000.
5*404b540aSrobert
6*404b540aSrobert This program is free software; you can redistribute it and/or modify it
7*404b540aSrobert under the terms of the GNU Library General Public License as published
8*404b540aSrobert by the Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert any later version.
10*404b540aSrobert
11*404b540aSrobert This program is distributed in the hope that it will be useful,
12*404b540aSrobert but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14*404b540aSrobert Library General Public License for more details.
15*404b540aSrobert
16*404b540aSrobert You should have received a copy of the GNU Library General Public
17*404b540aSrobert License along with this program; if not, write to the Free Software
18*404b540aSrobert Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
19*404b540aSrobert USA. */
20*404b540aSrobert
21*404b540aSrobert /* The bison generated parser uses alloca. AIX 3 forces us to put this
22*404b540aSrobert declaration at the beginning of the file. The declaration in bison's
23*404b540aSrobert skeleton file comes too late. This must come before <config.h>
24*404b540aSrobert because <config.h> may include arbitrary system headers. */
25*404b540aSrobert #if defined _AIX && !defined __GNUC__
26*404b540aSrobert #pragma alloca
27*404b540aSrobert #endif
28*404b540aSrobert
29*404b540aSrobert #ifdef HAVE_CONFIG_H
30*404b540aSrobert # include <config.h>
31*404b540aSrobert #endif
32*404b540aSrobert
33*404b540aSrobert #include <stddef.h>
34*404b540aSrobert #include <stdlib.h>
35*404b540aSrobert #include "plural-exp.h"
36*404b540aSrobert
37*404b540aSrobert /* The main function generated by the parser is called __gettextparse,
38*404b540aSrobert but we want it to be called PLURAL_PARSE. */
39*404b540aSrobert #ifndef _LIBC
40*404b540aSrobert # define __gettextparse PLURAL_PARSE
41*404b540aSrobert #endif
42*404b540aSrobert
43*404b540aSrobert #define YYLEX_PARAM &((struct parse_args *) arg)->cp
44*404b540aSrobert #define YYPARSE_PARAM arg
45*404b540aSrobert %}
46*404b540aSrobert %pure_parser
47*404b540aSrobert %expect 7
48*404b540aSrobert
49*404b540aSrobert %union {
50*404b540aSrobert unsigned long int num;
51*404b540aSrobert enum operator op;
52*404b540aSrobert struct expression *exp;
53*404b540aSrobert }
54*404b540aSrobert
55*404b540aSrobert %{
56*404b540aSrobert /* Prototypes for local functions. */
57*404b540aSrobert static struct expression *new_exp PARAMS ((int nargs, enum operator op,
58*404b540aSrobert struct expression * const *args));
59*404b540aSrobert static inline struct expression *new_exp_0 PARAMS ((enum operator op));
60*404b540aSrobert static inline struct expression *new_exp_1 PARAMS ((enum operator op,
61*404b540aSrobert struct expression *right));
62*404b540aSrobert static struct expression *new_exp_2 PARAMS ((enum operator op,
63*404b540aSrobert struct expression *left,
64*404b540aSrobert struct expression *right));
65*404b540aSrobert static inline struct expression *new_exp_3 PARAMS ((enum operator op,
66*404b540aSrobert struct expression *bexp,
67*404b540aSrobert struct expression *tbranch,
68*404b540aSrobert struct expression *fbranch));
69*404b540aSrobert static int yylex PARAMS ((YYSTYPE *lval, const char **pexp));
70*404b540aSrobert static void yyerror PARAMS ((const char *str));
71*404b540aSrobert
72*404b540aSrobert /* Allocation of expressions. */
73*404b540aSrobert
74*404b540aSrobert static struct expression *
new_exp(nargs,op,args)75*404b540aSrobert new_exp (nargs, op, args)
76*404b540aSrobert int nargs;
77*404b540aSrobert enum operator op;
78*404b540aSrobert struct expression * const *args;
79*404b540aSrobert {
80*404b540aSrobert int i;
81*404b540aSrobert struct expression *newp;
82*404b540aSrobert
83*404b540aSrobert /* If any of the argument could not be malloc'ed, just return NULL. */
84*404b540aSrobert for (i = nargs - 1; i >= 0; i--)
85*404b540aSrobert if (args[i] == NULL)
86*404b540aSrobert goto fail;
87*404b540aSrobert
88*404b540aSrobert /* Allocate a new expression. */
89*404b540aSrobert newp = (struct expression *) malloc (sizeof (*newp));
90*404b540aSrobert if (newp != NULL)
91*404b540aSrobert {
92*404b540aSrobert newp->nargs = nargs;
93*404b540aSrobert newp->operation = op;
94*404b540aSrobert for (i = nargs - 1; i >= 0; i--)
95*404b540aSrobert newp->val.args[i] = args[i];
96*404b540aSrobert return newp;
97*404b540aSrobert }
98*404b540aSrobert
99*404b540aSrobert fail:
100*404b540aSrobert for (i = nargs - 1; i >= 0; i--)
101*404b540aSrobert FREE_EXPRESSION (args[i]);
102*404b540aSrobert
103*404b540aSrobert return NULL;
104*404b540aSrobert }
105*404b540aSrobert
106*404b540aSrobert static inline struct expression *
new_exp_0(op)107*404b540aSrobert new_exp_0 (op)
108*404b540aSrobert enum operator op;
109*404b540aSrobert {
110*404b540aSrobert return new_exp (0, op, NULL);
111*404b540aSrobert }
112*404b540aSrobert
113*404b540aSrobert static inline struct expression *
new_exp_1(op,right)114*404b540aSrobert new_exp_1 (op, right)
115*404b540aSrobert enum operator op;
116*404b540aSrobert struct expression *right;
117*404b540aSrobert {
118*404b540aSrobert struct expression *args[1];
119*404b540aSrobert
120*404b540aSrobert args[0] = right;
121*404b540aSrobert return new_exp (1, op, args);
122*404b540aSrobert }
123*404b540aSrobert
124*404b540aSrobert static struct expression *
new_exp_2(op,left,right)125*404b540aSrobert new_exp_2 (op, left, right)
126*404b540aSrobert enum operator op;
127*404b540aSrobert struct expression *left;
128*404b540aSrobert struct expression *right;
129*404b540aSrobert {
130*404b540aSrobert struct expression *args[2];
131*404b540aSrobert
132*404b540aSrobert args[0] = left;
133*404b540aSrobert args[1] = right;
134*404b540aSrobert return new_exp (2, op, args);
135*404b540aSrobert }
136*404b540aSrobert
137*404b540aSrobert static inline struct expression *
new_exp_3(op,bexp,tbranch,fbranch)138*404b540aSrobert new_exp_3 (op, bexp, tbranch, fbranch)
139*404b540aSrobert enum operator op;
140*404b540aSrobert struct expression *bexp;
141*404b540aSrobert struct expression *tbranch;
142*404b540aSrobert struct expression *fbranch;
143*404b540aSrobert {
144*404b540aSrobert struct expression *args[3];
145*404b540aSrobert
146*404b540aSrobert args[0] = bexp;
147*404b540aSrobert args[1] = tbranch;
148*404b540aSrobert args[2] = fbranch;
149*404b540aSrobert return new_exp (3, op, args);
150*404b540aSrobert }
151*404b540aSrobert
152*404b540aSrobert %}
153*404b540aSrobert
154*404b540aSrobert /* This declares that all operators have the same associativity and the
155*404b540aSrobert precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
156*404b540aSrobert There is no unary minus and no bitwise operators.
157*404b540aSrobert Operators with the same syntactic behaviour have been merged into a single
158*404b540aSrobert token, to save space in the array generated by bison. */
159*404b540aSrobert %right '?' /* ? */
160*404b540aSrobert %left '|' /* || */
161*404b540aSrobert %left '&' /* && */
162*404b540aSrobert %left EQUOP2 /* == != */
163*404b540aSrobert %left CMPOP2 /* < > <= >= */
164*404b540aSrobert %left ADDOP2 /* + - */
165*404b540aSrobert %left MULOP2 /* * / % */
166*404b540aSrobert %right '!' /* ! */
167*404b540aSrobert
168*404b540aSrobert %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
169*404b540aSrobert %token <num> NUMBER
170*404b540aSrobert %type <exp> exp
171*404b540aSrobert
172*404b540aSrobert %%
173*404b540aSrobert
174*404b540aSrobert start: exp
175*404b540aSrobert {
176*404b540aSrobert if ($1 == NULL)
177*404b540aSrobert YYABORT;
178*404b540aSrobert ((struct parse_args *) arg)->res = $1;
179*404b540aSrobert }
180*404b540aSrobert ;
181*404b540aSrobert
182*404b540aSrobert exp: exp '?' exp ':' exp
183*404b540aSrobert {
184*404b540aSrobert $$ = new_exp_3 (qmop, $1, $3, $5);
185*404b540aSrobert }
186*404b540aSrobert | exp '|' exp
187*404b540aSrobert {
188*404b540aSrobert $$ = new_exp_2 (lor, $1, $3);
189*404b540aSrobert }
190*404b540aSrobert | exp '&' exp
191*404b540aSrobert {
192*404b540aSrobert $$ = new_exp_2 (land, $1, $3);
193*404b540aSrobert }
194*404b540aSrobert | exp EQUOP2 exp
195*404b540aSrobert {
196*404b540aSrobert $$ = new_exp_2 ($2, $1, $3);
197*404b540aSrobert }
198*404b540aSrobert | exp CMPOP2 exp
199*404b540aSrobert {
200*404b540aSrobert $$ = new_exp_2 ($2, $1, $3);
201*404b540aSrobert }
202*404b540aSrobert | exp ADDOP2 exp
203*404b540aSrobert {
204*404b540aSrobert $$ = new_exp_2 ($2, $1, $3);
205*404b540aSrobert }
206*404b540aSrobert | exp MULOP2 exp
207*404b540aSrobert {
208*404b540aSrobert $$ = new_exp_2 ($2, $1, $3);
209*404b540aSrobert }
210*404b540aSrobert | '!' exp
211*404b540aSrobert {
212*404b540aSrobert $$ = new_exp_1 (lnot, $2);
213*404b540aSrobert }
214*404b540aSrobert | 'n'
215*404b540aSrobert {
216*404b540aSrobert $$ = new_exp_0 (var);
217*404b540aSrobert }
218*404b540aSrobert | NUMBER
219*404b540aSrobert {
220*404b540aSrobert if (($$ = new_exp_0 (num)) != NULL)
221*404b540aSrobert $$->val.num = $1;
222*404b540aSrobert }
223*404b540aSrobert | '(' exp ')'
224*404b540aSrobert {
225*404b540aSrobert $$ = $2;
226*404b540aSrobert }
227*404b540aSrobert ;
228*404b540aSrobert
229*404b540aSrobert %%
230*404b540aSrobert
231*404b540aSrobert void
232*404b540aSrobert internal_function
FREE_EXPRESSION(exp)233*404b540aSrobert FREE_EXPRESSION (exp)
234*404b540aSrobert struct expression *exp;
235*404b540aSrobert {
236*404b540aSrobert if (exp == NULL)
237*404b540aSrobert return;
238*404b540aSrobert
239*404b540aSrobert /* Handle the recursive case. */
240*404b540aSrobert switch (exp->nargs)
241*404b540aSrobert {
242*404b540aSrobert case 3:
243*404b540aSrobert FREE_EXPRESSION (exp->val.args[2]);
244*404b540aSrobert /* FALLTHROUGH */
245*404b540aSrobert case 2:
246*404b540aSrobert FREE_EXPRESSION (exp->val.args[1]);
247*404b540aSrobert /* FALLTHROUGH */
248*404b540aSrobert case 1:
249*404b540aSrobert FREE_EXPRESSION (exp->val.args[0]);
250*404b540aSrobert /* FALLTHROUGH */
251*404b540aSrobert default:
252*404b540aSrobert break;
253*404b540aSrobert }
254*404b540aSrobert
255*404b540aSrobert free (exp);
256*404b540aSrobert }
257*404b540aSrobert
258*404b540aSrobert
259*404b540aSrobert static int
yylex(lval,pexp)260*404b540aSrobert yylex (lval, pexp)
261*404b540aSrobert YYSTYPE *lval;
262*404b540aSrobert const char **pexp;
263*404b540aSrobert {
264*404b540aSrobert const char *exp = *pexp;
265*404b540aSrobert int result;
266*404b540aSrobert
267*404b540aSrobert while (1)
268*404b540aSrobert {
269*404b540aSrobert if (exp[0] == '\0')
270*404b540aSrobert {
271*404b540aSrobert *pexp = exp;
272*404b540aSrobert return YYEOF;
273*404b540aSrobert }
274*404b540aSrobert
275*404b540aSrobert if (exp[0] != ' ' && exp[0] != '\t')
276*404b540aSrobert break;
277*404b540aSrobert
278*404b540aSrobert ++exp;
279*404b540aSrobert }
280*404b540aSrobert
281*404b540aSrobert result = *exp++;
282*404b540aSrobert switch (result)
283*404b540aSrobert {
284*404b540aSrobert case '0': case '1': case '2': case '3': case '4':
285*404b540aSrobert case '5': case '6': case '7': case '8': case '9':
286*404b540aSrobert {
287*404b540aSrobert unsigned long int n = result - '0';
288*404b540aSrobert while (exp[0] >= '0' && exp[0] <= '9')
289*404b540aSrobert {
290*404b540aSrobert n *= 10;
291*404b540aSrobert n += exp[0] - '0';
292*404b540aSrobert ++exp;
293*404b540aSrobert }
294*404b540aSrobert lval->num = n;
295*404b540aSrobert result = NUMBER;
296*404b540aSrobert }
297*404b540aSrobert break;
298*404b540aSrobert
299*404b540aSrobert case '=':
300*404b540aSrobert if (exp[0] == '=')
301*404b540aSrobert {
302*404b540aSrobert ++exp;
303*404b540aSrobert lval->op = equal;
304*404b540aSrobert result = EQUOP2;
305*404b540aSrobert }
306*404b540aSrobert else
307*404b540aSrobert result = YYERRCODE;
308*404b540aSrobert break;
309*404b540aSrobert
310*404b540aSrobert case '!':
311*404b540aSrobert if (exp[0] == '=')
312*404b540aSrobert {
313*404b540aSrobert ++exp;
314*404b540aSrobert lval->op = not_equal;
315*404b540aSrobert result = EQUOP2;
316*404b540aSrobert }
317*404b540aSrobert break;
318*404b540aSrobert
319*404b540aSrobert case '&':
320*404b540aSrobert case '|':
321*404b540aSrobert if (exp[0] == result)
322*404b540aSrobert ++exp;
323*404b540aSrobert else
324*404b540aSrobert result = YYERRCODE;
325*404b540aSrobert break;
326*404b540aSrobert
327*404b540aSrobert case '<':
328*404b540aSrobert if (exp[0] == '=')
329*404b540aSrobert {
330*404b540aSrobert ++exp;
331*404b540aSrobert lval->op = less_or_equal;
332*404b540aSrobert }
333*404b540aSrobert else
334*404b540aSrobert lval->op = less_than;
335*404b540aSrobert result = CMPOP2;
336*404b540aSrobert break;
337*404b540aSrobert
338*404b540aSrobert case '>':
339*404b540aSrobert if (exp[0] == '=')
340*404b540aSrobert {
341*404b540aSrobert ++exp;
342*404b540aSrobert lval->op = greater_or_equal;
343*404b540aSrobert }
344*404b540aSrobert else
345*404b540aSrobert lval->op = greater_than;
346*404b540aSrobert result = CMPOP2;
347*404b540aSrobert break;
348*404b540aSrobert
349*404b540aSrobert case '*':
350*404b540aSrobert lval->op = mult;
351*404b540aSrobert result = MULOP2;
352*404b540aSrobert break;
353*404b540aSrobert
354*404b540aSrobert case '/':
355*404b540aSrobert lval->op = divide;
356*404b540aSrobert result = MULOP2;
357*404b540aSrobert break;
358*404b540aSrobert
359*404b540aSrobert case '%':
360*404b540aSrobert lval->op = module;
361*404b540aSrobert result = MULOP2;
362*404b540aSrobert break;
363*404b540aSrobert
364*404b540aSrobert case '+':
365*404b540aSrobert lval->op = plus;
366*404b540aSrobert result = ADDOP2;
367*404b540aSrobert break;
368*404b540aSrobert
369*404b540aSrobert case '-':
370*404b540aSrobert lval->op = minus;
371*404b540aSrobert result = ADDOP2;
372*404b540aSrobert break;
373*404b540aSrobert
374*404b540aSrobert case 'n':
375*404b540aSrobert case '?':
376*404b540aSrobert case ':':
377*404b540aSrobert case '(':
378*404b540aSrobert case ')':
379*404b540aSrobert /* Nothing, just return the character. */
380*404b540aSrobert break;
381*404b540aSrobert
382*404b540aSrobert case ';':
383*404b540aSrobert case '\n':
384*404b540aSrobert case '\0':
385*404b540aSrobert /* Be safe and let the user call this function again. */
386*404b540aSrobert --exp;
387*404b540aSrobert result = YYEOF;
388*404b540aSrobert break;
389*404b540aSrobert
390*404b540aSrobert default:
391*404b540aSrobert result = YYERRCODE;
392*404b540aSrobert #if YYDEBUG != 0
393*404b540aSrobert --exp;
394*404b540aSrobert #endif
395*404b540aSrobert break;
396*404b540aSrobert }
397*404b540aSrobert
398*404b540aSrobert *pexp = exp;
399*404b540aSrobert
400*404b540aSrobert return result;
401*404b540aSrobert }
402*404b540aSrobert
403*404b540aSrobert
404*404b540aSrobert static void
yyerror(str)405*404b540aSrobert yyerror (str)
406*404b540aSrobert const char *str;
407*404b540aSrobert {
408*404b540aSrobert /* Do nothing. We don't print error messages here. */
409*404b540aSrobert }
410