在python中重命名elif

2024-10-02 14:19:36 发布

您现在位置:Python中文网/ 问答频道 /正文

最近,我在python中添加了一些可选的语句名,这一切都很好,直到我找到if语句并为else和elif添加了一个可选名称:

if_stmt = 'if' test ':' suite ('elif' test ':'suite)* ['else' ':' suite] |  'wenn' test ':' suite ('andernfalls' test ':'suite)* ['sonst' ':' suite]

然后编译没有错误,但是当我用wenn和andrenfalls运行测试时,解释器抛出了错误:

^{pr2}$

我做的每一个额外的工作都很好。那么为什么我不能加上其他和elif,我该怎么做呢?在

我正在修改python网站上最新的python2.7代码

编辑 我的测试代码是:

x = 1
y = 2
wenn x > y:
    print 1
andernfalls x < y:
    print 2
sonst:
    print 3

我在顶部添加行的文件是python源代码语法目录中的语法文件


Tags: 文件test名称if错误语法语句else
1条回答
网友
1楼 · 发布于 2024-10-02 14:19:36

elseelif在AST生成代码中有一些特殊情况处理:

static stmt_ty
ast_for_if_stmt(struct compiling *c, const node *n)
{
    ...
    /* s[2], the third character in the string, will be
       's' for el_s_e, or
       'i' for el_i_f
    */
    if (s[2] == 's') {
        ...
    }
    else if (s[2] == 'i') {
        ...
        if (TYPE(CHILD(n, (n_elif + 1))) == NAME
            && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
            ...

您必须修改^{}中的ast_for_if_stmt以更改该处理方式。在

相关问题 更多 >