簡體   English   中英

PLY LEX和YACC的問題

[英]Problems with PLY LEX and YACC

我試圖運行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

你試圖從某種REPL( ipython ,猜測)運行ply

無論出於何種原因,這都行不通。 Ply堅持認為語法是一個模塊,這意味着它必須在一個文件中。 該錯誤精確地表明沒有與語法源相關聯的文件。

事實證明,問題是我通過iPython Notebook運行代碼,並且由於某種原因它不喜歡它。 將代碼保存為常規.py文件並通過命令提示符運行它並且沒有發生錯誤!

PS我很感激,如果有人能詳細說明為什么代碼不能在iPython Notebook環境中運行!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM