anchor.diff:      token VAR: "[a-zA-Z_]+"
   
       # Each line can either be an expression or an assignment statement
  -    rule goal:   expr<<[]>> END            {{ print '=', expr }}
  -                                           {{ return expr }}
  +    rule goal:   expr<<[]>> END            {{ return expr }}
                  | "set" VAR expr<<[]>> END  {{ globalvars[VAR] = expr }}
  -                                           {{ print VAR, '=', expr }}
                                              {{ return expr }}
   
       # An expression is the sum and difference of factors
  
positive.code: 
      # A term is a number, variable, or an expression surrounded by parentheses
      rule term<<V>>:   
                   NUM                      {{ return atoi(NUM) }}
                 | VAR                      {{ return lookup(V, VAR) }}
                 | "\\(" expr "\\)"         {{ return expr }}
                 | "let" VAR "=" expr<<V>>  {{ V = [(VAR, expr)] + V }}
                   "in" expr<<V>>           {{ return expr }}
  %%
  if __name__=='__main__':
      print 'Welcome to the calculator sample for Yapps 2.'
      print '  Enter either "<expression>" or "set <var> <expression>",'
      print '  or just press return to exit.  An expression can have'
  
negative.code: # This calculator on ints supports the usual (numbers, add, subtract,
  # multiply, divide), global variables (stored in a global variable in
  # Python), and local variables (stored in an attribute passed around
  # in the grammar).
  
  globalvars = {}       # We will store the calculator's variables here
  
  def lookup(map, name):
      for x, v in map:  
          if x == name: return v
      if not globalvars.has_key(name): 
          print 'Undefined (defaulting to 0):', name
      return globalvars.get(name, 0)
  
  %%
  