• Keine Ergebnisse gefunden

What’s Katja?

N/A
N/A
Protected

Academic year: 2022

Aktie "What’s Katja?"

Copied!
21
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

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

(2)

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)

(3)

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)

(4)

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) Output: SpecificationName.jar

I Generated sources and class files

katja-common.jar (only with-j)

I Base library of Katja

(5)

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 )

(6)

Introduction

Katja Specifications With Syntactic Sugar

specification Formulas external Integer

external String

Formula ( Expression top ) Expression = False ( )

| Implies ( Expression left, Expression right )

| Not ( Expression expr )

| Predicate ( String name, Parameters vars ) Parameters * Variable

Variable ( Integer index )

(7)

Katja Terms

Formula

Not

Predicate

"P" Parameters

17 Variable

4 Variable

17 Variable top()

expr()

name() vars()

get(0) get(1) get(2)

index() index() index()

(8)

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)))));

}

(9)

Last missing part

specification Formulas backend java {

package softech.formulas import java.lang.String import java.lang.Integer }

external String external Integer ...

(10)

Abstract Datatype aka. Terms

Expressions

Literal ( Integer value ) Variable ( String name )

Plus ( Expression left, Expression right ) Minus ( Expression left, Expression right ) Mult ( Expression left, Expression right ) Div ( Expression left, Expression right )

Expression = Plus | Minus | Mult | Div | Literal | Variable

(11)

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 { ... } });

(12)

Abstract Datatype aka. Terms

Task 1

create a Katja specification file for expressions

compile it

write a methodInteger eval(Expression e)to evaluate expressions without variables

use an exception to signal the presence of variables

(13)

Solution

(14)

Context-dependent Terms aka. Term-Positions

Terms vs. Term-Positions

FormulaPos

NotPos

PredicatePos

StringPos ParametersPos

IntegerPos VariablePos

IntegerPos VariablePos

IntegerPos VariablePos top()

expr()

name() vars()

get(0) get(1) get(2)

index() index() index()

Formula

Not

Predicate

"P" Parameters

17 Variable

4 Variable

17 Variable top()

expr()

name() vars()

get(0) get(1) get(2)

index() index() index()

parent()

parent()

parent()

term()

term()

term()

root Formula Pos

(15)

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)

(16)

Context-dependent Terms aka. Term-Positions

Visitors

interface FormulaPos ... { class DefaultVisitor ... {

void visitFormulaPos(FormulaPos term) { ... } void visitNotPos(NotPos term) { ... }

void visitImpliesPos(ImpliesPos term) { ... } } ...

}

(17)

Task 2

implement a PrettyPrinter based on the DefaultVisitor

decide whether to print out parenthesis based on the parent

(18)

Context-dependent Terms aka. Term-Positions

Solution

(19)

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()

(20)

Context-dependent Terms aka. Term-Positions

Post- and Preorder

every position has apreOrder()method, which goes to the next position in pre order

every position haspostOrderandpostOrderStartmethods

(21)

Task 3

implement a method simplify which simplifies constant expressions to constants

use a post order path through the positions

Referenzen

ÄHNLICHE DOKUMENTE

Please include the following: Background / Research Questions / Methods (including data and/or biological samples required) / Planned outputs / Timescales for completion of

Ziel dieser Studie war es, mit einem möglichen Nachweis von RANK, RANKL und OPG im Harnblasenkarzinom eine Grundlage für einen solchen Marker zu erbringen, welcher sich im

Als zweiten chromatographischen Schritt bei der Aufreinigung von PhyC aus Bacillus amyloliquefaciens FZB45 und Nicotiana tabacum wurde eine Superdex 75 Prepgrade High Load

In order to learn more about that interesting regulation, mitochondrial gene copy numbers will be analyzed during leaf development and in green versus white

In the last few years, there is considerable growing interest in using generative adversarial network (GAN), a Deep Learning based technology to generate more facial expressions as

A comparison of differential gene expression in unstimulated, LPS and dex+LPS stimulated cells from wild type, GR dim and GR LysCre mice, demonstrated that the expression levels of

Ein Interface kann aufgefasst werden als eine abstrakte Klasse, wobei:.. • alle Objekt-Methoden

Thus, for Minkowski distances (e.g. Euclidean), the distance between samples is the same for relative (cDNA) and absolute (Affymetrix) expression measures. This does not hold for