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"
37 %parse
-param
{struct parse_args
*arg
}
38 %lex
-param
{struct parse_args
*arg
}
43 unsigned long int num
;
45 struct expression
*exp
;
49 /* Prototypes for local functions. */
50 static struct expression
*new_exp
(int nargs
, enum operator op
,
51 struct expression
* const *args
);
52 static inline
struct expression
*new_exp_0
(enum operator op
);
53 static inline
struct expression
*new_exp_1
(enum operator op
,
54 struct expression
*right
);
55 static struct expression
*new_exp_2
(enum operator op
,
56 struct expression
*left
,
57 struct expression
*right
);
58 static inline
struct expression
*new_exp_3
(enum operator op
,
59 struct expression
*bexp
,
60 struct expression
*tbranch
,
61 struct expression
*fbranch
);
62 static int yylex(YYSTYPE *lval
, struct parse_args
*arg
);
63 static void yyerror(struct parse_args
*arg
, const unsigned char *str
);
65 /* Allocation of expressions. */
67 static struct expression
*
68 new_exp
(int nargs
, enum operator op
, struct expression
* const *args
)
71 struct expression
*newp
;
73 /* If any of the argument could not be malloc'ed, just return NULL. */
74 for
(i
= nargs
- 1; i
>= 0; i
--)
78 /* Allocate a new expression. */
79 newp
= (struct expression
*) malloc
(sizeof
(*newp
));
84 for
(i
= nargs
- 1; i
>= 0; i
--)
85 newp
->val.args
[i
] = args
[i
];
90 for
(i
= nargs
- 1; i
>= 0; i
--)
91 gettext_free_exp__
(args
[i
]);
96 static inline
struct expression
*
97 new_exp_0
(enum operator op
)
99 return new_exp
(0, op
, NULL
);
102 static inline
struct expression
*
103 new_exp_1
(enum operator op
, struct expression
*right
)
105 struct expression
*args
[1];
108 return new_exp
(1, op
, args
);
111 static struct expression
*
112 new_exp_2
(enum operator op
, struct expression
*left
, struct expression
*right
)
114 struct expression
*args
[2];
118 return new_exp
(2, op
, args
);
121 static inline
struct expression
*
122 new_exp_3
(enum operator op
, struct expression
*bexp
, struct expression
*tbranch
,
123 struct expression
*fbranch
)
125 struct expression
*args
[3];
130 return new_exp
(3, op
, args
);
135 /* This declares that all operators have the same associativity and the
136 precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
137 There is no unary minus and no bitwise operators.
138 Operators with the same syntactic behaviour have been merged into a single
139 token, to save space in the array generated by bison. */
143 %left EQUOP2
/* == != */
144 %left CMPOP2
/* < > <= >= */
145 %left ADDOP2
/* + - */
146 %left MULOP2
/* * / % */
149 %token
<op
> EQUOP2 CMPOP2 ADDOP2 MULOP2
159 ((struct parse_args
*) arg
)->res
= $1;
163 exp: exp
'?' exp
':' exp
165 $$
= new_exp_3
(qmop
, $1, $3, $5);
169 $$
= new_exp_2
(lor
, $1, $3);
173 $$
= new_exp_2
(land
, $1, $3);
177 $$
= new_exp_2
($2, $1, $3);
181 $$
= new_exp_2
($2, $1, $3);
185 $$
= new_exp_2
($2, $1, $3);
189 $$
= new_exp_2
($2, $1, $3);
193 $$
= new_exp_1
(lnot
, $2);
197 $$
= new_exp_0
(var
);
201 if
(($$
= new_exp_0
(num
)) != NULL
)
213 gettext_free_exp__
(struct expression
*exp
)
218 /* Handle the recursive case. */
222 gettext_free_exp__
(exp
->val.args
[2]);
225 gettext_free_exp__
(exp
->val.args
[1]);
228 gettext_free_exp__
(exp
->val.args
[0]);
239 yylex(YYSTYPE *lval
, struct parse_args
*arg
)
241 const unsigned char *exp
= arg
->cp
;
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(struct parse_args
*arg
, const unsigned char *str
)
384 /* Do nothing. We don't print error messages here. */