Syntax vs. semantics. Detour: Natural deduction. Syntax vs. semantics (cont d) Syntax = grammatical structure Semantics = underlying meaning

Similar documents
CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC 330: Organization of Programming Languages

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

CSC 7101: Programming Language Structures 1. Operational Semantics. Winskel, Ch. 2 Slonneger and Kurtz Ch 8.4, 8.5, 8.6. Operational vs.

3.7 Denotational Semantics

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

Formal Semantics of Programming Languages

Formal Semantics of Programming Languages

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

Application: Programming Language Semantics

CMSC 330: Organization of Programming Languages. Operational Semantics

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

A Simple Syntax-Directed Translator

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

Lecture 2: Big-Step Semantics

Introduction to Denotational Semantics. Brutus Is An Honorable Man. Class Likes/Dislikes Survey. Dueling Semantics

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Introduction to Denotational Semantics. Class Likes/Dislikes Survey. Dueling Semantics. Denotational Semantics Learning Goals. You re On Jeopardy!

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

Meeting13:Denotations

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Introduction to Axiomatic Semantics

Parsing. CS321 Programming Languages Ozyegin University

The Substitution Model. Nate Foster Spring 2018

CIS 500 Software Foundations. Final Exam. May 3, Answer key

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Program Analysis: Lecture 02 Page 1 of 32

1 Lexical Considerations

Lectures 20, 21: Axiomatic Semantics

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

COMP 507: Computer-Aided Program Design

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CIS 500: Software Foundations

A simple syntax-directed

Induction and Semantics in Dafny

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Design

A CRASH COURSE IN SEMANTICS

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Meeting14:Denotations

CSCE 314 Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

In Our Last Exciting Episode

Programming Languages Fall 2013

7. Introduction to Denotational Semantics. Oscar Nierstrasz

15 212: Principles of Programming. Some Notes on Grammars and Parsing

Operational Semantics. One-Slide Summary. Lecture Outline

Mosig M1 - PLSCD Written exam

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Lexical Considerations

CMSC330 Spring 2016 Midterm #2 9:30am/12:30pm/3:30pm

Chapter 3. Describing Syntax and Semantics

Modules, Structs, Hashes, and Operational Semantics

CMSC 330: Organization of Programming Languages

COS 320. Compiling Techniques

CIS 500 Software Foundations Midterm II

Lexical Considerations

Verifying Safety Property of Lustre Programs: Temporal Induction

If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014

CSE 3302 Programming Languages Lecture 2: Syntax

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

Part 5 Program Analysis Principles and Techniques

Big-step Operational Semantics Revisited

The Substitution Model

CMSC 330: Organization of Programming Languages

CMSC 330, Fall 2018 Midterm 2

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Syntax and Grammars 1 / 21

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC 330: Organization of Programming Languages

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

Introduction to Denotational Semantics (1/2)

1 Introduction. 3 Syntax

Fundamental Concepts. Chapter 1

3.1 IMP: A Simple Imperative Language

Computing Fundamentals 2 Introduction to CafeOBJ

3.4 Denotational Semantics

CMSC 330: Organization of Programming Languages

LECTURE 17. Expressions and Assignment

Reasoning About Imperative Programs. COS 441 Slides 10

2 Introduction to operational semantics

Review: Concrete syntax for Impcore

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Handout 9: Imperative Programs and State

Operational Semantics of Cool

CS2104 Prog. Lang. Concepts

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

Foundations: Syntax, Semantics, and Graphs

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Transcription:

Syntax vs. semantics CMSC 631 Program nalysis and Understanding Spring 2013 Operational Semantics Syntax = grammatical structure Semantics = underlying meaning Sentences in a language can be syntactically wellformed but semantically meaningless Colorless green ideals sleep furiously. Syntactic Structures, Noam Chomsky, 1957. if ( foo > 37) { oogbooga(3); baz * qux ; } 2 Syntax vs. semantics (cont d) General principle: enforce correctness at the earliest stage possible Keywords identified in lexer Detour: Natural deduction We are going to use natural deduction rules to describe semantics So we need to understand how those work first alanced () s enforced in parser Types enforced afterward Why? Earlier in pipeline simpler to think about Reporting errors is easier - Less transformation from original program - Errors may be easier to localize Faster algorithms for detecting violations - Higher chance could employ them interactively in IDE 3 Natural deduction rules provide a syntax for writing down proofs Each rule is an axiom, or an allowable deduction Rules are composed together - The result is called a derivation The things rules prove are called judgments 4

