使用PLY解析python,如何编写缩进和缩进部分 [英] Parsing python with PLY, how to code the indent and dedent part

查看:232
本文介绍了使用PLY解析python,如何编写缩进和缩进部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to parse the function definition for the python language with PLY. I am encountering issues related to the indentation. For instance for a for statement, I would like to be able to know when the block ends. I read the python grammar here: http://docs.python.org/2/reference/grammar.html And the grammar for this part is:

for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT

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

I don't know how to describe the INDENT and DEDENT tokens with PLY. I was trying something like:

def t_indentation(t):
    r'    |\t'
    #some special treatment for the indentation.

但是PLY似乎认为带有空格的正则表达式匹配空字符串,并且不会构建词法分析器... 即使我本来可以拥有INDENT令牌,也不确定要获取DEDENT的方式...

But it seems that PLY consider that regexes with spaces match the empty string and does not build the lexer... Even if I would have managed to have the INDENT token I am not sure about the way to get the DEDENT one...

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

Is there a way to do that with PLY?

推荐答案

您必须使用状态来解析INDENT和UNDENT.

You have to use states to parse INDENT and UNDENT.

解析python之类语言的示例

example of parsing python like language

这篇关于使用PLY解析python,如何编写缩进和缩进部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