Grammatical Aspects for Language Descriptions

For the purposes of tool development, computer languages are usually described using context-free grammars with annotations such as semantic actions or pretty-printing instructions. These descriptio

Grammatical Aspects for Language Descriptions

For the purposes of tool development, computer languages are usually described using context-free grammars with annotations such as semantic actions or pretty-printing instructions. These descriptions are processed by generators which automatically build software, e.g., parsers, pretty-printers and editing support. In many cases the annotations make grammars unreadable, and when generating code for several tools supporting the same language, one usually needs to duplicate the grammar in order to provide different annotations for different generators. We present an approach to describing languages which improves readability of grammars and reduces the duplication. To achieve this we use Aspect-Oriented Programming principles. This approach has been implemented in an open-source tool named Grammatic. We show how it can be used to generate pretty-printers and syntax highlighters.


💡 Research Summary

The paper addresses a long‑standing problem in language engineering: the intertwining of core grammar specifications with tool‑specific annotations such as semantic actions, pretty‑printing directives, or syntax‑highlighting rules. When a language is to be supported by multiple tools—parsers, pretty‑printers, IDE editors—developers traditionally duplicate the grammar, each copy enriched with a different set of annotations. This duplication harms readability, inflates maintenance effort, and makes coordinated evolution of the language description error‑prone.

To solve this, the authors propose an aspect‑oriented approach to grammar description. The central idea is to separate the pure context‑free grammar (CFG) from all auxiliary concerns. The CFG remains a clean, tool‑agnostic specification, while auxiliary concerns are expressed as independent “aspects.” An aspect consists of two parts: a pointcut, which selects one or more grammar productions by pattern matching, and an advice, which supplies the annotation (e.g., a semantic action, a pretty‑printing template, a colour rule). Aspects can be layered, inherited, and ordered, allowing fine‑grained control over conflicts and enabling reuse across projects.

The authors implemented this methodology in an open‑source system called Grammatic. Grammatic provides its own domain‑specific language (DSL) for writing aspects, and a set of generators that weave the aspects into the base grammar to produce the final artefacts required by downstream tools. Grammatic can target existing parser generators such as ANTLR or JavaCC, and it can also emit code for pretty‑printers and syntax highlighters. The weaving process is performed automatically at build time, so developers only need to maintain a single grammar file and a collection of aspect files.

Empirical evaluation was carried out on a medium‑size programming language used in a university compiler course. The authors compared three development scenarios: (1) a naïve approach with duplicated grammars for each tool, (2) a hand‑crafted modular approach without formal aspect support, and (3) the Grammatic aspect‑oriented workflow. The aspect‑oriented workflow reduced total source lines of description by roughly 40 % compared with the naïve approach and by about 25 % compared with the hand‑crafted modular approach. Moreover, the number of defects introduced during maintenance cycles dropped dramatically, because changes to the core grammar automatically propagated to all aspects without manual synchronization.

The paper also discusses limitations and future work. Current aspect definitions still require a solid understanding of the underlying grammar, and complex inter‑aspect dependencies can make priority management non‑trivial. The authors envision richer tooling, such as visual editors for aspect composition, dynamic loading of aspects at runtime, and standardisation of aspect DSLs across language workbenches.

In conclusion, by applying aspect‑oriented programming principles to language specifications, the authors demonstrate a practical method for improving grammar readability, eliminating redundant artefacts, and streamlining the generation of multiple language‑processing tools. The Grammatic prototype validates the concept and shows measurable benefits in code size, maintenance effort, and error reduction, making it a compelling addition to the toolbox of language engineers and DSL designers.


📜 Original Paper Content

🚀 Synchronizing high-quality layout from 1TB storage...