PLY LEX 和 YACC 的问题

9

我试图运行PLY的一个简单示例的第一部分,但是遇到了一个奇怪的错误。当我运行以下代码时,它会给我关于lex.lex()的错误。 有人知道问题是什么吗?

import ply.lex as lex

tokens = [ 'NAME','NUMBER','PLUS','MINUS','TIMES', 'DIVIDE', 'EQUALS' ]
t_ignore =  '\t'
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_EQUALS = r'='
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
def t_NUMBER(t):
    r'\d+'
    t.value = int(t.value)
    return t

lex.lex() # Build the lexer

这是错误信息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-e527bd224769> in <module>()
     14     return t
     15 
---> 16 ply.lex.lex() # Build the lexer

c:\python27\lib\site-packages\ply\lex.pyc in lex(module, object, debug, optimize, lextab, reflags, nowarn, outputdir, debuglog, errorlog)
    904     linfo.get_all()
    905     if not optimize:
--> 906         if linfo.validate_all():
    907             raise SyntaxError("Can't build lexer")
    908 

c:\python27\lib\site-packages\ply\lex.pyc in validate_all(self)
    578         self.validate_tokens()
    579         self.validate_literals()
--> 580         self.validate_rules()
    581         return self.error
    582 

c:\python27\lib\site-packages\ply\lex.pyc in validate_rules(self)
    820 
    821         for module in self.modules:
--> 822             self.validate_module(module)
    823 
    824     # -----------------------------------------------------------------------------

c:\python27\lib\site-packages\ply\lex.pyc in validate_module(self, module)
    831 
    832     def validate_module(self, module):
--> 833         lines, linen = inspect.getsourcelines(module)
    834 
    835         fre = re.compile(r'\s*def\s+(t_[a-zA-Z_0-9]*)\(')

c:\python27\lib\inspect.pyc in getsourcelines(object)
    688     original source file the first line of code was found.  An IOError is
    689     raised if the source code cannot be retrieved."""
--> 690     lines, lnum = findsource(object)
    691 
    692     if ismodule(object): return lines, 0

c:\python27\lib\inspect.pyc in findsource(object)
    524     is raised if the source code cannot be retrieved."""
    525 
--> 526     file = getfile(object)
    527     sourcefile = getsourcefile(object)
    528     if not sourcefile and file[:1] + file[-1:] != '<>':

c:\python27\lib\inspect.pyc in getfile(object)
    401         if hasattr(object, '__file__'):
    402             return object.__file__
--> 403         raise TypeError('{!r} is a built-in module'.format(object))
    404     if isclass(object):
    405         object = sys.modules.get(object.__module__)

TypeError: <module '__main__' (built-in)> is a built-in module

我在 Python 2 和 3 中复制粘贴了该代码,运行良好,但唯一的问题是我收到了一个警告:WARNING: No t_error rule is defined - idjaw
2个回答

13

您正在尝试在某种REPL(猜测是ipython)中运行ply

出于某种原因,这不起作用。 Ply要求语法必须是一个模块,这意味着它必须在文件中。错误明确指出语法源没有关联的文件。


我认为这应该是被接受的答案,因为它陈述了与被接受的答案相同的内容 - 这也是由提问者自己回答的。 - Nasik Shafeek
有没有办法在Jupyter笔记本中修复这个问题? - EmmanuelMess
@EmmanuelMess:我不知道有没有。不过我认为Sly在笔记本电脑上应该可以工作。 - rici

5
结果表明,问题在于我是通过iPython笔记本运行代码,由于某种原因它不喜欢这样做。将代码保存为普通的.py文件,并通过命令提示符运行,就没有出现错误!
顺便说一句,如果有人能够详细说明代码为什么无法在iPython笔记本环境中运行,我将不胜感激!

2
是的,这就是我在回答中所说的。“Ply坚持认为语法应该是一个模块,这意味着它必须在一个文件中。”iPython笔记本不是一个文件。文件有名称并存储在文件系统上。我不知道为什么Ply有这个限制——我猜这与缓存解析表有关——但它确实存在。 - rici
看起来有点奇怪... 有什么快速的编辑方法可以在 REPL 中使其工作吗? - John Drinane
@JohnDrinane:我不知道。 - rici

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接