RecursionError:在python中使用百灵鸟时超出了最大递归深度

我已经写在cs143规定的无咖啡因的语法课程。


这是我的代码。


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 "<" EXPR) | (EXPR "<=" EXPR) | (EXPR ">" EXPR) | (EXPR ">=" EXPR) | (EXPR "==" EXPR) | (EXPR "!=" EXPR) | (EXPR "&&" 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" 



它抛出


RecursionError:超出最大递归深度。


我是第一次使用百灵


炎炎设计
浏览 242回答 2
2回答

繁华开满天机

你遇到的问题是你感觉不到云雀的规则和终端之间的区别。终端(它们只能以大写字母命名)应该匹配字符串,而不是语法结构。您必须支持的主要终端属性是,与规则不同,它们不是“递归的”。由于lark努力构建您的语法并进入无限递归和堆栈溢出。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python