继续C语法分析器相关项目的困惑
我的目标是制作一个程序(使用 C++),它将 C 源代码作为输入并检查“SYNTAX ERRORS ONLY”
。
现在为此,我需要了解正则表达式、语法生成和解析器吗?
我想使用像 Yacc/Flex/Bison 这样的工具 - 但我面临的问题是 -
如何使用这些工具?我的意思是,当我读到这些工具时,我只是触及了表面——我感到一无所知。
如何将这些工具与我的 C++ 源代码一起使用?
我该如何开始使用这个?
My target is to make a program (using C++) which would take C source code as input and check for "SYNTAX ERRORS ONLY"
.
Now for this, do i need to know about Regular Expressions, Grammar generation and Parsers??
I would like to use tools like Yacc/Flex/Bison - but the problems i am facing are -
How to use these tools? I mean i am only scratching at the surface when i read about these tools - i feel clueless.
How can i use these tools in tandem with my C++ source code?
How "The Hell" do i Get Started with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用别人的 C 解析器。例如,clang 项目使用的解析器。 http://clang.llvm.org/
然后你可以专注于问题的另一个困难部分:检测错误。
Use somebody else's C parser. For example, the parser used by the clang project. http://clang.llvm.org/
Then you can focus on the other hard part of your problem: detecting errors.
要开始使用 Yacc 和 Lex(或 Gnu 版本、Bison 和 Flex),我可以推荐 Tom Niemann 的 A Compact Guide to莱克斯&亚克。
To get started with Yacc and Lex (or the Gnu versions, Bison and Flex) I can recommend Tom Niemann's A Compact Guide to Lex & Yacc.
我还建议您看看其他做同样事情的项目。它们的名称通常以 lint 命名,如 http://www.splint.org/
I also suggest that you have a look of other projects doing the same thing. The are often named with lint in their name, as http://www.splint.org/
这完全取决于您想要检查哪种错误。
无论如何,您当然需要了解有关编译器体系结构的更多信息。本书是参考http://www.cs.princeton.edu/~appel /modern/c/
如果你想在句法层面上工作,
你当然想与 lex 和 Yacc 一起工作。
此链接可能会帮助您开始使用工作语法(尽管已经过时):http://www.lysator.liu.se/c/ANSI-C-grammar-y.html
可以使用正则表达式来完成不太强大的语法检查。与使用实际解析器相比,使用正则表达式可以做的事情更少(请参阅 http://en.wikipedia.org/wiki /Chomsky_hierarchy)。但它肯定更实用。
如果您想执行高级检查。就像“这组函数总是采用 const 参数吗?”等等...您可能可以使用 GCC 功能来转储抽象语法树(请参阅 http://digitocero.com/en/blog/exporting-and-visualizing-gccs-abstract-syntax-tree-ast)。还检查其他编译器或前端。抽象树包含许多可以“检查”的信息。
如果您想处理编译错误:与类型检查等相关...我无法帮助您,您可能想在开始编写自己的编译器之前查看其他人的项目。
另请参阅:
http://decomp.ulb.ac.be/roelwuyts/playground/canalysistools/
http://wiki.altium。 com/display/ADOH/Static+Code+Analysis+-+CERT+C+Secure+Code+Checking
我以前的实验室里有些人从事C和C++分析和转换
http://www.lrde.epita.fr/cgi-bin/ twiki/view/变形金刚/
该项目现在处于待命状态,即使对于习惯编译器编写的人来说(特别是在 C++ 转换的情况下),也被证明是一个复杂的主题。
最后,您的需求可能比这简单得多。你有没有想过
(然后检查 gcc 的输出)
It all depends on what kind of errors you want to check.
In any cases you certainly need to learn more about compiler architectures. This book is a reference http://www.cs.princeton.edu/~appel/modern/c/
If you want to work at the syntactic level,
you certainly want to work with lex and Yacc.
This link may help you to get started with a working grammar (though outdated): http://www.lysator.liu.se/c/ANSI-C-grammar-y.html
Less powerfull syntax checking can be done using regular expression. You can do less with regular expression than with an actual parser (see http://en.wikipedia.org/wiki/Chomsky_hierarchy). But it is certainly far more practical.
if you want to perform high level checking. Like "Does this group of function alway take const parameters ?" etc ... You can probably use GCC ability to dump abstract syntax trees (see http://digitocero.com/en/blog/exporting-and-visualizing-gccs-abstract-syntax-tree-ast). Checks other compilers or front-end as well. An abstract tree contains many information you can "check".
If you want to handle compilation errors: related to type checking etc... I can't help you, you probably want to look at other people projects before starting to write your own compiler.
see also:
http://decomp.ulb.ac.be/roelwuyts/playground/canalysistools/
http://wiki.altium.com/display/ADOH/Static+Code+Analysis+-+CERT+C+Secure+Code+Checking
Some people in my previous labs worked on C and C++ analysis and transformation
http://www.lrde.epita.fr/cgi-bin/twiki/view/Transformers/
The project is now in standby, and has proved to be a complex subject even for people used to compiler writting (especially in the case of C++ transformation).
Finally your needs are maybe far simpler than this. Did you think about
(and then just checking the output of gcc)
如何定义“仅限语法错误”?如果你只是想知道有什么错误,为什么不调用外部gcc来执行编译并报告错误呢?
How do you define "SYNTAX ERRORS ONLY"? If you just want to know what are the errors, why don't you call external gcc to perform a compilation and report the errors?