Exam 2 u Wednesday, November 4 u Topics: u Names, types, semantics, - - PDF document

exam 2
SMART_READER_LITE
LIVE PREVIEW

Exam 2 u Wednesday, November 4 u Topics: u Names, types, semantics, - - PDF document

11/1/15 CSCI 2325 Principles of Programming Languages Semantics Reading: Ch 7 (Tucker & Noonan) Exam 2 u Wednesday, November 4 u Topics: u Names, types, semantics, memory management, and function (up to Mondays lecture) u


slide-1
SLIDE 1

11/1/15 1

CSCI 2325 Principles of Programming Languages Semantics Reading: Ch 7 (Tucker & Noonan)

Exam 2

u Wednesday, November 4 u Topics:

u Names, types, semantics, memory management,

and function (up to Monday’s lecture)

u Must read book chapters 4, 5, 7, 11, and 9 and go

  • ver class notes and slides
slide-2
SLIDE 2

11/1/15 2

Sample questions

u Given a program, what will be the output if static scoping is

used? what will be its output if dynamic scoping is used? (Ch 4)

u Difference between lifetime and scope. Give example. (Ch

4)

u Static vs. dynamic typing – what is the difference? (Ch 5) u What does a strongly typed language mean? (Ch 5) u How are arrays implemented? Extend 1-dimensional array

shown in class to 2-dimensional array. (Ch 5)

u Are functions types? Give examples. (Ch 5) u Given a statement, give a verbal description of its

  • semantics. (Ch 7)

u Simulate the three memory management algorithms on a

given example. (Ch 11)

u Given a recursive function, draw the activities in the run-

time stack when the function is called. (Ch 9.7)

u Other questions from the topics covered during the classes.

Semantics

Precisely describes the meaning of all language constructs for:

1.

Programmers

2.

Compiler writers

3.

Standards developers

slide-3
SLIDE 3

11/1/15 3

How to define semantics?

  • 1. Operational semantics

u The meaning attached by compiling using compiler

C and executing using machine M. Ex: Fortran on IBM 709.

  • 2. Axiomatic semantics

u Use mathematical logic

  • 3. Denotational semantics

u Use mathematical functions u These functions transform the “state” of the

program u We’ll use the concept of denotational

semantics informally

Expression Semantics

u Infix (C, Java): a + b - c * d

u Ambiguous without rules of prec. & assoc.

u Polish Prefix (Ambi): - + a b * c d

u Unambiguous! u But cannot use – as both unary and binary

u Polish Postfix (Postscript, calculator): 


a b + c d * -

u Cambridge Polish (LISP, Scheme): 


(- (+ a b) (* c d)) Meaning of

slide-4
SLIDE 4

11/1/15 4

Associativity of Operators

Language + - * / Unary - ** == != < ... C-like L R L Ada L non non non Fortran L R R L

u Semantics of: a < b < c in C/C++?

u if (a < b) return 1 < c; else return 0 < c;

u Why did Ada make relational operators non-

associative?

u To make a < b < c illegal

Precedence of Operators

Operators C-like Ada Fortran Unary - 7 3 3 ** 5 5 * / 6 4 4 + - 5 3 3 == != 4 2 2 < <= ... 3 2 2 not 7 2 2

slide-5
SLIDE 5

11/1/15 5

Short Circuit Evaluation

u a and b evaluated as:

u if a then b else false

u a or b evaluated as:

u if a then true else b

Example

Node p = head; while (p != null && p.info != key) p = p.next; if (p == null) // not in list ... else // found it ...

slide-6
SLIDE 6

11/1/15 6

Question

u Is the meaning of

(a + b) + c the same as a + (b + c)?

u No! u There are considerations beyond just

  • perators, operands, and types

u Hardware representation of numbers

Question

u What is the value of a below?

i = 2; b = 2; c = 5; a = b * i++ + c * i;

u Here, the semantics of the RHS expression

above is undefined in C!

slide-7
SLIDE 7

11/1/15 7

Assignment semantics

u Allow multiple assignments?

u a = b = c = 0 u OK by C/C++/Java/Python

u Is assignment an expression?

u Why do I ask? If it’s an expression, you can use

assignment as a conditional expression!

Examples

u C

u //Kernighan and Ritchie’s strcpy function

while (*p++ = *q++) ;

u while (ch = getc(fp)) ... // ??? u while (p = p->next) ... // ???

u Java boolean x = false; if (x = true) System.out.println("x is true!"); //this will be printed! u Python

u These are illegal

slide-8
SLIDE 8

11/1/15 8

Assignment: copy vs. reference semantics

u x = y

Will the (R-)value of y be copied to x

  • r

will x and y point to the same data?

Denotational Semantics

u Program state

u collection of all active objects and their current

values (binding!)

