expression.g 6.4 KB

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