PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Growing Languages with Metamorphic Syntax Macros Claus Brabrand Michael Schwartzbach.

Slides:



Advertisements
Similar presentations
[ 1 ] Claus Brabrand, ITU BANANA ALGEBRA DIKU, KU, Denmark May 11, 2010 Banana Algebra: Jacob Andersen [ ] Aarhus University Claus Brabrand.
Advertisements

[ 1 ] Claus Brabrand, ITU BANANA ALGEBRA March 28, 2009 ETAPS/LDTA, York Banana Algebra: Jacob Andersen [ ] Aarhus University Claus Brabrand.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
ICE1341 Programming Languages Spring 2005 Lecture #5 Lecture #5 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Functional Design and Programming Lecture 9: Lexical analysis and parsing.
Maya: Multiple-Dispatch Syntax Extension in Java Jason Baker and Wilson C. Hsieh University of Utah.
ISBN Chapter 3 More Syntax –BNF –Derivations –Practice.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Compilation 2007 Domain-Specific Languages Syntax Extensions Michael I. Schwartzbach BRICS, University of Aarhus.
May 21, 2002 The metafront Tool AoPL, S'02 Language Transformation: The metafront Tool Claus Brabrand Michael I. Schwartzbach BRICS, University of Aarhus,
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
) y + 1 * => square ( January 31, 2005MacrosClaus Brabrand y + 1 Widespread … ……… … ……... Dylan Dylan Macro Mechamism M4 Unix Macro Preprocessor L A T.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
May 14, 2002 Macro Languages AoPL, S'02 Macro Languages Claus Brabrand Michael I. Schwartzbach BRICS, University of Aarhus, Denmark.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Lexing Discrete Mathematics and Its Applications Baojian Hua
Maya: Multiple-Dispatch Syntax Extension in Java Jason Baker and Wilson C. Hsieh University of Utah.
Winter 2003/4Pls – syntax – Catriel Beeri1 SYNTAX Syntax: form, structure The syntax of a pl: The set of its well-formed programs The rules that define.
UMBC Introduction to Compilers CMSC 431 Shon Vick 01/28/02.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS 330 Programming Languages 09 / 26 / 2006 Instructor: Michael Eckmann.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Context-Free Grammars
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
PART I: overview material
C H A P T E R TWO Syntax and Semantic.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CPS 506 Comparative Programming Languages Syntax Specification.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CS 330 Programming Languages 09 / 20 / 2007 Instructor: Michael Eckmann.
Parsing using CYK Algorithm Transform grammar into Chomsky Form: 1.remove unproductive symbols 2.remove unreachable symbols 3.remove epsilons (no non-start.
Mid-Terms Exam Scope and Introduction. Format Grades: 100 points -> 20% in the final grade Multiple Choice Questions –8 questions, 7 points each Short.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
CS 326 Programming Languages, Concepts and Implementation
A Simple Syntax-Directed Translator
Lexical and Syntax Analysis
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
Context-Free Grammars
Compiler Construction (CS-636)
Top-Down Parsing.
Department of Software & Media Technology
Compiler Design 4. Language Grammars
Context-Free Grammars
CPSC 388 – Compiler Design and Construction
Some CFL Practicalities
Context-Free Grammars
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Lecture 7: Introduction to Parsing (Syntax Analysis)
Compilers B V Sai Aravind (11CS10008).
CSC 4181Compiler Construction Context-Free Grammars
Syntax-Directed Translation
Subject: Language Processor
6.001 SICP Further Variations on a Scheme
CSC 4181 Compiler Construction Context-Free Grammars
Context-Free Grammars
Compiler Construction
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Context-Free Grammars
COMPILER CONSTRUCTION
Presentation transcript:

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Growing Languages with Metamorphic Syntax Macros Claus Brabrand Michael Schwartzbach BRICS, University of Aarhus, Denmark

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Outline Introduction Metamorphisms vDSL Specificity parsing Related and future work Conclusion

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Lexical Macros M LEX : (TOKENS) n  TOKENS, n  0

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Lexical Macros M LEX : (TOKENS) n  TOKENS, n  0 #define square(X) X*X

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Lexical Macros M LEX : (TOKENS) n  TOKENS, n  0 #define square(X) X*X square(y+1)  y+1*y+1 #define square(X) X*X square(y+1)  y+1*y+1

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Lexical Macros M LEX : (TOKENS) n  TOKENS, n  0  #define square(X) X*X square(y+1)  y+1*y+1 #define square(X) X*X square(y+1)  y+1*y+1

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Lexical Macros M LEX : (TOKENS) n  TOKENS, n  0  #define square(X) X*X square(y+1)  y+1*y+1 #define square(X) X*X square(y+1)  y+1*y+1 #define square(X) (X)*(X) square(y+1)  (y+1)*(y+1) #define square(X) (X)*(X) square(y+1)  (y+1)*(y+1)

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Lexical Macros M LEX : (TOKENS) n  TOKENS, n  0  Problem: Independent of syntax! Unsafe: parse errors discovered at invocation-time #define square(X) X*X square(y+1)  y+1*y+1 #define square(X) X*X square(y+1)  y+1*y+1 #define square(X) (X)*(X) square(y+1)  (y+1)*(y+1) #define square(X) (X)*(X) square(y+1)  (y+1)*(y+1)

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Invocation Syntax #define swap(X,Y) { int t=X; X=Y; Y=t; }

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Invocation Syntax #define swap(X,Y) { int t=X; X=Y; Y=t; } if (a>b) swap(a,b); else b=0; if (a>b) swap(a,b); else b=0; *** parse error!

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Invocation Syntax #define swap(X,Y) { int t=X; X=Y; Y=t; } if (a>b) swap(a,b); else b=0; if (a>b) swap(a,b); else b=0; *** parse error! 

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Invocation Syntax #define swap(X,Y) { int t=X; X=Y; Y=t; } if (a>b) swap(a,b); else b=0; if (a>b) swap(a,b); else b=0; *** parse error! #define swap(X,Y) do { int t=X; X=Y; Y=t; } while (0) if (a>b) swap(a,b); else b=0; if (a>b) swap(a,b); else b=0; 

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Invocation Syntax Problem: fixed invocation syntax! same for exp / stm / … #define swap(X,Y) { int t=X; X=Y; Y=t; } if (a>b) swap(a,b); else b=0; if (a>b) swap(a,b); else b=0; *** parse error! #define swap(X,Y) do { int t=X; X=Y; Y=t; } while (0) if (a>b) swap(a,b); else b=0; if (a>b) swap(a,b); else b=0;  M(x,y,z)

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Macro M SYN : (AST) n  AST, n  0 Typed with nonterminals of the host grammar Safe: no parse errors as a conseq. of expansion

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Macro M SYN : (AST) n  AST, n  0 Typed with nonterminals of the host grammar Safe: no parse errors as a conseq. of expansion stm  repeat stm until ( exp ) ;

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Macro M SYN : (AST) n  AST, n  0 Typed with nonterminals of the host grammar Safe: no parse errors as a conseq. of expansion macro  repeat until ( ) ; ::= { { bool first = true; while (first || ! ) { first = false; } } } macro  repeat until ( ) ; ::= { { bool first = true; while (first || ! ) { first = false; } } } stm  repeat stm until ( exp ) ;

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Macro M SYN : (AST) n  AST, n  0 Typed with nonterminals of the host grammar Safe: no parse errors as a conseq. of expansion macro  repeat until ( ) ; ::= { { bool first = true; while (first || ! ) { first = false; } } } macro  repeat until ( ) ; ::= { { bool first = true; while (first || ! ) { first = false; } } } 1.Invocation syntax: grammar extension stm  repeat stm until ( exp ) ;

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Macro M SYN : (AST) n  AST, n  0 Typed with nonterminals of the host grammar Safe: no parse errors as a conseq. of expansion macro  repeat until ( ) ; ::= { { bool first = true; while (first || ! ) { first = false; } } } macro  repeat until ( ) ; ::= { { bool first = true; while (first || ! ) { first = false; } } } 1.Invocation syntax: grammar extension 2.Transformation: morphing into host syntax stm  repeat stm until ( exp ) ;

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility? macro  enum { }; ::= {…} macro  enum {, }; ::= {…} macro  enum {,, }; ::= {…} macro  enum { }; ::= {…} macro  enum {, }; ::= {…} macro  enum {,, }; ::= {…}

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility? macro  enum { }; ::= {…} macro  enum {, }; ::= {…} macro  enum {,, }; ::= {…} macro  enum { }; ::= {…} macro  enum {, }; ::= {…} macro  enum {,, }; ::= {…} decls  enum { id } ;  enum { id, id } ;  enum { id, id, id } ; decls  enum { id } ;  enum { id, id } ;  enum { id, id, id } ;

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility? macro  enum { }; ::= {…} macro  enum {, }; ::= {…} macro  enum {,, }; ::= {…} macro  enum { }; ::= {…} macro  enum {, }; ::= {…} macro  enum {,, }; ::= {…} decls  enum { id } ;  enum { id, id } ;  enum { id, id, id } ; decls  enum { id } ;  enum { id, id } ;  enum { id, id, id } ; Problems: Only fixed (finite) arity Highly redundant

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility [Scheme]: special list constructor: “...”

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility [Scheme]: special list constructor: “...” decls  ( enum id * )

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility [Scheme]: special list constructor: “...” [MS 2 ]: lists +/ , options ?, tuples {…}, and token-separated lists  T decls  ( enum id * )

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility [Scheme]: special list constructor: “...” [MS 2 ]: lists +/ , options ?, tuples {…}, and token-separated lists  T decls  ( enum id * ) decls  enum { id , } ; ~ E-BNF

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility! Allow user-defined nonterminals (in invocation syntax):

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility! Allow user-defined nonterminals (in invocation syntax): decls  enum { id enums } ; enums , id enums   decls  enum { id enums } ; enums , id enums  

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility! Allow user-defined nonterminals (in invocation syntax): Transformation? without compromising safety decls  enum { id enums } ; enums , id enums   decls  enum { id enums } ; enums , id enums  

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorphisms Our solution: Metamorphisms Attach host nonterminals to a user-def’d nonterminal metamorph m();

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorphisms Our solution: Metamorphisms Attach host nonterminals to a user-def’d nonterminal metamorph m(); macro  … … ::= { … … } metamorph m(); macro  … … ::= { … … }

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorphisms Our solution: Metamorphisms Attach host nonterminals to a user-def’d nonterminal Specify morphing (into host syntax) inductively metamorph m(); macro  … … ::= { … … } metamorph m(); macro  … … ::= { … … }

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorphisms Our solution: Metamorphisms Attach host nonterminals to a user-def’d nonterminal Specify morphing (into host syntax) inductively metamorph m(); macro  … … ::= { … … } morph  … ::= { … } metamorph m(); macro  … … ::= { … … } morph  … ::= { … }

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorphisms Our solution: Metamorphisms Attach host nonterminals to a user-def’d nonterminal Specify morphing (into host syntax) inductively Non-local transformations (multiple results) metamorph m(); macro  … … ::= { … … } morph  … ::= { … } metamorph m(); macro  … … ::= { … … } morph  … ::= { … }

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorphisms Our solution: Metamorphisms Attach host nonterminals to a user-def’d nonterminal Specify morphing (into host syntax) inductively Non-local transformations (multiple results) metamorph m(); macro  … … ::= { … … … } morph  … ::= { … } { … } metamorph m(); macro  … … ::= { … … … } morph  … ::= { … } { … }

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Example: enum decls  enum { id enums } ; enums , id enums   decls  enum { id enums } ; enums , id enums  

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Example: enum decls  enum { id enums } ; enums , id enums   decls  enum { id enums } ; enums , id enums   enum { x, y, z }; const int x = 0; const int y = 1; const int z = 2; const int x = 0; const int y = 1; const int z = 2; 

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Example: enum decls  enum { id enums } ; enums , id enums   decls  enum { id enums } ; enums , id enums   enum { x, y, z }; const int x = 0; const int y = 1; const int z = 2; const int x = 0; const int y = 1; const int z = 2;  Without compile-time programming language (with AST values)

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Example: enum decls  enum { id enums } ; enums , id enums   decls  enum { id enums } ; enums , id enums   enum { x, y, z }; int e = 0; const int x = e++; const int y = e++; const int z = e++; int e = 0; const int x = e++; const int y = e++; const int z = e++;  Without compile-time programming language (with AST values)

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Example: enum metamorph enums(); decls  enum { id enums } ; enums , id enums   decls  enum { id enums } ; enums , id enums  

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Example: enum metamorph enums(); macro  enum { } ; ::= { int e = 0; const int = e++; } metamorph enums(); macro  enum { } ; ::= { int e = 0; const int = e++; } decls  enum { id enums } ; enums , id enums   decls  enum { id enums } ; enums , id enums  

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Example: enum metamorph enums(); macro  enum { } ; ::= { int e = 0; const int = e++; } morph , ::= { const int = e++; } metamorph enums(); macro  enum { } ; ::= { int e = 0; const int = e++; } morph , ::= { const int = e++; } decls  enum { id enums } ; enums , id enums   decls  enum { id enums } ; enums , id enums  

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Example: enum metamorph enums(); macro  enum { } ; ::= { int e = 0; const int = e++; } morph , ::= { const int = e++; } morph  ::= { } metamorph enums(); macro  enum { } ; ::= { int e = 0; const int = e++; } morph , ::= { const int = e++; } morph  ::= { } decls  enum { id enums } ; enums , id enums   decls  enum { id enums } ; enums , id enums  

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 stm  reserve ( res ) stm res  id res   stm  reserve ( res ) stm res  id res   Metamorph Example: reserve reserve ( a b c )...; acquire(a); acquire(b); acquire(c);...; release(c); release(b); release(a); acquire(a); acquire(b); acquire(c);...; release(c); release(b); release(a); 

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Multiple Results Example: reserve metamorph res(); stm  reserve ( res ) stm res  id res   stm  reserve ( res ) stm res  id res  

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Multiple Results Example: reserve metamorph res(); macro  reserve ( ) ::= { { } } metamorph res(); macro  reserve ( ) ::= { { } } stm  reserve ( res ) stm res  id res   stm  reserve ( res ) stm res  id res  

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Multiple Results Example: reserve metamorph res(); macro  reserve ( ) ::= { { } } morph  ::= { acquire( ); } { release( ); } metamorph res(); macro  reserve ( ) ::= { { } } morph  ::= { acquire( ); } { release( ); } stm  reserve ( res ) stm res  id res   stm  reserve ( res ) stm res  id res  

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Multiple Results Example: reserve metamorph res(); macro  reserve ( ) ::= { { } } morph  ::= { acquire( ); } { release( ); } morph  ::= { } { } metamorph res(); macro  reserve ( ) ::= { { } } morph  ::= { acquire( ); } { release( ); } morph  ::= { } { } stm  reserve ( res ) stm res  id res   stm  reserve ( res ) stm res  id res  

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Advantages Flexibility Safety Simplicity

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Advantages Flexibility: Tree structures Non-local transformations (multiple results) Safety Simplicity

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Advantages Flexibility: Tree structures Non-local transformations (multiple results) Safety: No parse errors as a conseq. of macro expansion Guaranteed termination Simplicity

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Advantages Flexibility: Tree structures Non-local transformations (multiple results) Safety: No parse errors as a conseq. of macro expansion Guaranteed termination Simplicity: Based entirely on declarative concepts: grammars and substitution

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 vDSL: very Domain Specific Language studies course Math101 title “Mathematics 101” 2 point fall term … exclusions Math101 <> MathA Math102 <> MathB prerequisites Math101, Math102 < Math201, Math202, Math203 Math101, CS101 < CS202 studies course Math101 title “Mathematics 101” 2 point fall term … exclusions Math101 <> MathA Math102 <> MathB prerequisites Math101, Math102 < Math201, Math202, Math203 Math101, CS101 < CS202

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Specificity Parsing macro  select from where macro  select all from where macro  select from where macro  select all from where

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Specificity Parsing Challenge rounds: –Select most specific productions (wrt. FIRST sets) macro  select from where macro  select all from where macro  select from where macro  select all from where

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Specificity Parsing Challenge rounds: –Select most specific productions (wrt. FIRST sets) Resolves many ambiguities Independent of definition-order Overloading Avoids keywordification Commit  no branch explosion, no backtracking macro  select from where macro  select all from where macro  select from where macro  select all from where

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Related Work: Macro Survey 8x Macro languages: { CPP, M4, T E X, Dylan, C++ Templates, Scheme, JTS, MS 2 }

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Related Work: Macro Survey 8x Macro languages: { CPP, M4, T E X, Dylan, C++ Templates, Scheme, JTS, MS 2 } 31x Macro properties: { Level of operation, Programmability, Definition scope, Termination, Argument syntax, Error trailing, … }

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Related Work: Macro Survey 8x Macro languages: { CPP, M4, T E X, Dylan, C++ Templates, Scheme, JTS, MS 2 } 31x Macro properties: { Level of operation, Programmability, Definition scope, Termination, Argument syntax, Error trailing, … }

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Related Work “Extensible Syntax with Lexical Scoping” - by Cardelli, Matthes, and Abadi : Not a macro language, but a parser generator: –Target language (not host language) –Extend  recompile parser Localized transformation only Disjoint productions Keywordification Explicit alpha conversion

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Future Work: metafront Extensible syntax processor, based on: Specificity parsing and Metamorphic syntax macros: metafront base grammar: L macros: L+  L program in L+ program in L

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Future Work: metafront Extensible syntax processor, based on: Specificity parsing and Metamorphic syntax macros: Safe transformation: L+  L metafront base grammar: L macros: L+  L program in L+ program in L

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Future Work: metafront Extensible syntax processor, based on: Specificity parsing and Metamorphic syntax macros: Safe transformation: L+  L Safe transformation: L’  L, (vDSL) metafront base grammar: L macros: L+  L program in L+ program in L

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Conclusion Metamorphic Syntax Macros is a… flexible safe simple …way of Growing Languages

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Conclusion Metamorphic Syntax Macros is a… flexible safe simple …way of Growing Languages Fully implemented in (language for developing interactive Web services)

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 The End next: bonus slides

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Pretty Printing & Error Reporting Pretty Printing: Terminal printers: ASCII, L A E X, HTML ( +/- expansion) Error Reporting: stdout, HTML *** symbol errors: *** test.wig:7: Identifier ‘inf’ not declared in macro argument ‘S’ in macro invocation ‘reader’ (test.wig:7) defined in [std.wigmac:44] *** symbol errors: *** test.wig:7: Identifier ‘inf’ not declared in macro argument ‘S’ in macro invocation ‘reader’ (test.wig:7) defined in [std.wigmac:44] T

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002  -Conversion macro  repeat until ( ) ; ::= { { bool first = true; while (first || ! ) { first = false; } macro  repeat until ( ) ; ::= { { bool first = true; while (first || ! ) { first = false; }

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002  -Conversion metamorph enums(); macro  enum { } ; ::= { int e = 0; const int = e++; } morph , ::= { const int = e++; } morph  ::= { } metamorph enums(); macro  enum { } ; ::= { int e = 0; const int = e++; } morph , ::= { const int = e++; } morph  ::= { } decls: enum { id enums } ; enums:, id enums |  decls: enum { id enums } ; enums:, id enums | 

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Example: switch metamorph swb(); macro  switch ( ) { } ::= { { var x = ; } morph  case : break; ::= { if (x== ) { } else } morph  case : break; ::= { if (x== ) { } } metamorph swb(); macro  switch ( ) { } ::= { { var x = ; } morph  case : break; ::= { if (x== ) { } else } morph  case : break; ::= { if (x== ) { } } stm: switch ( exp ) { swb } swb: case exp : stms break; swb | case exp : stms break; stm: switch ( exp ) { swb } swb: case exp : stms break; swb | case exp : stms break;

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Wellformedness Check at definition time: “no left recursion” (guarantees termination):  “derivability” (metamorphisms must derive something finite) :  xlist  xlist X   xlist  xlist X   xlist  X xlist   xlist  X xlist   xlist  X xlist   xlist  X xlist   xlist  X xlist

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Representation macro  M XY ( ) ::= { X,, Y } A, M XY (B, C), D  A, X, B, C, Y, D macro  M XY ( ) ::= { X,, Y } A, M XY (B, C), D  A, X, B, C, Y, D “Weaving” yields transparency! weave

PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Macros M SYN : (AST) n  AST, n  0 ~ square( )  macro  square ( ) ::= { * } macro  square ( ) ::= { * } * * exp E exp E * * exp y + 1y + 1 y + 1y + 1 y + 1y + 1 y + 1y + 1 E exp E y + 1y + 1 y + 1y + 1 exp