expression.g 6.1 KB

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