我通过pip在CentOS 7上设置pyparser时看到以下错误
/usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-PMzCYU/pycparser/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-0bpBrX-record/install-record.txt --single-version-externally-managed --compile
Traceback (most recent call last):
File "", line 1, in init.py", line 12, in
import setuptools.version
File "/usr/lib/python2.7/site-packages/setuptools/version.py", line 1, in
import pkg_resources
File "/usr/lib/python2.7/site-packages/pkg_resources/init.py", line 72, in
import packaging.requirements
File "/usr/lib/python2.7/site-packages/packaging/requirements.py", line 59, in
MARKER_EXPR = originalTextFor(MARKER_EXPR())("marker")
TypeError: call() takes exactly 2 arguments (1 given)
我在我的C文件中有这个代码:
printf("Worker name is %s and id is %d", worker.name, worker.id);
Run Code Online (Sandbox Code Playgroud)
我希望,使用Python,能够解析格式字符串并找到"%s"
和"%d"
.
所以我想要一个功能:
>>> my_function("Worker name is %s and id is %d")
[Out1]: ((15, "%s"), (28, "%d))
Run Code Online (Sandbox Code Playgroud)
我试图使用libclang的Python绑定和pycparser来实现这一点,但我没有看到如何使用这些工具完成.
我使用正则表达式来解决这个也试过,但是这不是简单-想使用情况下,当printf
有"%%s"
和这样的东西.
gcc和clang显然都是编译的一部分 - 没有人将这个逻辑导出到Python?
我需要获取功能块(定义和所有内容,而不仅仅是声明),以便获取功能依赖关系图。从功能依赖关系图中,确定连接的组件并模块化我庞大的C代码库,一次仅一个文件。
问题:我需要一个C解析器来识别功能块,仅此而已。我们有自定义类型等,但签名去了
storage_class return_type function_name ( comma separated type value pairs )
{
//some content I view as generic stuff
}
Run Code Online (Sandbox Code Playgroud)
我想出的解决方案:显然,像任何理智的人一样使用sly和pycparser。
pycparser的问题:需要从其他文件编译预处理器,只是为了识别代码块。就我而言,事情深入到6个层次。很抱歉,我无法显示实际代码。
尝试使用Sly进行代码:
from sly import Lexer, Parser
import re
def comment_remover(text):
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return " " # note: a space and not an empty string
else:
return s
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)
class CLexer(Lexer):
ignore = ' \t\n'
tokens = {LEXEME, PREPROP, FUNC_DECL,FUNC_DEF,LBRACE,RBRACE, SYMBOL}
literals …
Run Code Online (Sandbox Code Playgroud) 我有一组用"A"语言编写的文件,需要翻译成语言"B"的相应文件.我想创建一个可以自动执行此任务的程序/解析器(可能是工具链而不是单个程序).但是,我正在努力为我的工具链的程序找到合适的选择.
语言A是嵌入式软件代码,即低级语言.它是90%标准C代码和10%"自定义"代码,即文件还包含标准C编译器无法理解的小段.90%的C代码不是 C中可能的任何随机C构造(这对于语义很难解析),而是遵循某些重复的表达式,动作和模式.并且它总是以(或多或少)相同的方式遵循这些模式.它主要对内存执行写操作,不包括复杂的结构,如C-struct或enum等.
语言A中常规低级C代码的示例:
#define MYMACRO 0x123
uint32_t regAddr;
regAddr = MYMACRO;
*(uint32_t*)(regAddr) = 0xdeadbeef;
Run Code Online (Sandbox Code Playgroud)
语言A中"自定义代码"的示例:
custom_printf("hello world! Cpu number: %d \n", cpu_nr);
Run Code Online (Sandbox Code Playgroud)
语言B是100%自定义语言.此转换是必要的,以便在另一个工具中使用该文件进行调试.上面例子的翻译看起来大致如下:
definemacro MYMACRO 0x123
define_local_int regAddr
localint.set regAddr = MYMACRO
data.write regAddr 0xdeadbeef
Run Code Online (Sandbox Code Playgroud)
注意:我很清楚Stackoverflow并不是一个关于"您更喜欢哪种工具?"的公开讨论的网站.但我认为这个问题更像是"我至少需要一个能够完成工作的有意义的工具集",也就是说,无论如何,可能没有那么多明智的讨论选择.
到目前为止,这些是我的考虑和方法:
有人可以给我一些关于这项任务的一套有意义的工具的建议吗?
编辑: 如果这似乎是一个模糊的问题,我道歉,我试着尽可能准确地说出来.我为语言A和B添加了一个示例,以使语言的组成更加清晰,并且为了表明语言A遵循某些重复出现的模式,这些模式可以很容易地理解语义.
如果这个编辑没有提高清晰度和广度,我将按照建议重新发布给程序员.
编辑2:好的,由于这个主题似乎仍然在这里被取消,我在这里撤回了这个问题.我已经从前几张海报中收到了一些有价值的信息,这些信息鼓励我使用通用解析器生成器进行进一步的实验.
当我试图使用pycparser
注释解析文件时,我得到了ParseError
import pycparser
parser = pycparser.CParser()
parser.parse("int main(void){return 0;}")
parser.parse("/* comment */ int main(void){return 0;}")
Traceback (most recent call last):
File "test_pycparser.py", line 18, in <module> parser.parse("/* comment */ int main(void){return 0;}")
File "build\bdist.win32\egg\pycparser\c_parser.py", line 124, in parse
File "build\bdist.win32\egg\pycparser\ply\yacc.py", line 265, in parse
File "build\bdist.win32\egg\pycparser\ply\yacc.py", line 1047, in parseopt_notrack
File "build\bdist.win32\egg\pycparser\c_parser.py", line 1423, in p_error
File "build\bdist.win32\egg\pycparser\plyparser.py", line 54, in _parse_error
pycparser.plyparser.ParseError: :1:1: before: /
Run Code Online (Sandbox Code Playgroud)
解决方案:pycparser
在当前版本中不支持源代码中的注释,但是这个fork允许它,或者您可以使用来自问题Python片段的配方来删除C和C++注释以从源代码中删除注释.
import pycparser …
Run Code Online (Sandbox Code Playgroud) 我正在使用最新版本的诗歌(1.1.10),在尝试更新我的锁定文件时,我在尝试安装 pycparser==2.20 时遇到了错误:
\nWriting lock file\n\nPackage operations: 65 installs, 0 updates, 0 removals\n\n \xe2\x80\xa2 Installing pycparser (2.20)\n\n ValueError\n\n File \\C:\\Users\\user\\AppData\\Local\\pypoetry\\Cache\\artifacts\\3b\\b4\\2e\\87ea123a592747e6f6c798c66837974694db0bca4783357f71718a538d\\pycparser-2.20-py2.py3-none-any.whl does not exist\n\n at c:\\users\\boris\\miniconda3\\envs\\project\\lib\\site-packages\\poetry\\core\\packages\\file_dependency.py:40 in __init__\n 36\xe2\x94\x82 except FileNotFoundError:\n 37\xe2\x94\x82 raise ValueError("Directory {} does not exist".format(self._path))\n 38\xe2\x94\x82\n 39\xe2\x94\x82 if not self._full_path.exists():\n \xe2\x86\x92 40\xe2\x94\x82 raise ValueError("File {} does not exist".format(self._path))\n 41\xe2\x94\x82\n 42\xe2\x94\x82 if self._full_path.is_dir():\n 43\xe2\x94\x82 raise ValueError("{} is a directory, expected a file".format(self._path))\n
Run Code Online (Sandbox Code Playgroud)\n我尝试更改我的诗歌版本以及 pycparser 的版本,但这似乎只会破坏其他软件包。以前有人处理过这个问题吗?如果是的话,你能给我指出正确的方向吗?
\n规格:\n操作系统:Windows 10\nPython 版本:3.6\npoetry==1.1.10\npycparser==2.20
\nC语句的作用是什么?
i=!({ printf("%d\n",r); });
Run Code Online (Sandbox Code Playgroud)
我和r是整数.
我正在尝试使用pycparser解析它,它无法识别它并引发错误:
pycparser.plyparser.ParseError :: 7:6:before:{
谢谢
我需要在预处理的 C 代码('gcc -E' 产生的结果)上使用 pycparser。但是,我目前遇到了无法理解或解决的问题。
我正在使用提供的示例 year2.c 和 func_defs.py,我对其进行了修改以使用各种预处理器和假库,但无济于事。也许你们中的一些人可以研究一下,看看你是否可以重现/解决这个问题。我将附加所有必要的代码。
错误是使用 year2.c(常规示例文件)和 year2.i('gcc -E' 输出)生成的。后者没有可用的结果,而前者同时使用预处理器/fakelib 变体。
我创建了一个包含所有相关错误的 bitbucket 存储库、使用的脚本(尽管只是它的最后一个变体)以及 year2.c 和 year2.i 文件。
谢谢你的时间。
pycparser ×8
c ×5
python ×4
bison ×1
centos7 ×1
clang ×1
parsing ×1
pip ×1
preprocessor ×1
python-2.7 ×1
python-3.6 ×1
python-3.x ×1
regex ×1