Ato CC Compiler Construction Workshop CREATING AN INTERPRETER

  • Slides: 43
Download presentation
Ato. CC Compiler Construction Workshop CREATING AN INTERPRETER AND COMPILER FOR A MUSIC LANGUAGE

Ato. CC Compiler Construction Workshop CREATING AN INTERPRETER AND COMPILER FOR A MUSIC LANGUAGE ML Aalborg, 26. 03. 08 Michael Hielscher

Content 2 Introduction Why we created Ato. CC What is Ato. CC The music

Content 2 Introduction Why we created Ato. CC What is Ato. CC The music language ML Create an interpreter for ML Create a ML SVG compiler

Why we created Ato. CC ? 3 1. We want to motivate students to

Why we created Ato. CC ? 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.

Why we created Ato. CC ? 4 We needed a software package solving this

Why we created Ato. CC ? 4 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.

Abstraction and problem solving model 5 develop Software Computer Science based layer Model based

Abstraction and problem solving model 5 develop Software Computer Science based layer Model based layer Problem based layer use Software

What is Ato. CC ? 6 § The learning environment Ato. CC can be

What is Ato. CC ? 6 § The learning environment Ato. CC can be of use in teaching abstract automata, formal languages, and some of its applications in compiler construction. § From a teacher's perspective Ato. CC aims to address a broad range of different learning activities forcing the students to actively interact with the subjects being taught. § Ato. CC contains 6 programs: Auto. Edit, Auto. Edit Workbook, kf. G-Edit, TDiag, VCC, Scheme. Edit www. atocc. de

Software we need today 7 Please install the following software: Java JDK Mozilla Fire.

Software we need today 7 Please install the following software: Java JDK Mozilla Fire. Fox as SVG viewer Ato. CC (www. atocc. de) Workshop ZIP file: Ato. CC Website A. Education Aalborg Materials

The music language ML 8 § Like for mobile phones we can find several

The music language ML 8 § 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: C 1 -2 G 1 -8 A 0 -4 A 0 -8 G 0 -4 G 0 -8 C 0 -8 P-2

The music language ML 9 § This language is so primitive, that it is

The music language ML 9 § This language is so primitive, that it is even a regular language. § A more complex language could include loops: [C 1 -2 G 1 -8 [A 0 -4 A 0 -8]] P-2 (typical bracket example for push down automata)

The music language ML 10 § Play with ML: § Open folder „Songs“ and

The music language ML 10 § Play with ML: § Open folder „Songs“ and execute Console. bat Task: Change song file content and get familiar with ML

Creating an Interpreter 11 Create an Interpreter for ML 1) Define a T-Diagram 2)

Creating an Interpreter 11 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 TDiagram Worksheet 1

T-Diagrams 12 We use T-Diagrams to model our compiler processes. Our diagrams look slightly

T-Diagrams 12 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 Compiler Program Interpreter Input/Output

Create a T-Diagram 13 Note down a T-Diagram for applying our ML Interpreter written

Create a T-Diagram 13 Note down a T-Diagram for applying our ML Interpreter written in Java. Bytecode on a program written in ML.

Create a T-Diagram 14

Create a T-Diagram 14

Define a grammar for ML 15 One part of our T-Diagram, the Interpreter, we

Define a grammar for ML 15 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: G 0 -4 G 1 -2 A 0 -1 D 1 -32 P-16 A-8 P-2 C 0 -16 C 1 -8 F 0 -1 H 1 -2 P-1 Start with: Song Notes ?

Define a grammar for ML 16

Define a grammar for ML 16

Define a grammar for ML 17

Define a grammar for ML 17

Define a grammar for ML 18

Define a grammar for ML 18

Create an Interpreter for ML 19 Basically we can say that an interpreter is

Create an Interpreter for ML 19 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.

How does a compiler work 20 Sourcecode in ML Scanner Tokenlist Parser Output in

How does a compiler work 20 Sourcecode in ML Scanner Tokenlist Parser Output in target language For an Interpreter we don’t care for output code, we want to execute something directly

Creating an Interpreter from GML 21 We need to declare a scanner and a

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 (Reg. Exp. ) 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

Creating an Interpreter from GML 22 Key. Name all keynames C|D|E|F|G|H|A Octave 1 0|1

Creating an Interpreter from GML 22 Key. Name all keynames C|D|E|F|G|H|A Octave 1 0|1 allowed octaves Duration 1 1|2|4|8|16|32 duration values (full, half, ¼, …) Token class list

