Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

plural.y 7.2KB

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