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 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library 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 the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 /* Names for the libintl functions are a problem. They must not clash
29 with existing names and they should follow ANSI C. But this source
30 code is also used in GNU C Library where the names have a __
31 prefix. So we have to make a difference here. */
33 # define FREE_EXPRESSION __gettext_free_exp
35 # define FREE_EXPRESSION gettext_free_exp__
36 # define __gettextparse gettextparse__
39 #define YYLEX_PARAM &((struct parse_args *) arg)->cp
40 #define YYPARSE_PARAM arg
46 unsigned long int num
;
48 struct expression
*exp
;
52 /* Prototypes for local functions. */
53 static struct expression
*new_exp PARAMS
((int nargs
, enum operator op
,
54 struct expression
* const *args
));
55 static inline
struct expression
*new_exp_0 PARAMS
((enum operator op
));
56 static inline
struct expression
*new_exp_1 PARAMS
((enum operator op
,
57 struct expression
*right
));
58 static struct expression
*new_exp_2 PARAMS
((enum operator op
,
59 struct expression
*left
,
60 struct expression
*right
));
61 static inline
struct expression
*new_exp_3 PARAMS
((enum operator op
,
62 struct expression
*bexp
,
63 struct expression
*tbranch
,
64 struct expression
*fbranch
));
65 static int yylex PARAMS
((YYSTYPE *lval
, const char **pexp
));
66 static void yyerror PARAMS
((const char *str
));
68 /* Allocation of expressions. */
70 static struct expression
*
71 new_exp
(nargs
, op
, args
)
74 struct expression
* const *args
;
77 struct expression
*newp
;
79 /* If any of the argument could not be malloc'ed, just return NULL. */
80 for
(i
= nargs
- 1; i
>= 0; i
--)
84 /* Allocate a new expression. */
85 newp
= (struct expression
*) malloc
(sizeof
(*newp
));
90 for
(i
= nargs
- 1; i
>= 0; i
--)
91 newp
->val.args
[i
] = args
[i
];
96 for
(i
= nargs
- 1; i
>= 0; i
--)
97 FREE_EXPRESSION
(args
[i
]);
102 static inline
struct expression
*
106 return new_exp
(0, op
, NULL
);
109 static inline
struct expression
*
110 new_exp_1
(op
, right
)
112 struct expression
*right
;
114 struct expression
*args
[1];
117 return new_exp
(1, op
, args
);
120 static struct expression
*
121 new_exp_2
(op
, left
, right
)
123 struct expression
*left
;
124 struct expression
*right
;
126 struct expression
*args
[2];
130 return new_exp
(2, op
, args
);
133 static inline
struct expression
*
134 new_exp_3
(op
, bexp
, tbranch
, fbranch
)
136 struct expression
*bexp
;
137 struct expression
*tbranch
;
138 struct expression
*fbranch
;
140 struct expression
*args
[3];
145 return new_exp
(3, op
, args
);
150 /* This declares that all operators have the same associativity and the
151 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
152 There is no unary minus and no bitwise operators.
153 Operators with the same syntactic behaviour have been merged into a single
154 token, to save space in the array generated by bison. */
158 %left EQUOP2
/* == != */
159 %left CMPOP2
/* < > <= >= */
160 %left ADDOP2
/* + - */
161 %left MULOP2
/* * / % */
164 %token
<op
> EQUOP2 CMPOP2 ADDOP2 MULOP2
174 ((struct parse_args
*) arg
)->res
= $1;
178 exp: exp
'?' exp
':' exp
180 $$
= new_exp_3
(qmop
, $1, $3, $5);
184 $$
= new_exp_2
(lor
, $1, $3);
188 $$
= new_exp_2
(land
, $1, $3);
192 $$
= new_exp_2
($2, $1, $3);
196 $$
= new_exp_2
($2, $1, $3);
200 $$
= new_exp_2
($2, $1, $3);
204 $$
= new_exp_2
($2, $1, $3);
208 $$
= new_exp_1
(lnot
, $2);
212 $$
= new_exp_0
(var
);
216 if
(($$
= new_exp_0
(num
)) != NULL
)
229 FREE_EXPRESSION
(exp
)
230 struct expression
*exp
;
235 /* Handle the recursive case. */
239 FREE_EXPRESSION
(exp
->val.args
[2]);
242 FREE_EXPRESSION
(exp
->val.args
[1]);
245 FREE_EXPRESSION
(exp
->val.args
[0]);
260 const char *exp
= *pexp
;
271 if
(exp
[0] != ' ' && exp
[0] != '\t')
280 case
'0': case
'1': case
'2': case
'3': case
'4':
281 case
'5': case
'6': case
'7': case
'8': case
'9':
283 unsigned long int n
= result
- '0';
284 while
(exp
[0] >= '0' && exp
[0] <= '9')
310 lval
->op
= not_equal
;
317 if
(exp
[0] == result
)
327 lval
->op
= less_or_equal
;
330 lval
->op
= less_than
;
338 lval
->op
= greater_or_equal
;
341 lval
->op
= greater_than
;
375 /* Nothing, just return the character. */
381 /* Be safe and let the user call this function again. */
404 /* Do nothing. We don't print error messages here. */