2 /* plural.y - Expression parsing for plural form selection. */
4 /* Copyright (C) 2000, 2001, 2005-2009 Free Software Foundation, Inc.
5 Written by Ulrich Drepper <drepper@cygnus.com>, 2000.
7 This file is part of GNU Bash.
9 Bash is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Bash is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 /* The bison generated parser uses alloca. AIX 3 forces us to put this
24 declaration at the beginning of the file. The declaration in bison's
25 skeleton file comes too late. This must come before <config.h>
26 because <config.h> may include arbitrary system headers. */
27 #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;
54 struct expression *exp;
58 /* Prototypes for local functions. */
59 static struct expression *new_exp PARAMS ((int nargs, enum operator op,
60 struct expression * const *args));
61 static inline struct expression *new_exp_0 PARAMS ((enum operator op));
62 static inline struct expression *new_exp_1 PARAMS ((enum operator op,
63 struct expression *right));
64 static struct expression *new_exp_2 PARAMS ((enum operator op,
65 struct expression *left,
66 struct expression *right));
67 static inline struct expression *new_exp_3 PARAMS ((enum operator op,
68 struct expression *bexp,
69 struct expression *tbranch,
70 struct expression *fbranch));
71 static int yylex PARAMS ((YYSTYPE *lval, const char **pexp));
72 static void yyerror PARAMS ((const char *str));
74 /* Allocation of expressions. */
76 static struct expression *
77 new_exp (nargs, op, args)
80 struct expression * const *args;
83 struct expression *newp;
85 /* If any of the argument could not be malloc'ed, just return NULL. */
86 for (i = nargs - 1; i >= 0; i--)
90 /* Allocate a new expression. */
91 newp = (struct expression *) malloc (sizeof (*newp));
96 for (i = nargs - 1; i >= 0; i--)
97 newp->val.args[i] = args[i];
102 for (i = nargs - 1; i >= 0; i--)
103 FREE_EXPRESSION (args[i]);
108 static inline struct expression *
112 return new_exp (0, op, NULL);
115 static inline struct expression *
116 new_exp_1 (op, right)
118 struct expression *right;
120 struct expression *args[1];
123 return new_exp (1, op, args);
126 static struct expression *
127 new_exp_2 (op, left, right)
129 struct expression *left;
130 struct expression *right;
132 struct expression *args[2];
136 return new_exp (2, op, args);
139 static inline struct expression *
140 new_exp_3 (op, bexp, tbranch, fbranch)
142 struct expression *bexp;
143 struct expression *tbranch;
144 struct expression *fbranch;
146 struct expression *args[3];
151 return new_exp (3, op, args);
156 /* This declares that all operators have the same associativity and the
157 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
158 There is no unary minus and no bitwise operators.
159 Operators with the same syntactic behaviour have been merged into a single
160 token, to save space in the array generated by bison. */
164 %left EQUOP2 /* == != */
165 %left CMPOP2 /* < > <= >= */
166 %left ADDOP2 /* + - */
167 %left MULOP2 /* * / % */
170 %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
180 ((struct parse_args *) arg)->res = $1;
184 exp: exp '?' exp ':' exp
186 $$ = new_exp_3 (qmop, $1, $3, $5);
190 $$ = new_exp_2 (lor, $1, $3);
194 $$ = new_exp_2 (land, $1, $3);
198 $$ = new_exp_2 ($2, $1, $3);
202 $$ = new_exp_2 ($2, $1, $3);
206 $$ = new_exp_2 ($2, $1, $3);
210 $$ = new_exp_2 ($2, $1, $3);
214 $$ = new_exp_1 (lnot, $2);
218 $$ = new_exp_0 (var);
222 if (($$ = new_exp_0 (num)) != NULL)
235 FREE_EXPRESSION (exp)
236 struct expression *exp;
241 /* Handle the recursive case. */
245 FREE_EXPRESSION (exp->val.args[2]);
248 FREE_EXPRESSION (exp->val.args[1]);
251 FREE_EXPRESSION (exp->val.args[0]);
266 const char *exp = *pexp;
277 if (exp[0] != ' ' && exp[0] != '\t')
286 case '0': case '1': case '2': case '3': case '4':
287 case '5': case '6': case '7': case '8': case '9':
289 unsigned long int n = result - '0';
290 while (exp[0] >= '0' && exp[0] <= '9')
316 lval->op = not_equal;
323 if (exp[0] == result)
333 lval->op = less_or_equal;
336 lval->op = less_than;
344 lval->op = greater_or_equal;
347 lval->op = greater_than;
381 /* Nothing, just return the character. */
387 /* Be safe and let the user call this function again. */
410 /* Do nothing. We don't print error messages here. */