Title: Growing Languages with Metamorphic Syntax Macros
1Growing Languages with Metamorphic Syntax Macros
- Claus Brabrand
- Michael Schwartzbach
- BRICS, University of Aarhus, Denmark
2Outline
- Introduction
- Metamorphisms
- vDSL
- Specificity parsing
- Related and future work
- Conclusion
3Lexical Macros
- MLEX (TOKENS)n ? TOKENS, n ? 0
-
4Lexical Macros
- MLEX (TOKENS)n ? TOKENS, n ? 0
-
define square(X) XX
5Lexical Macros
- MLEX (TOKENS)n ? TOKENS, n ? 0
-
define square(X) XX square(y1) ? y1y1
6Lexical Macros
- MLEX (TOKENS)n ? TOKENS, n ? 0
- ?
define square(X) XX square(y1) ? y1y1
7Lexical Macros
- MLEX (TOKENS)n ? TOKENS, n ? 0
- ?
- ?
define square(X) XX square(y1) ? y1y1
define square(X) (X)(X) square(y1) ?
(y1)(y1)
8Lexical Macros
- MLEX (TOKENS)n ? TOKENS, n ? 0
- ?
- ?
- Problem Independent of syntax!
- Unsafe parse errors discovered at invocation-time
define square(X) XX square(y1) ? y1y1
define square(X) (X)(X) square(y1) ?
(y1)(y1)
9Invocation Syntax
define swap(X,Y) int tX XY Yt
10Invocation Syntax
define swap(X,Y) int tX XY Yt
if (agtb) swap(a,b) else b0
parse error!
11Invocation Syntax
define swap(X,Y) int tX XY Yt
if (agtb) swap(a,b) else b0
?
parse error!
12Invocation Syntax
define swap(X,Y) int tX XY Yt
if (agtb) swap(a,b) else b0
?
parse error!
define swap(X,Y) do int tX XY Yt while
(0)
if (agtb) swap(a,b) else b0
?
13Invocation Syntax
define swap(X,Y) int tX XY Yt
if (agtb) swap(a,b) else b0
?
parse error!
define swap(X,Y) do int tX XY Yt while
(0)
if (agtb) swap(a,b) else b0
?
- Problem fixed invocation syntax!
- same for exp / stm /
M(x,y,z)
14Syntax Macro
- MSYN (AST)n ? AST, n ? 0
- Typed with nonterminals of the host grammar
- Safe no parse errors as a conseq. of expansion
15Syntax 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 )
16Syntax 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 )
macro ltstmgt ? repeat ltstm Sgt until ( ltexp Egt )
bool first true while (first
!ltEgt) ltSgt first false
17Syntax 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 )
macro ltstmgt ? repeat ltstm Sgt until ( ltexp Egt )
bool first true while (first
!ltEgt) ltSgt first false
- Invocation syntax
- grammar extension
18Syntax 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 )
macro ltstmgt ? repeat ltstm Sgt until ( ltexp Egt )
bool first true while (first
!ltEgt) ltSgt first false
- Invocation syntax
- grammar extension
- Transformation
- morphing into
- host syntax
19Syntax Flexibility?
macro ltdeclsgt ? enum ltid Xgt macro
ltdeclsgt ? enum ltid Xgt, ltid Ygt macro
ltdeclsgt ? enum ltid Xgt, ltid Ygt, ltid Zgt
20Syntax Flexibility?
macro ltdeclsgt ? enum ltid Xgt macro
ltdeclsgt ? enum ltid Xgt, ltid Ygt macro
ltdeclsgt ? enum ltid Xgt, ltid Ygt, ltid Zgt
decls ? enum id ? enum id , id
? enum id , id , id
21Syntax Flexibility?
macro ltdeclsgt ? enum ltid Xgt macro
ltdeclsgt ? enum ltid Xgt, ltid Ygt macro
ltdeclsgt ? enum ltid Xgt, ltid Ygt, ltid Zgt
decls ? enum id ? enum id , id
? enum id , id , id
- Problems
- Only fixed (finite) arity
- Highly redundant
-
22Syntax Flexibility
- Scheme
- special list constructor ...
23Syntax Flexibility
- Scheme
- special list constructor ...
decls ? ( enum id )
24Syntax Flexibility
- Scheme
- special list constructor ...
- MS2
- lists /?, options ?, tuples , and
token-separated lists ?T
decls ? ( enum id )
25Syntax Flexibility
- Scheme
- special list constructor ...
- MS2
- lists /?, options ?, tuples , and
token-separated lists ?T
decls ? ( enum id )
decls ? enum id ?,
E-BNF
26Syntax Flexibility!
- Allow user-defined nonterminals(in invocation
syntax)
27Syntax Flexibility!
- Allow user-defined nonterminals(in invocation
syntax)
decls ? enum id enums enums ? , id
enums ? ?
28Syntax Flexibility!
- Allow user-defined nonterminals(in invocation
syntax) - Transformation?
- without compromising safety
decls ? enum id enums enums ? , id
enums ? ?
29Metamorphisms
- Our solution Metamorphisms
- Attach host nonterminals to a user-defd
nonterminal
metamorph ltngt m()
30Metamorphisms
- Our solution Metamorphisms
- Attach host nonterminals to a user-defd
nonterminal
metamorph ltngt m() macro ltstmgt ? ltm Agt
ltAgt
31Metamorphisms
- Our solution Metamorphisms
- Attach host nonterminals to a user-defd
nonterminal - Specify morphing (into host syntax) inductively
metamorph ltngt m() macro ltstmgt ? ltm Agt
ltAgt
32Metamorphisms
- Our solution Metamorphisms
- Attach host nonterminals to a user-defd
nonterminal - Specify morphing (into host syntax) inductively
metamorph ltngt m() macro ltstmgt ? ltm Agt
ltAgt morph ltmgt ?
33Metamorphisms
- Our solution Metamorphisms
- Attach host nonterminals to a user-defd
nonterminal - Specify morphing (into host syntax) inductively
- Non-local transformations (multiple results)
metamorph ltngt m() macro ltstmgt ? ltm Agt
ltAgt morph ltmgt ?
34Metamorphisms
- Our solution Metamorphisms
- Attach host nonterminals to a user-defd
nonterminal - Specify morphing (into host syntax) inductively
- Non-local transformations (multiple results)
metamorph ltn,ngt m() macro ltstmgt ? ltm A, Bgt
ltAgt ltBgt morph ltmgt ?
35Metamorph Example enum
decls ? enum id enums enums ? , id
enums ? ?
36Metamorph Example enum
decls ? enum id enums enums ? , id
enums ? ?
const int x 0 const int y 1 const int z
2
?
enum x, y, z
37Metamorph Example enum
decls ? enum id enums enums ? , id
enums ? ?
const int x 0 const int y 1 const int z
2
?
enum x, y, z
- Without compile-time programminglanguage (with
AST values)
38Metamorph Example enum
decls ? enum id enums enums ? , id
enums ? ?
int e 0 const int x e const int y
e const int z e
?
enum x, y, z
- Without compile-time programminglanguage (with
AST values)
39Metamorph Example enum
- metamorph ltdeclsgt enums()
decls ? enum id enums enums ? , id
enums ? ?
40Metamorph Example enum
- metamorph ltdeclsgt enums()
- macro ltdeclsgt ? enum ltid Xgt ltenums Dsgt
- int e 0
- const int ltXgt e
- ltDsgt
-
decls ? enum id enums enums ? , id
enums ? ?
41Metamorph Example enum
- metamorph ltdeclsgt enums()
- macro ltdeclsgt ? enum ltid Xgt ltenums Dsgt
- int e 0
- const int ltXgt e
- ltDsgt
-
- morph ltenumsgt ? , ltid Xgt ltenums Dsgt
- const int ltXgt e
- ltDsgt
-
decls ? enum id enums enums ? , id
enums ? ?
42Metamorph Example enum
- metamorph ltdeclsgt enums()
- macro ltdeclsgt ? enum ltid Xgt ltenums Dsgt
- int e 0
- const int ltXgt e
- ltDsgt
-
- morph ltenumsgt ? , ltid Xgt ltenums Dsgt
- const int ltXgt e
- ltDsgt
-
- morph ltenumsgt ?
decls ? enum id enums enums ? , id
enums ? ?
43Metamorph Example reserve
stm ? reserve ( res ) stm res ? id res ?
?
acquire(a) acquire(b) acquire(c) ...
release(c) release(b) release(a)
?
reserve ( a b c ) ...
44Multiple ResultsExample reserve
metamorph ltstms,stmsgt res()
stm ? reserve ( res ) stm res ? id res ?
?
45Multiple ResultsExample reserve
metamorph ltstms,stmsgt res() macro ltstmgt ?
reserve ( ltres S1,S2gt ) ltstm Sgt ltS1gt
ltSgt ltS2gt
stm ? reserve ( res ) stm res ? id res ?
?
46Multiple ResultsExample reserve
metamorph ltstms,stmsgt res() macro ltstmgt ?
reserve ( ltres S1,S2gt ) ltstm Sgt ltS1gt
ltSgt ltS2gt morph ltresgt ? ltid Xgt ltres S1,S2gt
acquire(ltXgt) ltS1gt ltS2gt
release(ltXgt)
stm ? reserve ( res ) stm res ? id res ?
?
47Multiple ResultsExample reserve
metamorph ltstms,stmsgt res() macro ltstmgt ?
reserve ( ltres S1,S2gt ) ltstm Sgt ltS1gt
ltSgt ltS2gt morph ltresgt ? ltid Xgt ltres S1,S2gt
acquire(ltXgt) ltS1gt ltS2gt
release(ltXgt) morph ltresgt ?
stm ? reserve ( res ) stm res ? id res ?
?
48Metamorph Advantages
- Flexibility
- Safety
- Simplicity
49Metamorph Advantages
- Flexibility
- Tree structures
- Non-local transformations (multiple results)
- Safety
- Simplicity
50Metamorph Advantages
- Flexibility
- Tree structures
- Non-local transformations (multiple results)
- Safety
- No parse errors as a conseq. of macro expansion
- Guaranteed termination
- Simplicity
51Metamorph 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 conceptsgrammars
and substitution
52vDSLvery Domain Specific Language
- studies
- course Math101
- title Mathematics 101
- 2 point fall term
-
- exclusions
- Math101 ltgt MathA
- Math102 ltgt MathB
- prerequisites
- Math101, Math102 lt Math201, Math202,
Math203 - Math101, CS101 lt CS202
53Specificity Parsing
macro ltexpgt ? select ltid Igt from ltexp Egt where
ltexp E2gt macro ltexpgt ? select all from ltexp
Egt where ltexp E2gt
54Specificity Parsing
macro ltexpgt ? select ltid Igt from ltexp Egt where
ltexp E2gt macro ltexpgt ? select all from ltexp
Egt where ltexp E2gt
- Challenge rounds
- Select most specific productions (wrt. FIRST sets)
55Specificity Parsing
macro ltexpgt ? select ltid Igt from ltexp Egt where
ltexp E2gt macro ltexpgt ? select all from ltexp
Egt where ltexp E2gt
- 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
56Related Work Macro Survey
- 8x Macro languages
- CPP, M4, TEX, Dylan, C Templates, Scheme,
JTS, MS2
57Related 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,
58Related 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,
59Related 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
60Future Work metafront
- Extensible syntax processor, based on
- Specificity parsing and
- Metamorphic syntax macros
base grammar L macros L ? L program in L
metafront
program in L
61Future Work metafront
- Extensible syntax processor, based on
- Specificity parsing and
- Metamorphic syntax macros
- Safe transformation L ? L
base grammar L macros L ? L program in L
metafront
program in L
62Future Work metafront
- Extensible syntax processor, based on
- Specificity parsing and
- Metamorphic syntax macros
- Safe transformation L ? L
- Safe transformation L ? L, (vDSL)
base grammar L macros L ? L program in L
metafront
program in L
63Conclusion
- Metamorphic Syntax Macros is a
- flexible
- safe
- simple
- way of Growing Languages
64Conclusion
- Metamorphic Syntax Macros is a
- flexible
- safe
- simple
- way of Growing Languages
- Fully implemented in ltbigwiggt
- (language for developing interactive Web services)
http//www.brics.dk/bigwig/
65The End
66Pretty Printing Error Reporting
- Pretty Printing
- Terminal printersASCII, LA EX, HTML(/-
expansion) - Error Reporting
- stdout, HTML
T
symbol errors test.wig7 Identifier
inf not declared in macro argument S
in macro invocation reader (test.wig7)
defined in std.wigmac44
67?-Conversion
macro ltstmgt ? repeat ltstm Sgt until ( ltexp Egt )
bool first true while
(first !ltEgt) ltSgt first false
68?-Conversion
- metamorph ltdeclsgt enums()
- macro ltdeclsgt ? enum ltid Xgt ltenums Dsgt
- int e 0
- const int ltXgt e
- ltDsgt
-
- morph ltenumsgt ? , ltid Xgt ltenums Dsgt
- const int ltXgt e
- ltDsgt
-
- morph ltenumsgt ?
decls enum id enums enums , id
enums ?
69Example switch
- metamorph ltstmgt swb()
- macro ltstmgt ? switch ( ltexp Egt ) ltswb Sgt
- var x ltEgt
- ltSgt
-
- morph ltswbgt ? case
- ltexp Egt ltstms Ssgt break ltswb
Sgt - if (xltEgt) ltSsgt else ltSgt
-
- morph ltswbgt ? case ltexp Egt ltstms Ssgt break
- if (xltEgt) ltSsgt
stm switch ( exp ) swb swb case exp
stms break swb case exp stms break
70Metamorph Wellformedness
- Check at definition time
- no left recursion (guarantees termination)
- ? ?
- derivability (metamorphisms must derive
something finite) - ? ?
xlist ? xlist X ? ?
xlist ? X xlist ? ?
xlist ? X xlist ? ?
xlist ? X xlist
71Representation
macro ltidsgt ? MXY ( ltids Isgt ) X, ltIsgt, Y
A, MXY(B, C), D ? A, X, B, C, Y, D
weave
- Weaving yields transparency!
72Syntax Macros
- MSYN (AST)n ? AST , n ? 0
-
- square( ) ?
exp
macro ltexpgt ? square ( ltexp Egt ) ltEgt
ltEgt
exp E
exp E
exp
exp
y 1
exp E
exp E
y 1
y 1