Abstract

Language designers usually need to implement parsers and printers. Despite being two closely related programs, in practice they are often designed separately, and then need to be revised and kept consistent as the language evolves. It will be more convenient if the parser and printer can be unified and developed in a single program, with their consistency guaranteed automatically. Furthermore, in certain scenarios (like showing compiler optimisation results to the programmer), it is desirable to have a more powerful reflective printer that, when an abstract syntax tree corresponding to a piece of program text is modified, can propagate the modification to the program text while preserving layouts, comments, and syntactic sugar. To address these needs, we propose a domain-specific language BiYacc, whose programs denote both a parser and a reflective printer for a fully disambiguated context-free grammar. BiYacc is based on the theory of bidirectional transformations, which helps to guarantee by construction that the generated pairs of parsers and reflective printers are consistent. Handling grammatical ambiguity is particularly challenging: we propose an approach based on generalised parsing and disambiguation filters, which produce all the parse results and (try to) select the only correct one in the parsing direction; the filters are carefully bidirectionalised so that they also work in the printing direction and do not break the consistency between the parsers and reflective printers. We show that BiYacc is capable of facilitating many tasks such as Pombrio and Krishnamurthi’s ‘resugaring’, simple refactoring, and language evolution.

Highlights

  • Whenever we come up with a new programming language, as the front-end part of the system we need to design and implement a parser and a printer to convert between program text and an internal representation

  • A printer converts an abstract syntax tree (AST) back to a piece of program text, which can be understood by the user of the system; this is useful for debugging the system, or reporting internal information to the user

  • We propose a domain-specific language BIYACC, which enables the user to describe both a parser and a reflective printer for a fully disambiguated context-free grammar (CFG) in a single program

Read more

Summary

Introduction

Whenever we come up with a new programming language, as the front-end part of the system we need to design and implement a parser and a printer to convert between program text and an internal representation. Parsers and printers do conversions in opposite directions and are closely related—for example, the program text printed from an AST should be parsed to the same tree It is certainly far from being economical to write parsers and printers separately: the parser and printer need to be revised from time to time as the language evolves, and each time we must revise the parser and printer and keep them consistent with each other, which is a time-consuming and error-prone task. Have been proposed, in which the user can describe both a parser and a printer in a single program Despite their advantages, these domain-specific languages cannot deal with synchronisation between program text and ASTs. Let us look at a concrete example in Fig. 1: the original program text is an arithmetic expression, containing a negation, a comment, and parentheses (one pair of which is redundant). Throughout this paper, we typeset general definitions and properties in math style and specific examples in code style

A First Look at BIYACC
Related Work
Conclusion
Full Text
Published version (Free)

Talk to us

Join us for a 30 min session where you can share your feedback and ask us any queries you have

Schedule a call