Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICE1341 Programming Languages Spring 2005 Lecture #12 Lecture #12 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.

Similar presentations


Presentation on theme: "ICE1341 Programming Languages Spring 2005 Lecture #12 Lecture #12 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."— Presentation transcript:

1 ICE1341 Programming Languages Spring 2005 Lecture #12 Lecture #12 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University (ICU)

2 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Postmortem of Midterm Why the Von Neumann machine is important in designing programming languages? Why the Von Neumann machine is important in designing programming languages?

3 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Postmortem of Midterm The set of all strings with an equal number of 0’s and 1’s The set of all strings with an equal number of 0’s and 1’s  Can’t generate by using a regular grammar! The set of all strings with an equal number of 0’s and 1’s, such that no prefix has two more 0’s than 1’s, nor two more 1’s than 0’s The set of all strings with an equal number of 0’s and 1’s, such that no prefix has two more 0’s than 1’s, nor two more 1’s than 0’s  (01|10)*

4 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Postmortem of Midterm sm1234 sm1234 Samsung Samsung 1,500,000 1,500,000 Intel Intel Pentium 4 Pentium Samsung Samsung DDR SDRAM DDR SDRAM .........

5 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Postmortem of Midterm sm1234 sm1234 Samsung Samsung 1,500,000 1,500,000 Intel Intel Pentium 4 Pentium Samsung Samsung DDR SDRAM DDR SDRAM ......... computers  ‘ ’ { computer } ‘ ’ computer  ‘ ’ model manufacturer price motherboard ‘ ’ model  ‘ ’ string ‘ ’ manufacturer  ‘ ’ string ‘ ’ price  ‘ ’ num ‘ ’ motherboard  ‘ ’ cpu memory ‘ ’ …

6 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Announcements Midterm Project Midterm Project Due: Thursday April 14 th Due: Thursday April 14 th The content of the project report and presentation The content of the project report and presentation Main design goals and application domains of the programming language designed Main design goals and application domains of the programming language designed The language design described in EBNF The language design described in EBNF A sample program (displayed in MS IE) A sample program (displayed in MS IE) Discussions about the language design factors & lessons learned Discussions about the language design factors & lessons learned 10 minute presentation per each team 10 minute presentation per each team Send me your presentation file by 10:00AM Send me your presentation file by 10:00AM

7 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Data Types Data Types More on Arrays More on Arrays Records Records Unions Unions Pointers Pointers Last Lecture

8 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University This Lecture Chapter 7 – Expressions and Assignment Statements Chapter 7 – Expressions and Assignment Statements Chapter 8 – Statement-Level Control Structures Chapter 8 – Statement-Level Control Structures

9 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Pointer Types int n = 11; int *p = 22; int *q, *r, *h; q = &n; r = NULL; h = (int *)malloc(sizeof(int)); *h = 33; int m = *h; 11 n 22 p NULL q r … 33h

10 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Pointer Types int n = 11; int *p = (int *)malloc(sizeof(int)); *p = 22; int *q, *r, *h; q = &n; r = NULL; h = (int *)malloc(sizeof(int)); *h = 33; int m = *h; 11 n 22 p NULL q r … 33h

11 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Chapter 7 Expressions and Assignment Statements

12 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls Arithmetic expressions consist of operators, operands, parentheses, and function calls Unary Operators Unary Operators e.g., !, ++, --, + (identity operator), - Binary Operators Binary Operators e.g., &&, ||, ==, !=, +, -, *, / Ternary Operators Ternary Operators e.g., expr1 ? expr2 : expr3 a + b * Math.sqrt(c) + (f (obj) obj.d)

13 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Design Issues for Arithmetic Expressions What are the operator precedence rules? What are the operator precedence rules? What are the operator associativity rules? What are the operator associativity rules? What is the order of operand evaluation? What is the order of operand evaluation? Are there restrictions on operand evaluation side effects? Are there restrictions on operand evaluation side effects? Does the language allow user-defined operator overloading? Does the language allow user-defined operator overloading? What type mixing is allowed in expressions? What type mixing is allowed in expressions? * AW Lecture Notes a + b * Math.sqrt(c) + (f (obj) obj.d)

