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 Office hours No office hours 9/7 or 9/11 Additional office hours on 9/15 11am-1pm CS 252: Advanced Programming Language Principles


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

  2. Office hours • No office hours 9/7 or 9/11 • Additional office hours on 9/15 11am-1pm

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

  4. Lab Review (in-class)

  5. Why do we need formal semantics?

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

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

  8. 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?

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

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

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

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

  13. ASTs are the key to Abstract understanding a language Syntax Tree (AST)

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

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

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

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

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

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

  20. TAPL Excellent reference for type systems and operational semantics. Available at library.

  21. 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

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

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

  24. 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.

  25. 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

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

  27. 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))

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

  29. 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

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

  31. 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

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