expression.g 7.2 KB

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