Programming Languages Meeting 4 September 16/17, 2014.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
CS252: Systems Programming Ninghui Li Topic 4: Regular Expressions and Lexical Analysis.
Compilers and Language Translation
CS 330 Programming Languages 09 / 19 / 2006 Instructor: Michael Eckmann.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
CS 355 – Programming Languages
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
CS5371 Theory of Computation
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Context-Free Grammars Lecture 7
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
CS 536 Spring Ambiguity Lecture 8. CS 536 Spring Announcement Reading Assignment –“Context-Free Grammars” (Sections 4.1, 4.2) Programming.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Chapter 3: Formal Translation Models
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Denotational Semantics ICS.
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
CS 2104 Prog. Lang. Concepts Dr. Abhik Roychoudhury School of Computing Introduction.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
OPERATOR PRECEDENCE PARSING
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
1 Homework #7 (Models of Computation, Spring, 2001) Due: Section 1; April 16 (Monday) Section 2; April 17 (Tuesday) 2. Covert the following context-free.
Syntax Specification and BNF © Allan C. Milne Abertay University v
Copyright © Cengage Learning. All rights reserved. CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF.
By the end of this session you should be able to...
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
PART I: overview material
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1B) UTPA – Fall 2011.
CPS120: Introduction to Computer Science Decision Making in Programs.
sequence of execution of high-level statements
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Introduction to Parsing
CPS 506 Comparative Programming Languages Syntax Specification.
Language: Set of Strings
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Chapter 3 Describing Syntax and Semantics
Dr. Muhammed Al-MulhemICS (Denotational Semantics)
Mathematical Preliminaries
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Syntax and Grammars.
Semantics (1).
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics.
1 Section 8.2 Program Correctness (for imperative programs) A theory of program correctness needs wffs, axioms, and inference rules. Wffs (called Hoare.
Programming Languages Meeting 3 September 9/10, 2014.
Dr. M. Al-Mulhem Denotational Semantics 1 Chapter 4 Fundamentals (Denotational Semantics)
“It is impossible to define every concept.” For example a “set” can not be defined. But Here are a list of things we shall simply assume about sets. A.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Concepts of Programming Languages
System Software Unit-1 (Language Processors) A TOY Compiler
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
CSC 8310 Programming Languages
Syntax versus Semantics
Programming Languages
Programming Languages (CS 550) Mini Language Semantics
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Assignments and Procs w/Params
Presentation transcript:

Programming Languages Meeting 4 September 16/17, 2014

Planning Ahead Short exam next time – Construct a grammar given a description of strings in the language – Parse a sentence in a grammar – Describe a language defined by a grammar

Short Exam (2) – Inferences from the grammar Precedence of operators Associativity of operators Ambiguity of sentences – Construct one of the various semantic domains in a specific situation

REF Results Declarations followed by 1. A := RRF + B; Ans: 4 2. B := RRE; Ans: 1 3. RC := RRF + RD; Ans: Type mismatch (see slide Semantics(6) from last week).

REF Results (2) 4. RRF : = RRE; Ans: Location of RC 5. RRE : = RD; Ans: Location of RD

Multiple Assignments Let’s now investigate statements of the form ref1,ref2,…,refm := exp1,exp2,…,expn What conditions should we put on m and n?

MA (2) m = 1, n = 1 The ordinary assignment statement A := 0; (ref1 is A and the location it is mapped to; exp1 has value 0)

MA (3) m > 1, n = 1 A,B,C := 0; should represent the sequence A := 0; B := 0; C := 0; or any of the other 5 arrangements of it. (Why 5?)

MA (4) m = 2, n = 2 A,B = 0,1; A,B = X,Y; A,B = B,A; If initially A:=9; B:=23; X:=7; Y:=12; what is the state after each of the statements above, accumulating the changes?

MA (5) More complexity: Suppose A has been declared to be an array of size 10 of integers, all initialized to 0. Also j has been declared to be an integer and currently has value 4. What is the result of executing A[j-1], j, A[j+1] := j+1; Work with your partner to find several plausible answers.

MA(6) Case 1: Evaluate exp to obtain a value v for i=1 to m step 1 evaluate ref i to obtain loc i assign v to loc i In our case j becomes 5 and A is [0,0,5,0,0,5,0,0,0,0]

MA (7) Case 2: Evaluate exp to obtain a value v for i=m to 1 step −1 evaluate ref i to obtain loc i assign v to loc i In our case j becomes 5 and A is [0,0,0,5,5,0,0,0,0,0]

MA (8) Case 3: for i=1 to m step 1 evaluate ref i to obtain loc i evaluate exp to obtain value v assign v to loc i In our case j becomes 5 and A is [0,0,5,0,0,6,0,0,0,0]

MA (9) Case 4: for i=m to 1 step −1 evaluate ref i to obtain loc i evaluate exp to obtain value v assign v to loc i In our case j becomes 5 and A is [0,0,0,6,5,0,0,0,0,0]

MA (10) Case 5: for i=1 to m step 1 evaluate ref i to obtain loc i evaluate exp to obtain value v for i=1 to m step 1 assign v to loc i In our case j becomes 5 and A is [0,0,5,0,5,0,0,0,0,0]

Control Structures Start with the three fundamental structures and assume the existence of an empty statement (NOP) Sequence S 1 ;S 2 If-then-else if P then S 1 else S 2 While-do while P do S

CS Semantics Use program functions, defined as follows Let P be a program (or program fragment) Let a be an identifier declared in P representing values Let V a be the set of legal values for a as determined by its type. Let V a * = V a + {undefined}

CS Semantics (2) Question: What are the possible outcomes after executing a program fragment?

CS Semantics (3) The program function for P is [P]: V a * × V b * × … −> V a * × V b * × … + {error, nonterminating} defined to show the state after executing P.

Example 1 P = X := X+3; Y:=Y−1 where X,Y are type integer

Example 2 P = while Y > 0 do begin X := X−1; Y := Y−1 end where X,Y are type 0..maxint

Example 3 P = if X <= Y then X := Y – X else X := 0 where X,Y are type integer

Constructing a PF Given a program P 1.Find the set of identifiers: X, Y, Z, … 2.Find the set of values V X *, … associated with each identifier, which includes {undefined} 3.Construct the function [P] with domain: V X * × V Y * × … codomain: V X * × V Y * × … + {error, nonterminating}

Constructing (2) Use the following notational conventions: 1.If an identifier in P does not appear as the target of an assignment statement, it can be suppressed in the domain and codomain 2.[P] (error) = error [P] (nonterminating) = nonterminating so are not written

Constructing (3) 3.undefined is denoted by 4.The identifier and its value are denoted by the same letter 5.Account for all possible values of the domain, perhaps by partitioning with conditions, e.g. x<0, x≥0, x =

Example 4 Program Function Exercise II.1 P = while x <= b do x := x + a end

If-Then Semantics Let P = if C then S 1 else S 2 Define [P](v) = [S 1 ](v) if [C](v) = true [S 2 ](v) if [C](v) = false error if [C](v) = error

If-Then-Else (2) Theorem: [if C then S] = [if C then S else Φ] where [Φ] = id

While-Do Semantics Let P = while C do S Define [P](v) = [S] k (v) where nonterminating error

Sequence Semantics Let P = S 1 ; S 2 Define [P](v) = [S 2 ] o [S 1 ](v)

Böhm – Jacopini Theorem The simple assignment statement and the three control structures sequence, if-then-else, while- do are enough to write any block structured program to compute any computable function to convert any flowchart specified program to code

Additional Control Structures Useful for human understanding of programs Another example of “syntactic sugar” Definition: [repeat S until C] = [S ; while (not C) do S]

Additional (2) Let P = case F of F1:S1; F2:S2; … ; FN:SN end Definition [P] = [if F=F1 then S1 else if F=F2 then S2 else if … else if F=FN then SN] Issues: Two selector values with same statement What if F does not equal any FI? Can some SI be empty?

Additional (3) Let P = switch(E) {case E1:S1;case E2:S2; … ; case EN:SN; default: SD} Definition [P] = [ if E=E1 then S1;S2;…;SN;SD else if E=E2 then S2;…;SN;SD else if E=E3 then S3;…;SN;SD else SD]

Assignment Due at beginning of next class meeting. Work by yourself. Ask questions only of the instructor. 1.Program Function Exercise I.5 2.Program Function Exercise II.5 and under the assumptions of Section III 3.Program Function Exercise I.4 4.Program Function Exercise II.4