43
AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Embed Size (px)

Citation preview

Page 1: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

AtoCC Compiler Construction Workshop

Michael Hielscher Aalborg, 26.03.08

Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter and Compiler for a music language ML

Page 2: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Content

Introduction

Why we created AtoCC

What is AtoCC

The music language ML

Create an interpreter for ML

Create a MLSVG compiler

2

Page 3: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Why we created AtoCC ?3

1. We want to motivate students to deal with theoretical

computer science.

We use Compiler Construction as an interesting topic

(practical use) right from the beginning.

We define a target project we want to solve (Compiler).

Target language is not machine code !!!

2. Cycles between teaching theory and using it in a practical

way (project based). The practical part is no add-on at the

end of theory class!

3. Students solve complex tasks in the area of theoretical

computer science with very high abstraction.

Page 4: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Why we created AtoCC ?

We needed a software package solving this

task without dealing with too much technical

stuff.

We needed software for teaching purposes

not for professional use.

4

Page 5: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Abstraction and problem solving model5

Computer Science based layer

Modelbased layer

Problembased layer

develop Software use Software

Page 6: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

What is AtoCC ?

The learning environment AtoCC can be of use in teaching abstract automata, formal languages, and some of its applications in compiler construction.

From a teacher's perspective AtoCC aims to address a broad range of different learning activities forcing the students to actively interact with the subjects being taught.

AtoCC contains 6 programs: AutoEdit, AutoEdit Workbook, kfG-Edit, TDiag, VCC, SchemeEdit

6

www.atocc.de

Page 7: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Software we need today

Please install the following software: Java JDK Mozilla FireFox as SVG viewer AtoCC (www.atocc.de) Workshop ZIP file:

AtoCC Website A. Education Aalborg Materials

7

Page 8: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

The music language ML

Like for mobile phones we can find several easy to read tone/music languages

We want to create an own primitive note language for monophonic songs (only one voice)

Example:C1-2 G1-8 A0-4 A0-8G0-4 G0-8 C0-8 P-2

8

Page 9: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

The music language ML

This language is so primitive, that it is even a regular language.

A more complex language could include loops:[C1-2 G1-8 [A0-4 A0-8]] P-2(typical bracket example for push down automata)

9

Page 10: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

The music language ML

Play with ML: Open folder „Songs“ and execute Console.bat

10

Task: Change song file content and get familiar with ML

Task: Change song file content and get familiar with ML

Page 11: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter

Create an Interpreter for ML

1) Define a T-Diagram

2) Define a grammar for ML

3) Define Scanner and Parser definition

4) Create S attributes

5) Generate the ML Interpreter compiler

6) Test the Interpreter with the help of the T-Diagram

11

Worksheet 1Worksheet 1

Page 12: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

T-Diagrams

We use T-Diagrams to model our compiler processes.

Our diagrams look slightly different: We have 4 element types. Compiler, Program, Interpreter und E/A for

Input/Output

12

Compiler Program Interpreter Input/Output

Page 13: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Create a T-Diagram

Note down a T-Diagram for applying our ML Interpreter written in JavaBytecode on a program written in ML.

13

Page 14: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Create a T-Diagram14

Page 15: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Define a grammar for ML

One part of our T-Diagram, the Interpreter, we want to create now.

Therefore we need to define how ML looks like (Syntax). We use a context free grammar GML (in BNF).

Look at examples and try to figure out a grammar: G0-4 G1-2 A0-1 D1-32 P-16 A-8 P-2 C0-16 C1-8 F0-1 H1-2 P-1

15

Start with:Song NotesNotes ?

Start with:Song NotesNotes ?

Page 16: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Define a grammar for ML16

Page 17: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Define a grammar for ML17

Page 18: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Define a grammar for ML18

Page 19: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Create an Interpreter for ML

Basically we can say that an interpreter is similar to a compiler, but without generating some target language.

Therefore we will generate a compiler used as an interpreter, which does not output some target language.

19

Page 20: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

How does a compiler work20

For an Interpreter we don’t care for output code, we want to execute something directly

For an Interpreter we don’t care for output code, we want to execute something directly

Page 21: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML

21

We need to declare a scanner and a parser (a description how they shall work).

We start with a scanner definition. We define Token classes with patterns

(RegExp.) Easiest solution: for each terminal of GML we use

