变量IS不变值-Ply YACC

发布于 2025-02-13 16:31:28 字数 1417 浏览 2 评论 0原文

我正在用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念三年u 2025-02-20 16:31:28

逻辑的问题在于,您仅在函数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 function p_error. Since p_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").

往日 2025-02-20 16:31:28

没关系,我通过在最后一行之前添加sintactico.Error =“”来解决它,我想它可以刷新并允许error in sintactico.py更新其值

Nevermind, I solved it by adding sintactico.error = "" right before the last line, I guess it refreshes and allows error in sintactico.py to update its value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文