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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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
43 #define YYLEX_PARAM &((struct parse_args *) arg)->cp
44 #define YYPARSE_PARAM arg
50 unsigned long int num;
52 struct expression *exp;
56 /* Prototypes for local functions. */
57 static struct expression *new_exp PARAMS ((int nargs, enum operator op,
58 struct expression * const *args));
59 static inline struct expression *new_exp_0 PARAMS ((enum operator op));
60 static inline struct expression *new_exp_1 PARAMS ((enum operator op,
61 struct expression *right));
62 static struct expression *new_exp_2 PARAMS ((enum operator op,
63 struct expression *left,
64 struct expression *right));
65 static inline struct expression *new_exp_3 PARAMS ((enum operator op,
66 struct expression *bexp,
67 struct expression *tbranch,
68 struct expression *fbranch));
69 static int yylex PARAMS ((YYSTYPE *lval, const char **pexp));
70 static void yyerror PARAMS ((const char *str));
72 /* Allocation of expressions. */
74 static struct expression *
75 new_exp (nargs, op, args)
78 struct expression * const *args;
81 struct expression *newp;
83 /* If any of the argument could not be malloc'ed, just return NULL. */
84 for (i = nargs - 1; i >= 0; i--)
88 /* Allocate a new expression. */
89 newp = (struct expression *) malloc (sizeof (*newp));
94 for (i = nargs - 1; i >= 0; i--)
95 newp->val.args[i] = args[i];
100 for (i = nargs - 1; i >= 0; i--)
101 FREE_EXPRESSION (args[i]);
106 static inline struct expression *
110 return new_exp (0, op, NULL);
113 static inline struct expression *
114 new_exp_1 (op, right)
116 struct expression *right;
118 struct expression *args[1];
121 return new_exp (1, op, args);
124 static struct expression *
125 new_exp_2 (op, left, right)
127 struct expression *left;
128 struct expression *right;
130 struct expression *args[2];
134 return new_exp (2, op, args);
137 static inline struct expression *
138 new_exp_3 (op, bexp, tbranch, fbranch)
140 struct expression *bexp;
141 struct expression *tbranch;
142 struct expression *fbranch;
144 struct expression *args[3];
149 return new_exp (3, op, args);
154 /* This declares that all operators have the same associativity and the
155 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
156 There is no unary minus and no bitwise operators.
157 Operators with the same syntactic behaviour have been merged into a single
158 token, to save space in the array generated by bison. */
162 %left EQUOP2 /* == != */
163 %left CMPOP2 /* < > <= >= */
164 %left ADDOP2 /* + - */
165 %left MULOP2 /* * / % */
168 %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
178 ((struct parse_args *) arg)->res = $1;
182 exp: exp '?' exp ':' exp
184 $$ = new_exp_3 (qmop, $1, $3, $5);
188 $$ = new_exp_2 (lor, $1, $3);
192 $$ = new_exp_2 (land, $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_2 ($2, $1, $3);
212 $$ = new_exp_1 (lnot, $2);
216 $$ = new_exp_0 (var);
220 if (($$ = new_exp_0 (num)) != NULL)
233 FREE_EXPRESSION (exp)
234 struct expression *exp;
239 /* Handle the recursive case. */
243 FREE_EXPRESSION (exp->val.args[2]);
246 FREE_EXPRESSION (exp->val.args[1]);
249 FREE_EXPRESSION (exp->val.args[0]);
264 const char *exp = *pexp;
275 if (exp[0] != ' ' && exp[0] != '\t')
284 case '0': case '1': case '2': case '3': case '4':
285 case '5': case '6': case '7': case '8': case '9':
287 unsigned long int n = result - '0';
288 while (exp[0] >= '0' && exp[0] <= '9')
314 lval->op = not_equal;
321 if (exp[0] == result)
331 lval->op = less_or_equal;
334 lval->op = less_than;
342 lval->op = greater_or_equal;
345 lval->op = greater_than;
379 /* Nothing, just return the character. */
385 /* Be safe and let the user call this function again. */
408 /* Do nothing. We don't print error messages here. */