tinyc
play

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:


  1. Concepts of Program Design TinyC Gabriele Keller

  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

  3. TinyC : The essence of imperative programming • EBNF: ::= prgm gdecs stmt ::= � | gdec gdecs gdecs ::= fdec | vdec gdec ::= � | vdec vdecs vdecs ::= vdec int Ident = v ; ::= fdec int Ident 2 ( arguments ) stmt ::= expr ; | if expr then stmt 1 else stmt 2 ; | return expr ; | stmt { vdecs stmts } | while ( expr ) stmt ::= � | stmt stmts stmts ::= Num | Ident | expr 1 + expr 2 | expr 1 - expr 2 | expr Ident = expr | Ident ( exprs ) arguments ::= � | int Ident 2 , arguments

  4. Concrete Syntax • Programs consist of ‣ a list of global variable and function declarations and ‣ a statement • Function declarations int f (int x1, int x2, ...) stmt • Variable declarations int x = v;

  5. Concrete Syntax • Statements versus expressions ★ statements are mainly about e ff ects ‣ 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 e ff ects) ‣ arithmetic expressions ‣ assignments: x = expr ‣ function calls : f ( expr 1 , expr 2 , ..., expr n ) ‣ variables, integer values

  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);

  7. Abstract Syntax • We skip this step - we know how to do it • We continue with the concrete syntax (readability!)

  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: { x 1 , x 2 ,.... } ★ Environment of functions with their arity: { f 1 : n 1 , f 2 :n 2 ,....}

  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 given environments V and F, expr /stmt is well formed ‣ V, F ⊢ stmt ok ★ well-formed declarations (determining a variable and function environment) the global declarations gdecs are well formed and declare the ‣ ⊢ gdecs decs V, F environments V and F ‣ V, F ⊢ ldecs decs V’ given V and F, the local decl. ldecs are well formed and declare the new environment V’ ★ Note: we write ⊢ expr ok instead of ∅ , ∅ ⊢ expr ok

  10. Static Semantics • Given a program gdecs stmt we have ⊢ gdecs decs V, F V,F ⊢ stmt ok ⊢ gdecs 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

  11. Static Semantics • Well-formedness of statements: a statement is well-formed if all its constituents are well-formed: V, F ⊢ expr ok V,F ⊢ stmt ok V, F ⊢ while ( expr ) stmt ok V, F ⊢ expr ok V,F ⊢ stmt 1 ok V,F ⊢ stmt 2 ok V, F ⊢ if ( expr ) then stmt 1 else stmt 2 ok • Well-formedness of a block: V, F ⊢ ldecs decs V’ V’ ∪ V,F ⊢ stmts ok V, F ⊢ { ldecs stmts } ok

  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 x ∈ V V, F ⊢ x ok V, F ⊢ expr ok x ∈ V V, F ⊢ x = expr ok V, F ⊢ expr i ok f:n ∈ F V, F ⊢ f(expr 1 ,...expr n ) ok

  13. Static Semantics • Well-formedness of declaration sequences: ★ o: empty sequence V, F ⊢ o decs ∅ , ∅ ★ d ds : declaration d followed by sequence of declarations ds : V, F ⊢ d decs V’, F’ V ∪ V’, F ∪ F’ ⊢ ds decs V’’, F’’ V, F ⊢ d ds decs V’ ∪ V’’, F’ ∪ F’’

  14. Static Semantics • Well-formedness of individual declarations: ★ variable declarations x ∉ V V, F ⊢ int x = v ; decs { x }, ∅ ★ function declarations x i ∉ V f ∉ F V ∪ { x 1 ,... x n }, F ∪ { f:n } ⊢ stmt ok V, F ⊢ int f (int x 1 , ... ,int x n ) stmt decs ∅ , { f:n }

  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 )

  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

  17. Dynamic Semantics • if-statements: ( g , e ) ⇓ ( g’ , 0 ) ( g’ , s 2 ) ⇓ ( g’’ , rv ;) ( g , if e then s 1 else s 2 ) ⇓ ( g’’ , rv; ) ( g , e ) ⇓ ( g’ , v ) ( g’ , s 1 ) ⇓ ( g’’ , rv; ) ( g , if e then s 1 else s 2 ) ⇓ ( g’’ , rv; ) • while-loops: ( g , e ) ⇓ ( g’ , 0 ) ( g , while ( e ) s ) ⇓ ( g’ , 0 ; ) ( g’ , s ; while ( e ) s ) ⇓ ( g’’,rv; ) ( g , e ) ⇓ ( g’ , v ) ( g , while ( e ) s ) ⇓ ( g’’ , rv; ) • alternatively, in terms of if-statements: ( g , if e then { s ;while ( e ) s } else 0) ⇓ ( g’’ , rv; ) ( g , while ( e ) s ) ⇓ ( g’’ , rv; )

  18. Dynamic Semantics • Return statements: ( g , e ) ⇓ ( g’ , v ) ( g , return ( e )) ⇓ ( g’ , return ( v ) ; ) • Blocks: ( g.l , ss ) ⇓ ( g’ . l’ , rv; ) ( g , { l ss }) ⇓ ( 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

  19. Dynamic Semantics • Sequence of statements: ( g , o) ⇓ ( g , 0 ; ) ( g’ , ss ) ⇓ ( g’’ , v’; ) ( g , s ) ⇓ ( g’ , v; ) ( g , s ss ) ⇓ ( g’’ , v ’ ; ) ( g , s ) ⇓ ( g’ , return ( v ) ; ) ( g , s ss ) ⇓ ( g’ , return ( v ) ; ) • Variables: g@x = v ( g , x ) ⇓ ( g , v ) ( g , e ) ⇓ ( g’ , v ) ( g, x = e; ) ⇓ ( g’ @ x ← v , v )

  20. Dynamic Semantics • Function application: g@f = int f (int x 1, ... int x n ) s ( g , ( e 1 ,...,e n )) ⇓ ( g’ , (v 1 ,...,v n )) ( g ’ {int x 1 =v 1 ; ... ; s } ) ⇓ ( g’’ , return ( v )) ( g , f (e 1 ,...,e n )) ⇓ ( g’’ , v ) g@f = int f (int x 1, ... int x n ) s ( g , ( e 1 ,...,e n )) ⇓ ( g’ , (v 1 ,...,v n )) ( g’ , {int x 1 =v 1 ; ... ; s }) ⇓ ( g’’ , v ) ( g , f (e 1 ,...,e n )) ⇓ ( g’’ , v )

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend