expression.g 6.4 KB

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