Python &引用;SyntaxError:Can';“t构建lexer”吗;?
这是我的密码:Python &引用;SyntaxError:Can';“t构建lexer”吗;?,python,regex,python-3.x,lexer,ply,Python,Regex,Python 3.x,Lexer,Ply,这是我的密码: import ply.lex as lex tokens = ( 'LANGLE', # < 'LANGLESLASH', # </ 'RANGLE', # > 'EQUAL', # = 'STRING', # "hello" 'WORD') # Welcome! state = ( ("htmlcomment", "exclusive"), ) t_ignore = ' ' def t_htmlcomme
import ply.lex as lex
tokens = (
'LANGLE', # <
'LANGLESLASH', # </
'RANGLE', # >
'EQUAL', # =
'STRING', # "hello"
'WORD') # Welcome!
state = (
("htmlcomment", "exclusive"),
)
t_ignore = ' '
def t_htmlcomment(token):
r'<!--'
token.lexer.begin('htmlcomment')
def t_htmlcomment_end(token):
r'-->'
token.lexer.lineno += token.value.count('\n')
token.lexer.begin('INITIAl')
def t_htmlcomment_error(token):
token.lexer.skip(1)
def t_newline(token):
r'\n'
token.lexer.lineno += 1
pass
def t_LANGLESLASH(token):
r'</'
return token
def t_LANGLE(token):
r'<'
return token
def t_RANGLE(token):
r'>'
return token
def t_EQUAL(token):
r'='
return token
def t_STRING(token):
r'"[^"]*"'
token.value = token.value[1:-1] # dropping off the double quotes
return token
def t_WORD(token):
r'[^ <>\n]+'
return token
webpage = "This is <b>my</b> webpage"
htmllexer = lex.lex()
htmllexer.input(webpage)
while True:
tok = htmllexer.token()
if not tok: break
print(tok)
但我无法修复它
有人对我如何解决这个问题有什么建议吗?
谢谢你的帮助 有几个令牌没有在
tokens
中声明,您编写了state=(
而不是states=(
@Casimir et Hippolyte,这实际上是一个答案:)事实上,这是答案。。。
Traceback (most recent call last):
File "/home/******/Documents/pro/main.py", line 62, in <module>
htmllexer = lex.lex()
File "/usr/lib/python2.7/dist-packages/ply/lex.py", line 901, in lex
raise SyntaxError("Can't build lexer")
SyntaxError: Can't build lexer
state = (
("htmlcomment", "exclusive"),
)