在 python 中重命名 elif

rename elif in python

我最近在尝试在 python 中添加一些可选的语句名称并且在我到达 if 语句之前效果很好并且我为 else 和 elif 添加了一个可选名称:

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

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

SystemError: unexpected token in 'if' statement: andernfalls 

我所做的所有其他添加都非常有效。那么,为什么我不能添加其他 else 和 elif,我该怎么做呢?

我正在修改来自 python 网站的最新 python 2.7 代码

编辑 我的测试代码是:

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

我在顶部添加行的文件是 python 源代码

的语法目录中的语法文件

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') {
            ...

您必须修改 Python/ast.c 中的 ast_for_if_stmt 才能更改该处理方式。