Python ply.lex中的t_ignore、pass和t.lexer.skip()之间有什么区别?
这三种方法都可用于跳过、忽略或传递字符。例如:Python ply.lex中的t_ignore、pass和t.lexer.skip()之间有什么区别?,python,lex,lexer,ply,Python,Lex,Lexer,Ply,这三种方法都可用于跳过、忽略或传递字符。例如: def t_error(t): pass def t_error(t): t.lexer.skip() def t_default(t): # put at the extreme end and assuming there are no string definitions r'.' pass or skip() 因此,在这种情况下,三者似乎都有相同的作用。我承认有些方法比其他方法更优雅 pass、tu ignore和t.
def t_error(t):
pass
def t_error(t):
t.lexer.skip()
def t_default(t): # put at the extreme end and assuming there are no string definitions
r'.'
pass or skip()
因此,在这种情况下,三者似乎都有相同的作用。我承认有些方法比其他方法更优雅
pass
、tu ignore
和t.lexer.skip()
之间的真正区别是什么?中介绍了所有这些函数
t.lexer.skip()
主要用于错误情况下,在不处理字符的情况下提前输入固定数量的字符
t_ignore
和pass
可以以类似的方式使用t_ignore
是一条特殊规则,用于忽略在识别过程中不起作用的字符。它主要用于忽略空白和类似类型的字符。例如,它可以用来忽略注释,但手册给出了警告,如果这样做会导致其他识别功能出现问题,则不要这样做。例如,注释可以被以下人员忽略:
t_ignore_COMMENT = r'\#.*'
pass
用于在识别词素后丢弃词素而不生成标记。手册中给出了类似的注释示例:
def t_COMMENT(t):
r'\#.*'
pass
# No return value. Token discarded