Growing Languages with Metamorphic Syntax Macros - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Growing Languages with Metamorphic Syntax Macros

Description:

Explicit programming on parse trees vs. Pattern matching and substitution ... Parse tree of macro could be transparent to following compiling phases: ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 27
Provided by: Guolia
Category:

less

Transcript and Presenter's Notes

Title: Growing Languages with Metamorphic Syntax Macros


1
Growing Languages with Metamorphic Syntax Macros
  • Claus Brabrand Michael I. Schwartzbach
  • BRICS, Dept. of Computer Science
  • University of Aarhus,Denmark

Presented by Guoliang Xing
Center for Distributed Object Computing Department
of Computer Science Washington University
2
Outline
  • Related Work Survey
  • Designing a Macro Language
  • Growing Language Concepts
  • Metamorphisms
  • Growing New Languages
  • Implementation

3
Related Work Survey
  • General properties
  • Syntax Properties
  • Type Properties
  • Definition Properties
  • Invocation Properties
  • Implementation properties

4
Related Work Survey contd.
  • CPP - C's preprocessor
  • M4 - the Unix macro preprocessor
  • TEXs built-in macro mechanism
  • The macro mechanism of Dylan
  • The C templates
  • Hygienic macros of Scheme
  • Macro mechanism of the Jakarta Tool Suite, JTS
  • Meta Syntactic Macro System, MS2

5
General properties
  • Lexical vs. Syntactical
  • Language dependence
  • Programmable
  • Explicit programming on parse trees vs. Pattern
    matching and substitution

6
Syntax Type Properties
  • How liberal an invocation syntax could be?
  • CPP requires parenthesized and comma separated
    arguments
  • Dylan almost arbitrary invocation syntax
  • Result types and argument types
  • Non-terminals of host language
  • Type checking in syntactical macro systems

7
Definition properties
  • Multiple definitions
  • Definition scope
  • One-pass vs. Two-pass
  • Recursion
  • Direct vs. Indirect
  • Argument structure
  • Fixed number vs. Variable number

8
Invocation Properties
  • Body expansion
  • Eager vs. Lazy
  • Order of expansion
  • Inner vs. Outer vs. Pre-scan
  • Parsing ambiguities
  • Hygienic expansion
  • Alpha Conversion

9
Implementation Properties
  • Transparence
  • Error trailing
  • Pretty printing
  • Package

10
(No Transcript)
11
Designing a Macro Language
  • Host language ltbigwiggt
  • http//www.brics.dk/bigwig/
  • Syntax
  • macro syntax ltnontermgt id ltparamgt body
  • param token
  • ltnonterm idgt

12
Simple Examples
  • syntax ltfloatconstgt pi
  • 3.1415927
  • syntax ltstmgt maybe ltstm Sgt
  • if (random(2)1) ltSgt
  • syntax ltstmgt repeat ltstm Sgt until (ltexp Egt)
  • bool first true
  • while (first !ltEgt)
  • ltSgt
  • first false

13
Parsing Definitions
  • Two phases
  • Collecting macro headers
  • Parsing macro bodies in topological order

14
Parsing Invocations
  • Tasks
  • Interpreting the macro parameter list
  • Matching required tokens
  • Collecting actual argument parse trees
  • Multiple definitions
  • Interpreting a set of parameter lists
  • Challenging each parameter list with the input
    tokens
  • Choosing the most specific one among survived
    lists

15
Growing Language Concepts
  • Growing new concepts and constructs in host
    language ltbigwiggt
  • Concurrency control between session threads
  • A stack of macros

16
Metamorphisms
  • Two aspects of Macro definitions
  • Syntax definition
  • Syntax transformation aspect
  • Metamorphisms
  • User defined non-terminal along with a rule
    specifying how the new macro syntax is morphed
    into host language syntax

17
Metamorphismscontd.
  • syntax ltdeclsgt enum ltid Xgt
  • const int ltXgt 0
  • syntax ltdeclsgt enum ltid Xgt , ltid Ygt
  • const int ltXgt 0
  • const int ltYgt 1
  • syntax ltdeclsgt enum ltid Xgt , ltid Ygt , ltid Zgt
  • const int ltXgt 0
  • const int ltYgt 1
  • const int ltZgt 2

18
Metamorphismscontd.
  • decls enum id
  • enum id , id
  • enum id , id , id
  • Extending the grammar by notation N
  • decls enum id
  • Extending the grammar by introducing a
  • user-defined non-terminal enums
  • decls enum id enums
  • enums , id enums
  • ?

19
Revised Macro Syntax
  • macro syntax ltnontermgt id ltparamgt body
  • metamorph ltnontermgt id --gt
    ltparamgt body
  • param token
  • ltnonterm idgt
  • ltid nonterm idgt

20
Example Revised
  • syntax ltdeclsgt enum ltid Igt ltenums decls Dsgt
  • int e 0
  • const int ltIgt e
  • ltDsgt
  • metamorph ltdeclsgt enums --gt , ltid Igt ltenums
    decls Dsgt
  • const int ltIgt e
  • ltDsgt
  • metamorph ltdeclsgt enums --gt

21
Growing New Language
  • Designing new language with metamorphism
  • Host language ltbigwiggt
  • Very domain-specific language (VDSL)
  • Customizable with low cost
  • No host language grammars are needed in new
    language

22
Example of VDSL--Backgroud
  • Bachelor's Contract at computer science
    department of University of Aarhus
  • CS students must complete a BS in one of several
    fields Physics, Mathematics etc.
  • The requirements are complicated
  • Web-based service needed to guide student
  • Must be easy to maintain

23
Example of VDSL Impl.
  • require "bach.wigmac"
  • studies
  • course Math101
  • title "Mathematics 101"
  • 2 points fall term
  • ...
  • course Lab304
  • title "Lab Work 304"
  • 1 point fall term
  • exclusions
  • Math101 ltgt MathA
  • Math102 ltgt MathB
  • prerequisites
  • Math101,Math102 lt Math201,Math202,Math203,Math204
  • CS101,CS102 lt CS201,CS203
  • CS202,CS203 lt CS301,CS302,CS303,CS304
  • Phys101,Phys102 lt Phys201,Phys202,Phys203,Phys301
  • Lab101,Lab102 lt Lab201,Lab202
  • field "CS-Mathematics"

24
Implementation
  • Transparent Representation
  • Parse tree of macro could be transparent to
    following compiling phases
  • syntax ltidsgt xIDy ( ltids Isgt ) X,ltIsgt,Y

25
Implementationcontd.
  • Generic Pretty Printing
  • Four indent directives control the pretty
    printing of macros
  • param ltwhitespacegt \n \ \-
  • Example
  • syntax ltstmgt switch (ltexp Egt) \\nltswbody
    stm Sgt\-\n ...
  • Error Reporting
  • Error messages can be viewed with or without
    macro expansion

26
Conclusion and Future Work
  • Contribution A macro language that can grow
    domain-specific extensions of host languages or
    even entire new languages
  • Future Work
  • Invocation constraints that restrict the possible
    uses of macros.
  • Implementations for other host languages (Java
    etc).
  • parser generator
Write a Comment
User Comments (0)
About PowerShow.com