Creating Textual Language Dialects Using Aspect-like Techniques

Here we present a work aimed on efficiently creating textual language dialects and 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 (relatively small) set …

Authors: Andrey Breslav

Creating T extual Language Dialects Using Asp ect-lik e T ec hniques Andrey Breslav ⋆ St. P etersburg State U niversi ty of IT, Mec hanics and Optics abreslav@g mail.com W e present a w o rk aimed on efficiently creating textual la nguage dialects and suppo rting to ols for them (e.g . compiler front-ends, IDE support, pr ett y- printers, etc.). A dialect is a la ng uage which may be descr ibe d with a (rela tively small) set of changes to some other language. F o r example we can conside r SQL dialects used in DB-mana gement sys tems. The need in crea ting dialects is witnes s ed by existence of extensible languag e s like LISP and many more [1,2,3,4]. There also are s pec ial to ols supp orting dia lect developmen t for C [5] and Jav a [6]. These approaches a llow extending langua ges efficien tly , but it to ok a lot of work to make it p ossible for each single lang ua ge. The pr oblem we are addr essing here is how c an we d o it for arbitr ary languages with not so mu ch effort? If we need o nly a syntax c heck er for a langua ge, creating dialects fo r it is easy . W e have a grammar desc r ibing the language s yntax and all w e need is to mo dify this grammar . This can be done easily b y a tra nsformation whic h may be formulated in ter ms o f asp ect application (a na logous to A OP [7]), we call such transformations syntactic al asp e cts . By pattern matching asp ects ident ify places in the source grammar that we need to mo dify and provide instruc tio ns for suc h modifica tions: ins ert s omething befor e o r a fter the sp ecified p oint, o r replace the ob ject with some other one. F or exa mple, ass ume w e hav e a grammar for arithmetical expres s ions for r eal num b ers: sum : mult (’+’ mult)* ; mult : factor (’*’ factor )*; factor : REAL : ’(’ sum ’)’ ; W e may wan t to replace reals with in tegers a nd in tro duce v ariables in this gram- mar, this could b e done b y mo difying rules for f actor a s follows: factor $produ ction =|: REAL @REAL. inste ad = << INT >> ⋆ Main results presented in t his p ap er are accepted for pu blication in journal V estink SPbSU ITMO. The w ork is partially supp orted by S ain t-Petersburg State grant #3.11/4-05 /55 2 @produ ction .afte r = <<: ID >> ; This r eplaces one token reference with ano ther and inser ts a new pro duction rule. Many real-world grammar transfor ma tions which are needed for dialect creation are easily expr essible in this manner. Things b ecome less obvious when w e need so mething more than a pa rser. Assume we are working with a language front-end that suppor ts parsing erro r recov ery , semantic chec king and abs tract syn tax tree cr e a tion. Now we need to transform the whole system, not o nly the gramma r, and c hanges to all parts m ust b e coher ent. T o achieve this we can observe that in most case s a structur e of fro nt-end features is highly dep endent o n the gr ammar structure – high-level descriptions of features lik e seman tic analys is or error-recovery are structured the same w ay as the grammar is. If we could reuse the grammar structure in these definitions, na mely , provide explicit connec tio n of definition parts to grammar ob jects, we w ould be able to transform the whole system coherently since all the structural changes would b e induced by g rammar changes. W e prop o s e an appr oach which is ba sed on this idea. In this approach all the features of the system are describ ed by high-level mo dels, which are a ttached to corres p o nding gra mmar ob jects. The working co de is g enerated using infor ma- tion fr om these mo dels. T echnically mo del data is divided into name-v alue pairs (where v alues may b e of different types) and attached to gr ammar ob jects with asp ect-like rules. W e call this met adata a sp e cts . The way of a ssigning metadata enables high mo dularity since we do not need to sp ecify all metadata in a single definition, but w e a re able to define a separate definition for eac h asp ect of the system. After metadata is assigne d, all the changes to grammar structure automat- ically re arrang e it, which is ensured b y sp ecial in teg rity chec kers. This makes dialect development very ea sy since in many c a ses all we need to cr e ate a dialect is to write a sy ntactical as pe c t to transform a gr ammar. References 1. Mo on, D. A.: Programming Language for Old Timers. (2008), http://use rs.rcn.co m/david- moon/PLOT/ 2. S k alski, K., Mosk al, M., Ols zta, P .: Meta-programming in Nemerle. (20 04), http://nem erle.org/ metaprogramming.pdf 3. S assenrath, C.: The REBOL Scripting Language. j-DDJ. 25(7) 66–69 (2000) 4. T ratt, L.: The Con verge programming language. T echnical report TR-05-01, De- partment of Computer Science, Kings College Lond on ( 2005) 5. Grimm, R.: XTC: Making C safely extensible. In: W orkshop on D omain-Sp ecific Languages for N u merical O ptimization, Argonne National Lab oratory (2004) 6. N ystrom, N ., Clarkson, M. R., Myers A. C.: P olyglot: A n Extensible Compiler F ramewor k f or Jav a. In: 12th In tern ational Conference on Compiler Construction, pp. 138–152. Sp rin ger-V erlag (2003) 7. K iczales, G., Lamping, J. , Mendhek ar, A., Maeda, C., Lop es, C. V., Loingtie r, J. M., Irwin, J.: A sp ect-oriented programming. In: ECOOP . Springer-V erlag ( 1997)

Original Paper

Loading high-quality paper...

Comments & Academic Discussion

Loading comments...

Leave a Comment