14 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Operator Precedence Rules Define the order in which adjacent operators of different precedence levels are evaluated Define the order in which adjacent operators of different precedence levels are evaluated Typical precedence levels: Parentheses  Unary operators  **  *, /  +, - Typical precedence levels: Parentheses  Unary operators  **  *, /  +, - a + b * c + ++d – (e + f) (1)(2)(3) (4) (5) (6) * AW Lecture Notes

15 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Operator Associativity Rules Define the order in which adjacent operators with the same precedence level are evaluated Define the order in which adjacent operators with the same precedence level are evaluated Typical associativity rules: Left to right (except ** in Ada and Fortran) Typical associativity rules: Left to right (except ** in Ada and Fortran) APL; all operators have equal precedence and all operators associate right to left APL; all operators have equal precedence and all operators associate right to left Precedence and associativity rules can be overriden with parentheses (e.g., to improve precision in calculating large numbers) Precedence and associativity rules can be overriden with parentheses (e.g., to improve precision in calculating large numbers) * AW Lecture Notes a + b - c; a ** b ** c; - a - b; - a ** b; - 17 mod 5

16 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Operand Evaluation Order Variables: just fetch the value from memory Variables: just fetch the value from memory Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction Parenthesized expressions: evaluate all operands and operators first Parenthesized expressions: evaluate all operands and operators first Function references: need to consider side effects Function references: need to consider side effects * AW Lecture Notes a + b * Math.sqrt(c) + (f (obj) obj.d)

17 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Side Effects Functional side effects occur when a function changes a two-way parameter or a nonlocal variable Functional side effects occur when a function changes a two-way parameter or a nonlocal variable Possible Solutions: Possible Solutions: Allow no two-way parameters and non-local references in functions (  impossible to return multiple values from a function, inefficiency in parameter passing) Allow no two-way parameters and non-local references in functions (  impossible to return multiple values from a function, inefficiency in parameter passing) Fix the operand evaluation order (  limits compiler optimizations) Fix the operand evaluation order (  limits compiler optimizations) int main() { int a = 10; int b = a + triple(&a); } int triple(int *val) { (*val) *= 3; return *val; } * AW Lecture Notes

18 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Overloaded Operators Operator Overloading: use of an operator for more than one purpose Operator Overloading: use of an operator for more than one purpose Need to consider readability and reliability of an overloaded operator Need to consider readability and reliability of an overloaded operator C++ and Ada allow user-defined overloaded operators C++ and Ada allow user-defined overloaded operators e.g., int a[10], b[]; b = a + 5; //??? b = a + 5; //??? int a, *b, c; float d; r = a * *b + c – (d ) r = 3 / 5 r := 3 div 5

19 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Type Conversions Narrowing Conversion: converts an object to a type that cannot include all of the values of the original type e.g., float  int Narrowing Conversion: converts an object to a type that cannot include all of the values of the original type e.g., float  int Widening Conversion : converts an object to a type that can include at least approximations to all of the values of the original type e.g., int  float Widening Conversion : converts an object to a type that can include at least approximations to all of the values of the original type e.g., int  float Mixed-mode Expression: has operands of different types Mixed-mode Expression: has operands of different types Coercion: an implicit type conversion e.g., int num = 3.14; Coercion: an implicit type conversion e.g., int num = 3.14; Ada provides virtually no coercions in expressions Ada provides virtually no coercions in expressions Type Casting: an explicit type conversion e.g., int num = (int)3.14; Float(sum) -- as a function call Type Casting: an explicit type conversion e.g., int num = (int)3.14; Float(sum) -- as a function call * AW Lecture Notes

20 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Relational and Boolean Expressions Relational Operator: an operator that compares the values of two operands and returns a Boolean value Relational Operator: an operator that compares the values of two operands and returns a Boolean value e.g., !=, /=,.NE., <> e.g., !=, /=,.NE., <> Boolean Operator: an operator which operands are Boolean and its result is Boolean Boolean Operator: an operator which operands are Boolean and its result is Boolean e.g.,.AND.,.OR.,.NOT., &&, ||, ! e.g.,.AND.,.OR.,.NOT., &&, ||, ! C has no Boolean type – it uses int type with 0 for false and nonzero for true C has no Boolean type – it uses int type with 0 for false and nonzero for true e.g., a > b > c // ??? * AW Lecture Notes !(a != b && c > d || e)

