expression.g 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. void ch7bin(arith *pval, int oper, arith val);
  13. void ch7mon(int oper, arith *pval);
  14. extern arith ifval;
  15. }
  16. if_expression
  17. :
  18. constant_expression(&ifval)
  19. ;
  20. /* 7.1 */
  21. primary(arith *pval;)
  22. :
  23. constant(pval)
  24. |
  25. '(' expression(pval) ')'
  26. ;
  27. unary(arith *pval;)
  28. {int oper;}
  29. :
  30. unop(&oper)
  31. unary(pval)
  32. { ch7mon(oper, pval); }
  33. |
  34. primary(pval)
  35. ;
  36. binary_expression(int maxrank; arith *pval;)
  37. {int oper; arith val1;}
  38. :
  39. unary(pval)
  40. [%while (rank_of(DOT) <= maxrank)
  41. binop(&oper)
  42. binary_expression(rank_of(oper)-1, &val1)
  43. {
  44. ch7bin(pval, oper, val1);
  45. }
  46. ]*
  47. ;
  48. /* 7.13 */
  49. conditional_expression(arith *pval;)
  50. {arith val1 = 0, val2 = 0;}
  51. :
  52. /* allow all binary operators */
  53. binary_expression(rank_of('?') - 1, pval)
  54. [ '?'
  55. expression(&val1)
  56. ':'
  57. assignment_expression(&val2)
  58. { *pval = (*pval ? val1 : val2); }
  59. ]?
  60. ;
  61. /* 7.14 */
  62. assignment_expression(arith *pval;)
  63. :
  64. conditional_expression(pval)
  65. ;
  66. /* 7.15 */
  67. expression(arith *pval;)
  68. {arith val1;}
  69. :
  70. assignment_expression(pval)
  71. [ ','
  72. assignment_expression(&val1)
  73. {
  74. ch7bin(pval, ',', val1);
  75. }
  76. ]*
  77. ;
  78. unop(int *oper;) :
  79. [ '-' | '!' | '~' ]
  80. {*oper = DOT;}
  81. ;
  82. multop:
  83. '*' | '/' | '%'
  84. ;
  85. addop:
  86. '+' | '-'
  87. ;
  88. shiftop:
  89. LEFT | RIGHT
  90. ;
  91. relop:
  92. '<' | '>' | LESSEQ | GREATEREQ
  93. ;
  94. eqop:
  95. EQUAL | NOTEQUAL
  96. ;
  97. arithop:
  98. multop | addop | shiftop
  99. |
  100. '&' | '^' | '|'
  101. ;
  102. binop(int *oper;) :
  103. [ arithop | relop | eqop | AND | OR ]
  104. {*oper = DOT;}
  105. ;
  106. constant(arith *pval;) :
  107. INTEGER
  108. {*pval = dot.tk_val;}
  109. ;
  110. constant_expression (arith *pval;) :
  111. assignment_expression(pval)
  112. ;