2 /* Expression parsing for plural form selection.
3 Copyright (C) 2000-2020 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 it
7 under the terms of the GNU Library General Public License as published
8 by 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 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 this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
21 /* The bison generated parser uses alloca. AIX 3 forces us to put this
22 declaration at the beginning of the file. The declaration in bison's
23 skeleton file comes too late. This must come before <config.h>
24 because <config.h> may include arbitrary system headers. */
25 #if defined _AIX && !defined __GNUC__
35 #include "plural-exp.h"
37 /* The main function generated by the parser is called __gettextparse,
38 but we want it to be called PLURAL_PARSE. */
40 # define __gettextparse PLURAL_PARSE
44 #define YYLEX_PARAM &((struct parse_args *) arg)->cp
45 #define YYPARSE_PARAM arg
49 /* BISON3 %parse-param {struct parse_args *arg} */
50 /* BISON3 %lex-param {struct parse_args *arg} */
51 /* BISON3 %define api.pure full */
55 unsigned long int num;
57 struct expression *exp;
61 /* Prototypes for local functions. */
62 static struct expression *new_exp PARAMS ((int nargs, enum operator op,
63 struct expression * const *args));
64 static inline struct expression *new_exp_0 PARAMS ((enum operator op));
65 static inline struct expression *new_exp_1 PARAMS ((enum operator op,
66 struct expression *right));
67 static struct expression *new_exp_2 PARAMS ((enum operator op,
68 struct expression *left,
69 struct expression *right));
70 static inline struct expression *new_exp_3 PARAMS ((enum operator op,
71 struct expression *bexp,
72 struct expression *tbranch,
73 struct expression *fbranch));
75 static int yylex PARAMS ((YYSTYPE *lval, struct parse_args *arg));
76 static void yyerror PARAMS ((struct parse_args *arg, const char *str));
78 static int yylex PARAMS ((YYSTYPE *lval, const char **pexp));
79 static void yyerror PARAMS ((const char *str));
82 /* Allocation of expressions. */
84 static struct expression *
85 new_exp (nargs, op, args)
88 struct expression * const *args;
91 struct expression *newp;
93 /* If any of the argument could not be malloc'ed, just return NULL. */
94 for (i = nargs - 1; i >= 0; i--)
98 /* Allocate a new expression. */
99 newp = (struct expression *) malloc (sizeof (*newp));
103 newp->operation = op;
104 for (i = nargs - 1; i >= 0; i--)
105 newp->val.args[i] = args[i];
110 for (i = nargs - 1; i >= 0; i--)
111 FREE_EXPRESSION (args[i]);
116 static inline struct expression *
120 return new_exp (0, op, NULL);
123 static inline struct expression *
124 new_exp_1 (op, right)
126 struct expression *right;
128 struct expression *args[1];
131 return new_exp (1, op, args);
134 static struct expression *
135 new_exp_2 (op, left, right)
137 struct expression *left;
138 struct expression *right;
140 struct expression *args[2];
144 return new_exp (2, op, args);
147 static inline struct expression *
148 new_exp_3 (op, bexp, tbranch, fbranch)
150 struct expression *bexp;
151 struct expression *tbranch;
152 struct expression *fbranch;
154 struct expression *args[3];
159 return new_exp (3, op, args);
164 /* This declares that all operators have the same associativity and the
165 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
166 There is no unary minus and no bitwise operators.
167 Operators with the same syntactic behaviour have been merged into a single
168 token, to save space in the array generated by bison. */
172 %left EQUOP2 /* == != */
173 %left CMPOP2 /* < > <= >= */
174 %left ADDOP2 /* + - */
175 %left MULOP2 /* * / % */
178 %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
188 ((struct parse_args *) arg)->res = $1;
192 exp: exp '?' exp ':' exp
194 $$ = new_exp_3 (qmop, $1, $3, $5);
198 $$ = new_exp_2 (lor, $1, $3);
202 $$ = new_exp_2 (land, $1, $3);
206 $$ = new_exp_2 ($2, $1, $3);
210 $$ = new_exp_2 ($2, $1, $3);
214 $$ = new_exp_2 ($2, $1, $3);
218 $$ = new_exp_2 ($2, $1, $3);
222 $$ = new_exp_1 (lnot, $2);
226 $$ = new_exp_0 (var);
230 if (($$ = new_exp_0 (num)) != NULL)
243 FREE_EXPRESSION (exp)
244 struct expression *exp;
249 /* Handle the recursive case. */
253 FREE_EXPRESSION (exp->val.args[2]);
256 FREE_EXPRESSION (exp->val.args[1]);
259 FREE_EXPRESSION (exp->val.args[0]);
273 struct parse_args *arg;
275 const char **pexp = &arg->cp;
283 const char *exp = *pexp;
294 if (exp[0] != ' ' && exp[0] != '\t')
303 case '0': case '1': case '2': case '3': case '4':
304 case '5': case '6': case '7': case '8': case '9':
306 unsigned long int n = result - '0';
307 while (exp[0] >= '0' && exp[0] <= '9')
333 lval->op = not_equal;
340 if (exp[0] == result)
350 lval->op = less_or_equal;
353 lval->op = less_than;
361 lval->op = greater_or_equal;
364 lval->op = greater_than;
398 /* Nothing, just return the character. */
404 /* Be safe and let the user call this function again. */
426 struct parse_args *arg;
433 /* Do nothing. We don't print error messages here. */