48
Formal Languages and Compilers Exercises Nataliia Bielova http://disi.unitn.it/~bielova/flc/index.html 1 Formal languages and compilers 2011

Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Formal Languages and

Compilers Exercises

Nataliia Bielova

http://disi.unitn.it/~bielova/flc/index.html

1

Formal languages and compilers 2011

Page 2: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Organization ¡  All the material on the website

http://disi.unitn.it/~bielova/flc/index.html

¡  Didattica online: Esse3

¡  Caml language http://caml.inria.fr/

¡  Suggested literature: ¡  OCaml manual + code of interpreter/compilator ¡  The “Dragon” book – 1st or 2nd edition

¡  “Compilers: Principles, Techniques, and Tools“ by A.V. Aho, M.S. Lam, R. Sethi and J.D. Ullman

¡  Contact: [email protected]

Formal languages and compilers 2011

2

Page 3: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Revisiting OCaml Lection 1

3

Page 4: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

How to run OCaml ¡  Run the interpreter with

¡  ocaml

¡  Exit the interpreter: ¡  # quit;;

¡  Compilers: ¡  ocamlc compiles in bytecode

¡  ocamlopt compiles in machine code

¡  Compilation of a single module ¡  ocamlc –c <fileName>.ml

¡  Produces <fileName>.cmo

¡  Linking different modules .cmo: ¡  ocamlc –o file1.cmo … fileN.cmo

4

Formal languages and compilers 2011

Page 5: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Characteristics of OCaml ¡  It’s a functional language

¡  Functions are “first-class” objects (as in mathematics, a function can be used as an argument of another function)

¡  Static type checking (the types are checked at compile-time “If you manage to compile it, then it will work for sure!”)

¡  Static scoping (the values of the variables are static at compile-time)

5

Formal languages and compilers 2011

Page 6: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Characteristics of OCaml (2) ¡  Type polymorphism

¡ Constructors of type

¡  The module system

¡  Simple types: int, float, char, string, bool, …

¡  Built-in simple datatypes: list, tuple, record, …

6

Formal languages and compilers 2011

Page 7: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Characteristics of OCaml (2) ¡  Type polymorphism

¡ Constructors of type

¡  The module system

¡  Simple types: int, float, char, string, bool, …

¡  Built-in simple datatypes: list, tuple, record, …

Remember! Since the language is strongly typed, the int and float types are not compatible: 1.4 + 5 gives an error!

7

Formal languages and compilers 2011

Page 8: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

int and float, what’s wrong with them? ¡  int are integer numbers together with operations: ¡  arithmetic: + - * / succ pred mod

¡  relational: < > <= >= = <>

¡  float are numbers in floating-point representation with operators: ¡  arithmetic: +. -. *. /. ** sqrt (notice the “.”)

¡  relational: < > <= >= = <>

¡ Conversions between float and int: int_to_float and float_to_int

8

Formal languages and compilers 2011

Page 9: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

bool, char, unit ¡  bool = {true, false}

¡  char representes the ASCII characters

¡  Useful functions in module Char: ¡  code returns the ASCII code of the argument

¡  chr returns the character with the given ASCII code

¡  unit is a “not so interesting type”. Its value is () and it’s similar to void in Java & C.

9

Formal languages and compilers 2011

Page 10: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

string ¡  Is a sequence of characters

¡  Operators: ¡  Concatenation s1 ^ s2

¡  pointing to one character s.[index]

¡  module String: length, contains, uppercase, …

¡ Conversions: string_to_int, float_to_string, …

10

Formal languages and compilers 2011

Page 11: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Tuple ¡  Tuple is a fixed-length list, but the fields may be of

differing type

(2, “hi”, (‘a’, false))

¡ Operators are applied element by element:

(1, 2, 3) < (4, 5, 6);; results in true

11

Formal languages and compilers 2011

Page 12: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

List ¡  List is a sequence of objects of the same type:

[1.5; 2.0; 3.2] (notice the “;”)

¡ Operators are applied like for tuples:

[1; 2; 3] < [4; 5; 6];; results in true

¡ Constructors: ¡  [] empty list

¡  :: add an element to a list:

4 :: [1; 2];; results in [4; 1; 2]

¡  (l1 @ l2) concatenation of the lists:

[1; 2] @ [3; 4];; results in [1; 2; 3; 4]

12

Formal languages and compilers 2011

Page 13: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Array and Record ¡ Array is a fixed-length list, but the fields have to be of the

same type:

¡  [| 1; 2; 3; 4 |].(2);; results in…

13

Formal languages and compilers 2011

Page 14: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Array and Record ¡ Array is a fixed-length list, but the fields have to be of the

same type:

¡  [| 1; 2; 3; 4 |].(2);; results in… 3!

14

Formal languages and compilers 2011

Page 15: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Array and Record ¡ Array is a fixed-length list, but the fields have to be of the

same type:

¡  [| 1; 2; 3; 4 |].(2);; results in… 3!

¡  Record is a sequence of elements of particular type:

¡  type address = {name: string; street: string; number: int} ;; let friend = {name = “Bart Simpson”; street = “15th avenue”;

number = 1};;

