是否有任何用于模式匹配和重写源代码的软件? [英] Any software for pattern-matching and -rewriting source code?

查看:71
本文介绍了是否有任何用于模式匹配和重写源代码的软件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些旧软件(使用的语言没有死,但对我来说是死的;-),它实现了源代码的基本模式匹配和重写系统.我正在考虑恢复此代码,将其翻译为现代语言,并将该项目作为重构的动力工具进行开源.在进一步介绍之前,我想知道是否已经存在这样的东西(今晚我的google-fu在煽风点火).

I have some old software (in a language that's not dead but is dead to me ;-)) that implements a basic pattern-matching and -rewriting system for source code. I am considering resurrecting this code, translating it into a modern language, and open-sourcing the project as a refactoring power-tool. Before I go much further, I want to know if anything like this exists already (my google-fu is fanning air on this tonight).

这是它的工作方式:

  • 模式匹配部分使用带有绑定变量的模板来匹配跨越多行代码的源代码模式,
  • 模式重写部分使用模板重写匹配的代码,从匹配的模板中插入绑定变量的内容
  • 匹配和重写模板通过简单(无条件)重写规则关联(1:1)

该软件在输入应用程序的抽象语法树(AST)上运行,并输出经过修改的AST,然后可以将其重新生成为新的源代码

the software operates on the abstract syntax tree (AST) of the input application, and outputs a modified AST which can then be regenerated into new source code

,我们发现了一堆while循环,这些循环实际上应该是for循环.以下模板将匹配while循环模式:

for example, suppose we find a bunch of while-loops that really should be for-loops. The following template will match the while-loop pattern:

Template oldLoopPtrn
    int @cnt@ = 0;
    while (@cnt@ < @max@)
    {
        … @body@
        ++@cnt@;
    }
End_Template

,而以下模板将指定输出重写模式:

while the following template will specify the output rewrite pattern:

Template newLoopPtrn
    for(int @cnt@ = 0; @cnt@ < @max@; @cnt@++)
    {
        @body@
    }
End_Template

和关联它们的简单规则

Rule oldLoopPtrn --> newLoopPtrn

看起来像这样的代码

int i=0;
while(i<arrlen)
{
    printf("element %d: %f\n",i,arr[i]);
    ++i;
}

会自动重写为如下形式

for(int i = 0; i < arrlen; i++)
{
    printf("element %d: %f\n",i,arr[i]);
}

我所看到的最接近的东西是一些代码重构工具,但它们似乎是针对选定片段的交互式重写,而不是批量自动更改.

The closest thing I've seen like this is some of the code-refactoring tools, but they seem to be geared towards interactive rewriting of selected snippets, not wholesale automated changes.

我相信这种工具可以增强重构,并且可以在多种语言(甚至HTML/CSS)上运行.我还相信,转换和完善代码库将是一个巨大的项目,我根本无法在任何合理的时间内独自完成该工作.

I believe that this kind of tool could supercharge refactoring, and would work on multiple languages (even HTML/CSS). I also believe that converting and polishing the code base would be a huge project that I simply cannot do alone in any reasonable amount of time.

那么,已经有类似的东西了吗?如果不是,是否要考虑任何明显的功能(除了重写规则条件)?

So, anything like this out there already? If not, any obvious features (besides rewrite-rule conditions) to consider?

我非常喜欢该系统的一个功能是模板模式非常明显并且易于阅读,因为它们是用与目标源代码相同的语言编写的,不是某些深奥的regex/BNF突变格式.

The one feature of this system that I like very much is that the template patterns are fairly obvious and easy to read because they're written in the same language as the target source code, not in some esoteric mutated regex/BNF format.

推荐答案

我正在考虑重新使用此代码,将其翻译为现代语言,并将该项目作为重构的强大工具开源.

I am considering resurrecting this code, translating it into a modern language, and open-sourcing the project as a refactoring power-tool.

我认为免费拥有这样的工具真是太好了.

I think it would be simply great to have such a tool freely available.

但是有一种商业产品可用: DMS软件再造工具包.

But there is a commercial product available: DMS Software Reengineering Toolkit.

DMS Software Reengineering Toolkit是一套工具,用于自动进行定制的源程序分析,修改或翻译或生成软件系统,其中包含语言的任意混合(域"). DMS的软件"一词​​非常广泛,涵盖任何正式符号,包括编程语言,标记语言,硬件描述语言,设计符号,数据描述等.该工具包是实现Design Maintenance System™的第一步,这是对21世纪软件工程环境的雄心勃勃的构想,该环境支持由语义和捕获的设计驱动的大型应用程序系统的增量构建和维护.

The DMS Software Reengineering Toolkit is a set of tools for automating customized source program analysis, modification or translation or generation of software systems, containing arbitrary mixtures of languages ("domains"). The term "software" for DMS is very broad and covers any formal notation, including programming languages, markup languages, hardware description languages, design notations, data descriptions, etc. This toolkit is the first step towards the implementation of The Design Maintenance System™, an ambitious vision of a 21st Century software engineering environment that supports the incremental construction and maintenance of large application systems, driven by semantics and captured designs.

此外,对于C源代码,有一个 coccinelle 工具:

Also, for C source code there is the coccinelle tool:

Coccinelle是一个程序匹配和转换引擎,它提供语言SmPL(语义补丁语言)来指定C代码中所需的匹配和转换. Coccinelle最初的目标是在Linux中执行附带进化.这样的演变包括响应库API的演变而在客户端代码中所需的更改,并且可能包括修改,例如重命名功能,添加其值在某种程度上取决于上下文的功能自变量以及重组数据结构.除了附带的发展之外,Coccinelle(我们和其他人)也成功地用于发现和修复系统代码中的错误.

Coccinelle is a program matching and transformation engine which provides the language SmPL (Semantic Patch Language) for specifying desired matches and transformations in C code. Coccinelle was initially targeted towards performing collateral evolutions in Linux. Such evolutions comprise the changes that are needed in client code in response to evolutions in library APIs, and may include modifications such as renaming a function, adding a function argument whose value is somehow context-dependent, and reorganizing a data structure. Beyond collateral evolutions, Coccinelle is successfully used (by us and others) for finding and fixing bugs in systems code.

这篇关于是否有任何用于模式匹配和重写源代码的软件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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