expression.g 1.7 KB

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