我在我的语法上运行 Python 的 PLY 库。它似乎可以编译,并且库不会提醒我任何 shift/reduce 或 reduce/reduce 错误。但是,在一个非常简单的示例上运行会引发错误。
挖掘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
为什么到达令牌时不会发生reduce ?
(如果有帮助,我可以粘贴我的语法,虽然它可能有点长。)
语法
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>