friend.street;; results in “15th avenue”

15

Formal languages and compilers 2011

Page 16: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Variables ¡  Binding

let x=5;;

¡  Parallel binding

let x=5 and y=4;;

¡  Local binding

let x=4 in x*2;;

¡  Remember: the binding is static

let x=3 in let x=2 in x-1;; …results in 1

16

Formal languages and compilers 2011

Page 17: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Pattern matching ¡  Matches the data composed using constructors:

let couple = (‘a’, 5.3);;

let (first, second)= couple;;

substitutes first with ‘a’ and second with 5.3

17

Formal languages and compilers 2011

Page 18: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Pattern matching ¡  Matches the data composed using constructors:

let couple = (‘a’, 5.3);;

let (first, second)= couple;;

substitutes first with ‘a’ and second with 5.3

¡  It’s possible to use [] and :: with the lists

let list = [1; 2; 3];;

let head::tail = list;;

results in head = 1 and tail = [2; 3]

18

Formal languages and compilers 2011

Page 19: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Pattern matching ¡  Matches the data composed using constructors:

let couple = (‘a’, 5.3);;

let (first, second)= couple;;

substitutes first with ‘a’ and second with 5.3

¡  It’s possible to use [] and :: with the lists

let list = [1; 2; 3];;

let head::tail = list;;

results in head = 1 and tail = [2; 3]

¡  _ is anonymous pattern that matches everything:

let head::_ = list;;

results in head = 1

19

Page 20: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Functions ¡  Definition

let f = fun x -> x*2;; let f x = x*2;;

val f: int -> int = <fun>

20

Formal languages and compilers 2011

Page 21: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Functions ¡  Definition

let f = fun x -> x*2;; let f x = x*2;;

val f: int -> int = <fun>

¡  Functions always have only one argument ¡  let f = fun (x, y) -> x + y;;

f: ( int * int) -> int function uncurry ¡  let f = fun x y -> x + y;;

f: int -> int -> int function curry

21

Formal languages and compilers 2011

Page 22: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Functions ¡  Definition

let f = fun x -> x*2;; let f x = x*2;;

val f: int -> int = <fun>

¡  Functions always have only one argument ¡  let f = fun (x, y) -> x + y;;

f: ( int * int) -> int function uncurry ¡  let f = fun x y -> x + y;;

f: int -> int -> int function curry

¡  Pattern matching over the functions

let rec factorial = function

0 -> 1

| n -> n * factorial(n-1);;

22

Page 23: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Higher-order functions ¡  Substitute a function as a result

let mult x y = x*y;;

let double = mult 2;;

val double: int -> int = <fun>

23

Formal languages and compilers 2011

Page 24: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Higher-order functions ¡  Substitute a function as a result

let mult x y = x*y;;

let double = mult 2;;

val double: int -> int = <fun>

¡  Taking another function as an argument

let rec map f list = match list with

[] -> []

| head::tail -> f head:: map f tail;;

24

Formal languages and compilers 2011

Page 25: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Higher-order functions ¡  Substitute a function as a result

let mult x y = x*y;;

let double = mult 2;;

val double: int -> int = <fun>

¡  Taking another function as an argument

let rec map f list = match list with

[] -> []

| head::tail -> f head:: map f tail;;

val map: (‘a -> ‘b) -> ‘a list -> ‘b list = <fun>

25

Formal languages and compilers 2011

Page 26: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Higher-order functions ¡  Substitute a function as a result

let mult x y = x*y;;

let double = mult 2;;

val double: int -> int = <fun>

¡  Taking another function as an argument

let rec map f list = match list with

[] -> []

| head::tail -> f head:: map f tail;;

val map: (‘a -> ‘b) -> ‘a list -> ‘b list = <fun>

map double [1; 2; 3];; results in [2; 4; 6]

26

Formal languages and compilers 2011

Page 27: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Polymorphism ¡  Variables of the type ‘a, ‘b, ‘c, …

let id x = x;; results in val id: ‘a -> ‘a = <fun>

¡  Polymorphic function

let comp f g x = f(g(x));;

val comp: (‘a -> ‘b) -> (‘c ->’a) -> c’ -> ‘b = <fun>

27

Formal languages and compilers 2011

Page 28: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Polymorphism ¡  Variables of the type ‘a, ‘b, ‘c, …

let id x = x;; results in val id: ‘a -> ‘a = <fun>

¡  Polymorphic function

let comp f g x = f(g(x));;

val comp: (‘a -> ‘b) -> (‘c ->’a) -> c’ -> ‘b = <fun>

‘a ‘b ‘c

f g

28

Formal languages and compilers 2011

Page 29: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Type declarations ¡  Simple type declaration

type color = Red | Blue | Green | Yellow;;

Formal languages and compilers 2011

29

Page 30: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Type declarations ¡  Simple type declaration

type color = Red | Blue | Green | Yellow;;

¡  Using type constructors

type money = Nothing | USDollars of float | Euro of float

let balance = function

Nothing -> 0.0

| USDollars (dollars) -> dollars

| Euro(euros) -> euros

Formal languages and compilers 2011

30

