expression.g 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. 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. [ %persistent
  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. ch7bin(expp, '?', e1);
  196. }
  197. ]?
  198. ;
  199. /* 7.14 */
  200. assignment_expression(struct expr **expp;)
  201. {
  202. int oper;
  203. struct expr *e1 = 0;
  204. }
  205. :
  206. conditional_expression(expp)
  207. [%prefer /* (rank_of(DOT) <= maxrank) for any asgnop */
  208. asgnop(&oper)
  209. assignment_expression(&e1)
  210. {ch7asgn(expp, oper, e1);}
  211. |
  212. empty /* LLgen artefact ??? */
  213. ]
  214. ;
  215. /* 7.15 */
  216. expression(struct expr **expp;)
  217. {struct expr *e1;}
  218. :
  219. assignment_expression(expp)
  220. [ ','
  221. assignment_expression(&e1)
  222. {
  223. ch7bin(expp, ',', e1);
  224. }
  225. ]*
  226. ;
  227. unop(int *oper;) :
  228. ['*' | '&' | '-' | '!' | '~' | PLUSPLUS | MINMIN]
  229. {*oper = DOT;}
  230. ;
  231. postop(int *oper;):
  232. PLUSPLUS {*oper = POSTINCR;}
  233. |
  234. MINMIN {*oper = POSTDECR;}
  235. ;
  236. multop:
  237. '*' | '/' | '%'
  238. ;
  239. addop:
  240. '+' | '-'
  241. ;
  242. shiftop:
  243. LEFT | RIGHT
  244. ;
  245. relop:
  246. '<' | '>' | LESSEQ | GREATEREQ
  247. ;
  248. eqop:
  249. EQUAL | NOTEQUAL
  250. ;
  251. arithop:
  252. multop | addop | shiftop
  253. |
  254. '&' | '^' | '|'
  255. ;
  256. binop(int *oper;) :
  257. [ arithop | relop | eqop | AND | OR ]
  258. {*oper = DOT;}
  259. ;
  260. asgnop(register int *oper;):
  261. '=' {*oper = DOT;}
  262. |
  263. '+' '=' {*oper = PLUSAB;}
  264. |
  265. '-' '=' {*oper = MINAB;}
  266. |
  267. '*' '=' {*oper = TIMESAB;}
  268. |
  269. '/' '=' {*oper = DIVAB;}
  270. |
  271. '%' '=' {*oper = MODAB;}
  272. |
  273. LEFT '=' {*oper = LEFTAB;}
  274. |
  275. RIGHT '=' {*oper = RIGHTAB;}
  276. |
  277. '&' '=' {*oper = ANDAB;}
  278. |
  279. '^' '=' {*oper = XORAB;}
  280. |
  281. '|' '=' {*oper = ORAB;}
  282. ;
  283. constant(struct expr **expp;) :
  284. [
  285. INTEGER
  286. |
  287. FLOATING
  288. ] {dot2expr(expp);}
  289. ;
  290. /* 15 */
  291. constant_expression (struct expr **expp;) :
  292. assignment_expression(expp)
  293. {chk_cst_expr(expp);}
  294. ;
  295. identifier(struct idf **idfp;) :
  296. [
  297. IDENTIFIER
  298. |
  299. TYPE_IDENTIFIER
  300. ]
  301. {
  302. *idfp = dot.tk_idf;
  303. }
  304. ;