]>
Commit | Line | Data |
---|---|---|
abbffdf9 UD |
1 | %{ |
2 | /* Expression parsing for plural form selection. | |
d614a753 | 3 | Copyright (C) 2000-2020 Free Software Foundation, Inc. |
abbffdf9 UD |
4 | Written by Ulrich Drepper <drepper@cygnus.com>, 2000. |
5 | ||
6d248857 WN |
6 | This program is free software: you can redistribute it and/or modify |
7 | it under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
abbffdf9 | 10 | |
6d248857 | 11 | This program is distributed in the hope that it will be useful, |
abbffdf9 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
6d248857 WN |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | GNU Lesser General Public License for more details. | |
abbffdf9 | 15 | |
6d248857 | 16 | You should have received a copy of the GNU Lesser General Public License |
5a82c748 | 17 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
abbffdf9 | 18 | |
6d248857 WN |
19 | /* For bison < 2.0, the bison generated parser uses alloca. AIX 3 forces us |
20 | to put this declaration at the beginning of the file. The declaration in | |
21 | bison's skeleton file comes too late. This must come before <config.h> | |
22 | because <config.h> may include arbitrary system headers. | |
23 | This can go away once the AM_INTL_SUBDIR macro requires bison >= 2.0. */ | |
0555fcce UD |
24 | #if defined _AIX && !defined __GNUC__ |
25 | #pragma alloca | |
26 | #endif | |
6d248857 | 27 | |
61402fd6 UD |
28 | #ifdef HAVE_CONFIG_H |
29 | # include <config.h> | |
30 | #endif | |
31 | ||
0555fcce | 32 | #include <stddef.h> |
abbffdf9 | 33 | #include <stdlib.h> |
85dd1003 | 34 | #include <string.h> |
0555fcce UD |
35 | #include "plural-exp.h" |
36 | ||
37 | /* The main function generated by the parser is called __gettextparse, | |
38 | but we want it to be called PLURAL_PARSE. */ | |
39 | #ifndef _LIBC | |
40 | # define __gettextparse PLURAL_PARSE | |
4a4d50f3 UD |
41 | #endif |
42 | ||
abbffdf9 | 43 | %} |
6d248857 WN |
44 | %parse-param {struct parse_args *arg} |
45 | %lex-param {struct parse_args *arg} | |
46 | %define api.pure full | |
8e57fc70 | 47 | %expect 7 |
abbffdf9 UD |
48 | |
49 | %union { | |
50 | unsigned long int num; | |
6d248857 | 51 | enum expression_operator op; |
abbffdf9 UD |
52 | struct expression *exp; |
53 | } | |
54 | ||
55 | %{ | |
56 | /* Prototypes for local functions. */ | |
6d248857 WN |
57 | static int yylex (YYSTYPE *lval, struct parse_args *arg); |
58 | static void yyerror (struct parse_args *arg, const char *str); | |
4a4d50f3 UD |
59 | |
60 | /* Allocation of expressions. */ | |
61 | ||
62 | static struct expression * | |
6d248857 WN |
63 | new_exp (int nargs, enum expression_operator op, |
64 | struct expression * const *args) | |
4a4d50f3 UD |
65 | { |
66 | int i; | |
67 | struct expression *newp; | |
68 | ||
69 | /* If any of the argument could not be malloc'ed, just return NULL. */ | |
70 | for (i = nargs - 1; i >= 0; i--) | |
71 | if (args[i] == NULL) | |
72 | goto fail; | |
73 | ||
74 | /* Allocate a new expression. */ | |
75 | newp = (struct expression *) malloc (sizeof (*newp)); | |
76 | if (newp != NULL) | |
77 | { | |
78 | newp->nargs = nargs; | |
79 | newp->operation = op; | |
80 | for (i = nargs - 1; i >= 0; i--) | |
81 | newp->val.args[i] = args[i]; | |
82 | return newp; | |
83 | } | |
84 | ||
85 | fail: | |
86 | for (i = nargs - 1; i >= 0; i--) | |
87 | FREE_EXPRESSION (args[i]); | |
88 | ||
89 | return NULL; | |
90 | } | |
91 | ||
92 | static inline struct expression * | |
6d248857 | 93 | new_exp_0 (enum expression_operator op) |
4a4d50f3 UD |
94 | { |
95 | return new_exp (0, op, NULL); | |
96 | } | |
97 | ||
98 | static inline struct expression * | |
6d248857 | 99 | new_exp_1 (enum expression_operator op, struct expression *right) |
4a4d50f3 UD |
100 | { |
101 | struct expression *args[1]; | |
102 | ||
103 | args[0] = right; | |
104 | return new_exp (1, op, args); | |
105 | } | |
106 | ||
107 | static struct expression * | |
6d248857 WN |
108 | new_exp_2 (enum expression_operator op, struct expression *left, |
109 | struct expression *right) | |
4a4d50f3 UD |
110 | { |
111 | struct expression *args[2]; | |
112 | ||
113 | args[0] = left; | |
114 | args[1] = right; | |
115 | return new_exp (2, op, args); | |
116 | } | |
117 | ||
118 | static inline struct expression * | |
6d248857 WN |
119 | new_exp_3 (enum expression_operator op, struct expression *bexp, |
120 | struct expression *tbranch, struct expression *fbranch) | |
4a4d50f3 UD |
121 | { |
122 | struct expression *args[3]; | |
123 | ||
124 | args[0] = bexp; | |
125 | args[1] = tbranch; | |
126 | args[2] = fbranch; | |
127 | return new_exp (3, op, args); | |
128 | } | |
129 | ||
abbffdf9 UD |
130 | %} |
131 | ||
4a4d50f3 UD |
132 | /* This declares that all operators have the same associativity and the |
133 | precedence order as in C. See [Harbison, Steele: C, A Reference Manual]. | |
134 | There is no unary minus and no bitwise operators. | |
135 | Operators with the same syntactic behaviour have been merged into a single | |
136 | token, to save space in the array generated by bison. */ | |
137 | %right '?' /* ? */ | |
138 | %left '|' /* || */ | |
139 | %left '&' /* && */ | |
140 | %left EQUOP2 /* == != */ | |
141 | %left CMPOP2 /* < > <= >= */ | |
142 | %left ADDOP2 /* + - */ | |
143 | %left MULOP2 /* * / % */ | |
144 | %right '!' /* ! */ | |
145 | ||
146 | %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2 | |
abbffdf9 UD |
147 | %token <num> NUMBER |
148 | %type <exp> exp | |
149 | ||
150 | %% | |
151 | ||
152 | start: exp | |
153 | { | |
4a4d50f3 UD |
154 | if ($1 == NULL) |
155 | YYABORT; | |
6d248857 | 156 | arg->res = $1; |
abbffdf9 UD |
157 | } |
158 | ; | |
159 | ||
160 | exp: exp '?' exp ':' exp | |
161 | { | |
4a4d50f3 | 162 | $$ = new_exp_3 (qmop, $1, $3, $5); |
abbffdf9 UD |
163 | } |
164 | | exp '|' exp | |
165 | { | |
4a4d50f3 | 166 | $$ = new_exp_2 (lor, $1, $3); |
abbffdf9 UD |
167 | } |
168 | | exp '&' exp | |
169 | { | |
4a4d50f3 | 170 | $$ = new_exp_2 (land, $1, $3); |
abbffdf9 | 171 | } |
4a4d50f3 | 172 | | exp EQUOP2 exp |
abbffdf9 | 173 | { |
4a4d50f3 | 174 | $$ = new_exp_2 ($2, $1, $3); |
abbffdf9 | 175 | } |
4a4d50f3 | 176 | | exp CMPOP2 exp |
abbffdf9 | 177 | { |
4a4d50f3 | 178 | $$ = new_exp_2 ($2, $1, $3); |
abbffdf9 | 179 | } |
4a4d50f3 | 180 | | exp ADDOP2 exp |
abbffdf9 | 181 | { |
4a4d50f3 | 182 | $$ = new_exp_2 ($2, $1, $3); |
abbffdf9 | 183 | } |
4a4d50f3 | 184 | | exp MULOP2 exp |
abbffdf9 | 185 | { |
4a4d50f3 | 186 | $$ = new_exp_2 ($2, $1, $3); |
abbffdf9 | 187 | } |
4a4d50f3 | 188 | | '!' exp |
abbffdf9 | 189 | { |
4a4d50f3 | 190 | $$ = new_exp_1 (lnot, $2); |
abbffdf9 UD |
191 | } |
192 | | 'n' | |
193 | { | |
4a4d50f3 | 194 | $$ = new_exp_0 (var); |
abbffdf9 UD |
195 | } |
196 | | NUMBER | |
197 | { | |
4a4d50f3 UD |
198 | if (($$ = new_exp_0 (num)) != NULL) |
199 | $$->val.num = $1; | |
abbffdf9 UD |
200 | } |
201 | | '(' exp ')' | |
202 | { | |
4a4d50f3 | 203 | $$ = $2; |
abbffdf9 UD |
204 | } |
205 | ; | |
206 | ||
207 | %% | |
208 | ||
abbffdf9 | 209 | void |
6d248857 | 210 | FREE_EXPRESSION (struct expression *exp) |
abbffdf9 UD |
211 | { |
212 | if (exp == NULL) | |
213 | return; | |
214 | ||
215 | /* Handle the recursive case. */ | |
4a4d50f3 | 216 | switch (exp->nargs) |
abbffdf9 | 217 | { |
4a4d50f3 UD |
218 | case 3: |
219 | FREE_EXPRESSION (exp->val.args[2]); | |
220 | /* FALLTHROUGH */ | |
221 | case 2: | |
222 | FREE_EXPRESSION (exp->val.args[1]); | |
223 | /* FALLTHROUGH */ | |
224 | case 1: | |
225 | FREE_EXPRESSION (exp->val.args[0]); | |
abbffdf9 | 226 | /* FALLTHROUGH */ |
abbffdf9 UD |
227 | default: |
228 | break; | |
229 | } | |
230 | ||
231 | free (exp); | |
232 | } | |
233 | ||
234 | ||
235 | static int | |
6d248857 | 236 | yylex (YYSTYPE *lval, struct parse_args *arg) |
abbffdf9 | 237 | { |
6d248857 | 238 | const char *exp = arg->cp; |
abbffdf9 UD |
239 | int result; |
240 | ||
241 | while (1) | |
242 | { | |
2f599545 UD |
243 | if (exp[0] == '\0') |
244 | { | |
6d248857 | 245 | arg->cp = exp; |
2f599545 UD |
246 | return YYEOF; |
247 | } | |
248 | ||
249 | if (exp[0] != ' ' && exp[0] != '\t') | |
abbffdf9 UD |
250 | break; |
251 | ||
252 | ++exp; | |
253 | } | |
254 | ||
255 | result = *exp++; | |
256 | switch (result) | |
257 | { | |
61402fd6 UD |
258 | case '0': case '1': case '2': case '3': case '4': |
259 | case '5': case '6': case '7': case '8': case '9': | |
abbffdf9 | 260 | { |
eda6c725 | 261 | unsigned long int n = result - '0'; |
abbffdf9 UD |
262 | while (exp[0] >= '0' && exp[0] <= '9') |
263 | { | |
264 | n *= 10; | |
265 | n += exp[0] - '0'; | |
266 | ++exp; | |
267 | } | |
268 | lval->num = n; | |
269 | result = NUMBER; | |
270 | } | |
271 | break; | |
272 | ||
273 | case '=': | |
abbffdf9 | 274 | if (exp[0] == '=') |
4a4d50f3 UD |
275 | { |
276 | ++exp; | |
277 | lval->op = equal; | |
278 | result = EQUOP2; | |
279 | } | |
abbffdf9 UD |
280 | else |
281 | result = YYERRCODE; | |
282 | break; | |
283 | ||
4a4d50f3 UD |
284 | case '!': |
285 | if (exp[0] == '=') | |
286 | { | |
287 | ++exp; | |
288 | lval->op = not_equal; | |
289 | result = EQUOP2; | |
290 | } | |
291 | break; | |
292 | ||
abbffdf9 UD |
293 | case '&': |
294 | case '|': | |
295 | if (exp[0] == result) | |
296 | ++exp; | |
297 | else | |
298 | result = YYERRCODE; | |
299 | break; | |
300 | ||
4a4d50f3 UD |
301 | case '<': |
302 | if (exp[0] == '=') | |
303 | { | |
304 | ++exp; | |
305 | lval->op = less_or_equal; | |
306 | } | |
307 | else | |
308 | lval->op = less_than; | |
309 | result = CMPOP2; | |
310 | break; | |
311 | ||
312 | case '>': | |
313 | if (exp[0] == '=') | |
314 | { | |
315 | ++exp; | |
316 | lval->op = greater_or_equal; | |
317 | } | |
318 | else | |
319 | lval->op = greater_than; | |
320 | result = CMPOP2; | |
321 | break; | |
322 | ||
abbffdf9 | 323 | case '*': |
4a4d50f3 UD |
324 | lval->op = mult; |
325 | result = MULOP2; | |
326 | break; | |
327 | ||
abbffdf9 | 328 | case '/': |
4a4d50f3 UD |
329 | lval->op = divide; |
330 | result = MULOP2; | |
331 | break; | |
332 | ||
abbffdf9 | 333 | case '%': |
4a4d50f3 UD |
334 | lval->op = module; |
335 | result = MULOP2; | |
336 | break; | |
337 | ||
abbffdf9 | 338 | case '+': |
4a4d50f3 UD |
339 | lval->op = plus; |
340 | result = ADDOP2; | |
341 | break; | |
342 | ||
abbffdf9 | 343 | case '-': |
4a4d50f3 UD |
344 | lval->op = minus; |
345 | result = ADDOP2; | |
346 | break; | |
347 | ||
348 | case 'n': | |
abbffdf9 UD |
349 | case '?': |
350 | case ':': | |
351 | case '(': | |
352 | case ')': | |
353 | /* Nothing, just return the character. */ | |
354 | break; | |
355 | ||
2f599545 | 356 | case ';': |
abbffdf9 UD |
357 | case '\n': |
358 | case '\0': | |
359 | /* Be safe and let the user call this function again. */ | |
360 | --exp; | |
361 | result = YYEOF; | |
362 | break; | |
363 | ||
364 | default: | |
365 | result = YYERRCODE; | |
366 | #if YYDEBUG != 0 | |
367 | --exp; | |
368 | #endif | |
369 | break; | |
370 | } | |
371 | ||
6d248857 | 372 | arg->cp = exp; |
abbffdf9 UD |
373 | |
374 | return result; | |
375 | } | |
376 | ||
377 | ||
378 | static void | |
6d248857 | 379 | yyerror (struct parse_args *arg, const char *str) |
abbffdf9 UD |
380 | { |
381 | /* Do nothing. We don't print error messages here. */ | |
382 | } |