exam 2
play

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


  1. 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 Monday’s lecture) u Must read book chapters 4, 5, 7, 11, and 9 and go over class notes and slides 1

  2. 11/1/15 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: Programmers 1. Compiler writers 2. Standards developers 3. 2

  3. 11/1/15 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 Meaning of 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)) 3

  4. 11/1/15 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 4

  5. 11/1/15 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 ... 5

  6. 11/1/15 Question u Is the meaning of (a + b) + c the same as a + (b + c)? u No! u There are considerations beyond just operators, 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! 6

  7. 11/1/15 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 7

  8. 11/1/15 Assignment: copy vs. reference semantics u x = y Will the (R-)value of y be copied to x or 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!) 8

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

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

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

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

  13. 11/1/15 // compute n! n i f 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 } 3 3 6 Control flow semantics u To be complete, an imperative language needs: • Statement sequencing • Conditional statement • Looping statement 13

  14. 11/1/15 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 If the test expression is true (here, no change u z = a; of state) u then the output state u else of the conditional is the output state of the u z = b; then branch, u else the output state of the conditional is the output state of the else branch. 14

  15. 11/1/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? 15

  16. 11/1/15 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 object 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 16

  17. 11/1/15 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. } 17

  18. 11/1/15 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 18

  19. 11/1/15 Python’s exception hierarchy 19

Recommend


More recommend