Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;SyntaxError:Can';“t构建lexer”吗;?_Python_Regex_Python 3.x_Lexer_Ply - Fatal编程技术网

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"),
)