Creating Textual Language Dialects Using Aspect-like Techniques
aa r X i v : . [ c s . P L ] J un Creating Textual Language Dialects UsingAspect-like Techniques
Andrey Breslav ⋆ St. Petersburg State University of IT, Mechanics and Optics [email protected]
We present a work aimed on efficiently creating textual language dialectsand supporting tools for them (e.g. compiler front-ends, IDE support, pretty-printers, etc.). A dialect is a language which may be described with a (relativelysmall) set of changes to some other language. For example we can consider SQLdialects used in DB-management systems.The need in creating dialects is witnessed by existence of extensible languageslike LISP and many more [1,2,3,4]. There also are special tools supporting dialectdevelopment for C [5] and Java [6].These approaches allow extending languages efficiently, but it took a lot ofwork to make it possible for each single language. The problem we are addressinghere is how can we do it for arbitrary languages with not so much effort?
If we need only a syntax checker for a language, creating dialects for it iseasy. We have a grammar describing the language syntax and all we need is tomodify this grammar. This can be done easily by a transformation which maybe formulated in terms of aspect application (analogous to AOP [7]), we callsuch transformations syntactical aspects . By pattern matching aspects identifyplaces in the source grammar that we need to modify and provide instructionsfor such modifications: insert something before or after the specified point, orreplace the object with some other one. For example, assume we have a grammarfor arithmetical expressions for real numbers: sum : mult (’+’ mult)*;mult : factor (’*’ factor)*;factor: REAL: ’(’ sum ’)’;
We may want to replace reals with integers and introduce variables in this gram-mar, this could be done by modifying rules for factor as follows: factor$production=|: [email protected] = << INT >> ⋆ Main results presented in this paper are accepted for publication in journal VestinkSPbSU ITMO. The work is partially supported by Saint-Petersburg State grant @production.after = <<: ID >>;
This replaces one token reference with another and inserts a new productionrule. Many real-world grammar transformations which are needed for dialectcreation are easily expressible in this manner.Things become less obvious when we need something more than a parser.Assume we are working with a language front-end that supports parsing errorrecovery, semantic checking and abstract syntax tree creation. Now we need totransform the whole system, not only the grammar, and changes to all partsmust be coherent. To achieve this we can observe that in most cases a structureof front-end features is highly dependent on the grammar structure – high-leveldescriptions of features like semantic analysis or error-recovery are structured thesame way as the grammar is. If we could reuse the grammar structure in thesedefinitions, namely, provide explicit connection of definition parts to grammarobjects, we would be able to transform the whole system coherently since all thestructural changes would be induced by grammar changes.We propose an approach which is based on this idea. In this approach all thefeatures of the system are described by high-level models, which are attached tocorresponding grammar objects. The working code is generated using informa-tion from these models. Technically model data is divided into name-value pairs(where values may be of different types) and attached to grammar objects withaspect-like rules. We call this metadata aspects . The way of assigning metadataenables high modularity since we do not need to specify all metadata in a singledefinition, but we are able to define a separate definition for each aspect of thesystem.After metadata is assigned, all the changes to grammar structure automat-ically rearrange it, which is ensured by special integrity checkers. This makesdialect development very easy since in many cases all we need to create a dialectis to write a syntactical aspect to transform a grammar.
References
1. Moon, D. A.: Programming Language for Old Timers. (2008), http://users.rcn.com/david-moon/PLOT/
2. Skalski, K., Moskal, M., Olszta, P.: Meta-programming in Nemerle. (2004), http://nemerle.org/metaprogramming.pdfhttp://nemerle.org/metaprogramming.pdf