1. Challenge the future
Delft
University of
Technology
Course IN4303 2013/14
Compiler Construction
Guido Wachsmuth
Declarative Semantics Definition
Term Rewriting
2. today’s lecture
Term Rewriting
Overview
manual implementation
• static analysis & error checking
• code generation
• semantic editor services
domain-specific language
• Stratego
• transform ASTs
• working on ATerms
• declarative style
2
10. compilation + compilation
Term Rewriting
Traditional Compiler Compilers
6
Language
Implementation
compile
Compiler
Language
Definition
compile
11. compilation + interpretation
Term Rewriting
Traditional Compiler Compilers
7
interpret
Interpreter
Language
Definition
compile
Language
Implementation
12. compilation + interpretation
Term Rewriting
Spoofax Language Workbench
8
interpret
Generic
Parser
Syntax
Definition
compile
Parse Table
13. manual implementation
Term Rewriting
Spoofax Language Workbench
Stratego
• declarative DSL
• domain: program transformation
• means to manipulate ASTs
• working on ATerms
• compiles to C or Java
Stratego in Spoofax
• static analysis & error checking
• code generation
• semantic editor services
9
14. concepts
Term Rewriting
Stratego
signatures
rewrite rules
• transform term to term
• left-hand side
• pattern matching & variable binding
• right-hand side
• pattern instantiation & variable substitution
rewrite strategies
• algorithm for applying rewrite rules
10
21. Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printint(x)
else
()
IfThen(
Var("x")
, Call(
"printint"
, [Var("x")]
)
)
IfThenElse(
Var("x")
, Call(
"printint"
, [Var("x")]
)
, NoVal()
)
22. desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal())
Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printint(x)
else
()
IfThen(
Var("x")
, Call(
"printint"
, [Var("x")]
)
)
IfThenElse(
Var("x")
, Call(
"printint"
, [Var("x")]
)
, NoVal()
)
23. desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal())
Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printint(x)
else
()
IfThen(
Var("x")
, Call(
"printint"
, [Var("x")]
)
)
IfThenElse(
Var("x")
, Call(
"printint"
, [Var("x")]
)
, NoVal()
)
pattern matching
24. desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal())
Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printint(x)
else
()
IfThen(
Var("x")
, Call(
"printint"
, [Var("x")]
)
)
IfThenElse(
Var("x")
, Call(
"printint"
, [Var("x")]
)
, NoVal()
)
pattern matching
e1
e2
25. desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal())
Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printint(x)
else
()
IfThen(
Var("x")
, Call(
"printint"
, [Var("x")]
)
)
IfThenElse(
Var("x")
, Call(
"printint"
, [Var("x")]
)
, NoVal()
)
pattern matching pattern instantiation
e1
e2
26. desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal())
Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printint(x)
else
()
IfThen(
Var("x")
, Call(
"printint"
, [Var("x")]
)
)
IfThenElse(
Var("x")
, Call(
"printint"
, [Var("x")]
)
, NoVal()
)
pattern matching pattern instantiation
e1
e2
48. rules and strategies
Term Rewriting
Transformation Definitions
rewrite rules
• term to term
• left-hand side matching
• right-hand side instantiation
• conditional
• partial transformation
rewrite strategies
• rule selection
• algorithm
• composition of transformations
27
62. rules and strategies
Term Rewriting
Strategy Combinators
named rewrite rule
l: t1 -> t2 where s ≣ l = ?t1 ; s ; !t2
32
63. rules and strategies
Term Rewriting
Strategy Combinators
named rewrite rule
l: t1 -> t2 where s ≣ l = ?t1 ; s ; !t2
unscoped rewrite rule
(t1 -> t2 where s) ≣ ?t1 ; s ; !t2
32
64. rules and strategies
Term Rewriting
Strategy Combinators
named rewrite rule
l: t1 -> t2 where s ≣ l = ?t1 ; s ; !t2
unscoped rewrite rule
(t1 -> t2 where s) ≣ ?t1 ; s ; !t2
strategy definition
f(x,y|a,b) = s
32
136. lessons learned
Term Rewriting
Summary
How do you define AST transformations in Stratego?
• signatures
• rewrite rules
• rewrite strategies
• strategy combinators
What kind of strategies can you find in the Stratego library?
• arithmetics
• map, zip, foldr
• generic traversals
56
138. learn more
Term Rewriting
Literature
Spoofax
Lennart C. L. Kats, Eelco Visser: The Spoofax Language Workbench.
Rules for Declarative Specification of Languages and IDEs. OOPSLA
2010
http://www.spoofax.org
57
139. learn more
Term Rewriting
Literature
Spoofax
Lennart C. L. Kats, Eelco Visser: The Spoofax Language Workbench.
Rules for Declarative Specification of Languages and IDEs. OOPSLA
2010
http://www.spoofax.org
Stratego
Martin Bravenboer, Karl Trygve Kalleberg, Rob Vermaas, Eelco Visser:
Stratego/XT 0.17. A language and toolset for program transformation.
Science of Computer Programming, 72(1-2), 2008.
http://www.strategoxt.org
57
140. coming next
Term Rewriting
Outlook
declarative semantics definition
• Lecture 5: Static Analysis and Error Checking
• Lecture 6: Code Generation
Assignment Oct 2: MiniJava grammar in SDF
• submit on Blackboard
Lab Oct 03
• syntactic editor services
• pretty-printer for MiniJava
58
142. attribution & copyrights
Term Rewriting
Pictures
Slides 1, 9, 10, 20, 27: Stratego by Kendrick Arnett, some rights reserved
Slides 23-26, 28, 31, 37-39, 41-43: PICOL icons by Melih Bilgil, some rights reserved
Slide 14: Thesaurus by Enoch Lau, some rights reserved
Slide 21: Google Maps Street View Camera by Ian Britton, some rights reserved
Slide 22: Zipper by Rabensteiner, some rights reserved
Slide 23: Fold by Kim Piper Werker, some rights reserved
Slide 24: Inverse by lanchongzi, some rights reserved
Slide 25: Maxwell's by Dominica Williamson, some rights reserved
Slide 58: Reichstag by Wolfgang Staudt, some rights reserved
60
Notas do Editor
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
feedback loop\n
feedback loop\n
feedback loop\n
feedback loop\n
feedback loop\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
round-up on every lecture\n\nwhat to take with you\n\ncheck yourself, pre- and post-paration\n