expression.g 7.3 KB

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