1 / 35

Programming Language Semantics

Programming Language Semantics . Mooly Sagiv Eran Yahav msagiv@post yahave@post Schrirber 317 Open space 03-640-7606 03-640-5358 html://www.cs.tau.ac.il/~msagiv/courses/sem03.html Textbook:Winskel The Formal Semantics of Programming Languages

fauve
Download Presentation

Programming Language Semantics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Language Semantics Mooly Sagiv Eran Yahav msagiv@post yahave@post Schrirber 317 Open space 03-640-7606 03-640-5358 html://www.cs.tau.ac.il/~msagiv/courses/sem03.html Textbook:Winskel The Formal Semantics of Programming Languages CS 0368-4348-01@listserv.tau.ac.il

  2. Outline • Course requirements • What is semantics • Who needs semantics • Forms of semantics • Tentative Plan • Introduction to operational semantics

  3. Course Requirements • Prerequisites • Compiler Course • Basic set theory and logic • A theoretical course • Forms of induction • Domain theory • No algorithms • Grade • Course Notes 10% • Assignments 60% • Mostly theoretical with some programming • Home exam 30%

  4. Modern Programming Languages • Imperative • PL/1 • Pascal • C • Object Oriented • C++ • Java • C# • Functional • Scheme • ML • Logic • Prolog

  5. Programming Languages • Syntax • Which string is a legal program? • Usually defined using context free grammar+ contextual constraints • Semantics • What does a program mean? • What is the output of the program on a given run? • When does a runtime error occur? • A formal definition

  6. Benefits of Formal Semantics • Programming language design • hard- to-define= hard-to-implement=hard-to-use • Avoid design mistakes • Programming language implementation • Compiler Correctness • Correctness of program optimizations • Design of Static Analysis • Programming language understanding • Program correctness • Program equivalence • Automatic generation of interpreter • Techniques used in software engineering

  7. Desired Features of PL Semantics • Tractable • as simple as possible without losing the ability to express behavior accurately • Abstract • uncluttered by irrelevant detail • Computational • an accurate abstraction from runtime behavior • Compositional • The meaning of compound language construct is defined using the meaning of subconstructs • Supports modular reasoning

  8. Alternative Formal Semantics • Operational Semantics [Plotkin] • The meaning of the program is described “operationally” • Structural Operational Semantics • Denotational Semantics [Strachey, Scott] • The meaning of the program is an input/output relation • Axiomatic Semantics [Floyd, Hoare] • The meaning of the program is observed properties • Proof rules to show that the program is correct • Complement each other

  9. Tentative Plan • A simple programming language IMP • Structural operational Semantics of IMP • Denotational Semantics of IMP • [Axiomatic Semantics of IMP] • Non-Determinism and Parallelism • Advanced programming languages • Java byte code

  10. Chapter 2 Introduction to Operational Semantics

  11. IMP: A Simple Imperative Language • numbers N • Positive and negative numbers • n, m  N • truth values T={true, false} • locations Loc • X, Y  Loc • arithmetic Aexp • a  Aexp • boolean expressions Bexp • b  Bexp • commands Com • c  Com

  12. (3+5)  3 + 5 3 + 5  5+ 3 Abstract Syntax for IMP • Aexp • a ::= n | X | a0 + a1 | a0 – a1 | a0 a1 • Bexp • b ::= true | false | a0 = a1 | a0 a1 | b | b0 b1 | b0  b1 • Com • c ::= skip | X := a | c0 ; c1 | if b then c0elsec1| while b do c 2+34-5 (2+(34))-5 ((2+3)4))-5

  13. Example Program Y := 1; while (X=1) do Y := Y * X; X := X - 1

  14. But what about semantics

  15. Expression Evaluation • States • Mapping locations to values •  - The set of states •  : Loc  N • (X)= X=value of X in  •  = [ X  5, Y  7] • The value of X is 5 • The value of Y is 7 • The value of Z is undefined • For a Exp, , n N, • <a, >  n • a is evaluated in  to n

  16. Evaluating (a0 + a1) at  • Evaluate a0 to get a number n0 at  • Evaluate a1 to get a number n1 at  • Add n0 and n1

  17. Expression Evaluation Rules • Numbers • <n, >  n • Locations • <X, > (X) • Sums • Subtractions • Products Axioms

  18. Derivations • Arule instance • Instantiating meta variables with corresponding values

  19. Derivation (Tree) • Axioms in the leafs • Rule instances at internal nodes

  20. Computing a derivation • We write <a, >  n when there exists a derivation tree whose root is<a, >  n • Can be computed in a top-down manner • At every node try all derivations “in parallel” 5 16 21

  21. Recap • Operational Semantics • The rules can be implemented easily • Define interpreter • Structural Operational Semantics • Syntax directed • Natural semantics

  22. Equivalence of IMP expressions iff a0 a1

  23. Boolean Expression Evaluation Rules • <true, >  true • <false, >  false

  24. Boolean Expression Evaluation Rules(cont)

  25. Equivalence of Boolean expressions iff b0b1

  26. Extensions • Shortcut evaluation of Boolean expressions • “Parallell” evaluation of Boolean expressions • Other data types

  27. The execution of commands • <c, >  ’ • c terminates on  in a final state ’ • Initial state 0 • 0(X)=0 for all X • Handling assignments <X:=5, >  ’ • <X:=5, >  [5/X]

  28. Rules for commands Atomic • <skip, >   • Sequencing: • Conditionals:

  29. Rules for commands (while)

  30. Example Program Y := 1; while (X=1) do Y := Y * X; X := X - 1

  31. Equivalence of commands iff c0c1

  32. Proposition 2.8 while b do c  if then (c; while b do c) else skip

  33. Small Step Operational Semantics • The natural semantics define evaluation in large steps • Abstracts “computation time” • It is possible to define a small step operational semantics • <a, > 1 <a’, ’> • “one” step of executing a in a state  yields a’ in a state ’

  34. Small Step Semantics for Additions

  35. Summary • Operational semantics enables to naturally express program behavior • Can handle • Non determinism • Concurrency • Procedures • Object oriented • Pointers and dynamically allocated structures • But remains very closed to the implementation • Two programs which compute the same functions are not necessarily equivalent

More Related