21 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Short-Circuit Evaluation Short-Circuit Expression: an expression which result is determined without evaluating all of the operands and operators Short-Circuit Expression: an expression which result is determined without evaluating all of the operands and operators A short-circuit expression may prevent side effects in the expression A short-circuit expression may prevent side effects in the expression e.g., (a > b || ((b++) / 3) Ada allows the programmers to specify short- circuit evaluation (and then, or else) Ada allows the programmers to specify short- circuit evaluation (and then, or else) (13 * a) * (b / 13 – 1) (a >= 0) && (b = 0) && (b < 10) if (i < len && a[i] != 0) a[i] *= 2;

22 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Assignment Statements Simple Assignment (e.g., a = 10, Pascal A := 10) Simple Assignment (e.g., a = 10, Pascal A := 10) Multiple Targets (e.g., PL/I A, B = 10) Multiple Targets (e.g., PL/I A, B = 10) Conditional Targets Conditional Targets (e.g., C-based (first==true)? total : subtotal = 0) Compound Assignment Operators Compound Assignment Operators (e.g., C-based sum += next;) Unary Assignment Operators (e.g., C-based a++;) Unary Assignment Operators (e.g., C-based a++;) Assignment as an Expression Assignment as an Expression (e.g., C while ((ch=getchar()) != EOF) { … } sum = count = 0; // multiple targets) sum = count = 0; // multiple targets)

23 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Mixed-mode Assignment In FORTRAN, C, and C++, any numeric value can be assigned to any numeric scalar variable In FORTRAN, C, and C++, any numeric value can be assigned to any numeric scalar variable e.g., float val = 12; int num = 12.35; In Java and C#, only widening assignment coercions are allowed In Java and C#, only widening assignment coercions are allowed e.g., float val = 12; int num = (int)12.35; In Ada, there is no assignment coercion In Ada, there is no assignment coercion

24 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Chapter 8 Statement-Level Control Structures

25 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Control Structures Control Structure: a control statement and the statements whose execution it controls Control Structure: a control statement and the statements whose execution it controls Selection Statements – if, switch Selection Statements – if, switch Iterative Statements – do, for, while, … Iterative Statements – do, for, while, … Unconditional Branching – goto Unconditional Branching – goto Guarded Commands – nondeterministic if Guarded Commands – nondeterministic if Levels of Control Flow Levels of Control Flow Within expressions Within expressions Among program statements Among program statements Among program units Among program units

26 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Selection Statements Selection Statement: a means of choosing between two or more execution paths in a program Selection Statement: a means of choosing between two or more execution paths in a program Two general categories: Two-way selectors, Multiple- way selectors Two general categories: Two-way selectors, Multiple- way selectors Design Issues of Two-Way Selectors Design Issues of Two-Way Selectors What is the form and type of the control expression? What is the form and type of the control expression? How are the then and else clauses specified? How are the then and else clauses specified? How should the meaning of nested selectors be specified? How should the meaning of nested selectors be specified? if (sum == 0) if (count == 0) result = 0; result = 0;else result = 1; IF (SUM.NE. 0) GOTO 10 IF (SUM.NE. 0) GOTO 10 IF (COUNT.NE. 0) GOTO 20 IF (COUNT.NE. 0) GOTO 20 RESULT = 0 RESULT = 0 GOTO 20 GOTO RESULT = 1 20 CONTINUE

27 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Nested Two-Way Selectors ALGOL 60 – disallow direct nesting ALGOL 60 – disallow direct nesting if... then if... then begin begin if...then... if...then... end end else... else... Java – else goes with the nearest if Java – else goes with the nearest if if... if... if... else... else... FORTRAN 90 and Ada – closing special words FORTRAN 90 and Ada – closing special words if... then if... then if... then... if... then... end if end if else... else... end if end if

28 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Multiple Selection Statements (1) Design Issues: 1. What is the form and type of the control expression? 2. How are the selectable segments specified? 3. Is execution flow through the structure restricted to include just a single selectable segment? 4. What is done about unrepresented expression values? switch (month) { case 3: case 3: case 4: case 4: case 5: season = "Spring"; case 5: season = "Spring"; break; break; case 6: case 6: case 7: case 7: case 8: season = "Summer"; case 8: season = "Summer"; break; break; case 9: case 9: case 10: case 10: case 11: season = "Fall"; case 11: season = "Fall"; break; break; default: season = "Winter"; default: season = "Winter"; break; break;} * AW Lecture Notes

29 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Multiple Selection Statements (2) Design Choices (C Switch): 1. Control expression can be only an integer type 2. Selectable segments can be statement sequences or blocks 3. No implicit branch at the end of selectable segments (reliability vs. flexibility) 4. default clause is for unrepresented values (it’s optional) switch (month) { case 3: case 3: case 4: case 4: case 5: season = "Spring"; case 5: season = "Spring"; break; break; case 6: case 6: case 7: case 7: case 8: season = "Summer"; case 8: season = "Summer"; break; break; case 9: case 9: case 10: case 10: case 11: season = "Fall"; case 11: season = "Fall"; break; break; default: season = "Winter"; default: season = "Winter"; break; break;} * AW Lecture Notes

30 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Other Multiple Selectors FORTRAN Arithmetic IF FORTRAN Arithmetic IF IF (arithmetic expression) N1, N2, N3 No encapsulation of selectable segments (they could be anywhere) No encapsulation of selectable segments (they could be anywhere) Ada's case statement Ada's case statement Constant lists can include: Constant lists can include: Subranges e.g., Subranges e.g., Boolean OR operators – e.g., 1..5 | 7 | Boolean OR operators – e.g., 1..5 | 7 | Lists of constants must be exhaustive Lists of constants must be exhaustive Often accomplished with others clause Often accomplished with others clause This makes it more reliable This makes it more reliable * AW Lecture Notes

31 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Iterative Statements Iterative Statement: causes a collection of statements to be executed multiple times Iterative Statement: causes a collection of statements to be executed multiple times cf. Recursion: unit-level control cf. Recursion: unit-level control Design issues: Design issues: 1. How is iteration controlled? 2. Where is the control mechanism in the loop? SUM = 0 SUM = 0 DO 20 N = 1, 100, 3 DO 20 N = 1, 100, 3 20 SUM = SUM + N SUM = 0 SUM = 0 N = 1 N = 1 20 SUM = SUM + N N = N + 3; N = N + 3; IF (N.LE. 100) THEN IF (N.LE. 100) THEN GOTO 20 GOTO 20

32 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Counter-Controlled Loops Design Issues: Design Issues: 1. What are the type and scope of the loop variable? 2. What is the value of the loop variable at loop termination? 3. Should it be legal for the loop variable or loop parameters to be changed in the loop body, and if so, does the change affect loop control? 4. Should the loop parameters be evaluated only once, or once for every iteration? DO 20 N = 1, 100, 3 DO 20 N = 1, 100, 3 20 SUM = SUM + N Loop Variable Initial Value Terminal Value Stepsize Loop Parameters

33 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University FORTRAN ‘DO’ Loop Syntax: DO label var = start, finish [, stepsize] Syntax: DO label var = start, finish [, stepsize] Stepsize can be any value but zero Stepsize can be any value but zero Parameters can be expressions Parameters can be expressions Design Choices: Design Choices: 1. Loop variable must be integer 2. Loop variable always has its last value 3. The loop variable cannot be changed in the loop, but the parameters can 4. Loop parameters are evaluated only once * AW Lecture Notes

34 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University ALGOL 60 ‘For’ Loop Syntax: for var := do statement where can have: Syntax: for var := do statement where can have: list of expressions list of expressions expression step expression until expression expression step expression until expression expression while boolean_expression expression while boolean_expression e.g., for index := 1 step 2 until 50, e.g., for index := 1 step 2 until 50, 60, 70, 80, 60, 70, 80, index + 1 until 100 do index + 1 until 100 do (index = 1, 3, 5, 7,..., 49, 60, 70, 80, 81, 82,..., 100) Parameters are evaluated with every iteration, making it very complex and difficult to read Parameters are evaluated with every iteration, making it very complex and difficult to read * AW Lecture Notes

35 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University ‘For’ Loops in C-based Languages Syntax: for ([expr_1] ; [expr_2] ; [expr_3]) statement Syntax: for ([expr_1] ; [expr_2] ; [expr_3]) statement The expressions can be statement sequences, with the statements separated by commas or null The expressions can be statement sequences, with the statements separated by commas or null e.g., for (int i=0, j=10; j==i; i++, j--) e.g., for (int i=0, j=10; j==i; i++, j--) printf(“%d, %d”, i, j); printf(“%d, %d”, i, j); for (;;) … for (;;) … In Java, the control expression must be Boolean In Java, the control expression must be Boolean Design Choices: Design Choices: 1, 2. There is no explicit loop variable 3. Everything can be changed in the loop 4. expr_1 is evaluated once, others are evaluated with each iteration others are evaluated with each iteration Flexible!

36 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Other Counter-Controlled Loops Pascal Pascal for variable := initial (to | downto) final do Ada Ada for var in [reverse] discrete_range loop end loop The loop variable is implicitly declared and undeclared as the loop begins and terminates The loop variable is implicitly declared and undeclared as the loop begins and terminates e.g., Count : Float := 1.35; for Count in loop for Count in loop Sum := Sum + Count; end loop end loop

37 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Logically Controlled Loops Posttest version executes the loop body at least once Posttest version executes the loop body at least once e.g., At the above examples, what happens if n is already greater than 100 before reaching to the loop? Pascal – while … do, repeat … until Pascal – while … do, repeat … until Ada and Perl support only pretest versions Ada and Perl support only pretest versions FORTRAN 77 and 90 support neither version FORTRAN 77 and 90 support neither version while (n <= 100) { sum += n; n += 3; } do { sum += n; n += 3; } while (n <= 100) PretestPosttest

38 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University User-Located Loop Control Design Issues: 1. Should the conditional be part of the exit? C-based – unconditional Ada – conditional (exit when …) 2. Can control be transferable out of more than one loop? Java, C#, Perl – Yes while (n <= 100) { sum += n; if (sum == m) continue; n += 3; } while (n <= 100) { sum += n; if (sum == m) break; n += 3; } out: for (int i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/3592381/12/slides/slide_37.jpg", "name": "Spring 2005 38 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University User-Located Loop Control Design Issues: 1.", "description": "Should the conditional be part of the exit. C-based – unconditional Ada – conditional (exit when …) 2. Can control be transferable out of more than one loop. Java, C#, Perl – Yes while (n <= 100) { sum += n; if (sum == m) continue; n += 3; } while (n <= 100) { sum += n; if (sum == m) break; n += 3; } out: for (int i=0; i

39 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Iteration Based on Data Structures & Unconditional Branching IBDS: Use order and number of elements of some data structures to control iteration IBDS: Use order and number of elements of some data structures to control iteration Unconditional Branching (Goto) Unconditional Branching (Goto) Problem: readability – Spaghetti Logic Problem: readability – Spaghetti Logic Some languages do not have them: e.g., Java, Modular-2 Some languages do not have them: e.g., Java, Modular-2 Loop exit statements are restricted and somewhat camouflaged goto’s Loop exit statements are restricted and somewhat camouflaged goto’s String[] wdays = { “Mon”, “Tue”, “Wed”, “Thu”, “Fri” }; … foreach (String name in wdays) Console.WriteLine(“Work Day: {0}”, name); C#

40 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Guarded Commands (Dijstra, 1975) If more than one are true, choose one nondeterministically If more than one are true, choose one nondeterministically Runtime error when non of the conditions is true Runtime error when non of the conditions is true if i = 0 -> sum := sum + i [] i > j -> sum := sum + j [] j > I -> sum := sum + I fi  Allow verification during program development do q1 > q2 -> temp := q1; q1 := q2; q2 := temp; [] q2 > q3 -> temp := q2; q2 := q3; q3 := temp; [] q3 > q4 -> temp := q3; q3 := q4; q4 := temp; od If more than one are true, choose one nondeterministically; then start loop again If more than one are true, choose one nondeterministically; then start loop again If none are true, exit loop If none are true, exit loop


Download ppt "ICE1341 Programming Languages Spring 2005 Lecture #12 Lecture #12 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."

Similar presentations


Ads by Google