如何实现标准C函数提取?
我有一个“a $$ 中的痛苦”任务来提取/解析 main() 函数中调用的所有标准 C 函数。例如: printf、fseek 等...
目前,我唯一的计划是读取 main() 中的每一行,并通过检查我也将定义的标准 C 函数列表来搜索标准 C 函数是否存在(#define CFUNCTIONS "printf...")
如您所知,有很多标准 C 函数,因此定义所有这些函数会很烦人。
关于如何检查字符串是否是标准 C 函数的任何想法?
I have a "a pain in the a$$" task to extract/parse all standard C functions that were called in the main() function. Ex: printf, fseek, etc...
Currently, my only plan is to read each line inside the main() and search if a standard C functions exists by checking the list of standard C functions that I will also be defining (#define CFUNCTIONS "printf...")
As you know there are so many standard C functions, so defining all of them will be so annoying.
Any idea on how can I check if a string is a standard C functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您听说过 cscope,请尝试查看它生成的数据库。 cscope 前端有可用的指令来列出给定函数调用的所有函数。
如果您查看 main() 的调用列表,您应该能够大大缩小您的工作范围。
如果您必须手动解析,我建议从包含的标准标头开始。它们应该能让您清楚地了解您希望在 main() 中看到哪些函数。
不管怎样,这项工作听起来不平凡而且有趣。
If you have heard of cscope, try looking into the database it generates. There are instructions available at the cscope front end to list out all the functions that a given function has called.
If you look at the list of the calls from main(), you should be able to narrow down your work considerably.
If you have to parse by hand, I suggest starting with the included standard headers. They should give you a decent idea about which functions could you expect to see in main().
Either way, the work sounds non-trivial and interesting.
乍一看,解析 C 源代码似乎很简单,但正如其他人指出的那样,程序员有可能通过使用
#define
和#include
摆脱束缚是比较常见的。除非知道要解析的特定程序在文本替换方面是温和的,否则解析任意 C 源代码的复杂性是相当大的。考虑较少使用但更有效的解析对象模块的策略。编译源模块,但不链接它。为了进一步简化,请重新处理包含 main 的文件以删除所有其他函数,但在其位置保留声明。
根据要求,有两种方法可以完成任务:
nm
列出带有U
的外部引用。Parsing C source code seems simple at first blush, but as others have pointed out, the possibility of a programmer getting far off the leash by using
#define
s and#include
s is rather common. Unless it is known that the specific program to be parsed is mild-mannered with respect to text substitution, the complexity of parsing arbitrary C source code is considerable.Consider the less used, but far more effective tactic of parsing the object module. Compile the source module, but do not link it. To further simplify, reprocess the file containing main to remove all other functions, but leave declarations in their places.
Depending on the requirements, there are two ways to complete the task:
nm
lists external references with aU
.该任务乍一看可能很简单,但为了真正 100% 确定,您需要解析 C 文件。仅仅查找名称是不够的,您还需要知道上下文,即何时检查 id,首先当您确定 id 是一个函数时,您可以检查它是否是标准的 c 运行时函数。
(而且我想这会让任务变得更有趣:-)
The task may look simple at first but in order to be really 100% sure you would need to parse the C-file. It is not sufficient to just look for the name, you need to know the context as well i.e. when to check the id, first when you have determined that the id is a function you can check if it is a standard c-runtime function.
(plus I guess it makes the task more interesting :-)
我认为没有任何方法可以绕过必须定义标准 C 函数列表来完成您的任务。但比这更烦人的是——考虑一下宏,
例如:
因此,您可能希望在检查之前通过预处理器运行代码
哪些函数是从 main() 调用的。那么你可能会遇到这样的情况:
所以你可能需要一个成熟的解析器来严格执行此操作,因为字符串文字
可以跨越多条线。
我不会提出三字母组合……那只是毫无意义的虐待狂。 :-) 不管怎样,祝你好运,编码愉快!
I don't think there's any way around having to define a list of standard C functions to accomplish your task. But it's even more annoying than that -- consider macros,
for example:
So you'll probably want to run your code through the preprocessor before checking
which functions are called from main(). Then you might have cases like this:
So you'll probably need a full-blown parser to do this rigorously, since string literals
may span multiple lines.
I won't bring up trigraphs...that would just be pointless sadism. :-) Anyway, good luck, and happy coding!