Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.

Slides:



Advertisements
Similar presentations
4/11/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Advertisements

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Semantics Static semantics Dynamic semantics attribute grammars
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Formal Semantics of Programming Languages 虞慧群 Topic 6: Advanced Issues.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Lecture 02 – Structural Operational Semantics (SOS) Eran Yahav 1.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Denotational Semantics ICS.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
CSE 341, S. Tanimoto Concepts 1- 1 Programming Language Concepts Formal Syntax Paradigms Data Types Polymorphism.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Imperative Programming
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 2: Operational Semantics I Roman Manevich Ben-Gurion University.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
CS 363 Comparative Programming Languages Semantics.
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Language Translation A programming language processor is any system that manipulates programs expressed in a PL A source program in some source language.
Dr. Muhammed Al-MulhemICS (Denotational Semantics)
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics.
CMSC 330: Organization of Programming Languages Operational Semantics.
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics.
Programming Languages Meeting 3 September 9/10, 2014.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Dr. M. Al-Mulhem Denotational Semantics 1 Chapter 4 Fundamentals (Denotational Semantics)
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Concepts of Programming Languages
Representation, Syntax, Paradigms, Types
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Spring 2017 Program Analysis and Verification Operational Semantics
Programming Languages (CS 550) Mini Language Semantics
Programming Languages and Compilers (CS 421)
Representation, Syntax, Paradigms, Types
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
Programming Languages and Compilers (CS 421)
Representation, Syntax, Paradigms, Types
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Representation, Syntax, Paradigms, Types
Programming Languages and Compilers (CS 421)
Language semantics Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Assignments and Procs w/Params
Spring 2016 Program Analysis and Verification Operational Semantics
Programming Languages Dan Grossman 2013
Presentation transcript:

Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories (terminals, non-terminals) –Specify value domains for semantic (meaning) functions –Define meaning functions to map syntactic constructs into their intended meaning Syntax-directed approach provides compositional rules for meanings.

Example: decimal numerals Syntactic categories: –D Є Digits the decimal digits: 0,1 … –N Є Num decimal numerals (strings) Productions –D ::= ‘0’ | ‘1’ | ‘2’…. –N ::= D | N D Value Domain: –Nat = {0,1,…} the natural numbers –Semantics defines mapping of strings into numbers

The meaning of decimal numerals Semantic functions –D : Digits → Nat –M : Num → Nat Semantic equations: –D [‘0’] = 0, D [‘1’] = 1, … –M [D] = D [D] production for M –M [N D] = 10 * M [N] + M [D] production for M

Propositional logic Boolean expressions with variables and logical connectives. Categories –P Є Prop : propositions (variable names) –F Є Form : Formulae Productions –F ::= P | ¬ F | F or F | F and F | F => F (implication) Value Domains –Bool = {True, False} boolean values –  Є Assign = P → Bool assignments (values of names)

The meaning of propositional formulae The value of a formula depends on the current assignments. Curried form is: –M : Form → Assign → Bool Semantic equations: –M [P]  =  (P) -- meaning of variable is its value –M [ ¬ F]  = ¬ M [F]  –M [F 1 or F 2 ]  = M [F 1 ]  or M [F 2 ]  –M [F 1 and F 2 ]  = M [F 1 ]  and M [F 2 ]  –M [F 1 => F 2 ]  = ¬ M [F 1 ]  or M [F 2 ]  Must distinguish “or” in syntax from or in semantics

A language of expressions Syntactic categories –Id Є Ident Identifiers –Num Є N numeric literals –E Є Exp Expressions Productions –E ::= num | Id | (E 1 + E 2 ) | (E 1 – E 2 ) –E ::= let Id = E 1 in E 2 end local binding Value domains –Semantics describes integer values of expressions, in terms of current bindings of identifiers: –Int : {…-2, -1, 0, 1, 2…} integers  Є Env : Ident → Int environments

The meaning of simple expressions Semantics describes rule of computation. Value is a function of environment: E : Exp → Env → Int Semantic functions: E [Id]  =  (Id) E [ num]  = M [num] given previously E [ E 1 + E 2 ]  = E [E 1 ]  + E [E 2 ]  E [ E 1 - E 2 ]  = E [E 1 ]  - E [E 2 ] 