Creating an Interpreter from GML 23 We need to make sure, that none token

Creating an Interpreter from GML 23 We need to make sure, that none token overlaps the pattern of another. But in program languages this is quite often the case: Keyword: begin Identifier: [a-z]+ To solve this, the ordering of token classes in list is important! Key. Name C|D|E|F|G|H|A Token 0 0 Token 1 1 Token 2_32 2|4|8|16|32 Minus Pause P

Creating an Interpreter from GML 24 We can rewrite our grammar to make use

Creating an Interpreter from GML 24 We can rewrite our grammar to make use of our tokens as new terminals: We will have 6 token classes with pattern for: Key. Name, Token 0, Token 1, Token 2_32, P and -

Creating an Interpreter from GML 25 We can represent such pattern also as primitive

Creating an Interpreter from GML 25 We can represent such pattern also as primitive Finite Automata: Token 2_32 2|4|8|16|32 Key. Name C|D|E|F|G|H|A

Creating an Interpreter from GML 26

Creating an Interpreter from GML 26

Creating an Interpreter from GML 27 We rename the generated token classes to useful

Creating an Interpreter from GML 27 We rename the generated token classes to useful names

Creating an Interpreter from GML 28 We need to specify the regular expressions we

Creating an Interpreter from GML 28 We need to specify the regular expressions we defined earlier

Creating an Interpreter from GML 29

Creating an Interpreter from GML 29

Creating an Interpreter from GML 30 We can generate an empty compiler (scanner +

Creating an Interpreter from GML 30 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

Creating an Interpreter from GML 31 We need to define S attributes for each

Creating an Interpreter from GML 31 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;

Creating an Interpreter from GML 32 The placeholders $1 to $n: In S attributes

Creating an Interpreter from GML 32 The placeholders $1 to $n: In S attributes we can use placeholders for the results of each rule right side element. C 1 -4 Input word: C 1 -8 D 1 -4 $1 $2 $2 C 1 - 8 $$ = "C 1 -8"; Ø From a token $n is always the input content (lexem)! Ø From a nonterminal $n is always the result $$ from this element!

Creating an Interpreter from GML 33 The placeholders $1 to $n: In S attributes

Creating an Interpreter from GML 33 The placeholders $1 to $n: In S attributes we can use placeholders for the results of each rule right side element. C 1 -4 Input word: C 1 -8 D 1 -4 Ø From a token $n is always the input content (lexem)! Ø From a nonterminal $n is always the result $$ from this element! $1 C 1 $1 $2 C 1 $$ = "C 1"; Ø All $n and $$ have the data type String !!!

Creating an Interpreter from GML 34 Now we can deal with: What will happen

Creating an Interpreter from GML 34 Now we can deal with: What will happen when a note rule is applied (playing a note or a pause) On Worksheet 1 we can find 3 helper functions for playing MIDI notes. We need to translate key names like C 0 into according MIDI keys to be played.

Creating an Interpreter from GML 35

Creating an Interpreter from GML 35

Creating an Interpreter from GML 36 Ø Generate the final interpreter again

Creating an Interpreter from GML 36 Ø Generate the final interpreter again

Execute the T-Diagram 37

Execute the T-Diagram 37

Creating a compiler 38 Create a ML SVG compiler 1) Define a T-Diagram 2)

Creating a compiler 38 Create a ML SVG compiler 1) Define a T-Diagram 2) Define Scanner and Parser definition 3) Create S attributes 4) Generate ML SVG compiler 5) Test the compiler with the help of the T-Diagram Worksheet 2

Create a T-Diagram 39

Create a T-Diagram 39

Creating a compiler 40

Creating a compiler 40

Execute the T-Diagram 41 We can attach our new compiler to the TDiagram and

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

Summary 42 C 1 -8 E 1 -4 D 0 -2 … Source in

Summary 42 C 1 -8 E 1 -4 D 0 -2 … Source in ML [Key. Name, "C"] [Token 1, "1"] [Minus, "-"] … Scanner Tokenlist <? xml version="1. 0" ? > <!DOCTYPE svg PUBLIC "//W 3 C//DTD SVG 20010904//EN" "http: //www. w 3. org/TR/2001/RE C-SVG-. . . Parser Target language SVG

Thanks for your attention Any Questions ? www. atocc. de

Thanks for your attention Any Questions ? www. atocc. de