有没有指定自动code修改任何语言? [英] Is there any language to specify automatic code modifications?

查看:330
本文介绍了有没有指定自动code修改任何语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一些工作,我需要能够描述修改一些程序code,它是自动完成。

I'm doing some work where I need to be able to describe modifications to some program code that are to be done automatically.

有没有让来描述这种任何语言?

Is there any language that allows to describe this?

语言应该具有在code其中所述修饰是必须要做的,并应允许指定的可能的修改来完成接收位置的模块或功能。结果
它应该允许描述的修改,如删除特定功能,增添周围一片code的if条件,增加一个新的函数声明,什么也不做,等结果
修改应在解析树,以便能够恢复到原来的code,仅修改来完成。结果
我甚至都不需要的语言有一个解析器或实施相关的,我需要的是语言本身的描述,无论是作为BNF语法甚至是非正式的。

The language should have modules or functions that receive the location in the code where the modification is to be done and should allow specifying the possible modifications to be done.
It should allow describing modifications such as removing a given function, adding an if condition around a piece of code, adding a new function declaration that does nothing, etc.
The modifications should be done over the parse tree so it is possible to restore the original code, only with the modifications.
I don't even need the language to have a parser or an implementation associated, all I need is the description of the language itself, either as a BNF grammar or even informally.

我知道PHC,提前时间编译器的PHP,是能够转换源$ C ​​$ C成XML再presentation和背部,使其更容易修改code和恢复。
我需要的是描述实际修改到XML,这样我可以运行一个程序,例如可以删除特定的函数调用的所有实例,或添加如果(假)围绕每个的方式。
此外,它会更好,如果语言是语言无关,虽然它不是必需的。

I know that phc, the PHP ahead of time compiler, is able to transform the source code into a XML representation and back, making it easier to modify the code and restore it. What I need is a way to describe the actual modifications to the XML so that I can run a program that can for example remove all instances of a specific function call, or add if(false) around each. Also, it would be better if the language was language-agnostic, although its not a requirement.

你认为这样的事情存在?

Do you think something like this exists?

推荐答案

的核心思想是程序转换的。的Ondrej与DMS正确的想法,但我DMS的作者,所以我可能会失之偏颇。

The key idea is program transformations. Ondrej has the right idea with DMS but I'm the author of DMS so I'm likely biased.

用于完成转换的DMS语言被称为(DMS)规则规范语言,或RSL,和用于指定(程序变换)的规则。这样的规则有:

The DMS language used to accomplish transformations is called the "(DMS) Rule Specification Language", or RSL, and is used to specify (program transformation) rules. Such a rule has:


  • 的名称(我们往往有很多人,这是指他们的简便方法)

  • (定义模式变量),参数会根据感兴趣的目标语言的语法类型,

  • 左手比赛这一模式

  • 右手模式这个替换

该模式通常写在目标语言的表面语法的,就是语言是与扩展模式变量变换的本地语法。区分
从目标的langauge的RSL语言的语法,图案都写里面(元)报价...。内部模式的\\字符是(元)逃回RSL。一个模式变量是写\\ X。 A(元)函数foobar的
写为\\ foobar的(......),注意在(元)功能argumements的(元)逃脱。报价之外,还需要元逃逸而这些结构都没有\\写的,例如,foobar的(...)。

The patterns are often written in the surface-syntax of the target language, that is the native syntax of the language being transform with extensions for pattern variables . To distinguish the RSL language syntax from the target langauge, patterns are written inside (meta) quotes "...". THe \ character inside patterns is a (meta)escape back into RSL. A pattern variable is written "\x". A (meta)function foobar is written as \foobar( ... ), note the (meta)escape on the (meta)functions argumements. Outside of the quotes, the meta-escapes are needed and these construct are written without \, e.g., foobar(...).

DMS规则可以有很多比这更复杂,但这些都是基本知识。表面语法模式不重新present文本;相反,他们真的重新​​present code相当于在AST的模式。 DMS规则用于匹配,改变的AST。当然程序变换系统必须具有解析器产生的AST,以及抗解析器(prettyprinters),以AST的转换回文本。 (DMS已的langauge面前的一大库对地球和很多生僻的人在所有广泛使用的汉语语言结束,我们只是增加了腮腺炎)。

