Interpreter for crme CAraMeL Lecture 5 Formal Languages and - - PowerPoint PPT Presentation

interpreter for cr me caramel
SMART_READER_LITE
LIVE PREVIEW

Interpreter for crme CAraMeL Lecture 5 Formal Languages and - - PowerPoint PPT Presentation

1 Interpreter for crme CAraMeL Lecture 5 Formal Languages and Compilers 2011 Nataliia Bielova 2 Definition Intepreter for a language L: Program in L Interpreter (Virtual Machine) Physical machine ( hosting ) 3 crme


slide-1
SLIDE 1

Interpreter for “crème CAraMeL”

Lecture 5 Formal Languages and Compilers 2011 Nataliia Bielova

1

slide-2
SLIDE 2

Definition

 Intepreter for a language L:

2

Physical machine (“hosting”) Interpreter (Virtual Machine) Program in L

slide-3
SLIDE 3

crème CAraMeL

 Basic types: int and float  Flow control: if then else, while do, for  Arithmetic operators: +, -, *, /  Assignment: : :=  Relational operators: =, <, <=  Boolean operators: &, |, !  Utility: write(val)

Formal languages and compilers 2011

3

slide-4
SLIDE 4

Objective

 Construct an interpreter for the language crème CAraMeL Interpreter

4

program var x : int; var y : int begin x := 0; y := 3; if (x < y) then begin x := 1; y := 0 end else begin x := 0; y := 1 end ; write(x); write(y) end

1

slide-5
SLIDE 5

Interpreter or compiler?

Formal languages and compilers 2011

5

Input

Lexer Parser

tokens

Interpreter syntax tree Output Compiler Executable

1

program var x : int; var y : int begin x := 0; y := 3; if (x < y) then begin x := 1; y := 0 end else begin x := 0; y := 1 end ; write(x); write(y) end

slide-6
SLIDE 6

Elements of interpreter

 Lexer: in: input, out: token Parser: in: token, out: abstract syntax tree (a.s.t.) Interpreter itself (??): in: a.s.t, out:

  • utput

Formal languages and compilers 2011

6

slide-7
SLIDE 7

Elements of interpreter

 Lexer: in: input, out: token  Parser: in: token, out: abstract syntax tree (a.s.t.) Interpreter itself (??): in: a.s.t, out:

  • utput

Formal languages and compilers 2011

7

slide-8
SLIDE 8

Elements of interpreter

 Lexer: in: input, out: token  Parser: in: token, out: abstract syntax tree (a.s.t.)  Interpreter itself: in: a.s.t, out:

  • utput

Formal languages and compilers 2011

8

slide-9
SLIDE 9

Base of the interpreter

http://disi.unitn.it/~bielova/flc/exercises/05-Interpreter_base.zip

 Definition of the lexer: lexer.mll  Definition of the parser: parser.mly  Definition for a.s.t: syntaxtree.ml  Definition of the interpreter: interpreter_base.ml  Main program: main.ml Compilation: make eval # compiles everything (win: make.bat) make clean # “cleans” from the compiled files (win: clean.bat) ./interpreter_base # starts the interpreter (input from console) ./interpreter_base < input/test_1.cre # interprets the input from test 1

Formal languages and compilers 2011

9

slide-10
SLIDE 10

Base of the interpreter

http://disi.unitn.it/~bielova/flc/exercises/05-Interpreter_base.zip

 Definition of the lexer: lexer.mll  Definition of the parser: parser.mly  Definition for a.s.t: syntaxtree.ml  Definition of the interpreter: interpreter_base.ml  Main program: main.ml Compilation: ./make.bat # compiles everything ./clean.bat # “cleans” from the compiled files ./interpreter_base # starts the interpreter (input from console) ./interpreter_base < input/test_1.cre # interprets the input from test 1

Formal languages and compilers 2011

10

slide-11
SLIDE 11

How interpreter is made

 parser.mly: definition of tokens

Formal languages and compilers 2011

11

slide-12
SLIDE 12

How interpreter is made

 parser.mly: definition of tokens  lexer.mll: regular expressions and creation of tokens

Formal languages and compilers 2011

12

slide-13
SLIDE 13

How interpreter is made

 parser.mly: definition of tokens  lexer.mll: regular expressions and creation of tokens  syntaxtree.ml: declarations of types for the syntax tree

Formal languages and compilers 2011

13

slide-14
SLIDE 14

How interpreter is made

 parser.mly: definition of tokens  lexer.mll: regular expressions and creation of tokens  syntaxtree.ml: declarations of types for the syntax tree  parser.mly: language grammar and creation of the syntax tree

Formal languages and compilers 2011

14

slide-15
SLIDE 15

How interpreter is made

 parser.mly: definition of tokens  lexer.mll: regular expressions and creation of tokens  syntaxtree.ml: declarations of types for the syntax tree  parser.mly: language grammar and creation of the syntax tree  mail.ml: starts lexer, parser, executes syntax tree

Formal languages and compilers 2011

15

slide-16
SLIDE 16

How interpreter is made

 parser.mly: definition of tokens  lexer.mll: regular expressions and creation of tokens  syntaxtree.ml: declarations of types for the syntax tree  parser.mly: language grammar and creation of the syntax tree  mail.ml: starts lexer, parser, executes syntax tree  interpreter_base.ml: functions for the execution of the syntax tree

Formal languages and compilers 2011

16

slide-17
SLIDE 17

Semantic analysis

evaluation of expressions and declarations, execution of commands

17

slide-18
SLIDE 18

Definition of the memory and environment

 Formal definition:  Updating the memory:

Formal languages and compilers 2011

18

Store : Loc "Val Env : Id# > (Loc $Val)

type store = loc -> value type env = ide -> env_entry let updatemem((s:store), addr, (v:value)): store = function x -> if (x = addr) then v else s(x)

slide-19
SLIDE 19

Arithmetic and boolean expressions: evaluation

Formal languages and compilers 2011

19

slide-20
SLIDE 20

Declaration: evaluation

Formal languages and compilers 2011

20

slide-21
SLIDE 21

Commands: execution

Formal languages and compilers 2011

21

C while b do c rs = s if B b rs = false C while b do c rs''

  • therwise

⎧ ⎨ ⎩ where s''= C c rs

slide-22
SLIDE 22

Example: repeat - until

Formal languages and compilers 2011

22 C repeat cmd until bexp rs = s' where : s'= s'' if E bexp rs'' = true C repeat cmd until bexp rs''

  • therwise

⎧ ⎨ ⎩ s''= C cmd rs

slide-23
SLIDE 23

Example: repeat - until

 parser.mly: token REPEAT and UNTIL  lexer.mll: strings ”repeat” and ”until”  syntaxtree.ml: contructor Repeat of cmd * bexp for type cmd  parser.mly: production REPEAT cmd UNTIL bexp { ... } for non-terminal symbol cmd  main.ml: nothing :)  interpreter_base.ml: execution of the command repeat - until

Formal languages and compilers 2011

23

slide-24
SLIDE 24

Programming in crème CAraMeL!

 Function for the Fibonacci number:  Factorial of the number:

Formal languages and compilers 2011

24

fib(n) = n if n < 2 fib(n-1) + fib(n-2)

  • therwise

⎧ ⎨ ⎩