+ All Categories
Home > Documents > Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter...

Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter...

Date post: 06-Mar-2018
Category:
Upload: phungcong
View: 213 times
Download: 0 times
Share this document with a friend
28
Compiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober 2016
Transcript
Page 1: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Compiler ConstructionWinter semester 2016/2017

University of Freiburg

Matthias Keil

University of Freiburg

14. Oktober 2016

Page 2: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

OutlineUniversity of Freiburg

1 General

2 Exercises

3 Tool Chain

4 Visitor Pattern

5 SableCC

6 Summary

Matthias Keil Compiler Construction 14. Oktober 2016 2 / 28

Page 3: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Time and PlaceUniversity of Freiburg

Lecture

Monday, 2pm - 4pmRoom SR 01-016, Building 101

Thursday, 2pm - 3pmRoom SR 01-016, Building 101

Exercise

Thursday, 3pm - 4pmRoom SR 01-016, Building 101

Matthias Keil Compiler Construction 14. Oktober 2016 3 / 28

Page 4: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

AssistantUniversity of Freiburg

Assistant

Matthias Keil

Address Room 00-013, Building 079

Email [email protected]

Phone +49 (0)761 203 8060

Matthias Keil Compiler Construction 14. Oktober 2016 4 / 28

Page 5: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Final GradeUniversity of Freiburg

Final Grade

No oral or written exam.

Final grade is calculated from the exercise program.

Matthias Keil Compiler Construction 14. Oktober 2016 5 / 28

Page 6: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Compiler Construction ExercisesUniversity of Freiburg

Compiler Project

Compiler from MiniJava to MIPS.

Six exercise sheets.

2-3 weeks per exercise sheet, depending on the difficulty.

Each sheet focuses on one part of a compiler.

Java knowledge is required.

Written Homework

2-4 pages.

Possible topics are given by us.

Matthias Keil Compiler Construction 14. Oktober 2016 6 / 28

Page 7: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Registration for ParticipationUniversity of Freiburg

Fill out the form untilThursday, 20.10.2016 12:00 (midnight)

Form https://goo.gl/forms/8Q61RZSQMv51Wi7y2

name, prename

email address

tf username (pool account)

Matthias Keil Compiler Construction 14. Oktober 2016 7 / 28

Page 8: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Subversion RepositoryUniversity of Freiburg

Repository

Link https://proglang.informatik.uni-freiburg.

de/svn/cc + SVNNUMBER

SVNNUMBER ∈ {00 . . . 25}

WWW Password

Link https://support.informatik.uni-freiburg.

de/cgi/support/fawmgr.cgi?wpassword:en

Matthias Keil Compiler Construction 14. Oktober 2016 8 / 28

Page 9: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Submission GuidelinesUniversity of Freiburg

Submission

Submit your solution to the repository.

Create one folder (e.g. exercise1) for each exercise sheet.

Submissions consist of an executable Jar file with sourcecode inside and a report.

Submission instructions are given on each exercise sheet.

Deadline

Thursday, 12:00 (noon).

Late submissions will not be accepted.

Matthias Keil Compiler Construction 14. Oktober 2016 9 / 28

Page 10: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Submission Guidelines (cont’d)University of Freiburg

Implementation

Efficient and clear code is required.

You are strongly encouraged to test your solution with theprovided test cases.

Provide your source code with comments.

Matthias Keil Compiler Construction 14. Oktober 2016 10 / 28

Page 11: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Submission Guidelines (cont’d)University of Freiburg

Comments

Clear and understandable style is required.

Comments do not explain it the syntax.

Comments clarify its intension.

Bad style

1 // declare name

2 // concatenates uid and nr

3 String name = uid + nr;

Matthias Keil Compiler Construction 14. Oktober 2016 11 / 28

Page 12: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Submission Guidelines (cont’d)University of Freiburg

Report

Pdf file (e.g. report exercise1.pdf) with a formal description.

ConceptionStructure. . .

Limited to one (two) page(s) per exercise.

Either in German or in English.

Clear and understandable style is required.

Matthias Keil Compiler Construction 14. Oktober 2016 12 / 28

Page 13: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Submission Guidelines (cont’d)University of Freiburg

The exercises have to be done by yourself!

Team work is not allowed.

You are not allowed to copy source code.

Matthias Keil Compiler Construction 14. Oktober 2016 13 / 28

Page 14: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Tools ChainUniversity of Freiburg

Tools you need to know/learn

Java ≥ 1.6

Eclipse ≥ 4.2 (other IDEs: you’re on your own)

SableCC 3.6

LaTeX (or anything else)

See exercise page for detailed installation instructions.

Tools you will use without knowing

Ant

Checkstyle

Matthias Keil Compiler Construction 14. Oktober 2016 14 / 28

Page 15: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

The Essence of the Visitor Pattern(Palsberg and Jay, 1998)University of Freiburg

Summing the Elements of a List

1 interface List {}

2

3 class Nil implements List {}

4 class Cons implements List {

5 int head;

6 List tail;

7 }

Matthias Keil Compiler Construction 14. Oktober 2016 15 / 28

Page 16: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

1. Approach: InstanceOf and Type CastsUniversity of Freiburg

1 List l;

2 int sum = 0;

3 boolean proceed = true;

4 while(proceed){

5 if (l instanceof Nil)

6 proceed = false;

7 else if (l instanceof Cons){

8 sum += ((Cons) l).head;

9 l = ((Cons) l).tail;

10 }

11 }

Classes are not touched.

