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