The document discusses domain-specific languages (DSLs) and compares two approaches to creating DSLs using Groovy and ANTLR. It provides an example of a simple turtle DSL to demonstrate moving a turtle and describes how such a DSL could be implemented in both Groovy and ANTLR. Groovy allows for quick DSL implementation but has limitations around security and syntax restrictions. ANTLR requires more work to define the grammar and build interpreters but provides more flexibility and control over the language syntax. Both approaches have their pros and cons for different use cases and security requirements.
DSL-Optimized Title for Domain Specific Languages Document
1. ahead of one’s time
Domain Specific Languages
Groovy, Antlr
2. “Computer programming language of limited
expressivenes focused on a particular domain”
Specialized to solve problems within that domain
Easy to understand
Executable by computer
Not necessarily a completely new language(Internal/External)
HTML, CSS, RegEx, XSLT, Spring xml configs, SQL...
What is Domain specific language?
3. DSL goals
To be more expressive in its domain – more powerful
Include domain experts in application development
Extraction of bussines logic – separate lifecycle
Easier work in its domain
4. Let's think about Turtle
● It can move in 2 dimensional space
● It can draw paths
● We can turn it, move and reset it
DSL Example – problem
5. Requirements & Goals
We want to move the turtle by DSL
We want to execute the DSL actions one by one
Comparison between Groovy and Antlr
6. DSL Example
●
4 actions
– Parametrized
●
excluding reset
– changing state of the turtle
– changing state of the world
●
Sequential evaluation
turn left
move 5
turn right
move 30
pen down
turn right
move 5
turn right
move 3
pen up
reset
7. DSL Example – Semantic model
Representation of the same subject DSL describes
DSL populates the semantic model
Covers semantic aspects of the problem
Not depending on the DSL
In this case quite simple:
Action Turtle World
9. DSL in Groovy
GroovyShell shell = new GroovyShell();
Script script = shell.parse(config);
script.run();
…..aand we are done, right?
10. DSL in Groovy
...not yet
Let's see how its evaluated
turn left
move 5
pen down
turn right
reset
●
●
●
turn(left) // left is a var
move(5) // 5 is number
pen(down) // down is var
turn(right) // right is var
reset // reset is var
11. DSL in Groovy – Binding methods
●
● Binding binding = new Binding()
● binding.setVariable("move", new Closure<Object>(null) {
– @Override
– public Object call(Object... args) {
● turtleActions.add(new MoveAction((Integer)
args[0]));
● return null;
– }
● });
15. Antlr
●
● ANTLR (ANother Tool for Language Recognition)
● Powerful parser generator for reading, processing,
executing, or translating structured text or binary files.
● From a grammar, ANTLR generates a parser that can build
and walk parse trees.