expression.g 2.1 KB

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