       result =
         type: "P"
         value: right
-    else # Error!
-      throw "Syntax Error. Expected identifier but found " + 
-        (if lookahead then lookahead.value else "end of input") + 
-        " near '#{input.substr(lookahead.from)}'"
+    else
+      throw "Syntax Error. Expected identifier but found " + ((if lookahead then lookahead.value else "end of input")) + (" near '" + (input.substr(lookahead.from)) + "'")
     result
 
   expression = ->
+    result = undefined
+    right = undefined
     result = term()
     if lookahead and lookahead.type is "+"
       match "+"

main = ()->
  source = INPUT.value
  
  try 
    result = JSON.stringify(parse(source), null, 2)
  catch result
    result = """<div class="error">#{result}</div>"""
    
  OUTPUT.innerHTML = result

window.onload = ()-> 
  PARSE.onclick = main

Object.constructor::error = (message, t) ->
  t = t or this
  t.name = "SyntaxError"
  t.message = message
  throw treturn

RegExp::bexec = (str) ->
  i = @lastIndex
  m = @exec(str)
  return m  if m and m.index is i
  null

String::tokens = ->
  from = undefined # The index of the start of the token.
  i = 0 # The index of the current character.
  n = undefined # The number value.
  m = undefined # Matching
  result = [] # An array to hold the results.
  WHITES = /\s+/g
  ID = /[a-zA-Z_]\w*/g
  NUM = /\b\d+(\.\d*)?([eE][+-]?\d+)?\b/g

    to: i

  getTok = ->
    str = undefined
    str = m[0]
    i += str.length
    str

  return  unless this
  while i < @length
    tokens.forEach (t) ->
      t.lastIndex = i
      return

    from = i
    if m = WHITES.bexec(this) or (m = ONELINECOMMENT.bexec(this)) or (m = MULTIPLELINECOMMENT.bexec(this))
      getTok()
    else if m = ID.bexec(this)
      rw = RESERVED_WORD[m[0]]
      if rw
        result.push make(rw, getTok())
      else
        result.push make("ID", getTok())
    else if m = NUM.bexec(this)
      n = +getTok()
      if isFinite(n)
        result.push make("NUM", n)
      else
        make("NUM", m[0]).error "Bad number"
    else if m = STRING.bexec(this)
      result.push make("STRING", getTok().replace(/^["']|["']$/g, ""))
    else if m = ONECHAROPERATORS.bexec(this)
      result.push make(m[0], getTok())
    else
      throw "Syntax error near '" + (@substr(i)) + "'"
  result

parse = (input) ->
  expression = undefined
  factor = undefined
  lookahead = undefined
  match = undefined
  statement = undefined
  statements = undefined
  term = undefined
  tokens = undefined
  tree = undefined
  tokens = input.tokens()
  lookahead = tokens.shift()
  match = (t) ->
    if lookahead.type is t
      lookahead = tokens.shift()
      lookahead = null  if typeof lookahead is "undefined"
    else
      throw ("Syntax Error. Expected " + t + " found '") + lookahead.value + "' near '" + input.substr(lookahead.from) + "'"
    return

  statements = ->
    result = undefined
    result = [statement()]
    while lookahead and lookahead.type is ";"
      match ";"
      result.push statement()
    if result.length is 1
      result[0]
    else
      result

  statement = ->
    left = undefined
    result = undefined
    right = undefined
    result = null
    if lookahead and lookahead.type is "ID"
      left =
