TinyC Gabriele Keller TinyC : The essence of imperative programming - - PowerPoint PPT Presentation

tinyc
SMART_READER_LITE
LIVE PREVIEW

TinyC Gabriele Keller TinyC : The essence of imperative programming - - PowerPoint PPT Presentation

Concepts of Program Design TinyC Gabriele Keller TinyC : The essence of imperative programming An imperative language with the following features: Global function declarations Global and local variables Assignment Iteration:


slide-1
SLIDE 1

Concepts of Program Design

TinyC

Gabriele Keller

slide-2
SLIDE 2

TinyC : The essence of imperative programming

  • An imperative language with the following features:

★Global function declarations ★Global and local variables ★Assignment ★Iteration: while-loops ★Conditional: if-statements ★Only a single value type: int

slide-3
SLIDE 3

TinyC : The essence of imperative programming

  • EBNF:

prgm ::= gdecs stmt gdecs ::= | gdec gdecs gdec ::= fdec | vdec vdecs ::= | vdec vdecs vdec ::= int Ident = v; fdec ::= int Ident2 (arguments)stmt stmt ::= expr; | if expr then stmt1 else stmt2; | return expr; | { vdecs stmts } | while ( expr ) stmt stmts ::= | stmt stmts expr ::= Num | Ident | expr1 + expr2 | expr1 - expr2 | Ident = expr | Ident (exprs) arguments ::= | int Ident2 , arguments

slide-4
SLIDE 4

Concrete Syntax

  • Programs consist of
  • a list of global variable and function declarations and
  • a statement
  • Function declarations
  • Variable declarations

int f (int x1, int x2, ...) stmt int x = v;

slide-5
SLIDE 5

Concrete Syntax

  • Statements versus expressions

★statements are mainly about effects

  • expr;
  • if expr then stmt else stmt
  • while (expr) stmt
  • return (expr);
  • {ldec stmts}: a block - local variable declarations followed by a

sequence of statements

★expressions are about values (but can also have effects)

  • arithmetic expressions
  • assignments: x = expr
  • function calls: f (expr1, expr2, ..., exprn)
  • variables, integer values
slide-6
SLIDE 6
  • Variables

★have to be initialised ★not true variables in the mathematical sense (MinHs’s are) ★for now, we assume that variables are not re-used within their scope

  • Example program:

int result = 0; int div (int x, int y) { int res = 0; while (x > y) { x = x - y; res = res + 1; } return res; } result = div (16, 5);

slide-7
SLIDE 7

Abstract Syntax

  • We skip this step - we know how to do it
  • We continue with the concrete syntax (readability!)
slide-8
SLIDE 8

Static Semantics

  • What kind of properties do we have to check?

★Are all variables and functions declared before use? ★Are functions called with the correct number of arguments? ★What about return statements in functions?

  • could we check that every possible control flow in a function block

ends with a return statement?

  • for now, we set the return value to the value of the last expression in the

block in case there is no explicit return statement

  • What kind of structure do we need to maintain for these checks?

★Environment of variables: {x1, x2,....} ★Environment of functions with their arity: {f1: n1, f2:n2,....}

slide-9
SLIDE 9

Static Semantics

  • Two kinds of language components:

★ expressions and statements ★ declarations

  • Two kinds of judgements:

★well-formed expressions and judgements (given both a variable

environment V and function environment F):

  • V, F ⊢ expr ok
  • V, F ⊢ stmt ok

★well-formed declarations (determining a variable and function environment)

  • ⊢ gdecs decs V, F
  • V, F ⊢ ldecs decs V’

★Note: we write ⊢ expr ok instead of ∅,∅ ⊢ expr ok given environments V and F, expr /stmt is well formed the global declarations gdecs are well formed and declare the environments V and F given V and F, the local decl. ldecs are well formed and declare the new environment V’

slide-10
SLIDE 10

Static Semantics

  • Given a program

gdecs stmt we have ⊢ gdecs stmt ok ⊢ gdecs decs V, F V,F ⊢ stmt ok That is, the program is well-formed if

  • its global declarations are well-formed and declare variables V and

functions F

  • and the body statement is well-formed with respect to those global

variables V and functions F

slide-11
SLIDE 11

Static Semantics

  • Well-formedness of statements: a statement is well-formed if all its

constituents are well-formed: V, F ⊢ while (expr) stmt ok V, F ⊢ expr ok V,F ⊢ stmt ok V, F ⊢ if (expr) then stmt1 else stmt2 ok V, F ⊢ expr ok V,F ⊢ stmt1 ok V,F ⊢ stmt2 ok

  • Well-formedness of a block:

