Model-Driven Software Development - Pretty-Printing, Editor Services, Term Rewriting
1. Pretty-Printing
Editor Services
Term Rewriting
Lecture 7
Course IN4308
Eelco Visser
Master Computer Science
http://eelcovisser.org Delft University of Technology
2. Outline
Pretty-printing
★ from abstract syntax to concrete syntax
Editor services
★ defining the behavior of editors
Term rewriting
★ transforming abstract syntax
24. Term Rewriting
Term rewrite rules
★ transform term to term
★ pattern matching
★ variable binding
★ substitution
Rewriting strategy
★ algorithm for applying rewrite rules
25. Grammar to Signature
context-free syntax
"entity" ID "{" Property* "}" -> Definition {cons("Entity")}
ID ":" Type -> Property {cons("Property")}
ID ":" Type "(" {Annotation ","}* ")" -> Property {cons("Property")}
module nwl
signature
constructors
Entity : ID * List(Property) -> Definition
Property : ID * Type * List(Annotation) -> Property
Property : ID * Type -> Property
30. Rewrite Strategy
generic strategy
strategy definition
desugar-all = innermost(desugar)
strategy instantiation
apply transformation s exhaustively to all sub-
innermost(s)
terms of subject term in bottom-up order
38. Conditional Rewrite Rules
bound in condition
eval :
BinOp(IntLit(x), "+", IntLit(y)) -> IntLit(z)
condition where z := <addS>(x, y)
match apply transformation
eval :
BinOp(IntLit(x), "+", IntLit(y)) -> IntLit(<addS>(x, y))
39. For to While
action foo() {
for(p : Post in b.posts) {
p.title := "foo";
} action foo ( ) {
} var i : Int := 0;
var c : Set<Post> := b.posts;
var l : Int := c.length();
while (i < l) {
var p : Post := c[i];
p.title := "foo" ;
i := i + 1;
}
}
specification (by example)
40. For to While: Transformation Schema
for(x : t in e) b
var i : Int := 0;
var c : Set<t> := e;
var l : Int := c.length();
while (i < l) {
var x : t := c[i];
b
i := i + 1;
}
abstraction of example
41. For to While: Rewrite Rule
normalize :
For(x, t, e, b) ->
Block([
Stat(VarDeclInit(i, SimpleType("Int"), IntLit("0"))),
Stat(VarDeclInit(c, SimpleType(t), e)),
Stat(VarDeclInit(l, SimpleType("Int"),
MethodCall(Var(c), "length", []))),
While(BinOp(Var(i), "<", Var(l)), Block([
Stat(VarDeclInit(x, t, IndexAccess(Var(c), Var(i)))),
b,
Assign(Var(i), BinOp(Var(i), "+", IntLit("1")))
]))
])
where i := <newname> "i";
c := <newname> "c";
l := <newname> "l"
42. For to While: Result
action foo() { specification by example
for(p : Post in b.posts) {
p.title := "foo";
action foo ( ) {
}
var i : Int := 0;
}
var c : Set<Post> := b.posts;
var l : Int := c.length();
action foo ( ) { while (i < l) {
{ var p : Post := c[i];
var i0 : Int := 0; p.title := "foo" ;
var c0 : Set<Post> := b.posts; i := i + 1;
var l0 : Int := c0.length(); }
while ( (i0 < l0) ) }
{
var p : Post := c0[i0];
p.title := "foo" ;
i0 := (i0 + 1) ;
}
}
} result
43. Normalization Strategy
strategies
normalize-all =
innermost(desugar + normalize)
rules
normalize :
For(x, t, e, b) ->
Block([
Stat(VarDecl(...
])
normalize :
[Block(stat1*) | stat2*] -> <conc> (stat1*, stat2*)
(is that a correct transformation?)
44. For to While: Final Result
action foo() { specification by example
for(p : Post in b.posts) {
p.title := "foo";
action foo ( ) {
}
var i : Int := 0;
}
var c : Set<Post> := b.posts;
var l : Int := c.length();
while (i < l) {
action foo ( ) { var p : Post := c[i];
var i0 : Int := 0; p.title := "foo" ;
var c0 : Set<Post> := b.posts; i := i + 1;
var l0 : Int := c0.length(); }
while ( (i0 < l0) ) }
{
var p : Post := c0[i0];
p.title := "foo" ;
i0 := (i0 + 1) ;
}
}
result
46. Schedule
Lab this week
★ Design 1: deadline is April 1
★ Design 2: what DSL will you design?
Cases
★ Case 2: web abstractions
★ Deadline Case 2: April 8
★ Case 3:
Next
★ Lecture 8: code generation