Constructing format-preserving printing from syntax-directed definitions

Transformations between source codes, such as refactorings and program analysis, are frequently used in software engineering. Typically, transformations are effectively implemented using an abstract syntax tree (AST) on the origin source code. However, a critical limitation of ASTs is the loss of layout information such as whitespace and comments, which can result in poor readability. To overcome this shortcoming, this paper proposes a bidirectional transformation (BX) method that maintains consistency in the layout between the origin and transformed. First, a section of origin source code will be translated to a concrete syntax tree (CST) that includes layout information. Second, to make the BX practical, a new method is constructed that matches an AST with its respective CST. Finally, to get a reasonable CST, a method to amend the CST is also provided. We prove that the BX is well-behaved, which implies that it satisfies both the PutGet and GetPut laws. Furthermore, we illustrate the correctness of the methodology by treating XML language as a case study.摘要创新点本文提出了一种基于语法制导定义的、符合双向变换机制的算法, 使得代码在经过基于语法树的变换后能够一定程度上保留原本的格式信息。本文提出的算法所构成的双向变换是良行为的, 这保证了它能够有效保持源代码与目标代码之间的一致性。除此之外, 为了保证算法有效性, 论文亦提出了匹配分析树与它对应语法树, 以及用来匹配两棵不同的语法树的新的树的匹配算法。本文提出的方法可以有效的保留基于抽象语法树转换后代码的格式信息, 从而增加目标代码的可读性。最后, 文章以XML为例, 将算法扩展到了那些不通过语法制导定义生成代码的树形结构语言。

[1]  Peter Fritzson Symbolic debugging through incremental compilation in an integrated environment , 1983, J. Syst. Softw..

[2]  Warren Teitelman,et al.  The interlisp reference manual , 1974 .

[3]  Christopher Mark Brown,et al.  Tool support for refactoring Haskell programs , 2008 .

[4]  Erik Sandewall,et al.  Programming in an Interactive Environment: the ``Lisp'' Experience , 1978, CSUR.

[5]  Benjamin C. Pierce,et al.  Combinators for bi-directional tree transformations: a linguistic approach to the view update problem , 2005, POPL '05.

[6]  Alfred V. Aho,et al.  Compilers: Principles, Techniques, and Tools (2nd Edition) , 2006 .

[7]  Olivier Danvy Functional Unparsing , 1998, J. Funct. Program..

[8]  HicksMichael,et al.  Understanding source code evolution using abstract syntax tree matching , 2005 .

[9]  Merijn de Jonge Pretty-Printing for Software Reengineering , 2002, ICSM.

[10]  Michael L. Van de Vanter Preserving the Documentary Structure of Source Code in Language-Based Transformation Tools , 2001, SCAM.

[11]  Jean G. Vaucher Pretty‐printing of trees , 1980, Softw. Pract. Exp..

[12]  Arie van Deursen,et al.  Origin Tracking , 1993, J. Symb. Comput..

[13]  Jeffrey S. Foster,et al.  Understanding source code evolution using abstract syntax tree matching , 2005, MSR.

[14]  Sebastian Erdweg,et al.  Layout-Sensitive Generalized Parsing , 2012, SLE.

[15]  Tim Roughgarden,et al.  Flexible Tree Matching , 2011, IJCAI.

[16]  Eelco Visser,et al.  An Algorithm for Layout Preservation in Refactoring Transformations , 2011, SLE.