Bindings and environments A binding is modeled by an update function, best described in a programming language: fun Update (Env, Id, Val) (x) = if Id = x then Val else Env (x) E [let Id = E 1 in E 2 end]  = E [E 2 ] Update ( , Id, E [E 1 ])

A domain with an error value To model unbound names or type errors in a dynamically typed language, extend domain with undefined value: –Int = {… -2, -2, 0, 1, 2, …} integers –  Є Env = Ident → (Int + {  } ) environments Type checking is explicit: –E [Id]  = let n =  (Id) in if Is_Int (n) then n else  end; –E [E 1 + E 2 ]  = let n 1 = E [E 1 ]  n 2 = E [E 2 ]  in if Is_int (n 1 ) andalso Is_Int (n 2 ) then n 1 + n 2 else  end;

Denotational Semantics of Imperative languages Need domains to describe the state of the computation, including sequences of instructions (the program counter) and memory (the mapping of names to locations). C Є Com : commands, describes control structures –C ::= C 1 ; C 2 –C ::= if E then C 1 else C 2 –C ::= while E do C Value domain  Є States configuration of computation

The meaning of commands The value of a command is a state: C [if E then C1 else C2]  = if IsTrue ( E [E]  ) then C [C1]  else C [C2]  C [C1 ; C2]  = let  ’ = C [C1]  in C [C2]  ’ end C [while E do C]  = let fun p (  ’) = if IsTrue ( E [E]  ) then p ( C [C]  ’ ) else  ’ in p (  ) end

Describing state New syntactic category: –L Є left-hand side : for bindings and references Value domains: –  Є Loc locations (addresses) –  Є States = Loc → (Int +  ) memory –  Є Env = Ident → (Int + Loc +  ) environments Semantic functions: –C : Com → Env → States → States –E : Exp → Env → States → Int –L : Left → Env → States → Loc

Manipulating state C ::= C1; C2 | if E then C1 else C2 | L := E assignment | local V:= E in C end local variable L ::= Id (lhs is simple name) C [L := E]  = let  = L [L]  ; meaning of l.h.s is a location n = E [E]  meaning of rhs is a value in update ( , , n); meaning of assignment is new state end; side effects of E are ignored

Local variables Assume pool of available locations C [local V := E in C end]  = let  Є Loc such that  = unused; n := E [ E]  in C [C] update ( , V,  ) update ( , , n) end Enlarge environment with new variable, enlarge state with its initial value, evaluate command in new environment / state

Dereferencing Obtaining the value of a variable is a two-step affair: name -> loc, loc -> value E ::= !L | if E1 then E2 else E3 | … E [ !L]  = let  = L [L]  in  end

Programs New syntactic category: P P ::= program (I) C end program is a command with a read / write parameter Semantic function: P : Prog -> int -> int P [ program (I); C end] n = let fun  (J) = undefined for all J fun  = unused for all   Є Loc  f = C [C ] update ( , I,  ) update ( , , n) in  f (  ) end

Function calls Semantic domains: A function affects the state: f Є Fun = States -> int > int * States The name of the function denotes a function, not a location: Denote = int + Loc + Fun  Є Env = Ident -> (Denote +  ) A function does not depend on the environment: static binding of identifiers.

Syntax and semantics of functions M [function f (a) = E 1 in E 2 end]  = E 1 is body of function, E 2 contains calls let fun g  ’ n = M [E 1 ] update ( , a, n)  ’ in M [E 2 ] update ( , f, g) end M [call f (E)]  = let (n,  ’) = M [E]  ; func =  (f) in func  ’ n end

Transfer of control In the presence of goto’s and exceptions, state must include notion of program counter. C ::= C1 ; C2 | if E then C1 else C2 | L := E | local I := E in C end | skip | goto L | L : C M ::= program (I) ; C end New semantic domains and functions:  Є CC : States -> int (next instruction to execute) C : Com -> Env -> CC -> States -> Int

Commands have continuations C [C 1 ; C 2 ]  = let fun  ’ (  ’) = C [C 2 ]  ’ in C [C 1 ]   ’  A label captures a continuation: C [L : C]  = let fun  ’ (  ’) = C [C] update ( , L,  ’)  ’ in  ’ (  ) end

The goto resets the continuation C [goto L]  = (  L)  The meaning of the goto is the result of applying the continuation of the label to the state. C [Here : goto Here]  =  ’ (  ) And  ’ (  ) =  ’ (  ) for any  => infinite loop