V, F ⊢ { ldecs stmts } ok V, F ⊢ ldecs decs V’ V’∪V,F ⊢ stmts ok

slide-12
SLIDE 12

Static Semantics

  • Well-formedness of expressions: an expression is well-formed if

★all of its variables and functions are declared and ★functions are called with the correct number of arguments

V, F ⊢ x ok x ∈ V V, F ⊢ x = expr ok V, F ⊢ expr ok x ∈ V V, F ⊢ f(expr1,...exprn) ok V, F ⊢ expri ok f:n ∈ F

slide-13
SLIDE 13

Static Semantics

  • Well-formedness of declaration sequences:

★o: empty sequence ★d ds : declaration d followed by sequence of declarations ds:

V, F ⊢ o decs V, F ⊢ d ds decs V, F ⊢ d decs V’, F’ V∪V’, F∪F’ ⊢ ds decs V’’, F’’ V’∪V’’, F’∪F’’ ∅,∅

slide-14
SLIDE 14

Static Semantics

  • Well-formedness of individual declarations:

★variable declarations ★function declarations

V, F ⊢ int x = v; decs x ∉ V V, F ⊢ int f (int x1,...,int xn) stmt decs xi ∉ V f ∉ F V ∪ {x1,... xn}, F ∪ {f:n} ⊢stmt ok {x}, ∅ ∅,{f:n}

slide-15
SLIDE 15

Big Step Dynamic Semantics

  • What information do we have to keep track of?

★the current statement ★the values of all variables

  • Evaluation relations

★program execution gs⇓(g’, rv) ★statement execution: (g,s)⇓(g’, rv;) ★expression execution: (g,e)⇓(g’,v)

where v is a integer value, rv either a value or return(v)

slide-16
SLIDE 16

Big Step Dynamic Semantics

  • The environment is an ordered sequence associating

★variables with integer values ★function names with parameter list and body

  • Operations on the environment:

★extension:

  • add a new declaration to the environment: g.(int x = 4)

★lookup: g@x = 5

  • x is currently bound to value 5
  • if x occurs more than once, choose right most binding

★update: g@x← 5

  • change value of x to 5
  • environment g has to contain x already
slide-17
SLIDE 17

(g’, s; while (e) s)⇓(g’’,rv;) (g, e)⇓(g’,v) (g, while (e) s)⇓ (g, while (e) s)⇓(g’’,rv;)

Dynamic Semantics

  • if-statements:

(g, e)⇓(g’,0) (g’, s2)⇓(g’’,rv;)

  • while-loops:

(g, if e then {s;while (e) s} else 0)⇓(g’’,rv;)

  • alternatively, in terms of if-statements:

(g, if e then s1 else s2)⇓ (g, if e then s1 else s2)⇓ (g, e)⇓(g’,0) (g, while (e) s)⇓(g’’,rv;) (g’’,rv;) (g, e)⇓(g’,v) (g’, s1)⇓(g’’,rv;) (g’’,rv;) (g’,0;)

slide-18
SLIDE 18

Dynamic Semantics

  • Return statements:

(g, return(e))⇓(g’, return(v);)

  • Blocks:

(g, {l ss})⇓ (g.l, ss)⇓(g’.l’,rv;) (g, e)⇓(g’,v) (g’, rv;)

add local variables with initial value temporarily into environment, only g is threaded through - the local environment l/l’ is discarded after the statements ss are executed

slide-19
SLIDE 19

Dynamic Semantics

  • Sequence of statements:

(g, o)⇓(g, 0;)

  • Variables:

(g, x)⇓(g, v) g@x = v (g, x=e;)⇓ (g, s ss)⇓ (g, s)⇓(g’, return(v);) (g, s ss)⇓(g’’, v’;) (g, s)⇓(g’, v;) (g’, ss)⇓(g’’, v’;) (g, e)⇓(g’, v) (g’@x← v, v) (g’, return(v);)

slide-20
SLIDE 20

Dynamic Semantics

  • Function application:

(g, f (e1,...,en))⇓ (g’ {int x1 =v1;...;s})⇓(g’’, return(v)) g@f = int f (int x1,... int xn)s (g, (e1,...,en))⇓(g’, (v1,...,vn)) (g, f (e1,...,en))⇓(g’’, v) (g’, {int x1 =v1;...;s})⇓(g’’, v) g@f = int f (int x1,... int xn)s (g, (e1,...,en))⇓(g’, (v1,...,vn)) (g’’, v)