exactly one pattern (one Token class). More complex pattern result into a much less

work for the parser we shall try to give the scanner a job to do

Page 22: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML

22

duration values (full, half, ¼, …)

all keynames

allowed octaves Tokenclasslist1

1

Page 23: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML

We need to make sure, that none token overlaps the pattern of another.

But in program languages this is quite often the case:Keyword: beginIdentifier: [a-z]+

To solve this, the ordering of token classes in list is important!

23

Page 24: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

We can rewrite our grammar to make use of our tokens as new terminals:

We will have 6 token classes with pattern for:KeyName, Token0, Token1, Token2_32, P and -

Creating an Interpreter from GML24

Page 25: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML25

We can represent such pattern also as primitive Finite Automata:

Token2_322|4|8|16|32

KeyName C|D|E|F|G|H|A

Page 26: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML

26

Page 27: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML

27

We rename the generated token classes to useful names

We rename the generated token classes to useful names

Page 28: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML

28

We need to specify the regular expressions we defined earlier

We need to specify the regular expressions we defined earlier

Page 29: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML

29

Page 30: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML

We can generate an empty compiler (scanner + parser) now and apply it on a program in ML:

We want to generate sound something is still missing

30

Page 31: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML

We need to define S attributes for each parser rule. These attributes are small code fragments that are

executed when this rule is applied. Each rule returns a result $$ by executing the code

fragment (we need to fill $$).

Example:Note Key – Duration$$ = “Note: “ + $1 + “ Length: “+ $3;

31

Page 32: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

The placeholders $1 to $n: In S attributes we can use placeholders for the results of

each rule right side element.

Creating an Interpreter from GML

32

$1$1 $2 $2

C1 C1 --

Input word: C1-8 C1-4Input word: C1-8 C1-4Input word: C1-8 D1-4Input word: C1-8 D1-4

From a token $n is always the input content (lexem)!

From a nonterminal $n is always the result $$ from this element!

From a token $n is always the input content (lexem)!

From a nonterminal $n is always the result $$ from this element!

$$ = "C1-8";$$ = "C1-8";

$2 $2

88

Page 33: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

The placeholders $1 to $n: In S attributes we can use placeholders for the results of

each rule right side element.

Creating an Interpreter from GML

33

All $n and $$ have the data type String !!!

All $n and $$ have the data type String !!!

$1$1 $2 $2

C C 11

Input word: C1-8 C1-4Input word: C1-8 C1-4Input word: C1-8 D1-4Input word: C1-8 D1-4

$$ = "C1";$$ = "C1";

From a token $n is always the input content (lexem)!

From a nonterminal $n is always the result $$ from this element!

From a token $n is always the input content (lexem)!

From a nonterminal $n is always the result $$ from this element!

$1$1

C1 C1

Page 34: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML

Now we can deal with: What will happen when a note rule is applied (playing a note or a pause)

On we can find 3 helper functions for playing MIDI notes.

We need to translate key names like C0 into according MIDI keys to be played.

34

Worksheet 1Worksheet 1

Page 35: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML

35

Page 36: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating an Interpreter from GML

36

Generate the final interpreter again

Generate the final interpreter again

Page 37: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Execute the T-Diagram37

Page 38: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating a compiler

Create a ML SVG compiler

1) Define a T-Diagram

2) Define Scanner and Parser definition

3) Create S attributes

4) Generate MLSVG compiler

5) Test the compiler with the help of the T-Diagram

38

Worksheet 2Worksheet 2

Page 39: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Create a T-Diagram39

Page 40: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Creating a compiler40

Page 41: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Execute the T-Diagram

We can attach our new compiler to the T-Diagram and execute it:

41

Page 42: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Summary 42

C1-8 E1-4 D0-2 …C1-8 E1-4 D0-2 …[KeyName, "C"][Token1, "1"][Minus, "-"]…

[KeyName, "C"][Token1, "1"][Minus, "-"]…

<?xml version="1.0" ?><!DOCTYPE svg PUBLIC

"-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-...

<?xml version="1.0" ?><!DOCTYPE svg PUBLIC

"-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-...

Page 43: AtoCC Compiler Construction Workshop Michael Hielscher Aalborg, 26.03.08 Creating an Interpreter and Compiler for a music language ML

Thanks for your attention

Any Questions ?

www.atocc.de