Structure of a rule Example: Logic -I -E_L -E_R H1 H2... Hn C name -I_L -I_R...... C C C -E H1... Hn are hypotheses, C is the conclusion If H1 and H2 and... and Hn hold, then C holds true true... -I -E (modus ponens) 5 6 Example: Logic (cont d) Example derivations...... C C -I (reductio ad absurdum) -E (noncontradiction) ( C) ( ( C)) Note these are axioms from classical logic ( ) ( ) 7 8

IMP: language of commands a ::= n X a0+a1 a0-a1 a0 a1 b ::= bv a0=a1 a0 a1 b b0 b1 b0 b1 c ::= skip X:=a c0;c1 if b then c0 else c1 while b do c n N = integers, X Var = variables, bv ool = {true, false} Could have written n ::= 0 1 2 3... - X ::= x y z xx... Notice grammar is for STs Not concerned about issues like ambiguity, associativity, precedence Program state IMP contains imperative updates, so we need to model the program state Here the state is simply the integer value of each variable (Notice can t assign a boolean to a variable, by syntax!) State: σ : Var N state σ is a mapping from variables to their values Syntax stratified into commands (c) and expressions (a,b) Expressions have no side effects No function calls (and no higher order functions) 9 10 Judgments rithmetic evaluation Operational semantics has three kinds of judgments a, σ n n, σ n X, σ σ(x) - In state σ, arithmetic expression a evaluates to n b, σ bv - In state σ, boolean expression b evaluates to true or false c, σ σ - Running command c in state σ produces state σ Can immediately see only commands have side effects Only form whose evaluation produces a new state Commands also do not return values Note this is math, so we express state changes by creating the new state σ. We can t just mutate σ. a0+a1, σ n0+n1 a0-a1, σ n0-n1 a0 a1, σ n0 n1 11 12

rithmetic evaluation (cont d) Notes: Rule for variables only defined if X is in dom(σ). Otherwise the program goes wrong - it produces no final value Hypotheses of last three rules stacked to save space Notice difference between syntactic operators, on the left side of arrows, and mathematical operators, on the right side of arrows One rule for each kind of expression - These are syntax-directed rules In the rules, we use terminals and non-terminals in the grammar to stand for anything producible from them - E.g., n stands for any integer; σ for any state; etc. Order of evaluation irrelevant, because there are no side effects Sample derivation 1+2+3 (2*x)-4 in σ = [x 3] 13 14 Correspondence to OCaml (* a ::= n X a0+a1 a0-a1 a0 a1 *) type aexpr = Int of int Var of string Plus of aexpr * aexpr Minus of aexpr * aexpr Times of aexpr * aexpr Represent σ as an association list let rec aeval sigma = function Int n -> n Var n -> List.assoc n sigma Plus (a1, a2) -> (aeval sigma a1) + (aeval sigma a2) Minus (a1, a2) -> (aeval sigma a1) - (aeval sigma a2) Times (a1, a2) -> (aeval sigma a1) * (aeval sigma a2) oolean evaluation true, σ true false, σ false b, σ bv b, σ bv a0=a1, σ n0=n1 a0 a1, σ n0 n1 b0, σ bv0 b1, σ bv1 b0 b1, σ bv0 bv1 b0, σ bv0 b1, σ bv1 b0 b1, σ bv0 bv1 15 16

