Python YACC EOF 立即到达

Python YACC EOF reached immediately

晚上好,

我正在学习使用 Ply(Python 3 / Win 8.1 系统)在 lex/yacc 上工作,但我 运行 陷入困境:我似乎无法使 yacc 正确读取输入文件。

如果我对输入文件的内容进行硬编码,我会得到预期的结果,但如果我尝试从中读取,yacc 刚好从状态 0 到达文件末尾,在真正执行之前停止句法分析开始了。

这是我的主要内容(标记和句法规则在上面定义 - 它们似乎不是问题,因为当输入是硬编码时程序 运行 没问题):

if __name__ == "__main__":
import sys
lexer = lex.lex()
yacc.yacc()
inputfile = open(sys.argv[1], 'r', encoding="UTF-8")
lexer.input(inputfile.read())
for token in lexer: #for this part, the file is read correctly
    print("line %d : %s (%s) " % (token.lineno, token.type, token.value))

result = yacc.parse(inputfile.read(), debug=True)
print(result) #Stack immediately contains . $end and the p_error(p) I've defined confirms EOF was reached
tmp = "{{var1 := 'some text' ; var2 := 'some other text' ; var3 := ( 'text', 'text2') ; }}" #Same contents as the input file
result = yacc.parse(tmp, debug=True)
print(result) #correct results

inputfile.read() 读取文件末尾,因此在成功完成后您知道文件位于 EOF。

您应该只读取()一次文件的内容:

if __name__ == "__main__":
    import sys
    lexer = lex.lex()
    yacc.yacc()

    with open(sys.argv[1], 'r', encoding="UTF-8") as inputfile:
        contents = inputfile.read()
        lexer.input(contents)
        for token in lexer: #for this part, the file is read correctly
            print("line %d : %s (%s) " % (token.lineno, token.type, token.value))

        result = yacc.parse(contents, debug=True)
        print(result) #Stack immediatly contains . $end and the p_error(p) I've defined confirms EOF was reached
        tmp = "{{var1 := 'some text' ; var2 := 'some other text' ; var3 := ( 'text', 'text2') ; }}" #Same contents as the input file
        result = yacc.parse(tmp, debug=True)
        print(result) #correct results

使用 "with as" 也是一个好主意,它更容易阅读 (imo) 并且更 'robust':

with open(sys.argv[1], 'r', encoding="UTF-8") as inputfile:
    lexer.input(inputfile.read())

参见:https://www.python.org/dev/peps/pep-0343/