2 /* Expression parsing for plural form selection.
3 Copyright (C) 2000-2024 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
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
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
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 /* For bison < 2.0, the bison generated parser uses alloca. AIX 3 forces us
20 to put this declaration at the beginning of the file. The declaration in
21 bison's skeleton file comes too late. This must come before <config.h>
22 because <config.h> may include arbitrary system headers.
23 This can go away once the AM_INTL_SUBDIR macro requires bison >= 2.0. */
24 #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 /* Later we provide those prototypes. Without these macros, bison may
44 generate its own prototypes with possible conflicts. */
45 #define YYLEX_IS_DECLARED
46 #define YYERROR_IS_DECLARED
49 %parse
-param
{struct parse_args
*arg
}
50 %lex
-param
{struct parse_args
*arg
}
55 unsigned long int num
;
56 enum expression_operator op
;
57 struct expression
*exp
;
61 /* Prototypes for local functions. */
62 static int yylex (YYSTYPE *lval
, struct parse_args
*arg
);
63 static void yyerror (struct parse_args
*arg
, const char *str
);
65 /* Allocation of expressions. */
67 static struct expression
*
68 new_exp
(int nargs
, enum expression_operator op
,
69 struct expression
* const *args
)
72 struct expression
*newp
;
74 /* If any of the argument could not be malloc'ed, just return NULL. */
75 for
(i
= nargs
- 1; i
>= 0; i
--)
79 /* Allocate a new expression. */
80 newp
= (struct expression
*) malloc
(sizeof
(*newp
));
85 for
(i
= nargs
- 1; i
>= 0; i
--)
86 newp
->val.args
[i
] = args
[i
];
91 for
(i
= nargs
- 1; i
>= 0; i
--)
92 FREE_EXPRESSION
(args
[i
]);
97 static inline
struct expression
*
98 new_exp_0
(enum expression_operator op
)
100 return new_exp
(0, op
, NULL
);
103 static inline
struct expression
*
104 new_exp_1
(enum expression_operator op
, struct expression
*right
)
106 struct expression
*args
[1];
109 return new_exp
(1, op
, args
);
112 static struct expression
*
113 new_exp_2
(enum expression_operator op
, struct expression
*left
,
114 struct expression
*right
)
116 struct expression
*args
[2];
120 return new_exp
(2, op
, args
);
123 static inline
struct expression
*
124 new_exp_3
(enum expression_operator op
, struct expression
*bexp
,
125 struct expression
*tbranch
, struct expression
*fbranch
)
127 struct expression
*args
[3];
132 return new_exp
(3, op
, args
);
137 /* This declares that all operators have the same associativity and the
138 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
139 There is no unary minus and no bitwise operators.
140 Operators with the same syntactic behaviour have been merged into a single
141 token, to save space in the array generated by bison. */
145 %left EQUOP2
/* == != */
146 %left CMPOP2
/* < > <= >= */
147 %left ADDOP2
/* + - */
148 %left MULOP2
/* * / % */
151 %token
<op
> EQUOP2 CMPOP2 ADDOP2 MULOP2
165 exp: exp
'?' exp
':' exp
167 $$
= new_exp_3
(qmop
, $1, $3, $5);
171 $$
= new_exp_2
(lor
, $1, $3);
175 $$
= new_exp_2
(land
, $1, $3);
179 $$
= new_exp_2
($2, $1, $3);
183 $$
= new_exp_2
($2, $1, $3);
187 $$
= new_exp_2
($2, $1, $3);
191 $$
= new_exp_2
($2, $1, $3);
195 $$
= new_exp_1
(lnot
, $2);
199 $$
= new_exp_0
(var
);
203 if
(($$
= new_exp_0
(num
)) != NULL
)
215 FREE_EXPRESSION
(struct expression
*exp
)
220 /* Handle the recursive case. */
224 FREE_EXPRESSION
(exp
->val.args
[2]);
227 FREE_EXPRESSION
(exp
->val.args
[1]);
230 FREE_EXPRESSION
(exp
->val.args
[0]);
241 yylex (YYSTYPE *lval
, struct parse_args
*arg
)
243 const char *exp
= arg
->cp
;
254 if
(exp
[0] != ' ' && exp
[0] != '\t')
263 case
'0': case
'1': case
'2': case
'3': case
'4':
264 case
'5': case
'6': case
'7': case
'8': case
'9':
266 unsigned long int n
= result
- '0';
267 while
(exp
[0] >= '0' && exp
[0] <= '9')
293 lval
->op
= not_equal
;
300 if
(exp
[0] == result
)
310 lval
->op
= less_or_equal
;
313 lval
->op
= less_than
;
321 lval
->op
= greater_or_equal
;
324 lval
->op
= greater_than
;
358 /* Nothing, just return the character. */
364 /* Be safe and let the user call this function again. */
384 yyerror (struct parse_args
*arg
, const char *str
)
386 /* Do nothing. We don't print error messages here. */