DMS rules can be a lot more complex than this, but these are the basics. The surface-syntax patterns do not represent text; rather, they really represent the equivalent ASTs of code in the patterns. DMS rules are used to match and change ASTs. The program transformation system of course has to have parsers to produce ASTs, and anti-parsers ("prettyprinters") to convert ASTs back to text. (DMS has a big library of langauge front ends for all the widely used langauges on the planet and a lot of the uncommon ones; we just added MUMPS).

有关您的具体的例子,下面的规则将做的伎俩:

For your specific examples, the following rules will do the trick:

删除......一个给定的功能:

"... removing a given function":

rule remove_function(f:IDENTIFIER,p:parameters,b:body): declarations -> declarations
  "  \f \p \b " -> " ; "  -- replace function delcaration by empty declation
  if f==target_function_name();

...添加如果周围code块条件:

... adding an if condition around a block of code:

rule wrap_in_if(s:statement): statement -> statement
 " \s " ->  " \if ( \generated_condition\(\) ) \s ";

...增加一个新的函数声明不做任何事情:

... adding a new function declaration that does nothing:

rule insert_noop_function(d:declarations): delcarations -> declarations
" \d " -> " \target_function\name\(\) ( )  { } ";

正如你所观察到的,你要的的这些地方;这是一个元程序,它位于凡在你AST你想要应用规则的作业,然后它适用于他们。对于你的规则,你需要(与DMS)和明确的程序性的方法来找到合适的位置。对于一些DMS的规则,你可以简单地再申请无处不在; DMS将主要走全国各地指定的AST和应用规则给你。

As you have observed, you have to point these somewhere; that's the job of a "metaprogram" which locates where in you AST you want the rules applied, and then it applies them. For your rules, you need (with DMS) and explicit procedural method to find the right location. For some DMS rules, you can simply apply then "everywhere"; DMS will essentially walk all over the AST designated and apply the rules for you.

若干规则从不非常即时pressive,以同样的方式,$ C $的c数行不是即时pressive。几百或几千规则可以做pretty壮观的东西(如完整的langauge翻译),以同样的方式,几百几千行code可产生pretty有趣的结果。不同的是,传统的code可与数字,字符串和结构,以及程序转换工具计算过的程序结构(AST的)。

Several rules are never very impressive, in the same way that several lines of code aren't impressive. A few hundred or thousand rules can do pretty spectacular things (like complete langauge translations), in the same way that few hundred or thousand lines of code can produce pretty interesting results. The difference is that conventional code works with numbers, strings and structures, and program transformation tools compute over program structures (ASTs).

有一个完整的样例展示如何定义的语言和规则DMS,以及这些规则如何应用才达到程序修改(这个例子实际上是修改代数前pressions但这些想法是完全一样的)。

There's a complete worked example showing how one defines a language and rules to DMS, and how those rules are applied to achive "program modifications" (the example actually modifies "algebraic expressions" but the ideas are exactly the same).

DMS是不加掩饰的商业,这是不是一个dimestore工具,因此它可能不是您所需要的你的论文。

DMS is unabashedly commercial, and it isn't a dimestore tool, so it might not be what you need for your thesis.

如果不DMS,你可以得到有同样想法的免费工具。考虑TXL(www.txl.ca)或StrategoXt(www.strategoxt.org)。 DMS,通心络,西洋陆军棋都做程序的转换采用表面语法模式,但通心络和西洋陆军棋不能处理到code以及巨大变化DMS恕我直言。 (阅读有关流量分析的DMS的网站由于某些原因)。通心络和西洋陆军棋有利于学习的基础知识,并建立强大的演示,虽然。

If not DMS, you can get free tools that have the same ideas. Consider TXL (www.txl.ca) or StrategoXt (www.strategoxt.org). DMS, TXL, Stratego all do program transformations using surface syntax patterns, but TXL and Stratego can't handle massive changes to code as well as DMS IMHO. (Read about flow analysis at the DMS website for some reasons). TXL and Stratego are good for learning the basics and build strong demos, though.

这篇关于有没有指定自动code修改任何语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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