   #! Return the body of the word that is the compiled version
   #! of the parser.
   [let* | parser-quot [ parser (compile) ] 
-          id          [ parser id>>      ]
         |
     [
       packrat get [ 
-        parser-quot id run-packrat-parser
+        parser-quot parser run-packrat-parser
       ] [
         parser-quot call
       ] if

C: <left-recursion> left-recursion

USE: prettyprint

:: handle-left-recursive-result ( result -- result )
  #! If the result is from a left-recursive call,
  #! note this and fail, otherwise return normal result
  #! See figure 4 of packrat_TR-2007-002.pdf.
  result [
    [let* | ast [ result ast>> ] |
      ast left-recursion? [ t ast (>>detected?) f ] [ result ] if
    ]
  ] [ 
    f
  ] if ;
         
USE: io

:: grow-lr ( input quot m -- result )
  #! 'Grow the Seed' algorithm to handle left recursion
  [let* | ans [ input quot call ] |
    [ ans not ] [ ans [ ans remaining>> input-from m remaining>> input-from <= ] [ f ] if ] 2array || [ 
      "recursion exiting with = " write ans . "m was " write m . 

      "recursion with = " write ans . 
      input quot ans grow-lr
    ] if
  ] 
  "<<grow-lr " write input . " for parser " write parser . " m is " write m . " result is " write dup .  
  ;

:: cached-result ( input-cache input quot parser -- result )
  #! Get the cached result for input position 
  #! from the input cache. If the item is not in the cache,
  #! call 'quot' with 'input' on the stack to get the result
  #! and store that in the cache and return it.
  #! See figure 4 of packrat_TR-2007-002.pdf.
  ">>cached-result " write input . "  for parser " write parser .
  input input-from input-cache [ 
    drop
    [let* | lr  [ f <left-recursion> ] 
            m   [ input lr <parse-result> ]
            ans [ m input input-from input-cache set-at input quot call ]
          |
      "--lr is " write lr . " ans is " write ans . " for parser " write parser .
      ans input input-from input-cache set-at
      lr detected?>> ans and [
        input quot parser ans grow-lr
      ] [
        ans
      ] if
    ]
  ] cache 
  dup [ handle-left-recursive-result ] when  
  "<<cached-result " write dup . " for parser " write parser . ;

:: run-packrat-parser ( input quot parser -- result )
  parser id>> input-cache 
  input quot parser cached-result ; inline

: run-parser ( input quot -- result )
  #! If a packrat cache is available, use memoization for
