expression.g 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /* EXPRESSION SYNTAX PARSER */
  7. {
  8. #include <alloc.h>
  9. #include "lint.h"
  10. #include "debug.h"
  11. #include <flt_arith.h>
  12. #include "arith.h"
  13. #include "LLlex.h"
  14. #include "type.h"
  15. #include "idf.h"
  16. #include "decspecs.h"
  17. #include "declar.h"
  18. #include "declarator.h"
  19. #include "label.h"
  20. #include "expr.h"
  21. #include "expr_loc.h"
  22. #include "proto_loc.h"
  23. #include "code.h"
  24. #include "sizes.h"
  25. #include "ch3.h"
  26. #include "ch3bin.h"
  27. #include "ch3mon.h"
  28. #include "error.h"
  29. int InSizeof = 0; /* inside a sizeof- expression */
  30. int ResultKnown = 0; /* result of the expression is already known */
  31. /* Since the grammar in the standard is not LL(n), it is modified so that
  32. * it accepts basically the same grammar. This means that there is no 1-1
  33. * mapping from the grammar in the standard to the grammar given here.
  34. * Such is life.
  35. */
  36. }
  37. /* 3.3.1 */
  38. primary(struct expr **expp;) :
  39. IDENTIFIER
  40. {dot2expr(expp);}
  41. |
  42. constant(expp)
  43. |
  44. string(expp)
  45. |
  46. '(' expression(expp) ')'
  47. { (*expp)->ex_flags |= EX_PARENS; }
  48. ;
  49. /* Character string literals that are adjacent tokens
  50. * are concatenated into a single character string
  51. * literal.
  52. */
  53. string(struct expr **expp;)
  54. { int i, len;
  55. char *str;
  56. int fund;
  57. }
  58. :
  59. STRING
  60. { str = dot.tk_bts;
  61. len = dot.tk_len;
  62. fund = dot.tk_fund;
  63. }
  64. [
  65. STRING
  66. { /* A pasted string keeps the type of the first
  67. * string literal.
  68. * The pasting of normal strings and wide
  69. * character strings are stated as having an
  70. * undefined behaviour.
  71. */
  72. if (dot.tk_fund != fund)
  73. warning("illegal pasting of string literals");
  74. str = Realloc(str, (unsigned) (--len + dot.tk_len));
  75. for (i = 0; i < dot.tk_len; i++)
  76. str[len++] = dot.tk_bts[i];
  77. }
  78. ]*
  79. { string2expr(expp, str, len); }
  80. ;
  81. /* 3.3.2 */
  82. postfix_expression(struct expr **expp;)
  83. { int oper;
  84. struct expr *e1 = 0;
  85. struct idf *idf;
  86. }
  87. :
  88. primary(expp)
  89. [
  90. '[' expression(&e1) ']'
  91. { ch3bin(expp, '[', e1); e1 = 0; }
  92. |
  93. '(' parameter_list(&e1)? ')'
  94. { ch3bin(expp, '(', e1); call_proto(expp); e1 = 0; }
  95. |
  96. [ '.' | ARROW ] { oper = DOT; }
  97. identifier(&idf) { ch3sel(expp, oper, idf); }
  98. |
  99. [
  100. PLUSPLUS { oper = POSTINCR; }
  101. |
  102. MINMIN { oper = POSTDECR; }
  103. ]
  104. { ch3incr(expp, oper); }
  105. ]*
  106. ;
  107. parameter_list(struct expr **expp;)
  108. {struct expr *e1 = 0;}
  109. :
  110. assignment_expression(expp)
  111. {any2opnd(expp, PARCOMMA);}
  112. [ %persistent
  113. ','
  114. assignment_expression(&e1)
  115. {any2opnd(&e1, PARCOMMA);}
  116. {ch3bin(expp, PARCOMMA, e1);}
  117. ]*
  118. ;
  119. %first first_of_type_specifier, type_specifier;
  120. /* 3.3.3 & 3.3.4 */
  121. unary(struct expr **expp;)
  122. {struct type *tp; int oper;}
  123. :
  124. %if (first_of_type_specifier(AHEAD) && AHEAD != IDENTIFIER)
  125. cast(&tp) unary(expp)
  126. { ch3cast(expp, CAST, tp);
  127. (*expp)->ex_flags |= EX_CAST;
  128. if (int_size != pointer_size)
  129. (*expp)->ex_flags &= ~EX_PTRDIFF;
  130. }
  131. |
  132. postfix_expression(expp)
  133. |
  134. unop(&oper) unary(expp)
  135. {ch3mon(oper, expp);}
  136. |
  137. size_of(expp)
  138. ;
  139. /* When an identifier is used in a sizeof()-expression, we must stil not
  140. * mark it as used.
  141. * extern int i; .... sizeof(i) .... need not have a definition for i
  142. */
  143. size_of(struct expr **expp;)
  144. {struct type *tp;}
  145. :
  146. SIZEOF { InSizeof++; } /* handle (sizeof(sizeof(int))) too */
  147. [%if (first_of_type_specifier(AHEAD) && AHEAD != IDENTIFIER)
  148. cast(&tp)
  149. {
  150. *expp = intexpr(size_of_type(tp, "type"), UNSIGNED);
  151. (*expp)->ex_flags |= EX_SIZEOF;
  152. }
  153. |
  154. unary(expp)
  155. {ch3mon(SIZEOF, expp);}
  156. ]
  157. { InSizeof--; }
  158. ;
  159. /* 3.3.5-3.3.17 */
  160. /* The set of operators in C is stratified in 15 levels, with level
  161. N being treated in RM 7.N (although this is not the standard
  162. anymore). The standard describes this in phrase-structure-grammar,
  163. which we are unable to parse. The description that follows comes
  164. from the old C-compiler.
  165. In principle each operator is assigned a rank, ranging
  166. from 1 to 15. Such an expression can be parsed by a construct
  167. like:
  168. binary_expression(int maxrank;)
  169. {int oper;}
  170. :
  171. binary_expression(maxrank - 1)
  172. [%if (rank_of(DOT) <= maxrank)
  173. binop(&oper)
  174. binary_expression(rank_of(oper)-1)
  175. ]?
  176. ;
  177. except that some call of 'unary' is necessary, depending on the
  178. grammar.
  179. This simple view is marred by three complications:
  180. 1. Level 15 (comma operator) is not allowed in many
  181. contexts and is different.
  182. 2. Level 13 (conditional operator) is a ternary operator,
  183. which does not fit this scheme at all.
  184. 3. Level 14 (assignment operators) group right-to-left, as
  185. opposed to 2-12, which group left-to-right (or are
  186. immaterial).
  187. 4. The operators in level 14 start with operators in levels
  188. 2-13 (RM 7.14: The two parts of a compound assignment
  189. operator are separate tokens.) This causes LL1 problems.
  190. This forces us to have four rules:
  191. binary_expression for level 2-12
  192. conditional_expression for level 13
  193. assignment_expression for level 14 and
  194. expression for the most general expression
  195. */
  196. binary_expression(int maxrank; struct expr **expp;)
  197. {int oper, OldResultKnown; struct expr *e1;}
  198. :
  199. unary(expp)
  200. [%while (rank_of(DOT) <= maxrank )
  201. /* '?', '=', and ',' are no binops
  202. */
  203. binop(&oper)
  204. { OldResultKnown = ResultKnown;
  205. if (oper == OR || oper == AND) {
  206. if (is_cp_cst(*expp) || is_fp_cst(*expp)) {
  207. if (is_zero_cst(*expp)) {
  208. if (oper == AND) ResultKnown++;
  209. } else if (oper == OR) ResultKnown++;
  210. }
  211. }
  212. }
  213. binary_expression(rank_of(oper)-1, &e1)
  214. {
  215. ch3bin(expp, oper, e1);
  216. ResultKnown = OldResultKnown;
  217. }
  218. ]*
  219. ;
  220. /* 3.3.15 */
  221. conditional_expression(struct expr **expp;)
  222. {struct expr *e1 = 0, *e2 = 0; int OldResultKnown, ConstExpr=0;}
  223. :
  224. /* allow all binary operators */
  225. binary_expression(rank_of('?') - 1, expp)
  226. [ '?'
  227. { OldResultKnown = ResultKnown;
  228. if (is_cp_cst(*expp) || is_fp_cst(*expp)) {
  229. ConstExpr++;
  230. if (is_zero_cst(*expp)) ResultKnown++;
  231. }
  232. }
  233. expression(&e1)
  234. ':'
  235. { if (ConstExpr) {
  236. if (OldResultKnown == ResultKnown) ResultKnown++;
  237. else ResultKnown = OldResultKnown;
  238. }
  239. }
  240. conditional_expression(&e2)
  241. {
  242. ResultKnown = OldResultKnown;
  243. ch3bin(&e1, ':', e2);
  244. opnd2test(expp, '?');
  245. ch3bin(expp, '?', e1);
  246. }
  247. ]?
  248. ;
  249. /* 3.3.16 */
  250. assignment_expression(struct expr **expp;)
  251. { int oper;
  252. struct expr *e1 = 0;
  253. }
  254. :
  255. conditional_expression(expp)
  256. [
  257. asgnop(&oper)
  258. assignment_expression(&e1)
  259. {ch3asgn(expp, oper, e1);}
  260. |
  261. empty /* LLgen artefact ??? */
  262. ]
  263. ;
  264. /* 3.3.17 */
  265. expression(struct expr **expp;)
  266. {struct expr *e1;}
  267. :
  268. assignment_expression(expp)
  269. [ ','
  270. assignment_expression(&e1)
  271. {
  272. ch3bin(expp, ',', e1);
  273. }
  274. ]*
  275. ;
  276. unop(int *oper;) :
  277. ['*' | '&' | '-' | '+' | '!' | '~' | PLUSPLUS | MINMIN]
  278. { if (DOT == '&') DOT = ADDRESSOF;
  279. *oper = DOT;
  280. }
  281. ;
  282. multop:
  283. '*' | '/' | '%'
  284. ;
  285. addop:
  286. '+' | '-'
  287. ;
  288. shiftop:
  289. LEFT | RIGHT
  290. ;
  291. relop:
  292. '<' | '>' | LESSEQ | GREATEREQ
  293. ;
  294. eqop:
  295. EQUAL | NOTEQUAL
  296. ;
  297. arithop:
  298. multop | addop | shiftop
  299. |
  300. '&' | '^' | '|'
  301. ;
  302. binop(int *oper;) :
  303. [ arithop | relop | eqop | AND | OR ]
  304. {*oper = DOT;}
  305. ;
  306. asgnop(int *oper;):
  307. [ '=' | PLUSAB | MINAB | TIMESAB | DIVAB | MODAB
  308. | LEFTAB | RIGHTAB | ANDAB | XORAB | ORAB ]
  309. { *oper = DOT; }
  310. ;
  311. constant(struct expr **expp;) :
  312. [
  313. INTEGER
  314. |
  315. FLOATING
  316. ] {dot2expr(expp);}
  317. ;
  318. /* 3.4 */
  319. constant_expression (struct expr **expp;) :
  320. conditional_expression(expp)
  321. { chk_cst_expr(expp); }
  322. ;
  323. identifier(struct idf **idfp;) :
  324. [ IDENTIFIER
  325. | TYPE_IDENTIFIER
  326. ]
  327. { *idfp = dot.tk_idf; }
  328. ;