Sample derivations Correspondence to OCaml false true 2 X X 4 in σ = [X 3] (* b ::= bv a0=a1 a0 a1 b b0 b1 b0 b1 *) type bexpr = V of bool Eq of aexpr * aexpr Leq of aexpr * aexpr Not of bexpr nd of bexpr * bexpr Or of bexpr * bexpr let rec beval sigma = function V b -> b Eq (a1, a2) -> (aeval sigma a1) = (aeval sigma a2) Leq (a1, a2) -> (aeval sigma a1) <= (aeval sigma a2) Not b -> not (beval sigma b) nd (b1, b2) -> (beval sigma b1) && (beval sigma b2) Or (b1, b2) -> (beval sigma b1) (beval sigma b2) 17 18 Command evaluation Command evaluation (cont d) skip, σ σ a, σ n X:=a, σ σ[x n] c0, σ σ0 c1, σ0 σ1 c0; c1, σ σ1 b, σ true c0, σ σ0 if b then c0 else c1, σ σ0 b, σ false c1, σ σ1 if b then c0 else c1, σ σ1 Here σ[x a] is the state that is the same as σ, except X now maps to a (σ[x a])(x) = a (σ[x a])(y) = σ(y) X Y Notice order of evaluation explicit in sequence rule Two rules for conditional Just like in logic we needed two rules for -E and -I Notice we specify only one command is executed 19 20

Command evaluation (cont d) Sample derivations n:=3; f:=1; while n 1 do f := f * n; n := n - 1 b, σ false while b do c, σ σ b, σ true c; while b do c, σ σ while b do c, σ σ' 21 22 Correspondence to OCaml (* c ::= skip X:=a c0;c1 if b then c0 else c1 while b do c *) type cmd = CSkip Cssn of string * aexpr CSeq of cmd * cmd CIf of bexpr * cmd * cmd CWhile of bexpr * cmd let rec ceval sigma = function CSkip -> sigma Cssn (x, a) -> (x:(aeval sigma a))::sigma (* note List.assoc in aeval stops at first match *) CSeq (c0, c1) -> let sigma0 = ceval sigma c0 in ceval sigma0 c1 (* or ceval (ceval sigma c0) c1 *) CIf (b, c0, c1) -> if (beval sigma b) then (ceval sigma c0) else (ceval sigma c1) CWhile (b, c) -> if (beval sigma b) then ceval sigma (CSeq (c, CWhile(b,c))) else sigma 23 ig-step semantics Semantics given are big step or natural semantics E.g., c, σ σ Commands fully evaluated to produce the final output state, in one, big step Limitation: Can t give semantics to non-terminating programs We would need to work with infinite derivations, which is typically not valid - Requires a coinductive, rather than inductive, interpretation 24

Small-step semantics Instead, can expose intermediate steps of computation a σ a - Evaluating a one step in state σ produces a b σ b - Evaluating b one step in state σ produces b c, σ 1 c, σ - Running command c in state σ for one step yields a new command c and new state σ Note putting σ on the arrow is just a convenience Good notation for stringing evaluations together - a0 σ a1 σ a2 σ... Put 1 on arrow for commands just to let us distinguish different kinds of arrows 25 Small-step rules for arithmetic X σ σ(x) a0 σ a0 a0+a1 σ a0 +a1 Similarly for - and a1 σ a1 n+a1 σ n+a1 Notice no rule for evaluating integer n n integer is in normal form, meaning no further evaluation is possible We ve fixed the order of evaluation Could also have made it non-deterministic p=m+n n+m σ p 26 Context rules We have some rules that do the real work The rest are context rules that define order of evaluation Cool trick (due to Hieb and Felleisen): Define a context as a term with a hole in it Small-step rules for booleans Very similar to arithmetic expressions Too boring to write them all down... - C ::= C+a n+c C-a n-c C a n C Notice the terms generated by this grammar always have exactly one, and it always appears at the next position that can be evaluated Define C[a] to be C where is replaced by a - Ex: (( +3) 5)[4] = (4+3) 5 Now add one, single context rule: a σ a C[a] σ C[a ] 27 28

Small-step rules for commands X:=n, σ 1 skip, σ[x n] skip; c1, σ 1 c1, σ if true then c0 else c1, σ 1 c0, σ if false then c0 else c1, σ 1 c1, σ while b do c, σ 1 if b then (c; while b do c) else skip, σ Define/extend contexts (for exps, and for comms): D ::= D; c C ::=... X:=C if C then c0 else c1 while C do c and the context rule: c, σ 1 c, σ D[c], σ 1 D[c ], σ 29