Posted to tcl by kbk at Mon May 26 02:08:01 GMT 2008view raw

  1. /*
  2. * Let the terminal symbols of the grammar include the various operators that
  3. * can appear in expressions. UMINUS and UPLUS are fictitious operators
  4. * that are used to establish precedence for unary '-' and '+'
  5. */
  6.  
  7. %left ';'
  8. %right '='
  9. %nonassoc '?' ':'
  10. %left OROR
  11. %left ANDAND
  12. %left '|'
  13. %left '^'
  14. %left '&'
  15. %nonassoc IN NI
  16. %nonassoc EQ NE
  17. %nonassoc EQUALS NOTEQUALS
  18. %left LSHIFT RSHIFT
  19. %left '-' '+'
  20. %left '*' '/' '%'
  21. %right EXPONENT
  22. %left '~' '!' UMINUS UPLUS
  23.  
  24. /*
  25. * The terminal symbols also include:
  26. * NUMBER - Any string recognizable as an integer or floating-point
  27. * constant.
  28. * BOOLEAN_CONSTANT - One of the barewords that can be a boolean constant
  29. * BAREWORD - A bare word that is neither a Boolean constant nor an
  30. * operator. Note that a bare word may contain any number
  31. * of :: separators for namespaces.
  32. * DOUBLE_QUOTED_STRING - A string enclosed in double quotes
  33. * BRACED_STRING - A string enclosed in braces
  34. * BRACKETED_TCL_COMMAND - A Tcl command enclosed in brackets
  35. * VARIABLE_REF - A dollar sign '$', followed by a variable name
  36. * and an optional subscript, exactly as recognized
  37. * for substitution in today's [expr] parser.
  38. * WORDSUBSCRIPTEQUALS - (Let the lexer help the parser!) - A
  39. * bareword, followed by a string in parentheses, followed
  40. * by an equal sign. This lexeme represents a subscripted
  41. * variable reference on the left hand side of an equal sign.
  42. */
  43.  
  44. %nonassoc NUMBER
  45. BOOLEAN_CONSTANT
  46. BAREWORD
  47. DOUBLE_QUOTED_STRING
  48. BRACED_STRING
  49. BRACKETED_TCL_COMMAND
  50. VARIABLE_REF
  51. WORDSUBSCRIPTEQUALS
  52.  
  53. /*
  54. * Finally, parentheses not appearing as part of subscripts are
  55. * terminal symbols.
  56. */
  57.  
  58. %nonassoc '(' ')'
  59.  
  60. %%
  61.  
  62. start : sequence;
  63.  
  64. /*
  65. * The argument to an [expr] comprises one or more expressions,
  66. * separated with semicolons. (NOT newlines - that's incompatible with
  67. * todays [expr] command.)
  68. */
  69.  
  70. sequence : assignment_expr
  71. | sequence ';' assignment_expr
  72. ;
  73.  
  74. /*
  75. * Expressions may contain assignments; the left hand sides of assignments
  76. * may be any bareword (including operators and Boolean constants), plus
  77. * a bareword with a subscript, a double-quoted string, or a braced string.
  78. */
  79.  
  80. assignment_expr : ternary_expr
  81. | word '=' assignment_expr
  82. | WORDSUBSCRIPTEQUALS assignment_expr
  83. | DOUBLE_QUOTED_STRING '=' assignment_expr
  84. | BRACED_STRING '=' assignment_expr
  85. ;
  86.  
  87. /*
  88. * An expression may contain the ternary ? : operator
  89. */
  90.  
  91. ternary_expr : binary_expr
  92. | binary_expr '?' ternary_expr ':' ternary_expr
  93. ;
  94.  
  95. /*
  96. * An expression may contain any of the usual binary operators.
  97. * Precedence and associativity are given in the table above.
  98. */
  99.  
  100. binary_expr : unary_expr
  101. | binary_expr OROR binary_expr
  102. | binary_expr ANDAND binary_expr
  103. | binary_expr '|' binary_expr
  104. | binary_expr '^' binary_expr
  105. | binary_expr '&' binary_expr
  106. | binary_expr IN binary_expr
  107. | binary_expr NI binary_expr
  108. | binary_expr EQ binary_expr
  109. | binary_expr NE binary_expr
  110. | binary_expr EQUALS binary_expr
  111. | binary_expr NOTEQUALS binary_expr
  112. | binary_expr LSHIFT binary_expr
  113. | binary_expr RSHIFT binary_expr
  114. | binary_expr '+' binary_expr
  115. | binary_expr '-' binary_expr
  116. | binary_expr '*' binary_expr
  117. | binary_expr '/' binary_expr
  118. | binary_expr '%' binary_expr
  119. | binary_expr EXPONENT binary_expr
  120. ;
  121.  
  122. /*
  123. * An expression may contain any of the unary operators
  124. */
  125.  
  126. unary_expr : primary_expr
  127. | '-' primary_expr %prec UMINUS
  128. | '+' primary_expr %prec UPLUS
  129. | '~' primary_expr
  130. | '!' primary_expr
  131. ;
  132.  
  133. /*
  134. * Primary symbols of an expression include:
  135. * $-substituted variable references
  136. * Function calls
  137. * Barewords
  138. * Numbers
  139. * Double quoted strings
  140. * Braced strings
  141. * Bracket-substituted commands
  142. * Parenthesized expressions
  143. */
  144.  
  145. primary_expr : VARIABLE_REF
  146. | word '(' expr_list ')'
  147. | word
  148. | NUMBER
  149. | DOUBLE_QUOTED_STRING
  150. | BRACED_STRING
  151. | BRACKETED_TCL_COMMAND
  152. | '(' assignment_expr ')'
  153. ;
  154.  
  155. /*
  156. * An expression list for a function call comprises zero or more
  157. * expressions, separated by commas.
  158. */
  159.  
  160. expr_list :
  161. | nonempty_expr_list
  162. ;
  163. nonempty_expr_list : assignment_expr
  164. | nonempty_expr_list ',' assignment_expr
  165. ;
  166.  
  167. /*
  168. * Barewords include bare words otherwise unrecognized, plus the operators
  169. * that are sequences of letters, plus the Boolean constants.
  170. */
  171.  
  172. word : BAREWORD
  173. | BOOLEAN_CONSTANT
  174. | IN %prec BAREWORD
  175. | NI %prec BAREWORD
  176. | EQ %prec BAREWORD
  177. | NE %prec BAREWORD
  178. ;
  179.  
  180. %%