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)
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,
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__
36 /* Names for the libintl functions are a problem. They must not clash
37 with existing names and they should follow ANSI C. But this source
38 code is also used in GNU C Library where the names have a __
39 prefix. So we have to make a difference here. */
41 # define FREE_EXPRESSION __gettext_free_exp
43 # define FREE_EXPRESSION gettext_free_exp__
44 # define __gettextparse gettextparse__
47 #define YYLEX_PARAM &((struct parse_args *) arg)->cp
48 #define YYPARSE_PARAM arg
54 unsigned long int num;
56 struct expression *exp;
60 /* Prototypes for local functions. */
61 static struct expression *new_exp PARAMS ((int nargs, enum operator op,
62 struct expression * const *args));
63 static inline struct expression *new_exp_0 PARAMS ((enum operator op));
64 static inline struct expression *new_exp_1 PARAMS ((enum operator op,
65 struct expression *right));
66 static struct expression *new_exp_2 PARAMS ((enum operator op,
67 struct expression *left,
68 struct expression *right));
69 static inline struct expression *new_exp_3 PARAMS ((enum operator op,
70 struct expression *bexp,
71 struct expression *tbranch,
72 struct expression *fbranch));
73 static int yylex PARAMS ((YYSTYPE *lval, const char **pexp));
74 static void yyerror PARAMS ((const char *str));
76 /* Allocation of expressions. */
78 static struct expression *
79 new_exp (nargs, op, args)
82 struct expression * const *args;
85 struct expression *newp;
87 /* If any of the argument could not be malloc'ed, just return NULL. */
88 for (i = nargs - 1; i >= 0; i--)
92 /* Allocate a new expression. */
93 newp = (struct expression *) malloc (sizeof (*newp));
98 for (i = nargs - 1; i >= 0; i--)
99 newp->val.args[i] = args[i];
104 for (i = nargs - 1; i >= 0; i--)
105 FREE_EXPRESSION (args[i]);
110 static inline struct expression *
114 return new_exp (0, op, NULL);
117 static inline struct expression *
118 new_exp_1 (op, right)
120 struct expression *right;
122 struct expression *args[1];
125 return new_exp (1, op, args);
128 static struct expression *
129 new_exp_2 (op, left, right)
131 struct expression *left;
132 struct expression *right;
134 struct expression *args[2];
138 return new_exp (2, op, args);
141 static inline struct expression *
142 new_exp_3 (op, bexp, tbranch, fbranch)
144 struct expression *bexp;
145 struct expression *tbranch;
146 struct expression *fbranch;
148 struct expression *args[3];
153 return new_exp (3, op, args);
158 /* This declares that all operators have the same associativity and the
159 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
160 There is no unary minus and no bitwise operators.
161 Operators with the same syntactic behaviour have been merged into a single
162 token, to save space in the array generated by bison. */
166 %left EQUOP2 /* == != */
167 %left CMPOP2 /* < > <= >= */
168 %left ADDOP2 /* + - */
169 %left MULOP2 /* * / % */
172 %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
182 ((struct parse_args *) arg)->res = $1;
186 exp: exp '?' exp ':' exp
188 $$ = new_exp_3 (qmop, $1, $3, $5);
192 $$ = new_exp_2 (lor, $1, $3);
196 $$ = new_exp_2 (land, $1, $3);
200 $$ = new_exp_2 ($2, $1, $3);
204 $$ = new_exp_2 ($2, $1, $3);
208 $$ = new_exp_2 ($2, $1, $3);
212 $$ = new_exp_2 ($2, $1, $3);
216 $$ = new_exp_1 (lnot, $2);
220 $$ = new_exp_0 (var);
224 if (($$ = new_exp_0 (num)) != NULL)
237 FREE_EXPRESSION (exp)
238 struct expression *exp;
243 /* Handle the recursive case. */
247 FREE_EXPRESSION (exp->val.args[2]);
250 FREE_EXPRESSION (exp->val.args[1]);
253 FREE_EXPRESSION (exp->val.args[0]);
268 const char *exp = *pexp;
279 if (exp[0] != ' ' && exp[0] != '\t')
288 case '0': case '1': case '2': case '3': case '4':
289 case '5': case '6': case '7': case '8': case '9':
291 unsigned long int n = result - '0';
292 while (exp[0] >= '0' && exp[0] <= '9')
318 lval->op = not_equal;
325 if (exp[0] == result)
335 lval->op = less_or_equal;
338 lval->op = less_than;
346 lval->op = greater_or_equal;
349 lval->op = greater_than;
383 /* Nothing, just return the character. */
389 /* Be safe and let the user call this function again. */
412 /* Do nothing. We don't print error messages here. */