1 languages and compilers (sprog og oversættere) lecture 13 (2) bent thomsen department of computer...

14
1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University ledgement to Hanne Riis Nielson and Flemming Nielson whose book this lecture

Upload: quinten-mellin

Post on 14-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

1

Languages and Compilers(SProg og Oversættere)

Lecture 13 (2)

Bent Thomsen

Department of Computer Science

Aalborg University

With acknowledgement to Hanne Riis Nielson and Flemming Nielson whose book this lecture is based on.

Page 2: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

2

The missing link

The connectionbetween

Syntax and Semanticsand

Languages and Compilers

Page 3: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

3

The Language While

The Language While can be considered a mini version of Mini TriangleWhile is almost the same as the BIMS language

Page 4: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

4

Single step operational semantics for While

Page 5: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

5

Recursive Interpreter and Semantics

public Object visitBinaryExpression(BinaryExpression expr, Object arg){

Value val1 = (Value) expr.E1.visit(this, null);Value val2 = (Value) expr.E2.visit(this, null);

return applyBinary(expr.O, val1, val2);}

• Code for Recursive Interpreter can be derived from big step semantics

Page 6: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

6

A stack based virtual machine - AM

States:

Transitions:

Page 7: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

7

Operational semantics for AM

Page 8: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

8

Translation of While to AM

Note similarity with code generation templates for Mini Triangle

Page 9: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

9

Example

Page 10: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

10

Correctness Proof

Page 11: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

11

Soundness and completeness

Page 12: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

12

Getting closer to TAM

Page 13: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

13

And closer

Page 14: 1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to

14

Conclusion

• With a bit of hard work it is possible to connect – The high level operational semantics for a language

– With the low level implementation

– And prove correctness of the translation

• This is called:

Provably correct implementations

• For more information read– Semantics with applications,

– Hanne Riis Nielson and Flemming Nielson

– Wiley 1992, ISBN 0 471 92980 8