我已经写在cs143规定的无咖啡因的语法课程。这是我的代码。import sysfrom lark import Lark, Transformer, v_argsdecaf_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:超出最大递归深度。我是第一次使用百灵
2 回答
繁华开满天机
TA贡献1816条经验 获得超4个赞
你遇到的问题是你感觉不到云雀的规则和终端之间的区别。终端(它们只能以大写字母命名)应该匹配字符串,而不是语法结构。
您必须支持的主要终端属性是,与规则不同,它们不是“递归的”。由于lark
努力构建您的语法并进入无限递归和堆栈溢出。
添加回答
举报
0/150
提交
取消