2 /* Expression parsing for plural form selection.
3 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Written by Ulrich Drepper <drepper@cygnus.com>, 2000.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 /* Names for the libintl functions are a problem. They must not clash
30 with existing names and they should follow ANSI C. But this source
31 code is also used in GNU C Library where the names have a __
32 prefix. So we have to make a difference here. */
34 # define FREE_EXPRESSION __gettext_free_exp
36 # define FREE_EXPRESSION gettext_free_exp__
37 # define __gettextparse gettextparse__
40 #define YYLEX_PARAM &((struct parse_args *) arg)->cp
41 #define YYPARSE_PARAM arg
47 unsigned long int num
;
49 struct expression
*exp
;
53 /* Prototypes for local functions. */
54 static struct expression
*new_exp PARAMS
((int nargs
, enum operator op
,
55 struct expression
* const *args
));
56 static inline
struct expression
*new_exp_0 PARAMS
((enum operator op
));
57 static inline
struct expression
*new_exp_1 PARAMS
((enum operator op
,
58 struct expression
*right
));
59 static struct expression
*new_exp_2 PARAMS
((enum operator op
,
60 struct expression
*left
,
61 struct expression
*right
));
62 static inline
struct expression
*new_exp_3 PARAMS
((enum operator op
,
63 struct expression
*bexp
,
64 struct expression
*tbranch
,
65 struct expression
*fbranch
));
66 static int yylex PARAMS
((YYSTYPE *lval
, const char **pexp
));
67 static void yyerror PARAMS
((const char *str
));
69 /* Allocation of expressions. */
71 static struct expression
*
72 new_exp
(nargs
, op
, args
)
75 struct expression
* const *args
;
78 struct expression
*newp
;
80 /* If any of the argument could not be malloc'ed, just return NULL. */
81 for
(i
= nargs
- 1; i
>= 0; i
--)
85 /* Allocate a new expression. */
86 newp
= (struct expression
*) malloc
(sizeof
(*newp
));
91 for
(i
= nargs
- 1; i
>= 0; i
--)
92 newp
->val.args
[i
] = args
[i
];
97 for
(i
= nargs
- 1; i
>= 0; i
--)
98 FREE_EXPRESSION
(args
[i
]);
103 static inline
struct expression
*
107 return new_exp
(0, op
, NULL
);
110 static inline
struct expression
*
111 new_exp_1
(op
, right
)
113 struct expression
*right
;
115 struct expression
*args
[1];
118 return new_exp
(1, op
, args
);
121 static struct expression
*
122 new_exp_2
(op
, left
, right
)
124 struct expression
*left
;
125 struct expression
*right
;
127 struct expression
*args
[2];
131 return new_exp
(2, op
, args
);
134 static inline
struct expression
*
135 new_exp_3
(op
, bexp
, tbranch
, fbranch
)
137 struct expression
*bexp
;
138 struct expression
*tbranch
;
139 struct expression
*fbranch
;
141 struct expression
*args
[3];
146 return new_exp
(3, op
, args
);
151 /* This declares that all operators have the same associativity and the
152 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
153 There is no unary minus and no bitwise operators.
154 Operators with the same syntactic behaviour have been merged into a single
155 token, to save space in the array generated by bison. */
159 %left EQUOP2
/* == != */
160 %left CMPOP2
/* < > <= >= */
161 %left ADDOP2
/* + - */
162 %left MULOP2
/* * / % */
165 %token
<op
> EQUOP2 CMPOP2 ADDOP2 MULOP2
175 ((struct parse_args
*) arg
)->res
= $1;
179 exp: exp
'?' exp
':' exp
181 $$
= new_exp_3
(qmop
, $1, $3, $5);
185 $$
= new_exp_2
(lor
, $1, $3);
189 $$
= new_exp_2
(land
, $1, $3);
193 $$
= new_exp_2
($2, $1, $3);
197 $$
= new_exp_2
($2, $1, $3);
201 $$
= new_exp_2
($2, $1, $3);
205 $$
= new_exp_2
($2, $1, $3);
209 $$
= new_exp_1
(lnot
, $2);
213 $$
= new_exp_0
(var
);
217 if
(($$
= new_exp_0
(num
)) != NULL
)
230 FREE_EXPRESSION
(exp
)
231 struct expression
*exp
;
236 /* Handle the recursive case. */
240 FREE_EXPRESSION
(exp
->val.args
[2]);
243 FREE_EXPRESSION
(exp
->val.args
[1]);
246 FREE_EXPRESSION
(exp
->val.args
[0]);
261 const char *exp
= *pexp
;
272 if
(exp
[0] != ' ' && exp
[0] != '\t')
281 case
'0': case
'1': case
'2': case
'3': case
'4':
282 case
'5': case
'6': case
'7': case
'8': case
'9':
284 unsigned long int n
= result
- '0';
285 while
(exp
[0] >= '0' && exp
[0] <= '9')
311 lval
->op
= not_equal
;
318 if
(exp
[0] == result
)
328 lval
->op
= less_or_equal
;
331 lval
->op
= less_than
;
339 lval
->op
= greater_or_equal
;
342 lval
->op
= greater_than
;
376 /* Nothing, just return the character. */
382 /* Be safe and let the user call this function again. */
405 /* Do nothing. We don't print error messages here. */