1 / 72

Growing Languages with Metamorphic Syntax Macros

Growing Languages with Metamorphic Syntax Macros. Claus Brabrand Michael Schwartzbach BRICS , University of Aarhus, Denmark. Outline. Introduction Metamorphisms vDSL Specificity parsing Related and future work Conclusion. Lexical Macros. M LEX : (TOKENS) n  TOKENS, n  0.

brooke
Download Presentation

Growing Languages with Metamorphic Syntax Macros

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

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

  3. Lexical Macros • MLEX: (TOKENS)n TOKENS, n  0 PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

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

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

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

  7. 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) PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  8. 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) PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

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

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

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

  12. Invocation Syntax #define swap(X,Y) { int t=X; X=Y; Y=t; } 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;  PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  13. Invocation Syntax #define swap(X,Y) { int t=X; X=Y; Y=t; } • Problem: fixed invocation syntax! • same for exp / stm / … 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;  M(x,y,z) PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  14. Syntax Macro • MSYN: (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

  15. 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 repeatstmuntil ( exp ) ; PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  16. 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 repeatstmuntil ( exp ) ; macro<stm>  repeat <stm S> until ( <exp E> ) ; ::= { { bool first = true; while (first || !<E>) { <S> first = false; } } } PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  17. 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 repeatstmuntil ( exp ) ; macro<stm>  repeat <stm S> until ( <exp E> ) ; ::= { { bool first = true; while (first || !<E>) { <S> first = false; } } } • Invocation syntax: • grammar extension PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  18. 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 repeatstmuntil ( exp ) ; macro<stm>  repeat <stm S> until ( <exp E> ) ; ::= { { bool first = true; while (first || !<E>) { <S> first = false; } } } • Invocation syntax: • grammar extension • Transformation: • morphing into • host syntax PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  19. Syntax Flexibility? macro<decls>enum { <id X> }; ::= {…} macro<decls>enum { <id X>, <id Y> }; ::= {…} macro<decls>enum { <id X>, <id Y>, <id Z> }; ::= {…} PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  20. Syntax Flexibility? 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 } ; PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

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

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

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

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

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

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

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

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

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

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

  31. 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> …} PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  32. 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> … ::= { …} PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  33. 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> … ::= { …} PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  34. 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> … ::= { …} { …} PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

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

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

  37. Metamorph Example: enum decls enum { idenums } ; enums , idenums  • Without compile-time programminglanguage (with AST values) constint x = 0; constint y = 1; constint z = 2;  enum { x, y, z }; PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  38. Metamorph Example: enum decls enum { idenums } ; enums , idenums  • Without compile-time programminglanguage (with AST values) int e = 0; constint x = e++; constint y = e++; constint z = e++;  enum { x, y, z }; PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

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

  40. Metamorph Example: enum metamorph<decls>enums(); macro<decls>enum { <id X><enums: Ds> } ; ::= { int e = 0; constint<X> = e++; <Ds> } decls enum { idenums } ; enums , idenums  PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  41. Metamorph Example: enum metamorph<decls>enums(); macro<decls>enum { <id X><enums: Ds> } ; ::= { int e = 0; constint<X> = e++; <Ds> } morph<enums> , <id X><enums: Ds> ::= { constint<X> = e++; <Ds> } decls enum { idenums } ; enums , idenums  PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  42. Metamorph Example: enum metamorph<decls>enums(); macro<decls>enum { <id X><enums: Ds> } ; ::= { int e = 0; constint<X> = e++; <Ds> } morph<enums> , <id X><enums: Ds> ::= { constint<X> = e++; <Ds> } morph<enums> ::= { } decls enum { idenums } ; enums , idenums  PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

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

  44. Multiple ResultsExample: reserve metamorph<stms,stms>res(); stmreserve ( res) stm residres   PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  45. Multiple ResultsExample: reserve metamorph<stms,stms>res(); macro<stm>reserve ( <res: S1,S2> ) <stm S> ::= { { <S1><S><S2> } } stmreserve ( res) stm residres   PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  46. 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>); } stmreserve ( res) stm residres   PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

  47. 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> ::= { } { } stmreserve ( res) stm residres   PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002

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

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

  50. 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

More Related