相关疑难解决方法(0)

用PLY解析python,如何编码缩进和缩进部分

我试图用 PLY 解析 python 语言的函数定义。我遇到了与缩进相关的问题。例如对于 for 语句,我希望能够知道块何时结束。我在这里阅读了 python 语法:http : //docs.python.org/2/reference/grammar.html 而这部分的语法是:

for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Run Code Online (Sandbox Code Playgroud)

我不知道如何用 PLY 描述 INDENT 和 DEDENT 令牌。我正在尝试类似的东西:

def t_indentation(t):
    r'    |\t'
    #some special treatment for the indentation.
Run Code Online (Sandbox Code Playgroud)

但似乎 PLY 认为带有空格的正则表达式匹配空字符串并且不构建词法分析器......即使我已经设法拥有 INDENT 令牌,我也不确定如何获得 DEDENT 令牌......

有没有办法用 PLY 做到这一点?

python grammar ply indentation

6
推荐指数
1
解决办法
2363
查看次数

标签 统计

grammar ×1

indentation ×1

ply ×1

python ×1