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
22 /* The bison generated parser uses alloca. AIX 3 forces us to put this
23 declaration at the beginning of the file. The declaration in bison's
24 skeleton file comes too late. This must come before <config.h>
25 because <config.h> may include arbitrary system headers. */
26 #if defined _AIX && !defined __GNUC__
36 #include "plural-exp.h"
38 /* The main function generated by the parser is called __gettextparse,
39 but we want it to be called PLURAL_PARSE. */
41 # define __gettextparse PLURAL_PARSE
44 #define YYLEX_PARAM &((struct parse_args *) arg)->cp
45 #define YYPARSE_PARAM arg
51 unsigned long int num;
53 struct expression *exp;
57 /* Prototypes for local functions. */
58 static struct expression *new_exp PARAMS ((int nargs, enum operator op,
59 struct expression * const *args));
60 static inline struct expression *new_exp_0 PARAMS ((enum operator op));
61 static inline struct expression *new_exp_1 PARAMS ((enum operator op,
62 struct expression *right));
63 static struct expression *new_exp_2 PARAMS ((enum operator op,
64 struct expression *left,
65 struct expression *right));
66 static inline struct expression *new_exp_3 PARAMS ((enum operator op,
67 struct expression *bexp,
68 struct expression *tbranch,
69 struct expression *fbranch));
70 static int yylex PARAMS ((YYSTYPE *lval, const char **pexp));
71 static void yyerror PARAMS ((const char *str));
73 /* Allocation of expressions. */
75 static struct expression *
76 new_exp (nargs, op, args)
79 struct expression * const *args;
82 struct expression *newp;
84 /* If any of the argument could not be malloc'ed, just return NULL. */
85 for (i = nargs - 1; i >= 0; i--)
89 /* Allocate a new expression. */
90 newp = (struct expression *) malloc (sizeof (*newp));
95 for (i = nargs - 1; i >= 0; i--)
96 newp->val.args[i] = args[i];
101 for (i = nargs - 1; i >= 0; i--)
102 FREE_EXPRESSION (args[i]);
107 static inline struct expression *
111 return new_exp (0, op, NULL);
114 static inline struct expression *
115 new_exp_1 (op, right)
117 struct expression *right;
119 struct expression *args[1];
122 return new_exp (1, op, args);
125 static struct expression *
126 new_exp_2 (op, left, right)
128 struct expression *left;
129 struct expression *right;
131 struct expression *args[2];
135 return new_exp (2, op, args);
138 static inline struct expression *
139 new_exp_3 (op, bexp, tbranch, fbranch)
141 struct expression *bexp;
142 struct expression *tbranch;
143 struct expression *fbranch;
145 struct expression *args[3];
150 return new_exp (3, op, args);
155 /* This declares that all operators have the same associativity and the
156 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
157 There is no unary minus and no bitwise operators.
158 Operators with the same syntactic behaviour have been merged into a single
159 token, to save space in the array generated by bison. */
163 %left EQUOP2 /* == != */
164 %left CMPOP2 /* < > <= >= */
165 %left ADDOP2 /* + - */
166 %left MULOP2 /* * / % */
169 %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
179 ((struct parse_args *) arg)->res = $1;
183 exp: exp '?' exp ':' exp
185 $$ = new_exp_3 (qmop, $1, $3, $5);
189 $$ = new_exp_2 (lor, $1, $3);
193 $$ = new_exp_2 (land, $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_2 ($2, $1, $3);
213 $$ = new_exp_1 (lnot, $2);
217 $$ = new_exp_0 (var);
221 if (($$ = new_exp_0 (num)) != NULL)
234 FREE_EXPRESSION (exp)
235 struct expression *exp;
240 /* Handle the recursive case. */
244 FREE_EXPRESSION (exp->val.args[2]);
247 FREE_EXPRESSION (exp->val.args[1]);
250 FREE_EXPRESSION (exp->val.args[0]);
265 const char *exp = *pexp;
276 if (exp[0] != ' ' && exp[0] != '\t')
285 case '0': case '1': case '2': case '3': case '4':
286 case '5': case '6': case '7': case '8': case '9':
288 unsigned long int n = result - '0';
289 while (exp[0] >= '0' && exp[0] <= '9')
315 lval->op = not_equal;
322 if (exp[0] == result)
332 lval->op = less_or_equal;
335 lval->op = less_than;
343 lval->op = greater_or_equal;
346 lval->op = greater_than;
380 /* Nothing, just return the character. */
386 /* Be safe and let the user call this function again. */
409 /* Do nothing. We don't print error messages here. */