expression.g 6.2 KB

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