Download Katja Laboratory Abstract Syntax Trees in Java with Katja

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Katja Laboratory
Abstract Syntax Trees in Java with Katja
Lecture Compiler and Language Processing Tools 2011
Jan Schäfer
(slides by Jean-Marie Gaillourdet)
Software Technology Group
TU Kaiserslautern
c Software Technology Group, TU KL
Katja Lab
1
Introduction
What’s Katja?
• code generator for order-sorted datatypes in Java (and Haskell
and Isabelle/HOL)
• goal: simplify generation and handling of ASTs in Java
• enable the implementation of attribute grammar systems
(not quite yet)
c Software Technology Group, TU KL
Katja Lab
2
Introduction
Design decisions
• functional terms in Java
I immutable
I globally shared
I strutcural equality implies reference equality
• declarative specification as order-sorted datatypes
• statically typed (as far as possible)
c Software Technology Group, TU KL
Katja Lab
3
Introduction
Katja - The Command Line Tool
Executing Katja
java -jar katja.jar -b java -o -j spec.katja
Input: Specification of order-sorted datatype (*.katja)
• SpecificationName.jar
Output:
I Generated sources and class files
• katja-common.jar (only with -j)
I Base library of Katja
c Software Technology Group, TU KL
Katja Lab
4
Introduction
Katja Specifications
specification Formulas
external Integer
external String
Formula
( Expression top )
Expression = False | Implies | Not | Predicate
False
( )
Implies ( Expression left, Expression right )
Not
( Expression expr )
Predicate ( String name, Parameters vars )
Parameters * Variable
Variable ( Integer index )
c Software Technology Group, TU KL
Katja Lab
5
Introduction
Katja Specifications With Syntactic Sugar
specification Formulas
external Integer
external String
Formula
( Expression top )
Expression =
|
|
|
False
Implies
Not
Predicate
(
(
(
(
)
Expression left, Expression right )
Expression expr )
String name, Parameters vars )
Parameters * Variable
Variable ( Integer index )
c Software Technology Group, TU KL
Katja Lab
6
Introduction
Katja Terms
Formula
top()
Not
expr()
Predicate
name()
vars()
"P"
Parameters
get(1)
get(0)
c Software Technology Group, TU KL
get(2)
Variable
Variable
Variable
index()
index()
index()
17
4
17
Katja Lab
7
Introduction
What is generated?
• an interface per sort
• a specification class with static constructor methods
• hidden implementations and utility classes/interfaces
Example
import static softech.formula.Formulas.*;
import softech.formula.*;
... {
Formula f = Formula( Not( Predicate( "P",
Parameters( Variable(17), Variable(4),
Variable(42)))));
}
c Software Technology Group, TU KL
Katja Lab
8
Introduction
Last missing part
specification Formulas
backend java {
package softech.formulas
import java.lang.String
import java.lang.Integer
}
external String
external Integer
...
c Software Technology Group, TU KL
Katja Lab
9
Abstract Datatype aka. Terms
Expressions
Literal
Variable
Plus
Minus
Mult
Div
Expression
(
(
(
(
(
(
=
Integer value )
String name )
Expression left, Expression
Expression left, Expression
Expression left, Expression
Expression left, Expression
Plus | Minus | Mult | Div |
c Software Technology Group, TU KL
Katja Lab
right )
right )
right )
right )
Literal | Variable
10
Abstract Datatype aka. Terms
Switch Classes
• simulate switch construct of Java over variants
...
f.top().Switch( new Expression.Switch<Integer,NE> {
public CaseFalse() throws NE { ... }
public CaseImplies(Expression left, Expression right)
throws NE { ... }
public CaseNot(Expression expr) throws NE { ... }
public CasePredicate(String name, Parameters vars)
throws NE { ... }
});
c Software Technology Group, TU KL
Katja Lab
11
Abstract Datatype aka. Terms
Task 1
• create a Katja specification file for expressions
• compile it
• write a method Integer eval(Expression e) to evaluate
expressions without variables
• use an exception to signal the presence of variables
c Software Technology Group, TU KL
Katja Lab
12
Abstract Datatype aka. Terms
Solution
c Software Technology Group, TU KL
Katja Lab
13
Context-dependent Terms aka. Term-Positions
Terms vs. Term-Positions
term()
Formula
FormulaPos
top()
top()
parent()
term()
Not
NotPos
expr()
expr()
PredicatePos
Predicate
parent()
parent()
name()
name()
vars()
StringPos
ParametersPos
get(1)
get(0)
VariablePos
vars()
"P"
Parameters
get(0)
get(2)
get(1)
get(2)
Variable
Variable
Variable
index()
17
VariablePos
VariablePos
index()
index()
index()
index()
index()
IntegerPos
IntegerPos
IntegerPos
17
4
term()
root Formula Pos
c Software Technology Group, TU KL
Katja Lab
14
Context-dependent Terms aka. Term-Positions
What is generated?
• a term position interface per sort
• hidden implementations
• one addition constructor method (FormulaPos)
• default visitor
• common super-type (softech.formula.Formulas.SortPos)
c Software Technology Group, TU KL
Katja Lab
15
Context-dependent Terms aka. Term-Positions
Visitors
interface FormulaPos ... {
class DefaultVisitor ... {
void visitFormulaPos(FormulaPos term) { ... }
void visitNotPos(NotPos term) { ... }
void visitImpliesPos(ImpliesPos term) { ... }
...
}
}
c Software Technology Group, TU KL
Katja Lab
16
Context-dependent Terms aka. Term-Positions
Task 2
• implement a PrettyPrinter based on the DefaultVisitor
• decide whether to print out parenthesis based on the parent
c Software Technology Group, TU KL
Katja Lab
17
Context-dependent Terms aka. Term-Positions
Solution
c Software Technology Group, TU KL
Katja Lab
18
Context-dependent Terms aka. Term-Positions
Modifying Term Positions
• every term position can be replaced by a term of equivalent sort
• immutable!
• KatjaSort<R> replace(Object o)
• sometimes you have to use .cast()
c Software Technology Group, TU KL
Katja Lab
19
Context-dependent Terms aka. Term-Positions
Post- and Preorder
• every position has a preOrder() method, which goes to the next
position in pre order
• every position has postOrder and postOrderStart methods
c Software Technology Group, TU KL
Katja Lab
20
Context-dependent Terms aka. Term-Positions
Task 3
• implement a method simplify which simplifies constant
expressions to constants
• use a post order path through the positions
c Software Technology Group, TU KL
Katja Lab
21
Related documents