我在语法上运行Python的PLY库。它似乎编译正常,并且库不会提醒我任何转移/减少或减少/减少错误。但是,运行一个非常简单的示例会引发错误。
深入parser.out
文件显示错误发生在程序的最后:
State : 113
Stack : DEFN ID ARROW type invariants statement . $end
ERROR: Error : DEFN ID ARROW type invariants statement . $end
Syntax error at: None
州113是:
state 113
(16) fn_def -> DEFN ID ARROW type invariants statement .
(24) tilde_loop -> statement . TILDE constant TILDE
SEMICOLON reduce using rule 16 (fn_def -> DEFN ID ARROW type inva\
riants statement .)
TILDE shift and go to state 62
据我所知,解析器应该减少到fn_def
。
为什么在达到$end
令牌时不会发生减少?
(如果有帮助,我可以粘贴我的语法,虽然它可能有点长。)
语法
Rule 0 S' -> program
Rule 1 program -> statement_list
Rule 2 statement -> definition SEMICOLON
Rule 3 statement -> expression SEMICOLON
Rule 4 statement -> control_statement
Rule 5 statement -> compound_statement
Rule 6 statement -> comment
Rule 7 statement -> empty SEMICOLON
Rule 8 statement_list -> statement_list statement
Rule 9 statement_list -> statement
Rule 10 control_statement -> loop
Rule 11 control_statement -> if_statement
Rule 12 compound_statement -> CURLY_OPEN statement_list CURLY_CLOSE
Rule 13 compound_statement -> CURLY_OPEN CURLY_CLOSE
Rule 14 definition -> fn_def
Rule 15 definition -> var_def
Rule 16 fn_def -> DEFN ID ARROW type invariants statement
Rule 17 var_def -> type assignment
Rule 18 assignment -> ID ASSIGN expression
Rule 19 loop -> for_loop
Rule 20 loop -> foreach_loop
Rule 21 loop -> tilde_loop
Rule 22 for_loop -> FOR statement statement statement compound_statement
Rule 23 foreach_loop -> FOREACH ID IN expression compound_statement
Rule 24 tilde_loop -> statement TILDE constant TILDE
Rule 25 if_statement -> IF condition compound_statement
Rule 26 if_statement -> IF condition compound_statement elseif_list
Rule 27 if_statement -> IF condition compound_statement elseif_list else
Rule 28 if_statement -> ternary
Rule 29 elseif_list -> ELSEIF condition compound_statement
Rule 30 elseif_list -> ELSEIF condition compound_statement elseif_list
Rule 31 else -> ELSE compound_statement
Rule 32 ternary -> condition QUESTION_MARK expression COLON expression
Rule 33 invariants -> invariants invariant
Rule 34 invariants -> invariant
Rule 35 invariants -> NONE
Rule 36 invariant -> type ID COLON invariant_tests
Rule 37 invariant_tests -> invariant_tests COMMA test
Rule 38 invariant_tests -> test
Rule 39 test -> ID operator constant
Rule 40 test -> STAR
Rule 41 expression -> declarator
Rule 42 expression -> assignment
Rule 43 expression -> container
Rule 44 expression -> test
Rule 45 expression -> constant
Rule 46 type -> INT_T
Rule 47 type -> DBL_T
Rule 48 type -> STR_T
Rule 49 type -> list_type
Rule 50 operator -> GT_OP
Rule 51 operator -> LT_OP
Rule 52 operator -> GTE_OP
Rule 53 operator -> LTE_OP
Rule 54 operator -> EQ_OP
Rule 55 operator -> NEQ_OP
Rule 56 constant -> INT
Rule 57 constant -> DBL
Rule 58 constant -> STR
Rule 59 declarator -> ID L_PAREN fn_args R_PAREN
Rule 60 declarator -> ID
Rule 61 fn_args -> fn_args COMMA expression
Rule 62 fn_args -> expression
Rule 63 container -> list
Rule 64 list -> L_BRACE container_items R_BRACE
Rule 65 list_type -> L_BRACE type R_BRACE
Rule 66 container_items -> container_items COMMA container_item
Rule 67 container_items -> container_item
Rule 68 container_item -> expression
Rule 69 container_item -> empty
Rule 70 bool -> TRUE
Rule 71 bool -> FALSE
Rule 72 condition -> bool
Rule 73 comment -> single_comment
Rule 74 single_comment -> COMMENT_LINE
Rule 75 empty -> <empty>
答案 0 :(得分:1)
好像它需要一个分号,它有输入结束。没有看到语法就很难说更多。
语法的相关部分:
(2) statement -> definition SEMICOLON
(14) definition -> fn_def
这些是fn_def
和definition
出现在右侧的唯一作品。
当前瞻标记为definition
时,显然statement
只能缩减为SEMICOLON
。由于fn_def
只能在可以立即缩减为definition
的地方的有效程序中出现(唯一的产品是单位产品),因此fn_def
后面必须跟SEMICOLON
1}}。所以你的解析器是正确的,你的样本输入是不合理的。
fn_def
只有一个产品:
(16) fn_def -> DEFN ID ARROW type invariants statement
显然fn_def
中的最后一项是statement
。有些陈述(definition
和expression
)必须由;
终止;如果fn_def
的{{1}}是其中之一(可能是一个表达式,因为它似乎不是单个定义构成一个有趣的函数体),那么statement
将不得不用两个分号书写。我怀疑这是否是你想要的。
fn_def
以definition
(如果是statement
)或fn_def
(如果是expression
)结束。您已尝试定义var_def
以使其自行分隔(例如,如果它不以statement
结束}
,那么它以分号结尾。所以{{1已经或者以分号或右括号结束,并且不应该需要另一个分号。另一方面,compound_statement
以一个表达式结束,因此也是如此。所以一个解决方案是推动结束分号为fn_def
。
编辑评论,与提出的具体问题无关:
事实上,没有明显的理由说明为什么你需要限制循环或条件体来复合语句,而不是你自己的美学;如果你允许一个lambda体是一个非复合语句,那么你没有明显的理由限制for循环。语法可以以任何方式工作。