Download presentation
Presentation is loading. Please wait.
Published byLaurence Gilbert Modified over 8 years ago
1
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
2
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Outline Introduction Metamorphisms vDSL Specificity parsing Related and future work Conclusion
3
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Lexical Macros M LEX : (TOKENS) n TOKENS, n 0
4
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
5
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
6
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
7
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)
8
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)
9
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Invocation Syntax #define swap(X,Y) { int t=X; X=Y; Y=t; }
10
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!
11
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!
12
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;
13
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)
14
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
15
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 ) ;
16
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 ) ;
17
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 ) ;
18
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 ) ;
19
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 {,, }; ::= {…}
20
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 } ;
21
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
22
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility [Scheme]: special list constructor: “...”
23
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility [Scheme]: special list constructor: “...” decls ( enum id * )
24
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 * )
25
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
26
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Syntax Flexibility! Allow user-defined nonterminals (in invocation syntax):
27
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
28
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
29
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();
30
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 … … ::= { … … }
31
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 … … ::= { … … }
32
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 … ::= { … }
33
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 … ::= { … }
34
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 … ::= { … } { … }
35
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
36
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;
37
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)
38
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)
39
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
40
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
41
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
42
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
43
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);
44
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
45
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
46
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
47
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
48
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Advantages Flexibility Safety Simplicity
49
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Metamorph Advantages Flexibility: Tree structures Non-local transformations (multiple results) Safety Simplicity
50
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
51
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
52
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
53
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
54
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
55
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
56
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 }
57
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, … }
58
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, … }
59
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
60
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
61
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
62
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
63
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Conclusion Metamorphic Syntax Macros is a… flexible safe simple …way of Growing Languages
64
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) http://www.brics.dk/bigwig/
65
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 The End next: bonus slides
66
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
67
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; }
68
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 |
69
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;
70
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
71
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
72
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.