Python-Lex-Yacc 符号无法访问

标签 python python-3.x yacc lex ply

我正在尝试使用 ply,但我不断收到这些错误:

WARNING: C:\Users\...\Documents\Kino\Kino-Source-Code\complier.py:84: Rule 'divide' defined, but not used
WARNING: C:\Users\...\complier.py:99: Rule 'vars' defined, but not used
WARNING: C:\Users\...\complier.py:122: Rule 'multiply' defined, but not used
WARNING: C:\Users\...\complier.py:144: Rule 'say' defined, but not used
WARNING: There are 4 unused rules
WARNING: Symbol 'divide' is unreachable
WARNING: Symbol 'vars' is unreachable
WARNING: Symbol 'multiply' is unreachable
WARNING: Symbol 'say' is unreachable

不知何故,规则 1) 已定义但未使用,2) 无法访问。 我已经尝试了所有我能想到的方法,但还是不行! 这是我的代码:

from ply import lex, yacc
import rich
import math

ERROR = False
reserved = {
    'say' : "SAY",

}

tokens = [
    'MULTIPLY',
    'QUOTE',
    'SPACE',
    'EQUAL',
    'QTEXT',
    'VARIABLES',
    'DIVIDE'
] + list(reserved.values())

meta = [

]
 
variables = {

}

t_DIVIDE = r"[A-Za-z0-9]+/[A-Za-z0-9]+"
t_MULTIPLY = r"\w_ ?\*\w_ ?"
t_SAY = "say"
t_QUOTE = r"\"" 
t_SPACE = r"\s"
t_QTEXT = r"\".+_ ?\""
t_EQUAL = r"\w+_ ?=\w+_ ?"
t_VARIABLES = r"\w+"

def t_error(t):
    global ERROR
    rich.print(f"[bold red]Illegal character {t.value[0]!r} on line {t.lexer.lineno}[/bold red]")
    t.lexer.skip(1)
    ERROR = True

t_ignore = '\n'

lexer = lex.lex()

def p_divide(t):
    """
    divide : DIVIDE
    """
    try:
        tmp = t[1].split("/")
        for x, i in enumerate(tmp):
            tmp[x] = float(i)
        t.value = tmp[0] / tmp[1]
        print(tmp[0] / tmp[1])
        return t.value
    except ValueError:
        rich.print("[bold red]Multiplying a non number[/bold red]\n[bold blue]Error Ignored, this may cause your program to malfunction, please fix[/bold blue]")


def p_vars_set(t):
    """
    vars : EQUAL
    """
    name = ""
    value = ""
    stripped = str(t[1]).split("=")
    name = stripped[0]
    value = stripped[1]
    variables[name] = value


def p_vars(t):
    """
    vars : VARIABLES
    """
    tmp = t[1]
    for i in t:
        print(i)
    t.value = variables[str(tmp)]
    #return t.value


def p_multiply(t):
    """
    multiply : MULTIPLY
    """
    try:
        tmp = str(t).split("*")
        for i in tmp:
            int(i)
        #t.value = NUM
        return t.value
    except ValueError:
        try:
            if "true" in t:
                print("1")
            if "false" in t:
                print("2")
            else:
                print("0")
        except:
            pass


def p_say_onlyText(t):
    """
    say : SAY QUOTE QTEXT QUOTE
        | SAY SPACE QTEXT 
    """
    l = len(t)
    start = False
    for i in (t):
        if str(i).startswith('"'):
            to_print = str(i).strip('"')
            print(to_print)


def p_error(t):
    global ERROR
    ERROR = True
    if t is None:  # lexer error
        return
    print(f"Syntax Error: {t.value!r}")

parser = yacc.yacc(debug=False, write_tables=False)

if __name__ == "__main__":
    rich.print("[yellow]Hello From The Alter Community[/yellow]")
    try:
       while True:
           i = input(">>")
           parser.parse(i)
    except IndexError:
        rich.print("[bold red]No File Specifed[/bold red]")
        rich.print("[bold blue]Program exited with code 5[/bold blue]")
        exit(5)
    except FileNotFoundError:
        rich.print("[bold red]File Not Found[/bold red]")
        rich.print("[bold blue]Program exited with code 5[/bold blue]")
        exit(5)
    if ERROR == True:
        rich.print("[bold red]Errors![/bold red]")
        rich.print("[bold blue]Program exited with code 1[/bold blue]")
    else:
        rich.print("[bold green]No Errors![/bold green]")
        rich.print("[bold blue]Program exited with code 0[/bold blue]")

不知何故,顶部的 bool 函数是唯一可访问的函数,而下面的任何函数都不可访问。

最佳答案

这些错误通常是由于没有从头开始造成的。

在 Ply 中,与许多解析器生成器一样,第一个定义的非终结符预计是语法的顶级符号:即派生所有有效输入的非终结符。

如果出于某种原因您不想将顶级符号放在前面,则可以使用 start 指定开始符号。请参阅Ply Manual了解详情。

关于Python-Lex-Yacc 符号无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63459505/

相关文章:

python - 循环内部引用子模块

vhdl - 如何编写不区分大小写的 Lex 模式规则?

python - Paramiko exec_command 失败, 'NoneType' 对象不可迭代

python - C 与 Python 中特征向量例程的不同结果

python - 在方法的开头和结尾做一些事情

c - Bison 递归在链表中表现异常

c - 在 bison/yacc 语义操作中构建异构数据类型数组(或集合)的最佳方法

python - 如何增量填充 Pandas 数据框中缺失的时间戳?

python - 当另一个进程仍在写入时删除文件

python - 从 python 运行 docker-compose