gpt4 book ai didi

python - 从 PLY 向解析器的调用者报告解析错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:56:41 25 4
gpt4 key购买 nike

所以我已经使用 PLY 实现了一个解析器——但是所有 PLY 文档都通过打印错误消息来处理解析和标记化错误。我想知道在 API 级别向解析器的调用者实现非 fatal error 报告的最佳方法是什么。显然,“非致命”限制意味着排除异常 — 感觉我会误用 warnings 模块来处理解析错误。有什么建议吗?

最佳答案

PLY 有一个 t_error() 函数,你可以在你的解析器中覆盖它来做任何你想做的事。文档中提供的示例打印出一条错误消息并跳过有问题的字符 - 但您可以轻松地更新遇到的解析失败列表,设置一个在 X 次失败后停止的阈值等。- http://www.dabeaz.com/ply/ply.html

4.9 Error handling

Finally, the t_error() function is used to handle lexing errors that occur when illegal characters are detected. In this case, the t.value attribute contains the rest of the input string that has not been tokenized. In the example, the error function was defined as follows:

# Error handling rule
def t_error(t):
print "Illegal character '%s'" % t.value[0]
t.lexer.skip(1)

你可以通过让你的解析器成为一个类并在其中存储错误状态来利用它 - 这是一个非常粗略的例子,因为你必须制作多个 MyLexer 实例,然后构建()它们,然后利用它们进行解析,如果你想要同时运行多个词法分析器。

您可以将错误存储与词法分析器实例本身的 __hash__ 结合起来,这样只需构建一次。我不清楚在一个类中运行多个词法分析器实例的细节,但实际上这只是给出一个粗略的示例,说明如何捕获和报告非 fatal error 。

为此,我修改了 Ply 文档中的简单计算器类示例。

#!/usr/bin/python

import ply.lex as lex

class MyLexer:

errors = []

# List of token names. This is always required
tokens = (
'NUMBER',
'PLUS',
'MINUS',
'TIMES',
'DIVIDE',
'LPAREN',
'RPAREN',
)

# Regular expression rules for simple tokens
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_LPAREN = r'\('
t_RPAREN = r'\)'

# A regular expression rule with some action code
# Note addition of self parameter since we're in a class
def t_NUMBER(self,t):
r'\d+'
t.value = int(t.value)
return t

# Define a rule so we can track line numbers
def t_newline(self,t):
r'\n+'
t.lexer.lineno += len(t.value)

# A string containing ignored characters (spaces and tabs)
t_ignore = ' \t'

# Error handling rule
def t_error(self,t):
self.errors.append("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)

# Build the lexer
def build(self,**kwargs):
self.errors = []
self.lexer = lex.lex(module=self, **kwargs)

# Test it output
def test(self,data):
self.errors = []
self.lexer.input(data)
while True:
tok = self.lexer.token()
if not tok: break
print tok

def report(self):
return self.errors

用法:

# Build the lexer and try it out
m = MyLexer()
m.build() # Build the lexer
m.test("3 + 4 + 5") # Test it
print m.report()
m.test("3 + A + B")
print m.report()

输出:

LexToken(NUMBER,3,1,0)
LexToken(PLUS,'+',1,2)
LexToken(NUMBER,4,1,4)
LexToken(PLUS,'+',1,6)
LexToken(NUMBER,5,1,8)
[]
LexToken(NUMBER,3,1,0)
LexToken(PLUS,'+',1,2)
LexToken(PLUS,'+',1,6)
["Illegal character 'A'", "Illegal character 'B'"]

关于python - 从 PLY 向解析器的调用者报告解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18046579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com