Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用PLY为一个解析器提供多个lexer?_Python_Lexer_Ply - Fatal编程技术网

Python 使用PLY为一个解析器提供多个lexer?

Python 使用PLY为一个解析器提供多个lexer?,python,lexer,ply,Python,Lexer,Ply,我正在尝试使用用于生成linux内核配置选项的Kconfig语言的PLY实现python解析器 有一个名为source的关键字执行包含,所以我要做的是,当lexer遇到这个关键字时,我更改lexer状态以创建一个新的lexer,它将lex源文件: def t_begin_source(t): r'source ' t.lexer.begin('source') def t_source_path(t): r'[^\n]+\n+' t.lexer.begin('I

我正在尝试使用用于生成linux内核配置选项的Kconfig语言的PLY实现python解析器

有一个名为source的关键字执行包含,所以我要做的是,当lexer遇到这个关键字时,我更改lexer状态以创建一个新的lexer,它将lex源文件:

def t_begin_source(t):
    r'source '
    t.lexer.begin('source')

def t_source_path(t):
    r'[^\n]+\n+'
    t.lexer.begin('INITIAL') 
    global path
    source_lexer = lex.lex(errorlog=lex.NullLogger())
    source_file_name = (path +  t.value.strip(' \"\n')) 
    sourced_file = file(path + t.value.strip(' \"\n')).read()

    source_lexer.input(sourced_file)

    while True:
        tok = source_lexer.token()
        if not tok:
            break
我在别的地方有这条线

lexer = lex.lex(errorlog=lex.NullLogger()) 
这是解析器将要调用的“main”或“root”词法分析器

我的问题是,我不知道如何告诉解析器使用不同的lexer,或者告诉“source_lexer”返回一些东西

也许应该使用克隆功能


谢谢

我不知道PLY的细节,但在我构建的其他类似系统中,使用一个lexer来管理包含文件的堆栈是最有意义的。因此lexer将返回一个统一的令牌流,打开和关闭遇到的include文件。

所以我所做的是构建一个所有标记的列表,它是在实际解析之前构建的

解析器不再调用lexer,因为在调用parse函数时,可以使用tokenfunc参数覆盖解析器使用的getToken函数

result = yacc.parse(kconfig,debug=1,tokenfunc=my_function)
我的函数现在是用来获取下一个令牌的函数,它在先前构建的令牌列表上迭代

考虑到词法分析,当我遇到一个源关键字时,我会克隆我的词法分析程序并更改输入以包含该文件

def sourcing_file(source_file_name):
    print "SOURCE FILE NAME " , source_file_name
    sourced_file = file(source_file_name).read()
    source_lexer = lexer.clone()
    source_lexer.input(sourced_file)
    print 'END OF SOURCING FILE'

    while True:
        tok = source_lexer.token()
        if not tok:
            break
        token_list.append(tok)

一个有趣的巧合是,一个来自同一个谷歌搜索引擎的链接解释了如何搜索。这篇文章简单而好地解释了这一点,但这是一个四个实例变量和单个
标记
方法的问题。

您可以使用lexer的
输入
方法重置lexer并提供新的输入。在包含的文件完成后,您必须返回您停止的原始文件(或多或少)。是的,但我不知道如何将这些标记返回到解析器。我也这样做了。。。但最后,构建一个完整的令牌列表并使用我自己的getToken函数的解决方案并没有那么糟糕。。。