如何在`GCC`中获取`c`程序的抽象语法树 [英] How to get abstract syntax tree of a `c` program in `GCC`

查看:51
本文介绍了如何在`GCC`中获取`c`程序的抽象语法树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在gcc中获取c程序的抽象语法树?
我正在尝试将 OpenMP pragma 自动插入到输入 c 程序中.
我需要分析嵌套的 for 循环以查找依赖项,以便我可以插入适当的 OpenMP 编译指示.
所以基本上我想做的就是遍历分析输入c程序的抽象语法树.
我如何实现这一目标?

How can I get the abstract syntax tree of a c program in gcc?
I'm trying to automatically insert OpenMP pragmas to the input c program.
I need to analyze nested for loops for finding dependencies so that I can insert appropriate OpenMP pragmas.
So basically what I want to do is traverse and analyze the abstract syntax tree of the input c program.
How do I achieve this?

推荐答案

您需要完整的数据流才能找到依赖项".然后您将需要实际插入 OpenMP 调用.

You need full dataflow to find 'dependencies'. Then you will need to actually insert the OpenMP calls.

您想要的是一个程序转换系统.GCC 可能有依赖信息,但众所周知,它很难用于自定义项目.其他人提到了 Clang 和 Rose.Clang 可能是一个不错的选择,但自定义分析/转换不是它的主要目的.Rose 旨在支持自定义工具,但恕我直言,这是一个在实践中使用的相当复杂的方案,因为它使用了 EDG 前端,该前端并非旨在支持转换.

What you want is a program transformation system. GCC probably has the dependency information, but it is famously difficult to work with for custom projects. Others have mentioned Clang and Rose. Clang might be a decent choice, but custom analysis/transformation isn't its main purpose. Rose is designed to support custom tools, but IMHO is a rather complicated scheme to use in practice because of its use of the EDG front end, which isn't designed to support transformation.

[以下文字已被版主删除.我已将其放回原处,因为它是此任务的有效转换系统之一.我对它负责的事实绝不会降低它作为对 OP 的有用回答的价值.]

[THE FOLLOWING TEXT WAS DELETED BY A MODERATOR. I HAVE PUT IT BACK, BECAUSE IT IS ONE THE VALID TRANSFORMATION SYSTEMS FOR THIS TASK. THE FACT THAT I AM RESPONSIBLE FOR IT IN NO WAY DIMINISHES ITS VALUE AS A USEFUL ANSWER TO THE OP.]

我们的 DMS 软件再造工具包及其 C 前端明确设计为程序转换系统.它具有以合理方式与 AST 相关联的完整数据流分析(包括指向分析、调用图构建和范围分析).它提供了源到源的重写规则,允许对以表面语法形式表示的 AST 进行更改;您可以阅读转换而不是检查一堆程序代码.通过修改后的 AST,DMS 可以重新生成源代码,包括可编译形式的注释.

Our DMS Software Reengineering Toolkit with its C front end is explicitly designed to be a program transformation system. It has full data flow analysis (including points-to analysis, call graph construction and range analyses) tied to the AST in sensible ways. It provides source-to-source rewrite rules enabling changes to the ASTs expressed in surface syntax form; you can read the transformations rather than inspect a bunch of procedural code. With a modified AST, DMS can regenerate source code including the comments in a compilable form.

这篇关于如何在`GCC`中获取`c`程序的抽象语法树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