自动源代码编辑工具

发布于 2024-10-10 17:52:48 字数 566 浏览 3 评论 0原文

我正在开展一个研究项目,自动修改代码以包含高级数学概念(例如将随机效应添加到循环中或使用添加更高级物理模型的新函数封装现有函数)。

我向社区提出的问题是:有没有什么好的工具可以直接操作源代码?我想做一些事情,比如

  • 交换函数、
  • 在需要的地方添加变量声明
  • 、确定函数是否乘以任何值
  • 确定在一行代码上调用哪些函数
  • 查看将哪些参数传递给函数并用替代方案替换它们
  • 在某些代码行上引入新的函数调用
  • 尽可能保持代码的其余部分不变并写出

我不想要的 结果为了实际编译代码,我只想了解使用了哪些符号,以语法正确的方式替换和添加,并能够在正确的位置声明变量。

我一直在使用最小的弹性/野牛方法并取得了一些成功,但我不认为它是强大的。我讨厌编写一个完整的语言解析器,只是为了在行尾或函数顶部添加一些新信息。看起来这几乎就是所需要的,但似乎应该已经有一些工具可以完成这些类型的操作。

要更改的代码有多种语言,但我对 FORTRAN 特别感兴趣。

有什么想法吗?

I'm working on a research project to automatically modify code to include advanced mathematical concepts (like adding random effects into a loop or encapsulating an existing function with a new function that adds in a more advanced physical model).

My question to the community is: are there are any good tools for manipulating source code directly? I want to do things like

  • Swap out functions
  • Add variable declarations wherever they are required
  • Determine if a function is multiplied by anything
  • Determine what functions are called on a line of code
  • See what parameters are passed to a function and replace them with alternatives
  • Introduce new function calls on certain lines of code
  • Wherever possible just leaving the rest of the code untouched and write out the results

I never want to actually compile the code I only want to understand what symbols are used, replace and add in a syntactically correct way, and be able to declare variables at the right position.

I've been using a minimal flex/bison approach with some success but I do not feel the it is robust. I hate to take on writing a full language parser just to add some new info to the end of a line or the top of a function. It seems like this is almost what is going to be required but it also seems like there should be some tools out there to do these types of manipulations already.

The code to be changed is in a variety of languages, but I'm particularly interested in FORTRAN.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

金兰素衣 2024-10-17 17:52:48

我们的 DMS 软件重组工具包是一个通用程序转换系统,可接受任意语言描述以允许它操纵这些语言。它具有 Fortran、C++、C、Java、C#、COBOL 和许多其他语言。这些前端将源代码解析为编译器数据结构(例如,完整的 AST),并使 AST 能够重新生成为有效的语言源文本,甚至保留注释。 DMS API 允许任意导航/检查/修改 AST,构建基于属性语法的分析器。
DMS 为构建特定于语言的符号表以及控制和数据流分析提供支持机制。最后,对于提供给 DMS 的任何语言,它可以将源模式匹配应用于 AST,以及源到源模式驱动的转换来匹配和修改 AST,其中每个转换都可以通过任意分析谓词来启用。

您的任务之一是找到乘以某个值的函数调用。此 DMS 模式会识别它:

   domain Fortran.
   pattern  match_multiplied_function_call(f: IDENTIFIER, a: arguments, t: term): product
   =   " \f(\a)*\t ";

它与找到相应语法的 AST 相匹配。

DMS 的开发和使用已超过 15 年。它已被用来对非常大的目标软件系统(C 语言 2500 万行、COBOL 1000 万行、Fortran 150 万行等)进行生产分析和转换。

Fortran 前端处理 F77 和 F90,它处理 Fortran 程序中常见的额外内容(F2003、Cray 指针等),甚至处理 Fortran 文本中使用的 C 预处理器指令。

Our DMS Software Reengineering Toolkit is a general purpose program transformation system, that accepts arbitrary language descriptions to allow it to manipulate those languages. It has front ends for Fortran, C++, C, Java, C#, COBOL and many other languages. These front ends parse source code to compiler data structures (e.g., complete ASTs), and enable the ASTs to be regenerated as valid language source text even retaining comments. The DMS APIs allow arbitrary navigation/inspection/modificaton of the ASTs, construction of attribute-grammar based analyzers.
DMS provides support machinery for building language specific symbol tables, as well as control and data flow analysis. Finally, for any language provided to DMS, it can apply source-pattern matches to the AST, as well as source-to-source pattern-driven transformations to match and modify the ASTs, where each transformation can be enabled by an arbitrary analysis predicate.

One of your tasks is to find a function call multipled by something. This DMS pattern would recognize it:

   domain Fortran.
   pattern  match_multiplied_function_call(f: IDENTIFIER, a: arguments, t: term): product
   =   " \f(\a)*\t ";

which matches the AST where the corresponding syntax is found.

DMS has been under development and use for over 15 years. It has been used to carry out production analyses and transformations on very large target software systems (for C, 25 million lines, for COBOL 10 million lines, for Fortran 1.5 million lines, etc.).

The Fortran front end handles F77 and F90, and it handles the usual extra gunk found in Fortran programs (smatterings of F2003, Cray pointers, ...) and even handles C preprocessor directives used inside the Fortran text.

孤独岁月 2024-10-17 17:52:48

我不确定这是否 100% 是您正在寻找的内容,但请查看 ANTLR。有人甚至为其制作了 Fortran 语法

它似乎是处理语言表示的良好环境,并且似乎足够模块化,可以支持您正在讨论的转换。

I am not sure that this is 100% of what you are looking for but check out ANTLR. Someone even made a Fortran grammar for it.

It appears to be a good environment for working with language representations and seems to be modular enough that it might support the transformations you are talking about.

忘东忘西忘不掉你 2024-10-17 17:52:48

就像我的前任回答你的问题一样,我不确定这是否是你正在寻找的(或者甚至它是否会满足你的任何要求),但我知道有 Photran Eclipse 插件。

我不使用 Eclipse,我从未使用过 Photran,但我知道有些人确实使用它,所以我只是想我可以传播这个消息......

Just like my predecessor in replying to your question I am not sure whether this is what you're looking for (or even whether it will meet any of your requirements at all), but I know there is the Photran plugin for Eclipse.

I don't use Eclipse, I have never used Photran, but I know some people who do use it, so I just thought I could spread the word...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文