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__
29 # include <gnulib/config.h>
31 # undef PACKAGE_VERSION
32 # undef PACKAGE_TARNAME
33 # undef PACKAGE_STRING
40 /* Names for the libintl functions are a problem. They must not clash
41 with existing names and they should follow ANSI C. But this source
42 code is also used in GNU C Library where the names have a __
43 prefix. So we have to make a difference here. */
45 # define FREE_EXPRESSION __gettext_free_exp
47 # define FREE_EXPRESSION gettext_free_exp__
48 # define __gettextparse gettextparse__
51 #define YYLEX_PARAM &((struct parse_args *) arg)->cp
52 #define YYPARSE_PARAM arg
58 unsigned long int num
;
60 struct expression
*exp
;
64 /* Prototypes for local functions. */
65 static struct expression
*new_exp PARAMS
((int nargs
, enum operator op
,
66 struct expression
* const *args
));
67 static inline
struct expression
*new_exp_0 PARAMS
((enum operator op
));
68 static inline
struct expression
*new_exp_1 PARAMS
((enum operator op
,
69 struct expression
*right
));
70 static struct expression
*new_exp_2 PARAMS
((enum operator op
,
71 struct expression
*left
,
72 struct expression
*right
));
73 static inline
struct expression
*new_exp_3 PARAMS
((enum operator op
,
74 struct expression
*bexp
,
75 struct expression
*tbranch
,
76 struct expression
*fbranch
));
77 static int yylex PARAMS
((YYSTYPE *lval
, const char **pexp
));
78 static void yyerror PARAMS
((const char *str
));
80 /* Allocation of expressions. */
82 static struct expression
*
83 new_exp
(nargs
, op
, args
)
86 struct expression
* const *args
;
89 struct expression
*newp
;
91 /* If any of the argument could not be malloc'ed, just return NULL. */
92 for
(i
= nargs
- 1; i
>= 0; i
--)
96 /* Allocate a new expression. */
97 newp
= (struct expression
*) malloc
(sizeof
(*newp
));
101 newp
->operation
= op
;
102 for
(i
= nargs
- 1; i
>= 0; i
--)
103 newp
->val.args
[i
] = args
[i
];
108 for
(i
= nargs
- 1; i
>= 0; i
--)
109 FREE_EXPRESSION
(args
[i
]);
114 static inline
struct expression
*
118 return new_exp
(0, op
, NULL
);
121 static inline
struct expression
*
122 new_exp_1
(op
, right
)
124 struct expression
*right
;
126 struct expression
*args
[1];
129 return new_exp
(1, op
, args
);
132 static struct expression
*
133 new_exp_2
(op
, left
, right
)
135 struct expression
*left
;
136 struct expression
*right
;
138 struct expression
*args
[2];
142 return new_exp
(2, op
, args
);
145 static inline
struct expression
*
146 new_exp_3
(op
, bexp
, tbranch
, fbranch
)
148 struct expression
*bexp
;
149 struct expression
*tbranch
;
150 struct expression
*fbranch
;
152 struct expression
*args
[3];
157 return new_exp
(3, op
, args
);
162 /* This declares that all operators have the same associativity and the
163 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
164 There is no unary minus and no bitwise operators.
165 Operators with the same syntactic behaviour have been merged into a single
166 token, to save space in the array generated by bison. */
170 %left EQUOP2
/* == != */
171 %left CMPOP2
/* < > <= >= */
172 %left ADDOP2
/* + - */
173 %left MULOP2
/* * / % */
176 %token
<op
> EQUOP2 CMPOP2 ADDOP2 MULOP2
186 ((struct parse_args
*) arg
)->res
= $1;
190 exp: exp
'?' exp
':' exp
192 $$
= new_exp_3
(qmop
, $1, $3, $5);
196 $$
= new_exp_2
(lor
, $1, $3);
200 $$
= new_exp_2
(land
, $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_2
($2, $1, $3);
220 $$
= new_exp_1
(lnot
, $2);
224 $$
= new_exp_0
(var
);
228 if
(($$
= new_exp_0
(num
)) != NULL
)
241 FREE_EXPRESSION
(exp
)
242 struct expression
*exp
;
247 /* Handle the recursive case. */
251 FREE_EXPRESSION
(exp
->val.args
[2]);
254 FREE_EXPRESSION
(exp
->val.args
[1]);
257 FREE_EXPRESSION
(exp
->val.args
[0]);
272 const char *exp
= *pexp
;
283 if
(exp
[0] != ' ' && exp
[0] != '\t')
292 case
'0': case
'1': case
'2': case
'3': case
'4':
293 case
'5': case
'6': case
'7': case
'8': case
'9':
295 unsigned long int n
= result
- '0';
296 while
(exp
[0] >= '0' && exp
[0] <= '9')
322 lval
->op
= not_equal
;
329 if
(exp
[0] == result
)
339 lval
->op
= less_or_equal
;
342 lval
->op
= less_than
;
350 lval
->op
= greater_or_equal
;
353 lval
->op
= greater_than
;
387 /* Nothing, just return the character. */
393 /* Be safe and let the user call this function again. */
416 /* Do nothing. We don't print error messages here. */