2. External DSL: simplest example
entity A {
number n
text t
t = “Hello World”
n = 5
print t
print n
}
entity B extends A {
number m
m = n
print m
}
language of entities
entity similar to Java class
field
can extend another entity
assignment statement
print statement
inheritance of fields
no expressions
can be integer, string, Boolean
to simplify development
scopes of visibility
data types
3. Crash course on compilers
entity A {
number amount
text hi
hi = “Hello World”
amount = 500
print hi
print n
}
entity B extends A {
number m
m = amount
print m
}
source code
find lexemes
figure out constructions
check context conditions
map constructions to new code
amount ident{ openparen 500 number
IntFieldDecl = “number” ident
precise name of a field doesn’t matter
all variables declared
IntFieldDecl(ident) “int” ident
no duplicate variables types match in assignments
lexical analysis
syntax analysis
semantic analysis
code generation optimization
x:=0; x:=1; becomes x:=1;
internal
representation
symbol table scoping rules
scope – set of allowed
targets for a reference
4. Crash course on compilers
function foo($a) {
// ...
}
source code in PHP
abstract syntax tree (AST)
Internal representation
program model
much more useful representation of program
can be manipulated by walking the tree
5. Semantic model
entity A {
number amount
text hi
hi = “Hello World”
amount = 500
print hi
print n
}
entity B extends A {
number m
m = amount
print m
}
semantic model
represents the same subject that DSL describes
Example
separation of concerns between language and semantics
object model can take other forms as well
A: amount like 0, hi like ’a’
hi is ‘Hello world’
amount is 500
@hi @amount
(A)B: m like 0
m is amount
@m
possible other
syntax for this DSL
metamodel
6. Semantic model vs. AST
semantic model
HelloWorld {
Component content = VerticalLayout {
Component myLabel = Label {
String Label caption = “Hello world!”;
}
Component myButton = Button {
String Button caption = “Push Me!”;
Function changeCaption = () {
caption = “Pushed!”;
} listens to click;
}
}
}
TouchScript (H. Westergård, 2013)
abstract syntax tree
7. Parser generators
automatically generate parser from grammar description
entity A {
number amount
text hi
hi = “Hello World”
amount = 500
print hi
print n
}
entity B extends A {
number m
m = amount
print m
}
Entity
‘entity’ ident [‘extends’ ident] ‘{‘
{FieldDeclaration}
{Statement}
‘}’
FieldDeclaration (‘number’ | ‘text’) ident
Statement Assignment | Print
Assignment ident ‘=‘ Expression
Expression integer_value | ident
Print ‘print’ Expression
grammar
[ ] – for optional parts
{ } – for parts that can
repeat any number of times
( ) – for grouping
| – for alternatives
lex/yacc CoCo/R ANTLR
8. Writing a grammar Ohm editor
https://ohmlang.github.io/editor/
http://tinlizzie.org/~awarth/live2016/
abstract syntax tree