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

when you write your essays in programming languages
SMART_READER_LITE
LIVE PREVIEW

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)


slide-1
SLIDE 1

http://www.somethingofthatilk.com/index.php?id=135

When You Write Your Essays in Programming Languages

slide-2
SLIDE 2

CS 252: Advanced Programming Language Principles

  • Prof. Tom Austin

San José State University

Operational Semantics

slide-3
SLIDE 3

Lab Review

(in-class)

slide-4
SLIDE 4

Why do we need formal semantics?

slide-5
SLIDE 5

Everyone knows what an if statement does, right? if true then x = 1 else x = 0

At the end of this code snippet, the value of x will be 1

slide-6
SLIDE 6

Everyone knows what an if statement does, right? if false then x = 1 else x = 0

At the end of this code snippet, the value of x will be 0

slide-7
SLIDE 7

Everyone knows what an if statement does, right? if 0 then x = 1 else x = 0

Will x be set to 0, like in C/C++? Will x be set to 1, like in Ruby? Or will it be an error, like in Java?

slide-8
SLIDE 8

Everyone knows what an if statement does, right? x = if true then 1 else 0

Is assignment valid or an error?

slide-9
SLIDE 9

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

slide-10
SLIDE 10

A Review of Compilers

Lexer/ Tokenizer Parser

source code tokens

Abstract Syntax Tree (AST) Compiler

Machine code

Interpreter

Commands

We don't care about lexing or parsing. We don't care if we have a compiler or interpreter

slide-11
SLIDE 11

A Review of Compilers

Lexer/ Tokenizer Parser

source code tokens

Compiler

Machine code

Interpreter

Commands

We don't care about lexing or parsing. We don't care if we have a compiler or interpreter

Abstract Syntax Tree (AST)

slide-12
SLIDE 12

Abstract Syntax Tree (AST)

ASTs are the key to understandin g a language

slide-13
SLIDE 13

Bool* Language

e ::= true | false | if e then e else e expressions: constant true constant false conditional

Despite appearances, these are really ASTs

slide-14
SLIDE 14

Values in Bool*

v ::= true | false values: constant true constant false

slide-15
SLIDE 15

Formal Semantic Styles

  • Operational semantics

–Big-step (or "natural") –Small-step (or "structural")

  • Axiomatic semantics
  • Denotational semantics
slide-16
SLIDE 16

Formal Semantic Styles

  • Operational semantics

–Big-step (or "natural") –Small-step (or "structural")

  • Axiomatic semantics
  • Denotational semantics
slide-17
SLIDE 17

Big-Step Evaluation Relation

e v

An expression e … … evaluates to … … a value v.

slide-18
SLIDE 18

Big-Step Evaluation Relation

e v

Preconditions

limits when the rule applies

slide-19
SLIDE 19

Big-step semantics for Bool*

e1 ⇓ true e2 ⇓ v if e1 then e2 else e3 ⇓ v

B-IfTrue

e1 ⇓ false e3 ⇓ v if e1 then e2 else e3 ⇓ v

B-IfFalse

v ⇓ v

B-Value

slide-20
SLIDE 20

Bool* big-step example

if (if true then false else true) then true else false

if true then false else true ⇓ false

true ⇓ true false ⇓ false

false ⇓ false

⇓ false

slide-21
SLIDE 21

Converting our rules into code

(in-class)

slide-22
SLIDE 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.

slide-23
SLIDE 23

BoolNum* Language

e ::= true | false | if e then e else e | n | succ e | pred e

Let's extend our semantics to handle these new language constructs

slide-24
SLIDE 24

Extended values and semantic rules

(in-class)

slide-25
SLIDE 25

Literate Haskell

  • Files use .lhs extension (rather than .hs)
  • Code lines begin with >
  • All other lines are comments
  • - Regular .hs
  • - source file

foo x = 1 + (foo (x – 1)) Literate Haskell src file (.lhs) > foo x = 1 > + (foo (x – 1))

slide-26
SLIDE 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

slide-27
SLIDE 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

slide-28
SLIDE 28

SecretKeeper Language

e ::= true | false | n | if e then e else e | succ e | pred e | secret e

slide-29
SLIDE 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 ⇓ vlabel