Skip to main content
Log in

Constructing format-preserving printing from syntax-directed definitions

基于语法制导定义构造保留格式的反语法分析

  • Research Paper
  • Published:
Science China Information Sciences Aims and scope Submit manuscript

Abstract

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为例, 将算法扩展到了那些不通过语法制导定义生成代码的树形结构语言。

This is a preview of subscription content, log in via an institution to check access.

Access this article

Price excludes VAT (USA)
Tax calculation will be finalised during checkout.

Instant access to the full article PDF.

Similar content being viewed by others

References

  1. Van De Wanter M L. Preserving the documentary structure of source code in language-based transformation tools. In: Proceedings of 1st IEEE International Workshop on Source Code Analysis and Manipulation, Florence, 2001. 131–141

    Google Scholar 

  2. Erdweg S, Rendel T, Kästner C, et al. Layout-sensitive generalized parsing. Lect Notes Comput Sci, 2013, 7745: 244–263

    Article  Google Scholar 

  3. Teltelman W, Goodwin J W, Bobrow D G. Interlisp reference manual. Xerox Palo Alto Research Centers, 1978

    Google Scholar 

  4. Sandewall E. Programming in an interactive environment: the “Lisp” experience. ACM Comput Surv, 1978, 10: 35–71

    Article  MATH  Google Scholar 

  5. Fritzson P. Towards a distributed programming environment based on incremental compilation. Dissertation for the Doctoral Degree. Linkopings Universitet, 1984

    Google Scholar 

  6. Fritzson P. Symbolic debugging through incremental compilation in an integrated environment. J Syst Softw, 1983, 3: 285–294

    Article  Google Scholar 

  7. Jonge M D, Visser E. An algorithm for layout preservation in refactoring transformations. Lect Notes Comput Sci, 2012, 6940: 40–59

    Article  Google Scholar 

  8. Brown C. Tool Support for Refactoring Haskell programs. Dissertation for the Doctoral Degree. Canterbury: University of Kent, 2008

    Google Scholar 

  9. Kumar R, Talton J O, Ahmad S, et al. Flexible tree matching. In: Proceedings of 22nd International Joint Conference on Artificial Intelligence. Reston: AAAI Press, 2011. 2674–2679

    Google Scholar 

  10. Fischer S, Hu Z J, Pacheco H. “Putback” is the essence of bidirectional programming. Technical Report GRACE-TR 2012-08, GRACE Center, National Institute of Informatics, 2012

    Google Scholar 

  11. Vaucher J G. Pretty-printing of trees. Softw Pract Exp, 1980, 10: 553–561

    Article  MATH  Google Scholar 

  12. Merijn D J. Pretty-printing for software reengineering. In: Proceedings of the International Conference on Software Maintenance, Montréal, 2002. 550–559

    Google Scholar 

  13. Neamtiu I, Foster J S, Hicks M. Understanding source code evolution using abstract syntax tree matching. ACM SIGSOFT Softw Eng Notes, 2005, 30: 1–5

    Article  Google Scholar 

  14. Foster J N, Greenwald M B, Moore J T, et al. Combinators for bidirectional tree transformations: a linguistic approach to the view-update problem. ACM Trans Program Lang Syst, 2007, 29: 17

    Article  Google Scholar 

  15. Aho A V, Lam M S, Sethi R, et al. Compilers: Principles, Techniques, and Tools. 2nd ed. New York: Addison-Wesley, 2006

    Google Scholar 

  16. Donnelly C, Stallman R M. Bison Manual: Using the YACC-compatible Parser Generator. 7th ed. Free Software Foundation, 2002

    Google Scholar 

  17. Visser E. Syntax definition for language prototyping. Dissertation for the Doctoral Degree. Amsterdam: University of Amsterdam, 1997

    Google Scholar 

  18. Vaucher J G, Klint P, Tip F. Origin tracking. J Symb Comput, 1993, 15: 523–545

    Article  Google Scholar 

  19. Wang L C. Constructing format-preserving printing from syntax-directed definition. Dissertation for the Master Degree. Shanghai: Shanghai Jiao Tong University, 2015

    Google Scholar 

  20. Danvy O. Functional unparsing. J Funct Program, 1998, 8: 621–625

    Article  MATH  Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to GuoQiang Li.

Rights and permissions

Reprints and permissions

About this article

Check for updates. Verify currency and authenticity via CrossMark

Cite this article

Wang, L., Li, G. & Hu, Z. Constructing format-preserving printing from syntax-directed definitions. Sci. China Inf. Sci. 58, 1–14 (2015). https://doi.org/10.1007/s11432-015-5368-9

Download citation

  • Received:

  • Accepted:

  • Published:

  • Issue Date:

  • DOI: https://doi.org/10.1007/s11432-015-5368-9

Keywords

关键词

Navigation