slide-9
SLIDE 9

11/1/15 9 // compute n! 1 void main ( ) { 2 int n, i, f; 3 n = 3; 4 i = 1; 5 f = 1; 6 while (i < n) { 7 i = i + 1; 8 f = f * i; 9 } 10 } // compute n! 1 void main ( ) { 2 int n, i, f; 3 n = 3; 4 i = 1; 5 f = 1; 6 while (i < n) { 7 i = i + 1; 8 f = f * i; 9 } 10 } n i f undef undef undef 3 undef undef

slide-10
SLIDE 10

11/1/15 10 // compute n! 1 void main ( ) { 2 int n, i, f; 3 n = 3; 4 i = 1; 5 f = 1; 6 while (i < n) { 7 i = i + 1; 8 f = f * i; 9 } 10 } n i f 3 undef undef 3 1 undef // compute n! 1 void main ( ) { 2 int n, i, f; 3 n = 3; 4 i = 1; 5 f = 1; 6 while (i < n) { 7 i = i + 1; 8 f = f * i; 9 } 10 } n i f 3 1 undef 3 1 1

slide-11
SLIDE 11

11/1/15 11 // compute n! 1 void main ( ) { 2 int n, i, f; 3 n = 3; 4 i = 1; 5 f = 1; 6 while (i < n) { 7 i = i + 1; 8 f = f * i; 9 } 10 } n i f 3 1 1 3 1 1 // compute n! 1 void main ( ) { 2 int n, i, f; 3 n = 3; 4 i = 1; 5 f = 1; 6 while (i < n) { 7 i = i + 1; 8 f = f * i; 9 } 10 } n i f 3 1 1 3 2 1

slide-12
SLIDE 12

11/1/15 12 // compute n! 1 void main ( ) { 2 int n, i, f; 3 n = 3; 4 i = 1; 5 f = 1; 6 while (i < n) { 7 i = i + 1; 8 f = f * i; 9 } 10 } n i f 3 2 1 3 2 2 // compute n! 1 void main ( ) { 2 int n, i, f; 3 n = 3; 4 i = 1; 5 f = 1; 6 while (i < n) { 7 i = i + 1; 8 f = f * i; 9 } 10 } n i f … …

slide-13
SLIDE 13

11/1/15 13 // compute n! 1 void main ( ) { 2 int n, i, f; 3 n = 3; 4 i = 1; 5 f = 1; 6 while (i < n) { 7 i = i + 1; 8 f = f * i; 9 } 10 } n i f 3 3 6

Control flow semantics

u To be complete, an imperative language

needs:

  • Statement sequencing
  • Conditional statement
  • Looping statement
slide-14
SLIDE 14

11/1/15 14

Sequence of statements

u s1 


s2

u Semantics:

u First execute s1 u Then execute s2 u Output state of s1 is the input state of s2

Conditional

u IfStatement → if ( Expresion ) Statement u

[ else Statement ]

u Example: u if (a > b) u z = a; u else u z = b;

u If the test expression is

true (here, no change

  • f state)

u then the output state

  • f the conditional is

the output state of the then branch,

u else the output state of

the conditional is the

  • utput state of the else

branch.

slide-15
SLIDE 15

11/1/15 15

While loops

u WhileStatement → while (Expr) Statement u The expression is evaluated (state does not

change)

u If it is true, first the statement is executed, u and then the loop is executed again. u Otherwise the loop terminates.

Exception Handling

Why?

slide-16
SLIDE 16

11/1/15 16

Hardware vs. Programming Lang.

u Hardware– Interrupt

u Your program: division by 0/illegal memory addr à

Interrupt handler routine in HW à Back to the next line of your program

u Only one interrupt handler routine for div by 0 u Resumption model

u Programming Lang. – Exception

u Your program: array index out of bound/access

  • bject using NULL pointer à

Corresponding exception handler à Back to a specific part of your program

u You can have multiple exception handlers for the

same exception

u Termination model

History of exception handling

u COBOL à PL/I à Ada à C++/Java… u No exception handling: Fortran/Pascal/C

u Programmer’s responsibility

slide-17
SLIDE 17

11/1/15 17

Resumption (HW) vs. termination (PL)

Exception in C++

try { //throw 20; //throw “Some error”; } catch(int a) { //This kicks in if an int is thrown. } catch(char* st) { //This kicks in if a string is thrown. }

slide-18
SLIDE 18

11/1/15 18

Exception in Java

u Code by Jakob Jenkov

try { // execute code that may throw // 1 of the 3 exceptions below. } catch(SQLException e) { logger.log(e); } catch(IOException e) { logger.log(e); } catch(Exception e) { logger.severe(e); }

Java’s exception hierarchy

slide-19
SLIDE 19

11/1/15 19

Python’s exception hierarchy