developing domain specific languages for the jvm n.
Skip this Video
Loading SlideShow in 5 Seconds..
Developing Domain-Specific Languages for the JVM PowerPoint Presentation
Download Presentation
Developing Domain-Specific Languages for the JVM

Loading in 2 Seconds...

play fullscreen
1 / 22

Developing Domain-Specific Languages for the JVM - PowerPoint PPT Presentation

  • Uploaded on

Developing Domain-Specific Languages for the JVM. Travis Dazell Systems Architect Digi -Key Corporation. Outline. What is a Domain Specific Language (DSL)? Internal Versus External DSLs Internal DSLs Java Example without a DSL Improved Java Example using Internal DSL Patterns

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Developing Domain-Specific Languages for the JVM' - gigi

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
developing domain specific languages for the jvm

Developing Domain-Specific Languages for the JVM

Travis Dazell

Systems Architect

Digi-Key Corporation

  • What is a Domain Specific Language (DSL)?
  • Internal Versus External DSLs
  • Internal DSLs
    • Java Example without a DSL
    • Improved Java Example using Internal DSL Patterns
    • Groovy for Internal DSLs
  • External DSLs
    • Writing a new language to solve our example problem
      • ANTLR
      • JetBrains MPS
      • Scala Parser Combinators
what is a dsl
What is a DSL?
  • DSLs have been around for a long time
    • SQL, CSS, Regular Expressions
  • DSLs are small languages, focused on a specific problem
  • DSLs are easy to use and understand, even for a non-programmer
    • Often accomplished through layers of abstraction
  • The DSL language relates directly to the problem domain
example problem golf course scheduler
Example Problem: Golf Course Scheduler
  • To demonstrate how DSLs can be used, we’ll look at writing a simple language for scheduling tee times at a golf course
our goal
Our Goal
  • Before

TeeTimeteeTime = new TeeTime();

teeTime.setTime(“Sep-23-2013 2:15 PM”);

Golfer golfer = new Golfer();






  • After (as a DSL)

new tee time at Sep-23-2013 2:15 PM for Travis Dazell and 3 guests

can we make the java dsl even better
Can We Make the Java DSL Even Better?
  • An internal Java DSL is limited by the inherent language constraints
    • Semi-colons are required
    • Parentheses for invoking methods are not optional
    • Dots cannot be avoided when dispatching methods
    • We often need to build a significant amount of indirection to achieve an expressive DSL
      • Example: Builder Pattern
solving the problem with a groovy dsl
Solving the Problem with a Groovy DSL
  • Optional semicolons
  • We can omit parentheses in many cases
  • Closures
  • methodMissing
  • Metaprogramming
  • DSL Descriptors for syntax highlighting and help hovers
using external dsls
Using External DSLs
  • What happens when we need to step outside of a host language?
  • External DSLs are custom languages
  • We write a grammar to define the syntax for our language
  • We need to write a lexerand parser
  • We may need to perform semantic analysis
  • We need to write an interpreter or code generator
  • Thankfully, there are tools that make this easier

Live Coding Demo

antlr review
ANTLR Review
  • We define our EBNF grammar
  • We can mix Java code into our grammar for processing input scripts
    • Note that ASTs can be constructed for more complex processing
  • ANTLR will generate the lexer and parser for us
scala parser combinators
Scala Parser Combinators

Live Coding Demo

scala parser combinators review
Scala Parser Combinators Review
  • You have the privilege of working entirely within Scala
  • You define parsing rules in an EBNF-like format
  • You combine each of your parsing rules to form your DSL syntax
  • Like the other examples, you can interpret the input and generate whatever output you need
jetbrains mps
JetBrains MPS

Live Coding Demo

jetbrains mps review
JetBrains MPS Review
  • Takes DSL design to a more abstract level
  • You work on the concepts and structure of your DSL, instead of the low-level grammar
  • You do very little, if any, traditional coding in the IDE
  • You can generate all kinds of output. We auto-generated Java code in this example
  • You can test your DSLs within the MPS IDE or export your generated Java code to another IDE for integration
applying dsls in your projects
Applying DSLs in Your Projects
  • Adapt legacy code
    • Wrapper APIs
  • Develop a DSL for a particular sub-system (i.e. domain) of your application
  • Flesh-out requirements and test cases
    • Using a DSL to code your business rules can help bridge the gap from requirements to implementation
    • You can show DSL source code to a business analyst and they’ll understand it completely
  • Enhancing custom IT tasks
    • Operators, System Administrators
  • References
    • DebasishGhosh, DSLs In Action, Manning, 2011
    • Fergal Dearle, Groovy for Domain-Specific Languages, PACKT, 2010
    • Cay S. Horstmann, Scala for the Impatient, Addison-Wesley, 2012
    • Martin Fowler, Domain-Specific Languages, 2010
    • Markus Voelter, DSL Engineering: Designing, Implementing, and Using Domain-Specific Languages, 2013
related javaone sessions
Related JavaOne Sessions
  • Attend these JavaOne sessions to learn more
    • Embedded DSL: Groovy and Scala Fair Duel
      • Monday 4:30 PM – 6:30 PM, Hilton Continental Ballroom 1/2/3
    • BOF2893: Scala Parser Combinators
      • Tuesday 7:30 PM – 8:15 PM, Hilton Yosemite B/C
    • CON5389: Groovy DSLs: Beginner to Expert
      • Wednesday 11:30 AM-12:30 PM, Hilton Golden Gate 4/5
    • CON2077: Integrating JVM Languages
      • Thursday 12:30 PM – 1:30 PM, Hilton Yosemite B/C
  • DSLs allow you to develop software using syntax that fits within the problem domain
  • Internal DSLs can be written in many JVM languages
    • Other JVM languages are also well-suited for internal DSL development, such as JRuby, Clojure, and Scala
  • External DSLs give you the flexibility to design the language any way you want
    • More involved, but thanks to tools like ANTLR, JetBrains MPS, and Scala Parser Combinators, this isn’t difficult
  • Contact Information:
  • Code examples are available on GitHub:
  • Follow me:
    • Blog:
    • Twitter: @travisdazell