Frequent type casts and instanceof checks.

Matthias Keil Compiler Construction 14. Oktober 2016 16 / 28

Page 17: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

2. Approach: Dedicated MethodsUniversity of Freiburg

1 interface List {

2 public int sum();

3 }

4 class Nil implements List {

5 public int sum() { return 0; }

6 }

7 class Cons implements List {

8 int head;

9 List tail;

10 public int sum() {

11 return head + tail.sum();

12 }

13 }

No type casts, systematic and object-oriented.

Need to change classes.

Matthias Keil Compiler Construction 14. Oktober 2016 17 / 28

Page 18: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

3. Approach: Visitor Pattern(Gamma et al., Design Patterns, 1995)University of Freiburg

Intension

Represent an operation to be performed on the elements of anobject structure. The Visitor pattern lets you define a newoperation without changing the classes of the elements on whichit operates.

Idea

Distinguish between object structure and the visitor.

Insert an accept method in each class of the objectstructure.

For each of these classes, a visitor contains a visit method.

Matthias Keil Compiler Construction 14. Oktober 2016 18 / 28

Page 19: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Visitor PatternUniversity of Freiburg

1 interface List {

2 void accept(Visitor v);

3 }

4

5 class Nil implements List {

6 public void accept(Visitor v) {

7 v.visitNil(this);

8 }

9 }

10 class Cons implements List {

11 int head;

12 List tail;

13 public void accept(Visitor v) {

14 v.visitCons(this);

15 }

16 }

Matthias Keil Compiler Construction 14. Oktober 2016 19 / 28

Page 20: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Visitor Pattern (cont’d)University of Freiburg

1 interface Visitor {

2 void visitNil(Nil l);

3 void visitCons(Cons l);

4 }

5 class SumVisitor implements Visitor {

6 int sum;

7 public void visitNil(Nil l) {}

8 public void visitCons(Cons l) {

9 sum += l.head;

10 l.tail.accept(this);

11 }

12 }

13 ...

14 SumVisitor sv = new SumVisitor ();

15 l.accept(sv);

16 System.out.println(sv.sum);

Matthias Keil Compiler Construction 14. Oktober 2016 20 / 28

Page 21: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Visitor Pattern (cont’d)University of Freiburg

The visitor pattern gives you:

New functionality without recompiling the object structure!

Related operations are structured together.

Visitors can accumulate (and also encapsulate) state.

But:

All classes must have an accept method.

Adding new classes to the object structure is nasty.

Be Careful!

The visit methods describe actions and access to subobjects.

Matthias Keil Compiler Construction 14. Oktober 2016 21 / 28

Page 22: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

SableCCUniversity of Freiburg

What is SableCC?

Open-source parser generator for Java.

http://sablecc.org.

Generates LALR(1) parsers.

Featuring: lexer, parser, nodes/ast, analysis/visitors

Matthias Keil Compiler Construction 14. Oktober 2016 22 / 28

Page 23: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

A specification for SableCCUniversity of Freiburg

Grammar

Package package-name;

Helpers id = regexp;

Tokens id = regexp;

Ignored Tokens token1,...,tokenN;

Productions (simplified)id = {altname} elem* — ... ;with elem = [id]: id (+—*—?)

Matthias Keil Compiler Construction 14. Oktober 2016 23 / 28

Page 24: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

A specification for SableCC (cont’d)University of Freiburg

Example

1 Package simpleAdder;

2

3 Tokens

4 l_par = ’(’;

5 r_par = ’)’;

6 plus = ’+’;

7 number = [’0’..’9’];

8

9 Productions

10

11 exp = {constant} number

12 | {add} addition;

13

14 addition = l_par [left]:exp plus [right]:exp

r_par;

Matthias Keil Compiler Construction 14. Oktober 2016 24 / 28

Page 25: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

A specification for SableCC (cont’d)University of Freiburg

Generated files

1 abstract class Node {}

2 abstract class PExp extends Node{}

3 class AConstantExp extends PExp {

4 TNumber getNumber () {...} ...

5 }

6 class AAddExp extends PExp {

7 PAddition getAddition () {...} ...

8 }

9 class AAddition extends PAddition {

10 TLPar getLPar () {...}

11 PExp getLeft () {...}

12 ...

13 }

Matthias Keil Compiler Construction 14. Oktober 2016 25 / 28

Page 26: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

Visitor Pattern in SableCC (cont’d)University of Freiburg

Generated Files

1 class DepthFirstAdapter extends AnalysisAdapter

{

2 void caseXxx(Xxx node) {

3 inXxx(node);

4 node.getYyy.apply(this); // first child of

Xxx

5 node.getZzz.apply(this); // second child of

Xxx

6 outXxx(node);

7 }

8 ...

9 }

Matthias Keil Compiler Construction 14. Oktober 2016 26 / 28

Page 27: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

ImportantUniversity of Freiburg

Do not

Modify any generated files!

Submit any homework late!

Copy anyone’s homework!

Panic! Ask for help!

Do

Comment your submissions!

Start early on the assignments!

Consult manuals, tutorials, our forum and the homepage!

Have fun!

Matthias Keil Compiler Construction 14. Oktober 2016 27 / 28

Page 28: Compiler Construction - Winter semester 2016/2017 · PDF fileCompiler Construction Winter semester 2016/2017 University of Freiburg Matthias Keil University of Freiburg 14. Oktober

QuestionsUniversity of Freiburg

Questions ?

Matthias Keil Compiler Construction 14. Oktober 2016 28 / 28


Recommended