expression.g 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /* $Header$ */
  6. /* EXPRESSION SYNTAX PARSER */
  7. {
  8. #include "lint.h"
  9. #include "arith.h"
  10. #include "LLlex.h"
  11. #include "type.h"
  12. #include "idf.h"
  13. #include "label.h"
  14. #include "expr.h"
  15. #include "code.h"
  16. #include "noRoption.h"
  17. extern char options[];
  18. extern struct expr *intexpr();
  19. }
  20. /* 7.1 */
  21. primary(register struct expr **expp;) :
  22. IDENTIFIER
  23. {dot2expr(expp);}
  24. |
  25. constant(expp)
  26. |
  27. STRING
  28. {dot2expr(expp);}
  29. |
  30. '(' expression(expp) ')'
  31. {(*expp)->ex_flags |= EX_PARENS;}
  32. ;
  33. secundary(register struct expr **expp;) :
  34. primary(expp)
  35. [
  36. index_pack(expp)
  37. |
  38. parameter_pack(expp)
  39. |
  40. selection(expp)
  41. ]*
  42. ;
  43. index_pack(struct expr **expp;)
  44. {struct expr *e1;}
  45. :
  46. '[' expression(&e1) ']'
  47. {ch7bin(expp, '[', e1);}
  48. ;
  49. parameter_pack(struct expr **expp;)
  50. {struct expr *e1 = 0;}
  51. :
  52. '(' parameter_list(&e1)? ')'
  53. {ch7bin(expp, '(', e1);}
  54. ;
  55. selection(struct expr **expp;)
  56. {int oper; struct idf *idf;}
  57. :
  58. [ '.' | ARROW ]
  59. {oper = DOT;}
  60. identifier(&idf)
  61. {ch7sel(expp, oper, idf);}
  62. ;
  63. parameter_list(struct expr **expp;)
  64. {struct expr *e1 = 0;}
  65. :
  66. assignment_expression(expp)
  67. {any2opnd(expp, PARCOMMA);}
  68. [ ','
  69. assignment_expression(&e1)
  70. {any2opnd(&e1, PARCOMMA);}
  71. {ch7bin(expp, PARCOMMA, e1);}
  72. ]*
  73. ;
  74. /* 7.2 */
  75. postfixed(struct expr **expp;)
  76. {int oper;}
  77. :
  78. secundary(expp)
  79. [
  80. postop(&oper)
  81. {ch7incr(expp, oper);}
  82. |
  83. empty
  84. ]
  85. ;
  86. %first first_of_type_specifier, type_specifier;
  87. unary(register struct expr **expp;)
  88. {struct type *tp; int oper;}
  89. :
  90. %if (first_of_type_specifier(AHEAD) && AHEAD != IDENTIFIER)
  91. cast(&tp) unary(expp)
  92. { ch7cast(expp, CAST, tp);
  93. (*expp)->ex_flags |= EX_CAST;
  94. }
  95. |
  96. postfixed(expp)
  97. |
  98. unop(&oper) unary(expp)
  99. {ch7mon(oper, expp);}
  100. |
  101. size_of(expp)
  102. ;
  103. size_of(register struct expr **expp;)
  104. {struct type *tp;}
  105. :
  106. SIZEOF
  107. [%if (first_of_type_specifier(AHEAD) && AHEAD != IDENTIFIER)
  108. cast(&tp)
  109. {
  110. *expp = intexpr(size_of_type(tp, "type"), INT);
  111. (*expp)->ex_flags |= EX_SIZEOF;
  112. }
  113. |
  114. unary(expp)
  115. {ch7mon(SIZEOF, expp);}
  116. ]
  117. ;
  118. /* 7.3-7.12 */
  119. /* The set of operators in C is stratified in 15 levels, with level
  120. N being treated in RM 7.N. In principle each operator is
  121. assigned a rank, ranging from 1 to 15. Such an expression can
  122. be parsed by a construct like:
  123. binary_expression(int maxrank;)
  124. {int oper;}
  125. :
  126. binary_expression(maxrank - 1)
  127. [%if (rank_of(DOT) <= maxrank)
  128. binop(&oper)
  129. binary_expression(rank_of(oper)-1)
  130. ]?
  131. ;
  132. except that some call of 'unary' is necessary, depending on the
  133. grammar.
  134. This simple view is marred by three complications:
  135. 1. Level 15 (comma operator) is not allowed in many
  136. contexts and is different.
  137. 2. Level 13 (conditional operator) is a ternary operator,
  138. which does not fit this scheme at all.
  139. 3. Level 14 (assignment operators) group right-to-left, as
  140. opposed to 2-12, which group left-to-right (or are
  141. immaterial).
  142. 4. The operators in level 14 start with operators in levels
  143. 2-13 (RM 7.14: The two parts of a compound assignment
  144. operator are separate tokens.) This causes LL1 problems.
  145. This forces us to have four rules:
  146. binary_expression for level 2-12
  147. conditional_expression for level 13
  148. assignment_expression for level 14 and
  149. expression for the most general expression
  150. */
  151. binary_expression(int maxrank; struct expr **expp;)
  152. {int oper; struct expr *e1;}
  153. :
  154. unary(expp)
  155. [%while (rank_of(DOT) <= maxrank && AHEAD != '=')
  156. /* '?', '=', and ',' are no binops, and the test
  157. for AHEAD != '=' keeps the other assignment
  158. operators out
  159. */
  160. binop(&oper)
  161. binary_expression(rank_of(oper)-1, &e1)
  162. {
  163. ch7bin(expp, oper, e1);
  164. }
  165. ]*
  166. ;
  167. /* 7.13 */
  168. conditional_expression(struct expr **expp;)
  169. /* There is some unfortunate disagreement about what is allowed
  170. between the '?' and the ':' of a conditional_expression.
  171. Although the Ritchie compiler does not even allow
  172. conditional_expressions there, some other compilers (e.g., VAX)
  173. accept a full assignment_expression there, and programs
  174. (like, e.g., emacs) rely on it. So we have little choice.
  175. */
  176. {struct expr *e1 = 0, *e2 = 0;}
  177. :
  178. /* allow all binary operators */
  179. binary_expression(rank_of('?') - 1, expp)
  180. [ '?'
  181. expression(&e1)
  182. {
  183. #ifndef NOROPTION
  184. check_conditional(e1, '?', "between ? and :");
  185. #endif
  186. }
  187. ':'
  188. assignment_expression(&e2)
  189. {
  190. #ifndef NOROPTION
  191. check_conditional(e2, '=', "after :");
  192. #endif
  193. ch7bin(&e1, ':', e2);
  194. opnd2test(expp, '?');
  195. #ifdef LINT
  196. if (is_cp_cst(*expp))
  197. hwarning("condition in ?: is constant");
  198. #endif LINT
  199. ch7bin(expp, '?', e1);
  200. }
  201. ]?
  202. ;
  203. /* 7.14 */
  204. assignment_expression(struct expr **expp;)
  205. {
  206. int oper;
  207. struct expr *e1 = 0;
  208. }
  209. :
  210. conditional_expression(expp)
  211. [%prefer /* (rank_of(DOT) <= maxrank) for any asgnop */
  212. asgnop(&oper)
  213. assignment_expression(&e1)
  214. {ch7asgn(expp, oper, e1);}
  215. |
  216. empty /* LLgen artefact ??? */
  217. ]
  218. ;
  219. /* 7.15 */
  220. expression(struct expr **expp;)
  221. {struct expr *e1;}
  222. :
  223. assignment_expression(expp)
  224. [ ','
  225. assignment_expression(&e1)
  226. {
  227. ch7bin(expp, ',', e1);
  228. }
  229. ]*
  230. ;
  231. unop(int *oper;) :
  232. ['*' | '&' | '-' | '!' | '~' | PLUSPLUS | MINMIN]
  233. {*oper = DOT;}
  234. ;
  235. postop(int *oper;):
  236. PLUSPLUS {*oper = POSTINCR;}
  237. |
  238. MINMIN {*oper = POSTDECR;}
  239. ;
  240. multop:
  241. '*' | '/' | '%'
  242. ;
  243. addop:
  244. '+' | '-'
  245. ;
  246. shiftop:
  247. LEFT | RIGHT
  248. ;
  249. relop:
  250. '<' | '>' | LESSEQ | GREATEREQ
  251. ;
  252. eqop:
  253. EQUAL | NOTEQUAL
  254. ;
  255. arithop:
  256. multop | addop | shiftop
  257. |
  258. '&' | '^' | '|'
  259. ;
  260. binop(int *oper;) :
  261. [ arithop | relop | eqop | AND | OR ]
  262. {*oper = DOT;}
  263. ;
  264. asgnop(register int *oper;):
  265. '=' {*oper = DOT;}
  266. |
  267. '+' '=' {*oper = PLUSAB;}
  268. |
  269. '-' '=' {*oper = MINAB;}
  270. |
  271. '*' '=' {*oper = TIMESAB;}
  272. |
  273. '/' '=' {*oper = DIVAB;}
  274. |
  275. '%' '=' {*oper = MODAB;}
  276. |
  277. LEFT '=' {*oper = LEFTAB;}
  278. |
  279. RIGHT '=' {*oper = RIGHTAB;}
  280. |
  281. '&' '=' {*oper = ANDAB;}
  282. |
  283. '^' '=' {*oper = XORAB;}
  284. |
  285. '|' '=' {*oper = ORAB;}
  286. |
  287. [ PLUSAB | MINAB | TIMESAB | DIVAB | MODAB |
  288. LEFTAB | RIGHTAB | ANDAB | XORAB | ORAB ]
  289. {
  290. char *symbol2str();
  291. warning("old-fashioned assignment operator, use %s",
  292. symbol2str(DOT));
  293. *oper = DOT;
  294. }
  295. ;
  296. constant(struct expr **expp;) :
  297. [
  298. INTEGER
  299. |
  300. FLOATING
  301. ] {dot2expr(expp);}
  302. ;
  303. /* 15 */
  304. constant_expression (struct expr **expp;) :
  305. assignment_expression(expp)
  306. {chk_cst_expr(expp);}
  307. ;
  308. identifier(struct idf **idfp;) :
  309. [
  310. IDENTIFIER
  311. |
  312. TYPE_IDENTIFIER
  313. ]
  314. {
  315. *idfp = dot.tk_idf;
  316. }
  317. ;