when you write your essays in programming languages
play

When You Write Your Essays in Programming Languages - PowerPoint PPT Presentation

When You Write Your Essays in Programming Languages http://www.somethingofthatilk.com/index.php?id=135 CS 252: Advanced Programming Language Principles Operational Semantics Prof. Tom Austin San Jos State University Lab Review (in-class)


  1. When You Write Your Essays in Programming Languages http://www.somethingofthatilk.com/index.php?id=135

  2. CS 252: Advanced Programming Language Principles Operational Semantics Prof. Tom Austin San José State University

  3. Lab Review (in-class)

  4. Why do we need formal semantics?

  5. Everyone knows what an if statement does, right? if true then x = 1 At the end of this code snippet, the value of x else will be 1 x = 0

  6. Everyone knows what an if statement does, right? if false then x = 1 At the end of this code else snippet, the value of x will be 0 x = 0

  7. Everyone knows what an if statement does, right? if 0 then Will x be set to 0, like in C/C++? x = 1 Will x be set to 1, else like in Ruby? x = 0 Or will it be an error, like in Java?

  8. Everyone knows what an if statement does, right? x = if true Is assignment valid or an error? then 1 else 0

  9. Formal semantics define how a language works concisely and with minimal ambiguity .

  10. A Review of Compilers We don't care about Lexer/ source tokens Parser code Tokenizer lexing or parsing. Abstract Compiler Interpreter Syntax Tree (AST) Machine code We don't care if we Commands have a compiler or interpreter

  11. A Review of Compilers We don't care about Lexer/ source tokens Parser code Tokenizer lexing or parsing. Abstract Compiler Interpreter Syntax Tree (AST) Machine code We don't care if we Commands have a compiler or interpreter

  12. ASTs are the Abstract key to understandin g a language Syntax Tree (AST)

  13. Bool* Language e ::= expressions: true constant true | false constant false | if e conditional then e Despite appearances, these are really ASTs else e

  14. Values in Bool* values: v ::= constant true true constant false | false

  15. Formal Semantic Styles • Operational semantics – Big-step (or "natural") – Small-step (or "structural") • Axiomatic semantics • Denotational semantics

  16. Formal Semantic Styles • Operational semantics – Big-step (or "natural") – Small-step (or "structural") • Axiomatic semantics • Denotational semantics

  17. Big-Step Evaluation Relation An expression e … … evaluates to … ⇓ e v … a value v .

  18. Big-Step Evaluation Relation limits when the rule applies Preconditions ⇓ e v

  19. Big-step semantics for Bool* B-IfTrue e1 ⇓ true e2 ⇓ v if e1 then e2 else e3 ⇓ v e1 ⇓ false e3 ⇓ v B-IfFalse if e1 then e2 else e3 ⇓ v v ⇓ v B-Value

  20. Bool* big-step example true ⇓ true false ⇓ false if true ⇓ false then false false ⇓ false else true if (if true then false else true) then true ⇓ false else false

  21. Converting our rules into code (in-class)

  22. Language extension: numbers Users demand a new feature – numbers! We will add 3 new features: • Numbers, represented by n • succ , which takes a number and returns the next highest number. • pred , which takes a number and returns the next lowest number.

  23. BoolNum* Language e ::= true | false | if e then e else e | n Let's extend our | succ e semantics to handle these new language | pred e constructs

  24. Extended values and semantic rules (in-class)

  25. Literate Haskell • Files use .lhs extension (rather than .hs) • Code lines begin with > • All other lines are comments -- Regular .hs Literate Haskell -- source file src file (.lhs) foo x = 1 > foo x = 1 + (foo (x – 1)) > + (foo (x – 1))

  26. Lab 2: Write a Bool* Interpreter • Starter code is available at http://www.cs.sjsu.edu/~austin/cs25 2-spring18/labs/lab02/interp.lhs • Part 1: Complete evaluate function • Part 2: Extend Bool* with 0 , succ , and pred

  27. Example: Information Flow Analysis • Goal: prevent secrets from leaking – E.g. protect credit card info – Attacker might control some code • We will – Mark sensitive data – Keep track of which data is secret

  28. SecretKeeper Language e ::= true | false | n | if e then e else e | succ e | pred e | secret e

  29. Semantics assignment 1 • Write the operational semantics for SecretKeeper • Hint: values need to be marked either secret or public. Your evaluation relation might be e ⇓ v label

Recommend


More recommend