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
7 it under the terms of the GNU General Public License as published by
8 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* The bison generated parser uses alloca. AIX 3 forces us to put this
21 declaration at the beginning of the file. The declaration in bison's
22 skeleton file comes too late. This must come before <config.h>
23 because <config.h> may include arbitrary system headers. */
24 #if defined _AIX && !defined __GNUC__
33 #include "intl/gettext/gettextP.h"
35 #define YYLEX_PARAM &((struct parse_args *) arg)->cp
36 #define YYPARSE_PARAM arg
42 unsigned long int num
;
44 struct expression
*exp
;
48 /* Prototypes for local functions. */
49 static struct expression
*new_exp
(int nargs
, enum operator op
,
50 struct expression
* const *args
);
51 static inline
struct expression
*new_exp_0
(enum operator op
);
52 static inline
struct expression
*new_exp_1
(enum operator op
,
53 struct expression
*right
);
54 static struct expression
*new_exp_2
(enum operator op
,
55 struct expression
*left
,
56 struct expression
*right
);
57 static inline
struct expression
*new_exp_3
(enum operator op
,
58 struct expression
*bexp
,
59 struct expression
*tbranch
,
60 struct expression
*fbranch
);
61 static int yylex(YYSTYPE *lval
, const unsigned char **pexp
);
62 static void yyerror(const unsigned char *str
);
64 /* Allocation of expressions. */
66 static struct expression
*
67 new_exp
(int nargs
, enum operator op
, struct expression
* const *args
)
70 struct expression
*newp
;
72 /* If any of the argument could not be malloc'ed, just return NULL. */
73 for
(i
= nargs
- 1; i
>= 0; i
--)
77 /* Allocate a new expression. */
78 newp
= (struct expression
*) malloc
(sizeof
(*newp
));
83 for
(i
= nargs
- 1; i
>= 0; i
--)
84 newp
->val.args
[i
] = args
[i
];
89 for
(i
= nargs
- 1; i
>= 0; i
--)
90 gettext_free_exp__
(args
[i
]);
95 static inline
struct expression
*
96 new_exp_0
(enum operator op
)
98 return new_exp
(0, op
, NULL
);
101 static inline
struct expression
*
102 new_exp_1
(enum operator op
, struct expression
*right
)
104 struct expression
*args
[1];
107 return new_exp
(1, op
, args
);
110 static struct expression
*
111 new_exp_2
(enum operator op
, struct expression
*left
, 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 operator op
, struct expression
*bexp
, struct expression
*tbranch
,
122 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
)
212 gettext_free_exp__
(struct expression
*exp
)
217 /* Handle the recursive case. */
221 gettext_free_exp__
(exp
->val.args
[2]);
224 gettext_free_exp__
(exp
->val.args
[1]);
227 gettext_free_exp__
(exp
->val.args
[0]);
238 yylex(YYSTYPE *lval
, const unsigned char **pexp
)
240 const unsigned char *exp
= *pexp
;
251 if
(exp
[0] != ' ' && exp
[0] != '\t')
260 case
'0': case
'1': case
'2': case
'3': case
'4':
261 case
'5': case
'6': case
'7': case
'8': case
'9':
263 unsigned long int n
= result
- '0';
264 while
(exp
[0] >= '0' && exp
[0] <= '9')
290 lval
->op
= not_equal
;
297 if
(exp
[0] == result
)
307 lval
->op
= less_or_equal
;
310 lval
->op
= less_than
;
318 lval
->op
= greater_or_equal
;
321 lval
->op
= greater_than
;
355 /* Nothing, just return the character. */
361 /* Be safe and let the user call this function again. */
381 yyerror(const unsigned char *str
)
383 /* Do nothing. We don't print error messages here. */