expression.g 6.0 KB

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