On the granularity of linguistic reuse

Francesco Bertolotti, Walter Cazzola, Luca FavalliJ. Syst. Softw 2023 · DOI

Abstract

Programming language evolution often leads to a "maintenance nightmare" as developers struggle to manage the complex ecosystems of compilers, debuggers, and IDEs. This paper proposes a 10-level taxonomy of linguistic reuse to address this complexity. By implementing two new fine-grained operators—desugaring and delegation—within the Neverlang workbench, the authors demonstrate how to eliminate "clone-and-own" coding. Their evaluation shows that these operators can halve the lines of code required for new features while significantly boosting maintainability.

Introduction: The Evolution Challenge

Programming languages like ECMAScript (JavaScript) evolve rapidly, with new features added annually. However, building and maintaining the tools that support these languages is incredibly difficult. While Language Workbenches help manage this complexity, they often lack mechanisms for fine-grained reuse. When developers want to add overlapping features—such as the ||= operator—they frequently resort to "clone-and-own": copy-pasting code from existing components. This practice results in code duplication, making the language harder to update and more prone to bugs.

A Taxonomy of Reuse

The authors expand the existing Erdweg taxonomy for language composition, identifying six new levels of granularity for a total of ten. These range from coarse-grained language unification to fine-grained action extension. Two critical operators introduced to bridge the "granularity gap" are:

  1. Desugaring (Action Composition): This involves rewriting a complex Abstract Syntax Tree (AST) node into simpler, existing ones.
  2. Delegation (Action Extension): This allows a new semantic action to call an existing one, similar to a super() call in object-oriented programming.

Conceptualizing Desugaring

Desugaring is particularly powerful because it ensures that improvements or fixes in "base" features automatically propagate to "sugared" features. By transforming a new, complex syntax into established primitives, developers can leverage existing logic without duplication.

AST transformation showing the desugaring of the logical OR assignment operator into a standard assignment and logical OR.

Implementation in Neverlang

The authors implemented these operators in Neverlang, a modular language workbench. This allows developers to define language features as reusable modules that can be composed opportunistically. The following code snippet illustrates how the OrAssignment operator is defined using desugaring (tree-rewriting) logic.

Neverlang implementation of the OrAssignment operator using the tree-rewrite (desugaring) operator.

Evaluation: The ECMAScript Case Study

To test their approach, the authors implemented 10 features from ECMAScript 2015+ (ES6+), such as ArrowFunction, Const, and NumericSeparators. They compared three strategies: traditional "clone-and-own," Desugaring, and Delegation.

Key Results

The quantitative evaluation demonstrated a clear advantage for explicit reuse operators:

  • Code Reduction: Features implemented with Delegation or Desugaring required approximately 23-24 Lines of Code (LoC), compared to 46 LoC for clone-and-own—a 50% reduction.
  • Improved Maintainability: The Maintainability Index (MI) rose from a "moderate" 73.57 for clone-and-own to over 88 for the new operators, placing them in the "highly maintainable" category.
  • Logic Reuse: For Delegation, the "reuse percentage" of semantic actions reached as high as 74.46%.

Discussion and Future Work

While fine-grained reuse offers significant benefits, it introduces new challenges. Desugaring can break the link between source code and the AST, potentially complicating IDE features like breakpoints. Furthermore, Delegation makes static analysis more complex due to dynamic dependencies between features.

Future work should focus on "IDE-aware" desugaring that preserves source mapping and developing tools to help developers identify reuse opportunities automatically during the design phase. By supporting all 10 levels of the taxonomy, language workbenches can move toward a more sustainable and modular future for language engineering.

Summary updated

BibTeX
@article{bertolotti2023granularity,
  author = {Francesco Bertolotti and Walter Cazzola and Luca Favalli},
  title = {{On the granularity of linguistic reuse}},
  journal = {J. Syst. Softw.},
  year = {2023},
  doi = {10.1016/j.jss.2023.111704},
}
Figure