变量IS不变值-Ply YACC
我正在用UI进行打字稿代码分析仪,我想从代码输入中获取错误语法。我在我的sintactico.py中有这个:
sintactico.py
error = ""
def p_error(p):
global error
error = ""
if p:
error = p
print("sintactico.py IF -> Error en token ", p)
else:
error = "EOF"
print("sintactico.py IF -> Error: se encontró EOF")
print("sintactico.py -> ", error)
syntax = yacc.yacc()
因此,在这里,我会在代码中的检测到的错误中分配错误,但是错误会保留其检测到的第一个错误,然后我将其称为main.py。
main.py
import sintactico
#codigo is a list of strings
for code in codigo:
# ANÁLISIS SINTÁCTICO
parser = sintactico.syntax.parse(code)
r_error = sintactico.error #keeps the first error parser returns
print("main.py -> ", r_error)
item_syn = QListWidgetItem(code)
if r_error == "EOF":
texto = code +"\nError en linea "+str(linea)+"\nPosible error: EOF inesperado"
item_syn = QListWidgetItem(texto)
item_syn.setForeground(QColor(255,0,0))
elif r_error != "":
token_p = sintactico.error
texto = code +"\nError en linea "+str(token_p.lineno)+"\nPosible error: "+str(token_p.value)
item_syn = QListWidgetItem(texto)
item_syn.setForeground(QColor(255,0,0))
self.list_syn.addItem(item_syn)
print(parser)
linea += 1
r_error = ""
self.inp_codigo.clear()
即使代码没有语法错误,它也会继续显示第一个语法错误。我不知道为什么它不会改变值,尽管每个迭代中的代码都不同,并且可以解析不同的代码。
I'm working in a UI for a Typescript code analyzer and I want to get error syntax from code input. I have this in my sintactico.py:
sintactico.py
error = ""
def p_error(p):
global error
error = ""
if p:
error = p
print("sintactico.py IF -> Error en token ", p)
else:
error = "EOF"
print("sintactico.py IF -> Error: se encontró EOF")
print("sintactico.py -> ", error)
syntax = yacc.yacc()
So in here I'm getting error assigned with the detected error in code, but error keeps the first error it detects, then I call it in main.py where the code for the UI is:
main.py
import sintactico
#codigo is a list of strings
for code in codigo:
# ANÁLISIS SINTÁCTICO
parser = sintactico.syntax.parse(code)
r_error = sintactico.error #keeps the first error parser returns
print("main.py -> ", r_error)
item_syn = QListWidgetItem(code)
if r_error == "EOF":
texto = code +"\nError en linea "+str(linea)+"\nPosible error: EOF inesperado"
item_syn = QListWidgetItem(texto)
item_syn.setForeground(QColor(255,0,0))
elif r_error != "":
token_p = sintactico.error
texto = code +"\nError en linea "+str(token_p.lineno)+"\nPosible error: "+str(token_p.value)
item_syn = QListWidgetItem(texto)
item_syn.setForeground(QColor(255,0,0))
self.list_syn.addItem(item_syn)
print(parser)
linea += 1
r_error = ""
self.inp_codigo.clear()
Even if the code has no syntax error, it keeps showing the first syntax error. I don't know why it doesn't change the value although code is different in every iteration and it does parse a different code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
逻辑的问题在于,您仅在函数
p_error
中重置global错误
(即sintactico.error
)。由于p_error
仅在遇到语法错误时由ply调用,因此这意味着仅在下一个错误下清除上一个错误。结果是,如果您解析具有错误的输入,然后解析另一个没有错误的输入,sintactico.error
仍然是指原始错误,这正是您报告的问题(即使是“即使是”代码没有语法错误,它不断显示第一个语法错误”)。The problem with your logic is that you only reset the global
error
(i.e.sintactico.error
) in the functionp_error
. Sincep_error
is only called by Ply when a syntax error is encountered, that means that the previous error is only cleared at the next error. The result is that if you parse an input with an error and then parse another input without an error,sintactico.error
still refers to the original error, which is precisely the problem you reported ("Even if the code has no syntax error, it keeps showing the first syntax error").没关系,我通过在最后一行之前添加
sintactico.Error =“”
来解决它,我想它可以刷新并允许error
insintactico.py
更新其值Nevermind, I solved it by adding
sintactico.error = ""
right before the last line, I guess it refreshes and allowserror
insintactico.py
to update its value