Title: Ingen bildrubrik
1Declarative OO Language Implementation using
JastAdd
Torbjörn Ekman, University of OxfordGörel
Hedin, Lund University
2Agenda
- Introduction principles, mechanisms,
- Demo implement a small language from scratch
- Demo extend Java with the foreach loop
- Getting started skeletons, reusable components,
3Things you can do with JastAdd
- Implement a compiler
- Java, Modelica, RAPID
- Extend with new language constructs
- Java 1.4 ??Java 5 ??AspectJ
- Extend with new source code analyses
- non-null analysis, dataflow analysis, CK metrics,
- Build tools for domain-specific languages
- consistency analysis, source code generation,
- Source-to-source translation
- normalization, prettyprinting, update code to use
new library, - Generate IDEs
- name completion, refactoring support,
4Principle 1
Dont do semantics in the parser.Just build the
tree.
5Example output
JastAdd
bytecode
source code
scanner parser
AST
another AST
Do the complex things here!
Build the tree
analysis results
6Principle 2
The AST is the only datastructure you need
7The usual way
AST
x
symbol table
x
flow graph
8Principle 3
Think declaratively!
9Attribute grammars
What attributes would I like this node to have?
Define by equations a f(b, c, d) b c d
a
10Synthesized and inherited attributes
Inherited attribute A parent defines the
attribute
child.i
s
i
s
Synthesized attribute The node defines the
attribute
11Think declaratively! Example
Are assignments type correct?
Assign
Ida
Add
Attributes
syn String Exp.type() eq Add.type()
integer eq Id.type() decl().type() syn
Id.decl() eq Id.decl() lookup(name) inh
Id.lookup(String s) eq Method.getBody().lookup(s)
Idb
Idc
a bc
12JastAdd solves the equations
Think declaratively!
The AST is automatically correctly attributed!
13Main program
Program
Program p parse(inputfile) p.printErrors()
Assign
14Summary of principles
- The parser just builds the AST
- The AST is the only datastructure you need
- Think declaratively!
15Mechanisms
- Basics
- Synthesized and inherited attributes
- Reference attributes
- Parameterized attributes
- Broadcasting inherited values to subtree
- Intertype declarations for modularizing across
AST - Integration with imperative code (can use the
attributes) - Advanced
- Circular attributes (fixed-point evaluation)
- Collection attributes (e.g., for
cross-references) - Nonterminal attributes (for growing the AST)
- Rewrites (for adjusting the AST)