Page 31: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Recursive data type: tree ¡  type ‘a tree =

Leaf of ‘a

| Tree of ‘a * ‘a tree * ‘a tree

let mytree = Tree (4,

Tree(2, Leaf(1), Leaf(3)),

Tree(6, Leaf(5), Leaf(7)));;

Formal languages and compilers 2011

31

4

62

1 3 5 7

Page 32: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Exceptions ¡  Predefined exceptions: Division_by_zero, Out_of_memory,

Invalid_argument, …

32

Formal languages and compilers 2011

Page 33: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Exceptions ¡  Predefined exceptions: Division_by_zero, Out_of_memory,

Invalid_argument, …

¡  User-defined exceptions:

exception Empty_list of string;;

33

Formal languages and compilers 2011

Page 34: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Exceptions ¡  Predefined exceptions: Division_by_zero, Out_of_memory,

Invalid_argument, …

¡  User-defined exceptions:

exception Empty_list of string;;

let head = fun l -> match l with

[] -> raise (Empty_list("Empty!"))

| hd::tl -> hd;;

34

Formal languages and compilers 2011

Page 35: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Exceptions ¡  Predefined exceptions: Division_by_zero, Out_of_memory,

Invalid_argument, …

¡  User-defined exceptions:

exception Empty_list of string;;

let head = fun l -> match l with

[] -> raise (Empty_list("Empty!"))

| hd::tl -> hd;;

head [];;

35

Formal languages and compilers 2011

Page 36: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Exceptions ¡  Predefined exceptions: Division_by_zero, Out_of_memory,

Invalid_argument, …

¡  User-defined exceptions:

exception Empty_list of string;;

let head = fun l -> match l with

[] -> raise (Empty_list("Empty!"))

| hd::tl -> hd;;

head [];;

Exception: Empty_list "Empty!".

36

Page 37: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

try … with ¡  Handle the excetions

try dangerous expression

with exception1-> action1

| exception2 -> action2

| exceptionN -> actionN

| _ -> lastChance

Formal languages and compilers 2011

37

Page 38: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Abstract Data Types (ADT) ¡ Abstract Data Types: ¡  Interface: declarations of data types and functions

(in C: file .h, in Java: interface)

¡  Implementation: … (In C: file .c, in Java: class)

¡  It’s realized with ¡  Compilation unit (1 file ↔ 1 module)

¡  Module system (1 file ↔ 1 or more modules)

Formal languages and compilers 2011

38

Page 39: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Compilation unit ¡  Interface

File: .mli

Content: what is visible outside of the module

¡  Implementation

File .ml

Content: implementation of the module J

Formal languages and compilers 2011

39

Page 40: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Compilation unit - interface ¡  File myset.mli:

type 'a set

val emptySet : 'a set

val member : 'a -> 'a set -> bool

val insert : 'a -> 'a set -> 'a set

Formal languages and compilers 2011

40

Page 41: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Compilation unit - interface ¡  File myset.mli:

type 'a set – abstract type, hence is not used directly

val emptySet : 'a set

val member : 'a -> 'a set -> bool

val insert : 'a -> 'a set -> 'a set

Formal languages and compilers 2011

41

Page 42: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Compilation unit - implementation ¡  File myset.ml:

type 'a set = Null | Ins of 'a * 'a set

let emptySet = Null

let insert x = fun s -> Ins (x, s)

let rec member x = function

Null -> false

| Ins(v, s) -> x=v || member x s

Formal languages and compilers 2011

42

Page 43: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Compilation unit - use $ ocamlc –c myset.mli

$ ocamlc –c myset.ml

$ ocaml

# #load “myset.cmo”;;

# open Myset;;

# let s1 = emptySet;;

val s1 : ‘a Myset.set -> <abstr>

# let s1 = insert 3 s1;;

val s1: int Myset.set = <abstr>

43

Formal languages and compilers 2011

Page 44: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Module system ¡  Signature = interface

¡  Structure = implementation

Formal languages and compilers 2011

44

Page 45: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Module system ¡  Signature = interface

¡  Structure = implementation

¡ Correspondence between the signature and structure: ¡  1 structure → many signatures: changes the visible

functionality depending on the needs

¡  1 signature → many structures: changes the implementation without impact on the elements of the module

Formal languages and compilers 2011

45

Page 46: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Module system - signature module type mysetSig = sig

type ‘a set

val emptySet : ‘a set

val insert : ‘a -> ‘a set -> ‘a set

val member : ‘a -> ‘a set -> bool

end;;

Formal languages and compilers 2011

46

Page 47: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Module system - structure module Set: mysetSig = struct

type 'a set = Null | Ins of 'a * 'a set

let emptySet = Null

let insert x = function s -> Ins(x, s)

let rec member x = function

Null -> false

| Ins (v, s) -> x=v || member x s

end;;

Formal languages and compilers 2011

47

Page 48: Formal Languages and Compilers - Sophia - Inria · Formal Languages and Compilers Exercises Nataliia Bielova bielova/flc/index.html 1 Formal languages and compilers 2011

Try the exercises ¡  http://disi.unitn.it/~bielova/flc/index.html

Formal languages and compilers 2011

48