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)
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__
35
/* Names for the libintl functions are a problem. They must not clash
36
with existing names and they should follow ANSI C. But this source
37
code is also used in GNU C Library where the names have a __
38
prefix. So we have to make a difference here. */
40
# define FREE_EXPRESSION __gettext_free_exp
42
# define FREE_EXPRESSION gettext_free_exp__
43
# define __gettextparse gettextparse__
46
#define YYLEX_PARAM &((struct parse_args *) arg)->cp
47
#define YYPARSE_PARAM arg
53
unsigned long int num;
55
struct expression *exp;
59
/* Prototypes for local functions. */
60
static struct expression *new_exp PARAMS ((int nargs, enum operator op,
61
struct expression * const *args));
62
static inline struct expression *new_exp_0 PARAMS ((enum operator op));
63
static inline struct expression *new_exp_1 PARAMS ((enum operator op,
64
struct expression *right));
65
static struct expression *new_exp_2 PARAMS ((enum operator op,
66
struct expression *left,
67
struct expression *right));
68
static inline struct expression *new_exp_3 PARAMS ((enum operator op,
69
struct expression *bexp,
70
struct expression *tbranch,
71
struct expression *fbranch));
72
static int yylex PARAMS ((YYSTYPE *lval, const char **pexp));
73
static void yyerror PARAMS ((const char *str));
75
/* Allocation of expressions. */
77
static struct expression *
78
new_exp (nargs, op, args)
81
struct expression * const *args;
84
struct expression *newp;
86
/* If any of the argument could not be malloc'ed, just return NULL. */
87
for (i = nargs - 1; i >= 0; i--)
91
/* Allocate a new expression. */
92
newp = (struct expression *) malloc (sizeof (*newp));
97
for (i = nargs - 1; i >= 0; i--)
98
newp->val.args[i] = args[i];
103
for (i = nargs - 1; i >= 0; i--)
104
FREE_EXPRESSION (args[i]);
109
static inline struct expression *
113
return new_exp (0, op, NULL);
116
static inline struct expression *
117
new_exp_1 (op, right)
119
struct expression *right;
121
struct expression *args[1];
124
return new_exp (1, op, args);
127
static struct expression *
128
new_exp_2 (op, left, right)
130
struct expression *left;
131
struct expression *right;
133
struct expression *args[2];
137
return new_exp (2, op, args);
140
static inline struct expression *
141
new_exp_3 (op, bexp, tbranch, fbranch)
143
struct expression *bexp;
144
struct expression *tbranch;
145
struct expression *fbranch;
147
struct expression *args[3];
152
return new_exp (3, op, args);
157
/* This declares that all operators have the same associativity and the
158
precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
159
There is no unary minus and no bitwise operators.
160
Operators with the same syntactic behaviour have been merged into a single
161
token, to save space in the array generated by bison. */
165
%left EQUOP2 /* == != */
166
%left CMPOP2 /* < > <= >= */
167
%left ADDOP2 /* + - */
168
%left MULOP2 /* * / % */
171
%token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
181
((struct parse_args *) arg)->res = $1;
185
exp: exp '?' exp ':' exp
187
$$ = new_exp_3 (qmop, $1, $3, $5);
191
$$ = new_exp_2 (lor, $1, $3);
195
$$ = new_exp_2 (land, $1, $3);
199
$$ = new_exp_2 ($2, $1, $3);
203
$$ = new_exp_2 ($2, $1, $3);
207
$$ = new_exp_2 ($2, $1, $3);
211
$$ = new_exp_2 ($2, $1, $3);
215
$$ = new_exp_1 (lnot, $2);
219
$$ = new_exp_0 (var);
223
if (($$ = new_exp_0 (num)) != NULL)
236
FREE_EXPRESSION (exp)
237
struct expression *exp;
242
/* Handle the recursive case. */
246
FREE_EXPRESSION (exp->val.args[2]);
249
FREE_EXPRESSION (exp->val.args[1]);
252
FREE_EXPRESSION (exp->val.args[0]);
267
const char *exp = *pexp;
278
if (exp[0] != ' ' && exp[0] != '\t')
287
case '0': case '1': case '2': case '3': case '4':
288
case '5': case '6': case '7': case '8': case '9':
290
unsigned long int n = result - '0';
291
while (exp[0] >= '0' && exp[0] <= '9')
317
lval->op = not_equal;
324
if (exp[0] == result)
334
lval->op = less_or_equal;
337
lval->op = less_than;
345
lval->op = greater_or_equal;
348
lval->op = greater_than;
382
/* Nothing, just return the character. */
388
/* Be safe and let the user call this function again. */
411
/* Do nothing. We don't print error messages here. */