plural.y 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. %{
  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.
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Library General Public License as published
  7. by the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  16. USA. */
  17. /* For bison < 2.0, the bison generated parser uses alloca. AIX 3 forces us
  18. to put this declaration at the beginning of the file. The declaration in
  19. bison's skeleton file comes too late. This must come before <config.h>
  20. because <config.h> may include arbitrary system headers.
  21. This can go away once the AM_INTL_SUBDIR macro requires bison >= 2.0. */
  22. #if defined _AIX && !defined __GNUC__
  23. #pragma alloca
  24. #endif
  25. #ifdef HAVE_CONFIG_H
  26. # include <config.h>
  27. #endif
  28. #include <stddef.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "plural-exp.h"
  32. /* The main function generated by the parser is called __gettextparse,
  33. but we want it to be called PLURAL_PARSE. */
  34. #ifndef _LIBC
  35. # define __gettextparse PLURAL_PARSE
  36. #endif
  37. #define YYLEX_PARAM &((struct parse_args *) arg)->cp
  38. #define YYPARSE_PARAM arg
  39. %}
  40. %pure_parser
  41. %expect 7
  42. %union {
  43. unsigned long int num;
  44. enum expression_operator op;
  45. struct expression *exp;
  46. }
  47. %{
  48. /* Prototypes for local functions. */
  49. static int yylex (YYSTYPE *lval, const char **pexp);
  50. static void yyerror (const char *str);
  51. /* Allocation of expressions. */
  52. static struct expression *
  53. new_exp (int nargs, enum expression_operator op,
  54. struct expression * const *args)
  55. {
  56. int i;
  57. struct expression *newp;
  58. /* If any of the argument could not be malloc'ed, just return NULL. */
  59. for (i = nargs - 1; i >= 0; i--)
  60. if (args[i] == NULL)
  61. goto fail;
  62. /* Allocate a new expression. */
  63. newp = (struct expression *) malloc (sizeof (*newp));
  64. if (newp != NULL)
  65. {
  66. newp->nargs = nargs;
  67. newp->operation = op;
  68. for (i = nargs - 1; i >= 0; i--)
  69. newp->val.args[i] = args[i];
  70. return newp;
  71. }
  72. fail:
  73. for (i = nargs - 1; i >= 0; i--)
  74. FREE_EXPRESSION (args[i]);
  75. return NULL;
  76. }
  77. static inline struct expression *
  78. new_exp_0 (enum expression_operator op)
  79. {
  80. return new_exp (0, op, NULL);
  81. }
  82. static inline struct expression *
  83. new_exp_1 (enum expression_operator op, struct expression *right)
  84. {
  85. struct expression *args[1];
  86. args[0] = right;
  87. return new_exp (1, op, args);
  88. }
  89. static struct expression *
  90. new_exp_2 (enum expression_operator op, struct expression *left,
  91. struct expression *right)
  92. {
  93. struct expression *args[2];
  94. args[0] = left;
  95. args[1] = right;
  96. return new_exp (2, op, args);
  97. }
  98. static inline struct expression *
  99. new_exp_3 (enum expression_operator op, struct expression *bexp,
  100. struct expression *tbranch, struct expression *fbranch)
  101. {
  102. struct expression *args[3];
  103. args[0] = bexp;
  104. args[1] = tbranch;
  105. args[2] = fbranch;
  106. return new_exp (3, op, args);
  107. }
  108. %}
  109. /* This declares that all operators have the same associativity and the
  110. precedence order as in C. See [Harbison, Steele: C, A Reference Manual].
  111. There is no unary minus and no bitwise operators.
  112. Operators with the same syntactic behaviour have been merged into a single
  113. token, to save space in the array generated by bison. */
  114. %right '?' /* ? */
  115. %left '|' /* || */
  116. %left '&' /* && */
  117. %left EQUOP2 /* == != */
  118. %left CMPOP2 /* < > <= >= */
  119. %left ADDOP2 /* + - */
  120. %left MULOP2 /* * / % */
  121. %right '!' /* ! */
  122. %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2
  123. %token <num> NUMBER
  124. %type <exp> exp
  125. %%
  126. start: exp
  127. {
  128. if ($1 == NULL)
  129. YYABORT;
  130. ((struct parse_args *) arg)->res = $1;
  131. }
  132. ;
  133. exp: exp '?' exp ':' exp
  134. {
  135. $$ = new_exp_3 (qmop, $1, $3, $5);
  136. }
  137. | exp '|' exp
  138. {
  139. $$ = new_exp_2 (lor, $1, $3);
  140. }
  141. | exp '&' exp
  142. {
  143. $$ = new_exp_2 (land, $1, $3);
  144. }
  145. | exp EQUOP2 exp
  146. {
  147. $$ = new_exp_2 ($2, $1, $3);
  148. }
  149. | exp CMPOP2 exp
  150. {
  151. $$ = new_exp_2 ($2, $1, $3);
  152. }
  153. | exp ADDOP2 exp
  154. {
  155. $$ = new_exp_2 ($2, $1, $3);
  156. }
  157. | exp MULOP2 exp
  158. {
  159. $$ = new_exp_2 ($2, $1, $3);
  160. }
  161. | '!' exp
  162. {
  163. $$ = new_exp_1 (lnot, $2);
  164. }
  165. | 'n'
  166. {
  167. $$ = new_exp_0 (var);
  168. }
  169. | NUMBER
  170. {
  171. if (($$ = new_exp_0 (num)) != NULL)
  172. $$->val.num = $1;
  173. }
  174. | '(' exp ')'
  175. {
  176. $$ = $2;
  177. }
  178. ;
  179. %%
  180. void
  181. internal_function
  182. FREE_EXPRESSION (struct expression *exp)
  183. {
  184. if (exp == NULL)
  185. return;
  186. /* Handle the recursive case. */
  187. switch (exp->nargs)
  188. {
  189. case 3:
  190. FREE_EXPRESSION (exp->val.args[2]);
  191. /* FALLTHROUGH */
  192. case 2:
  193. FREE_EXPRESSION (exp->val.args[1]);
  194. /* FALLTHROUGH */
  195. case 1:
  196. FREE_EXPRESSION (exp->val.args[0]);
  197. /* FALLTHROUGH */
  198. default:
  199. break;
  200. }
  201. free (exp);
  202. }
  203. static int
  204. yylex (YYSTYPE *lval, const char **pexp)
  205. {
  206. const char *exp = *pexp;
  207. int result;
  208. while (1)
  209. {
  210. if (exp[0] == '\0')
  211. {
  212. *pexp = exp;
  213. return YYEOF;
  214. }
  215. if (exp[0] != ' ' && exp[0] != '\t')
  216. break;
  217. ++exp;
  218. }
  219. result = *exp++;
  220. switch (result)
  221. {
  222. case '0': case '1': case '2': case '3': case '4':
  223. case '5': case '6': case '7': case '8': case '9':
  224. {
  225. unsigned long int n = result - '0';
  226. while (exp[0] >= '0' && exp[0] <= '9')
  227. {
  228. n *= 10;
  229. n += exp[0] - '0';
  230. ++exp;
  231. }
  232. lval->num = n;
  233. result = NUMBER;
  234. }
  235. break;
  236. case '=':
  237. if (exp[0] == '=')
  238. {
  239. ++exp;
  240. lval->op = equal;
  241. result = EQUOP2;
  242. }
  243. else
  244. result = YYERRCODE;
  245. break;
  246. case '!':
  247. if (exp[0] == '=')
  248. {
  249. ++exp;
  250. lval->op = not_equal;
  251. result = EQUOP2;
  252. }
  253. break;
  254. case '&':
  255. case '|':
  256. if (exp[0] == result)
  257. ++exp;
  258. else
  259. result = YYERRCODE;
  260. break;
  261. case '<':
  262. if (exp[0] == '=')
  263. {
  264. ++exp;
  265. lval->op = less_or_equal;
  266. }
  267. else
  268. lval->op = less_than;
  269. result = CMPOP2;
  270. break;
  271. case '>':
  272. if (exp[0] == '=')
  273. {
  274. ++exp;
  275. lval->op = greater_or_equal;
  276. }
  277. else
  278. lval->op = greater_than;
  279. result = CMPOP2;
  280. break;
  281. case '*':
  282. lval->op = mult;
  283. result = MULOP2;
  284. break;
  285. case '/':
  286. lval->op = divide;
  287. result = MULOP2;
  288. break;
  289. case '%':
  290. lval->op = module;
  291. result = MULOP2;
  292. break;
  293. case '+':
  294. lval->op = plus;
  295. result = ADDOP2;
  296. break;
  297. case '-':
  298. lval->op = minus;
  299. result = ADDOP2;
  300. break;
  301. case 'n':
  302. case '?':
  303. case ':':
  304. case '(':
  305. case ')':
  306. /* Nothing, just return the character. */
  307. break;
  308. case ';':
  309. case '\n':
  310. case '\0':
  311. /* Be safe and let the user call this function again. */
  312. --exp;
  313. result = YYEOF;
  314. break;
  315. default:
  316. result = YYERRCODE;
  317. #if YYDEBUG != 0
  318. --exp;
  319. #endif
  320. break;
  321. }
  322. *pexp = exp;
  323. return result;
  324. }
  325. static void
  326. yyerror (const char *str)
  327. {
  328. /* Do nothing. We don't print error messages here. */
  329. }