Growing Languages with Metamorphic Syntax Macros
description
Transcript of Growing Languages with Metamorphic Syntax Macros
![Page 1: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/1.jpg)
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
![Page 2: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/2.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Outline
• Introduction
• Metamorphisms
• vDSL
• Specificity parsing
• Related and future work
• Conclusion
![Page 3: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/3.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Lexical Macros
• MLEX: (TOKENS)n TOKENS, n 0
![Page 4: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/4.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Lexical Macros
• MLEX: (TOKENS)n TOKENS, n 0
#define square(X) X*X#define square(X) X*X
![Page 5: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/5.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Lexical Macros
• MLEX: (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
![Page 6: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/6.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Lexical Macros
• MLEX: (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
![Page 7: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/7.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Lexical Macros
• MLEX: (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)
![Page 8: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/8.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Lexical Macros
• MLEX: (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)
![Page 9: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/9.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Invocation Syntax
#define swap(X,Y) { int t=X; X=Y; Y=t; }#define swap(X,Y) { int t=X; X=Y; Y=t; }
![Page 10: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/10.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Invocation Syntax
#define swap(X,Y) { int t=X; X=Y; Y=t; }#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!
![Page 11: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/11.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Invocation Syntax
#define swap(X,Y) { int t=X; X=Y; Y=t; }#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!
![Page 12: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/12.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Invocation Syntax
#define swap(X,Y) { int t=X; X=Y; Y=t; }#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)#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;
![Page 13: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/13.jpg)
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; }#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)#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)M(x,y,z)
![Page 14: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/14.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Macro• MSYN: (AST)n AST, n 0
• Typed with nonterminals of the host grammar• Safe: no parse errors as a conseq. of expansion
![Page 15: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/15.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Macro• MSYN: (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 ) ;stm repeat stm until ( exp ) ;
![Page 16: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/16.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Macro• MSYN: (AST)n AST, n 0
• Typed with nonterminals of the host grammar• Safe: no parse errors as a conseq. of expansion
macro <stm> repeat <stm S> until ( <exp E> ) ; ::= { { bool first = true;
while (first || !<E>) {
<S>
first = false;
} }
}
macro <stm> repeat <stm S> until ( <exp E> ) ; ::= { { bool first = true;
while (first || !<E>) {
<S>
first = false;
} }
}
stm repeat stm until ( exp ) ;stm repeat stm until ( exp ) ;
![Page 17: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/17.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Macro• MSYN: (AST)n AST, n 0
• Typed with nonterminals of the host grammar• Safe: no parse errors as a conseq. of expansion
macro <stm> repeat <stm S> until ( <exp E> ) ; ::= { { bool first = true;
while (first || !<E>) {
<S>
first = false;
} }
}
macro <stm> repeat <stm S> until ( <exp E> ) ; ::= { { bool first = true;
while (first || !<E>) {
<S>
first = false;
} }
}
1. Invocation syntax: grammar extension
stm repeat stm until ( exp ) ;stm repeat stm until ( exp ) ;
![Page 18: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/18.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Macro• MSYN: (AST)n AST, n 0
• Typed with nonterminals of the host grammar• Safe: no parse errors as a conseq. of expansion
macro <stm> repeat <stm S> until ( <exp E> ) ; ::= { { bool first = true;
while (first || !<E>) {
<S>
first = false;
} }
}
macro <stm> repeat <stm S> until ( <exp E> ) ; ::= { { bool first = true;
while (first || !<E>) {
<S>
first = false;
} }
}
1. Invocation syntax: grammar extension
2. Transformation: morphing into host syntax
stm repeat stm until ( exp ) ;stm repeat stm until ( exp ) ;
![Page 19: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/19.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Flexibility?
macro <decls> enum { <id X> }; ::= {…}macro <decls> enum { <id X>, <id Y> }; ::= {…} macro <decls> enum { <id X>, <id Y>, <id Z> }; ::= {…}
macro <decls> enum { <id X> }; ::= {…}macro <decls> enum { <id X>, <id Y> }; ::= {…} macro <decls> enum { <id X>, <id Y>, <id Z> }; ::= {…}
![Page 20: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/20.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Flexibility?
macro <decls> enum { <id X> }; ::= {…}macro <decls> enum { <id X>, <id Y> }; ::= {…} macro <decls> enum { <id X>, <id Y>, <id Z> }; ::= {…}
macro <decls> enum { <id X> }; ::= {…}macro <decls> enum { <id X>, <id Y> }; ::= {…} macro <decls> enum { <id X>, <id Y>, <id Z> }; ::= {…}
decls enum { id } ;
enum { id , id } ;
enum { id , id , id } ;
decls enum { id } ;
enum { id , id } ;
enum { id , id , id } ;
![Page 21: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/21.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Flexibility?
macro <decls> enum { <id X> }; ::= {…}macro <decls> enum { <id X>, <id Y> }; ::= {…} macro <decls> enum { <id X>, <id Y>, <id Z> }; ::= {…}
macro <decls> enum { <id X> }; ::= {…}macro <decls> enum { <id X>, <id Y> }; ::= {…} macro <decls> enum { <id X>, <id Y>, <id Z> }; ::= {…}
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
![Page 22: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/22.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Flexibility
• [Scheme]:• special list constructor: “...”
![Page 23: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/23.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Flexibility
• [Scheme]:• special list constructor: “...”
decls ( enum id * )decls ( enum id * )
![Page 24: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/24.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Flexibility
• [Scheme]:• special list constructor: “...”
• [MS2]:• lists +/, options ?, tuples {…}, and
token-separated lists T
decls ( enum id * )decls ( enum id * )
![Page 25: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/25.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Flexibility
• [Scheme]:• special list constructor: “...”
• [MS2]:• lists +/, options ?, tuples {…}, and
token-separated lists T
decls ( enum id * )decls ( enum id * )
decls enum { id , } ;decls enum { id
, } ; ~ E-BNF
![Page 26: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/26.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Flexibility!
• Allow user-defined nonterminals(in invocation syntax):
![Page 27: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/27.jpg)
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
![Page 28: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/28.jpg)
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
![Page 29: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/29.jpg)
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 <n> m();metamorph <n> m();
![Page 30: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/30.jpg)
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 <n> m();
macro <stm> … <m: A> … ::= { … <A> … }
metamorph <n> m();
macro <stm> … <m: A> … ::= { … <A> … }
![Page 31: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/31.jpg)
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 <n> m();
macro <stm> … <m: A> … ::= { … <A> … }
metamorph <n> m();
macro <stm> … <m: A> … ::= { … <A> … }
![Page 32: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/32.jpg)
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 <n> m();
macro <stm> … <m: A> … ::= { … <A> … }
morph <m> … ::= { … }
metamorph <n> m();
macro <stm> … <m: A> … ::= { … <A> … }
morph <m> … ::= { … }
![Page 33: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/33.jpg)
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 <n> m();
macro <stm> … <m: A> … ::= { … <A> … }
morph <m> … ::= { … }
metamorph <n> m();
macro <stm> … <m: A> … ::= { … <A> … }
morph <m> … ::= { … }
![Page 34: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/34.jpg)
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 <n,n’> m();
macro <stm> … <m: A, B> … ::= { … <A> … <B> … }
morph <m> … ::= { … } { … }
metamorph <n,n’> m();
macro <stm> … <m: A, B> … ::= { … <A> … <B> … }
morph <m> … ::= { … } { … }
![Page 35: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/35.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Metamorph Example: enumdecls enum { id enums } ;
enums , id enums
decls enum { id enums } ;
enums , id enums
![Page 36: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/36.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Metamorph Example: enumdecls enum { id enums } ;
enums , id enums
decls enum { id enums } ;
enums , id enums
enum { x, y, z };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;
![Page 37: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/37.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Metamorph Example: enumdecls enum { id enums } ;
enums , id enums
decls enum { id enums } ;
enums , id enums
enum { x, y, z };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 programminglanguage (with AST values)
![Page 38: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/38.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Metamorph Example: enumdecls enum { id enums } ;
enums , id enums
decls enum { id enums } ;
enums , id enums
enum { x, y, z };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 programminglanguage (with AST values)
![Page 39: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/39.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Metamorph Example: enum
metamorph <decls> enums();metamorph <decls> enums();
decls enum { id enums } ;
enums , id enums
decls enum { id enums } ;
enums , id enums
![Page 40: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/40.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Metamorph Example: enum
metamorph <decls> enums();
macro <decls> enum { <id X> <enums: Ds> } ; ::= { int e = 0;
const int <X> = e++;
<Ds>
}
metamorph <decls> enums();
macro <decls> enum { <id X> <enums: Ds> } ; ::= { int e = 0;
const int <X> = e++;
<Ds>
}
decls enum { id enums } ;
enums , id enums
decls enum { id enums } ;
enums , id enums
![Page 41: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/41.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Metamorph Example: enum
metamorph <decls> enums();
macro <decls> enum { <id X> <enums: Ds> } ; ::= { int e = 0;
const int <X> = e++;
<Ds>
}
morph <enums> , <id X> <enums: Ds> ::= { const int <X> = e++;
<Ds>
}
metamorph <decls> enums();
macro <decls> enum { <id X> <enums: Ds> } ; ::= { int e = 0;
const int <X> = e++;
<Ds>
}
morph <enums> , <id X> <enums: Ds> ::= { const int <X> = e++;
<Ds>
}
decls enum { id enums } ;
enums , id enums
decls enum { id enums } ;
enums , id enums
![Page 42: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/42.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Metamorph Example: enum
metamorph <decls> enums();
macro <decls> enum { <id X> <enums: Ds> } ; ::= { int e = 0;
const int <X> = e++;
<Ds>
}
morph <enums> , <id X> <enums: Ds> ::= { const int <X> = e++;
<Ds>
}
morph <enums> ::= { }
metamorph <decls> enums();
macro <decls> enum { <id X> <enums: Ds> } ; ::= { int e = 0;
const int <X> = e++;
<Ds>
}
morph <enums> , <id X> <enums: Ds> ::= { const int <X> = e++;
<Ds>
}
morph <enums> ::= { }
decls enum { id enums } ;
enums , id enums
decls enum { id enums } ;
enums , id enums
![Page 43: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/43.jpg)
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 ) ...;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);
![Page 44: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/44.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Multiple ResultsExample: reserve
metamorph <stms,stms> res();metamorph <stms,stms> res();
stm reserve ( res ) stm
res id res
stm reserve ( res ) stm
res id res
![Page 45: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/45.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Multiple ResultsExample: reserve
metamorph <stms,stms> res();
macro <stm> reserve ( <res: S1,S2> ) <stm S> ::= { { <S1> <S> <S2> }
}
metamorph <stms,stms> res();
macro <stm> reserve ( <res: S1,S2> ) <stm S> ::= { { <S1> <S> <S2> }
}
stm reserve ( res ) stm
res id res
stm reserve ( res ) stm
res id res
![Page 46: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/46.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Multiple ResultsExample: reserve
metamorph <stms,stms> res();
macro <stm> reserve ( <res: S1,S2> ) <stm S> ::= { { <S1> <S> <S2> }
}
morph <res> <id X> <res: S1,S2> ::= { acquire(<X>); <S1>
} {
<S2> release(<X>);
}
metamorph <stms,stms> res();
macro <stm> reserve ( <res: S1,S2> ) <stm S> ::= { { <S1> <S> <S2> }
}
morph <res> <id X> <res: S1,S2> ::= { acquire(<X>); <S1>
} {
<S2> release(<X>);
}
stm reserve ( res ) stm
res id res
stm reserve ( res ) stm
res id res
![Page 47: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/47.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Multiple ResultsExample: reserve
metamorph <stms,stms> res();
macro <stm> reserve ( <res: S1,S2> ) <stm S> ::= { { <S1> <S> <S2> }
}
morph <res> <id X> <res: S1,S2> ::= { acquire(<X>); <S1>
} {
<S2> release(<X>);
}
morph <res> ::= { } { }
metamorph <stms,stms> res();
macro <stm> reserve ( <res: S1,S2> ) <stm S> ::= { { <S1> <S> <S2> }
}
morph <res> <id X> <res: S1,S2> ::= { acquire(<X>); <S1>
} {
<S2> release(<X>);
}
morph <res> ::= { } { }
stm reserve ( res ) stm
res id res
stm reserve ( res ) stm
res id res
![Page 48: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/48.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Metamorph Advantages
• Flexibility
• Safety
• Simplicity
![Page 49: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/49.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Metamorph Advantages
• Flexibility:• Tree structures• Non-local transformations (multiple results)
• Safety
• Simplicity
![Page 50: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/50.jpg)
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
![Page 51: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/51.jpg)
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
![Page 52: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/52.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
vDSL:very Domain Specific Languagestudies
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
![Page 53: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/53.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Specificity Parsing
macro <exp> select <id I> from <exp E> where <exp E2>
macro <exp> select all from <exp E> where <exp E2>
macro <exp> select <id I> from <exp E> where <exp E2>
macro <exp> select all from <exp E> where <exp E2>
![Page 54: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/54.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Specificity Parsing
• Challenge rounds:– Select most specific productions (wrt. FIRST sets)
macro <exp> select <id I> from <exp E> where <exp E2>
macro <exp> select all from <exp E> where <exp E2>
macro <exp> select <id I> from <exp E> where <exp E2>
macro <exp> select all from <exp E> where <exp E2>
![Page 55: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/55.jpg)
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 <exp> select <id I> from <exp E> where <exp E2>
macro <exp> select all from <exp E> where <exp E2>
macro <exp> select <id I> from <exp E> where <exp E2>
macro <exp> select all from <exp E> where <exp E2>
![Page 56: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/56.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Related Work: Macro Survey• 8x Macro languages:
• { CPP, M4, TEX, Dylan, C++ Templates, Scheme, JTS, MS2 }
![Page 57: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/57.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Related Work: Macro Survey• 8x Macro languages:
• { CPP, M4, TEX, Dylan, C++ Templates, Scheme, JTS, MS2 }
• 31x Macro properties:• { Level of operation, Programmability, Definition scope,
Termination, Argument syntax, Error trailing, … }
![Page 58: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/58.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Related Work: Macro Survey• 8x Macro languages:
• { CPP, M4, TEX, Dylan, C++ Templates, Scheme, JTS, MS2 }
• 31x Macro properties:• { Level of operation, Programmability, Definition scope,
Termination, Argument syntax, Error trailing, … }
![Page 59: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/59.jpg)
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
![Page 60: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/60.jpg)
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 metafrontbase grammar: Lmacros: L+ Lprogram in L+
program in L
![Page 61: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/61.jpg)
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 metafrontbase grammar: Lmacros: L+ Lprogram in L+
program in L
![Page 62: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/62.jpg)
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 metafrontbase grammar: Lmacros: L+ Lprogram in L+
program in L
![Page 63: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/63.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
ConclusionMetamorphic Syntax Macros is a…
• flexible• safe• simple
…way of Growing Languages
![Page 64: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/64.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
ConclusionMetamorphic Syntax Macros is a…
• flexible• safe• simple
…way of Growing Languages
Fully implemented in <bigwig> (language for developing interactive Web services)
http://www.brics.dk/bigwig/http://www.brics.dk/bigwig/
![Page 65: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/65.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
The End
next: bonus slides
![Page 66: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/66.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Pretty Printing & Error Reporting• Pretty Printing:
• Terminal printers:ASCII, LA EX, 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
![Page 67: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/67.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
-Conversion
macro <stm> repeat <stm S> until ( <exp E> ) ; ::= { {
bool first = true;
while (first || !<E>) {
<S>
first = false;
}
}
}
macro <stm> repeat <stm S> until ( <exp E> ) ; ::= { {
bool first = true;
while (first || !<E>) {
<S>
first = false;
}
}
}
![Page 68: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/68.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
-Conversion
metamorph <decls> enums();
macro <decls> enum { <id X> <enums: Ds> } ; ::= { int e = 0;
const int <X> = e++;
<Ds>
}
morph <enums> , <id X> <enums: Ds> ::= { const int <X> = e++;
<Ds>
}
morph <enums> ::= { }
metamorph <decls> enums();
macro <decls> enum { <id X> <enums: Ds> } ; ::= { int e = 0;
const int <X> = e++;
<Ds>
}
morph <enums> , <id X> <enums: Ds> ::= { const int <X> = e++;
<Ds>
}
morph <enums> ::= { }
decls : enum { id enums } ;
enums : , id enums
|
decls : enum { id enums } ;
enums : , id enums
|
![Page 69: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/69.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Example: switchmetamorph <stm> swb();
macro <stm> switch ( <exp E> ) { <swb: S> } ::= { { var x = <E>; <S>}}
morph <swb> case <exp E> : <stms Ss> break; <swb: S>::= { if (x==<E>) { <Ss> } else <S>}
morph <swb> case <exp E> : <stms Ss> break; ::= { if (x==<E>) { <Ss> }}
metamorph <stm> swb();
macro <stm> switch ( <exp E> ) { <swb: S> } ::= { { var x = <E>; <S>}}
morph <swb> case <exp E> : <stms Ss> break; <swb: S>::= { if (x==<E>) { <Ss> } else <S>}
morph <swb> case <exp E> : <stms Ss> break; ::= { if (x==<E>) { <Ss> }}
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;
![Page 70: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/70.jpg)
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 xlistxlist X xlist
![Page 71: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/71.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Representationmacro <ids> MXY ( <ids Is> ) ::= { X, <Is>, Y }
A, MXY(B, C), D A, X, B, C, Y, D
macro <ids> MXY ( <ids Is> ) ::= { X, <Is>, Y }
A, MXY(B, C), D A, X, B, C, Y, D
• “Weaving” yields transparency!
weave
![Page 72: Growing Languages with Metamorphic Syntax Macros](https://reader035.fdocuments.in/reader035/viewer/2022070404/56813a68550346895da2600a/html5/thumbnails/72.jpg)
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002
Syntax Macros
• MSYN: (AST)n AST , n 0
~
square( )
macro <exp> square ( <exp E> ) ::= { <E> * <E>
}
macro <exp> square ( <exp E> ) ::= { <E> * <E>
}**
exp
exp
Eexp
E
**
exp
y + 1y
+ 1 y + 1y
+ 1
exp
Eexp
Ey
+ 1y + 1
exp