首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Python在$end令牌上停止解析?

为什么Python在$end令牌上停止解析?
EN

Stack Overflow用户
提问于 2013-07-22 06:34:53
回答 1查看 569关注 0票数 0

我正在我的语法上运行Python的PLY库。它似乎编译得很好,而且库没有提醒我注意任何移位/减少或减少/减少错误。但是,在一个非常简单的示例上运行会引发一个错误。

深入了解parser.out文件可以发现,错误发生在程序的末尾:

代码语言:javascript
复制
State  : 113
Stack  : DEFN ID ARROW type invariants statement . $end
ERROR: Error  : DEFN ID ARROW type invariants statement . $end
Syntax error at: None

113国是:

代码语言:javascript
复制
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令牌时不会发生减少呢?

(如果语法有帮助的话,我可以粘贴它,尽管它可能有点长。)

语法

代码语言:javascript
复制
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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-22 14:23:33

好像它需要一个分号,而且它有输入结束。很难不看语法就说得更多。

你语法的相关部分:

代码语言:javascript
复制
 (2) statement -> definition SEMICOLON
(14) definition -> fn_def

这些是fn_defdefinition出现在右侧的唯一产品。

显然,只有当查找令牌是definition时,才能将statement还原为SEMICOLON。因为fn_def只能出现在一个有效的程序中,在那里它可以立即还原为definition (唯一的生产是单位生产),所以fn_def必须后面跟着一个SEMICOLON。所以你的解析器是正确的,你的样本输入是不语法的。

fn_def只有一个产品:

代码语言:javascript
复制
(16) fn_def -> DEFN ID ARROW type invariants statement

显然,fn_def中的最后一项是statement。有些语句(definitionexpression)必须用;来结束;如果fn_defstatement是其中之一(大概是一个表达式,因为它看起来不像是一个有趣的函数体),那么fn_def必须用两个分号来写。我怀疑这是不是你想要的。

definitionstatement (如果是fn_def)或expression (如果是var_def)结尾。您已经尝试过定义statement,以便它是自定界的(也就是说,如果它没有以终止compound_statement}结尾,则以分号结尾。因此,fn_def已经以分号或结束大括号结尾,不应该需要另一个分号。另一方面,var_def以表达式结尾,因此确实如此。因此,一种解决方案是将结束分号推入var_def

与被问及的具体问题无关的社论评论:

事实上,除了自己的美学之外,没有明显的理由需要限制循环或条件体来复合语句;如果允许lambda body是非复合语句,则没有明显的理由限制for循环。无论哪种方式,语法都可以发挥作用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17781297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档