2 /* Expression parsing for plural form selection.
3 Copyright (C) 2000-2001, 2003, 2005-2006 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 /* For bison < 2.0, the bison generated parser uses alloca. AIX 3 forces us
22 to put this declaration at the beginning of the file. The declaration in
23 bison's skeleton file comes too late. This must come before <config.h>
24 because <config.h> may include arbitrary system headers.
25 This can go away once the AM_INTL_SUBDIR macro requires bison >= 2.0. */
26 #if defined _AIX && !defined __GNUC__
37 #include "plural-exp.h"
39 /* The main function generated by the parser is called __gettextparse,
40 but we want it to be called PLURAL_PARSE. */
42 # define __gettextparse PLURAL_PARSE
45 #define YYLEX_PARAM &((struct parse_args *) arg)->cp
46 #define YYPARSE_PARAM arg
52 unsigned long int num;
53 enum expression_operator op;
54 struct expression *exp;
58 /* Prototypes for local functions. */
59 static int yylex (YYSTYPE *lval, const char **pexp);
60 static void yyerror (const char *str);
62 /* Allocation of expressions. */
64 static struct expression *
65 new_exp (int nargs, enum expression_operator op,
66 struct expression * const *args)
69 struct expression *newp;
71 /* If any of the argument could not be malloc'ed, just return NULL. */
72 for (i = nargs - 1; i >= 0; i--)
76 /* Allocate a new expression. */
77 newp = (struct expression *) malloc (sizeof (*newp));
82 for (i = nargs - 1; i >= 0; i--)
83 newp->val.args[i] = args[i];
88 for (i = nargs - 1; i >= 0; i--)
89 FREE_EXPRESSION (args[i]);
94 static inline struct expression *
95 new_exp_0 (enum expression_operator op)
97 return new_exp (0, op, NULL);
100 static inline struct expression *
101 new_exp_1 (enum expression_operator op, struct expression *right)
103 struct expression *args[1];
106 return new_exp (1, op, args);
109 static struct expression *
110 new_exp_2 (enum expression_operator op, struct expression *left,
111 struct expression *right)
113 struct expression *args[2];
117 return new_exp (2, op, args);
120 static inline struct expression *
121 new_exp_3 (enum expression_operator op, struct expression *bexp,
122 struct expression *tbranch, struct expression *fbranch)
124 struct expression *args[3];
129 return new_exp (3, op, args);
134 /* This declares that all operators have the same associativity and the
135 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
136 There is no unary minus and no bitwise operators.
137 Operators with the same syntactic behaviour have been merged into a single
138 token, to save space in the array generated by bison. */
142 %left EQUOP2 /* == != */
143 %left CMPOP2 /* < > <= >= */
144 %left ADDOP2 /* + - */
145 %left MULOP2 /* * / % */
148 %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
158 ((struct parse_args *) arg)->res = $1;
162 exp: exp '?' exp ':' exp
164 $$ = new_exp_3 (qmop, $1, $3, $5);
168 $$ = new_exp_2 (lor, $1, $3);
172 $$ = new_exp_2 (land, $1, $3);
176 $$ = new_exp_2 ($2, $1, $3);
180 $$ = new_exp_2 ($2, $1, $3);
184 $$ = new_exp_2 ($2, $1, $3);
188 $$ = new_exp_2 ($2, $1, $3);
192 $$ = new_exp_1 (lnot, $2);
196 $$ = new_exp_0 (var);
200 if (($$ = new_exp_0 (num)) != NULL)
213 FREE_EXPRESSION (struct expression *exp)
218 /* Handle the recursive case. */
222 FREE_EXPRESSION (exp->val.args[2]);
225 FREE_EXPRESSION (exp->val.args[1]);
228 FREE_EXPRESSION (exp->val.args[0]);
239 yylex (YYSTYPE *lval, const char **pexp)
241 const char *exp = *pexp;
252 if (exp[0] != ' ' && exp[0] != '\t')
261 case '0': case '1': case '2': case '3': case '4':
262 case '5': case '6': case '7': case '8': case '9':
264 unsigned long int n = result - '0';
265 while (exp[0] >= '0' && exp[0] <= '9')
291 lval->op = not_equal;
298 if (exp[0] == result)
308 lval->op = less_or_equal;
311 lval->op = less_than;
319 lval->op = greater_or_equal;
322 lval->op = greater_than;
356 /* Nothing, just return the character. */
362 /* Be safe and let the user call this function again. */
382 yyerror (const char *str)
384 /* Do nothing. We don't print error messages here. */