PLY LEX和YACC的问题

编程入门 行业动态 更新时间:2024-10-10 23:16:13
本文介绍了PLY LEX和YACC的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am trying to run the first part of a simple example of the PLY but I encounter a strange error. When I run the following code, it gives me an error regarding lex.lex() Anyone knows what the problem is?

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 = repile(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.

You are trying to run ply from some kind of REPL (ipython, at a guess).

无论出于何种原因,这都不起作用. Ply坚持认为语法是一个模块,这意味着它必须在文件中.该错误恰好表明没有与语法源关联的文件.

For whatever reason, that won't work. Ply insists that the grammar be a module, which means it must be in a file. The error precisely indicates that there was no file associated with the grammar source.

更多推荐

PLY LEX和YACC的问题

本文发布于:2023-11-28 06:00:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PLY   LEX   YACC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!