title: RecursionError: maximum recursion depth exceeded while using lark in python
body: <p>I've written the decaf grammar specified in cs143 <a href="https://web.stanford.edu/class/archive/cs/cs143/cs143.1128/handouts/030%20Decaf%20Specification.pdf" rel="nofollow noreferrer">course</a>.</p>
  
  <p>Here is my code.</p>
  
  <pre><code>import sys
  
  from lark import Lark, Transformer, v_args
  
  decaf_grammar = r"""
      start : PROGRAM
      PROGRAM : DECL+
      DECL : VARIABLEDECL | FUNCTIONDECL | CLASSDECL | INTERFACEDECL
      VARIABLEDECL : VARIABLE ";"
      VARIABLE : TYPE  "ident"
      TYPE : "int" | "double" | "bool" | "string" | "ident" | TYPE "[]"
      FUNCTIONDECL : ( TYPE "ident"  "(" FORMALS ")" STMTBLOCK ) | ( "void" "ident" "(" FORMALS ")"  STMTBLOCK )
      FORMALS : VARIABLE ("," VARIABLE)*
      CLASSDECL : "class" "ident" ["extends" "ident"] ["implements" "ident" ("," "ident")*] "{" FIELD* "}"
      FIELD : VARIABLEDECL | FUNCTIONDECL
      INTERFACEDECL : "interface" "ident" "{" PROTOTYPE* "}"
      PROTOTYPE : (TYPE "ident" "(" FORMALS ")" ";") | ("void" "ident" "(" FORMALS ")" ";")
      STMTBLOCK : "{" VARIABLEDECL* STMT* "}"
      STMT : ( EXPR? ";") | IFSTMT | WHILESTMT | FORSTMT | BREAKSTMT | RETURNSTMT | RETURNSTMT | PRINTSTMT | STMTBLOCK
      IFSTMT : "if" "(" EXPR ")" STMT ["else" STMT]
      WHILESTMT : "while" "(" EXPR ")" STMT
      FORSTMT : "for" "(" EXPR? ";" EXPR ";" EXPR? ")" STMT
      RETURNSTMT :  "return" EXPR? ";"
      BREAKSTMT : "break" ";"
      PRINTSTMT : "print" "(" EXPR ("," EXPR)* ")" ";"
      EXPR : (LVALUE "=" EXPR) | CONSTANT | LVALUE | "this" | CALL | "(" EXPR ")" | (EXPR "+" EXPR) | (EXPR "-" EXPR) | (EXPR "*" EXPR) | (EXPR "/" EXPR) | (EXPR "%" EXPR) | ("-" EXPR) | (EXPR "&lt;" EXPR) | (EXPR "&lt;=" EXPR) | (EXPR "&gt;" EXPR) | (EXPR "&gt;=" EXPR) | (EXPR "==" EXPR) | (EXPR "!=" EXPR) | (EXPR "&amp;&amp;" EXPR) | (EXPR "||" EXPR) | ("!" EXPR) | ("ReadInteger" "(" ")") | ("ReadLine" "(" ")") | ("new" "ident") | ("NewArray" "(" EXPR "," TYPE ")")
      LVALUE : "ident" | (EXPR "." "ident") | (EXPR "[" EXPR "]")
      CALL : ("ident" "(" ACTUALS ")") | (EXPR "." "ident" "(" ACTUALS ")")
      ACTUALS : EXPR ("," EXPR)* | ""
      CONSTANT : "intConstant" | "doubleConstant" | "boolConstant" | "stringConstant" | "null" 
  
      """
  
  
  class TreeToJson(Transformer):
      @v_args(inline=True)
      def string(self, s):
          return s[1:-1].replace('\\"', '"')
  
  
  json_parser = Lark(decaf_grammar, parser='lalr', lexer='standard', transformer=TreeToJson())
  parse = json_parser.parse
  
  
  def test():
      test_json = '''
          {
  
          }
      '''
  
      j = parse(test_json)
      print(j)
      import json
      assert j == json.loads(test_json)
  
  
  if __name__ == '__main__':
       test()
      #with open(sys.argv[1]) as f:
          #print(parse(f.read()))
  </code></pre>
  
  <p>It throws</p>
  
  <blockquote>
    <p>RecursionError: maximum recursion depth exceeded. </p>
  </blockquote>
  
  <p>I'm using lark for the first time</p>