我一直在研究一些PLY代码,在我的解析器语法中,有一些东西我想要“含糊不清”。例如,与“打印”等价的是“显示”。我不希望show只打印特定类型,而是希望它打印任何内容,为此我尝试在语法中使用表达式,但它给了我一个错误: 这是我的密码
import ply.lex as lex
import ply.yacc as yacc
reserved = {
# 'if' : 'IF',
# 'then' : 'THEN',
# 'else' : 'ELSE',
# 'repeat-if' : 'REPEAT-IF',
'is' : "IS",
'show' : "SHOW",
'receive': "RECEIVE"
}
tokens = ['LPAREN','RPAREN','STRING', 'NAME'] + list(reserved.values())
def t_ID(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
t.type = reserved.get(t.value,'NAME') # Check for reserved words
return t
t_LPAREN =r'\('
t_RPAREN = r'\)'
t_STRING = r'\".*?\"'
t_ignore = " \t"
def t_error(t):
print("Syntax error at %s"%t.value)
Variables = {}
def p_receive(p):
'''statement : NAME IS RECEIVE LPAREN STRING RPAREN'''
value = input(f"{(p.slice[5].value[1:-1])}")
Variables[p.slice[1].value]=value
def p_show(p):
'statement : SHOW LPAREN expression RPAREN '
value = p.slice[3]
#TO-DO
def p_is(p):
'statement : NAME IS STRING'
Variables[p[1]] = p[3]
def p_variable(p):
'statement : NAME'
return Variables[p.slice[1].value]
def p_error(p):
print("Syntax error at %s"%p)
lexer = lex.lex()
yacc.yacc()
with open('CODE.txt','r') as file:
code = file.read().splitlines()
for line in code:
try:
s = line
except EOFError:
break
yacc.parse(s)
我得到以下错误:
Symbol 'expression' used, but not defined as a token or a rule
然而,使用来自https://www.dabeaz.com/ply/example.html的PLY示例,我从未看到必须定义表达式
目前没有回答
相关问题 更多 >
编程相关推荐