问题描述
我需要编写一个工具,可以采取C code和投入的编译指示上的某些功能之上。这编译器框架是最容易做这样的工作。此外,如果你能提供一个例子,我真的AP preciate它。
I need to write a tool that can take a C code and put pragmas on top of some functions. Which compiler framework is the easiest to do such a task. Also if you can provide an example, I would really appreciate it.
推荐答案
如果你想可靠地做到这一点,你需要一个完整的C前端,修改解析的code的能力。
If you want to do this reliably, you need a full C front end, and the ability to modify parsed code.
我们的也许可以做你想做的。 DMS可以解析,建立的AST,并开展原文自定义转换,无论是程序上还是作为表面语法转换。
Our DMS Software Reengineering Toolkit with its C Front End can probably do what you want. DMS can parse, build ASTs, and carry out custom transformations on source text, either procedural or as a surface syntax transform.
有一些问题与宏和preprocessor指令;如果解析和保留这些,它在许多情况下这样做,但它expandis这样的指令,他们是不结构化。保留意味着你没有得到一个符号表。如果展开所有的指令,解析后你可以得到一个符号表与C编译器产生相同的内容。
There are some issues with macros and preprocessor directives; if you parse and retain these, it can do so in many cases but it expandis such directives where they are not "structured". Retention means you don't get a symbol table. If you expand all the directives, after parsing you can get a symbol table with the same content that a C compiler produces.
有关OP的具体任务,他希望写一个源源转换类似如下:
For OP's specific task, he'd like write a source to source transform something like the following:
rule decorate_function_definition_with_pragma(fh:function_head, b: block): declaration -> declaration
= " \fh \b " ->
" \fh
#pragma my_pragma
\b "
if some_condiiton(fh);
,其中my_pragma是他想要的杂文字基本上是替换和some_condition是定制predicate能过滤为其编译应插入匹配function_headers。
where "my_pragma" is replace essentially by the pragma text he wants, and some_condition is custom predicate that filters matched function_headers for which the pragma should be inserted.
模式对语法树相匹配,因此它不能像不匹配或者sed正则表达式的威力。的秘密,这是该模式的变量引用语法C前端规则;类型function_head的模式变量只能匹配那些树的function_head语法规则(S)能够满足。
The pattern matches against the syntax tree, so it cannot mismatch like sed or a regex might. The secret to this is that the pattern variables reference to grammar rules in the C Front End; a pattern variable of type function_head can only match those trees that the function_head grammar rule(s) can satisfy.
一个人需要一些琐碎的控制逻辑,只为一次遇到的每一个函数定义运行这个转换。
One needs some trivial control logic to run this transformation just once for each encountered function definition.
这篇关于如何自动插入编译指示在你的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!