expression.g 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* EXPRESSION SYNTAX PARSER */
  7. %lexical LLlex;
  8. %start If_expr, if_expression;
  9. {
  10. #include "LLlex.h"
  11. #include <em_arith.h>
  12. extern arith ifval;
  13. }
  14. if_expression
  15. :
  16. constant_expression(&ifval)
  17. ;
  18. /* 7.1 */
  19. primary(arith *pval;)
  20. :
  21. constant(pval)
  22. |
  23. '(' expression(pval) ')'
  24. ;
  25. unary(arith *pval;)
  26. {int oper;}
  27. :
  28. unop(&oper)
  29. unary(pval)
  30. { ch7mon(oper, pval); }
  31. |
  32. primary(pval)
  33. ;
  34. binary_expression(int maxrank; arith *pval;)
  35. {int oper; arith val1;}
  36. :
  37. unary(pval)
  38. [%while (rank_of(DOT) <= maxrank)
  39. binop(&oper)
  40. binary_expression(rank_of(oper)-1, &val1)
  41. {
  42. ch7bin(pval, oper, val1);
  43. }
  44. ]*
  45. ;
  46. /* 7.13 */
  47. conditional_expression(arith *pval;)
  48. {arith val1 = 0, val2 = 0;}
  49. :
  50. /* allow all binary operators */
  51. binary_expression(rank_of('?') - 1, pval)
  52. [ '?'
  53. expression(&val1)
  54. ':'
  55. assignment_expression(&val2)
  56. { *pval = (*pval ? val1 : val2); }
  57. ]?
  58. ;
  59. /* 7.14 */
  60. assignment_expression(arith *pval;)
  61. :
  62. conditional_expression(pval)
  63. ;
  64. /* 7.15 */
  65. expression(arith *pval;)
  66. {arith val1;}
  67. :
  68. assignment_expression(pval)
  69. [ ','
  70. assignment_expression(&val1)
  71. {
  72. ch7bin(pval, ',', val1);
  73. }
  74. ]*
  75. ;
  76. unop(int *oper;) :
  77. [ '-' | '!' | '~' ]
  78. {*oper = DOT;}
  79. ;
  80. multop:
  81. '*' | '/' | '%'
  82. ;
  83. addop:
  84. '+' | '-'
  85. ;
  86. shiftop:
  87. LEFT | RIGHT
  88. ;
  89. relop:
  90. '<' | '>' | LESSEQ | GREATEREQ
  91. ;
  92. eqop:
  93. EQUAL | NOTEQUAL
  94. ;
  95. arithop:
  96. multop | addop | shiftop
  97. |
  98. '&' | '^' | '|'
  99. ;
  100. binop(int *oper;) :
  101. [ arithop | relop | eqop | AND | OR ]
  102. {*oper = DOT;}
  103. ;
  104. constant(arith *pval;) :
  105. INTEGER
  106. {*pval = dot.tk_val;}
  107. ;
  108. constant_expression (arith *pval;) :
  109. assignment_expression(pval)
  110. ;