1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
1 Languages and Compilers (SProg og Oversættere) Lecture 8 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Simon.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Run time vs. Compile time
Languages and Compilers (SProg og Oversættere) Lecture 8
Chapter 8 . Sequence Control
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
1 Languages and Compilers (SProg og Oversættere) Lecture 8 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Simon.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Structure of Programming Language
PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07A - Expressions Programming Language Design and Implementation.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Programming Language C++ Xulong Peng CSC415 Programming Languages.
Compiler Construction
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
1 CS Programming Languages Class 11 September 26, 2000.
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Programming Languages and Paradigms Imperative Programming.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
Arithmetic Expressions
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Expressions and Assignment Statements
ISBN Chapter 7 Expressions and Assignment Statements.
PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07A - Expressions Programming Language Design and Implementation.
PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07A - Expressions Programming Language Design and Implementation.
1 Subprograms In Text: Chapter 8. 2 Chapter 8: Subprograms Outline Definitions Referencing environments Parameter passing modes and mechanisms Independent.
1 Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to John Mitchell and.
VHDL Discussion Subprograms IAY 0600 Digital Systems Design Alexander Sudnitson Tallinn University of Technology 1.
W E E K F I V E Statement-Level Control Structures.
Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Definition of the Programming Language CPRL
Precedence and Associativity
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Chap. 6 :: Control Flow Michael L. Scott.
Chap. 6 :: Control Flow Michael L. Scott.
Final Review In Text: Chapters 1-3, 5-12,
Final Review In Text: Chapters 1-3, 5-16.
UNIT V Run Time Environments.
Languages and Compilers (SProg og Oversættere)
Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Flow of Control.
PZ07A - Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section PZ07A.
Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control

2 –Evaluation of expressions –Explicit sequence control vs. structured sequence control –Subprogram implementation –Parameter mechanisms

3 Sequence control Implicit and explicit sequence control –Expressions Precedence rules Associativity –Statements Sequence Conditionals Iterations –Subprograms –Declarative programming Functional Logic programming

4 Expression Evaluation Determined by –operator evaluation order –operand evaluation order Operators: –Most operators are either infix or prefix (some languages have postfix) –Order of evaluation determined by operator precedence and associativity

5 Example What is the result for: * Possible answers: – 41 = ((3 + 4) * 5) + 6 – 47 = 3 + (4 * (5 + 6)) – 29 = (3 + (4 * 5)) + 6 = 3 + ((4 * 5) + 6) – 77 = (3 + 4) * (5 + 6) In most language, * = 29 … but it depends on the precedence of operators

6 Operator Precedence Operators of highest precedence evaluated first (bind more tightly). Precedence for operators usually given in a table, e.g.: In APL, all infix operators have same precedence LevelOperatorOperation Highest** abs notExp, abs, negation * / mod rem + -Unary + - &Binary = =>Relations LowestAnd or xorBoolean Precedence table for ADA

7 C precedence levels Precedence Operators Operator names 17 tokens, a[k], f()Literals, subscripting, function call.,-> Selection 16 ++, -- Postfix increment/decrement 15* ++, -- Prefix inc/dec , -, sizeof Unary operators, storage !,&,* Logical negation, indirection 14 typename Casts 13 *, /, % Multiplicative operators 12 +,- Additive operators 11 > Shift 10, = Relational 9 ==, != Equality 8 & Bitwise and 7  Bitwise xor 6 | Bitwise or 5 && Logical and 4 || Logical or 3 ?: Conditional 2 =, +=, -=, *=, Assignment /=, %=, >=, &=,  =, |= 1, Sequential evaluation Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

8 Control of Statement Execution Sequential Conditional Selection Looping Construct Must have all three to provide full power of a Computing Machine

9 Loops Main types: Counter-controlled iteraters (For-loops) Logical-test iterators Recursion

10 For-loops Controlled by loop variable of scalar type with bounds and increment size Scope of loop variable? –Extent beyond loop? –Within loop? When are loop parameters calculated? –Once at start –At beginning of each pass

11 Logic-Test Iterators While-loops –Test performed before entry to loop repeat…until and do…while –Test performed at end of loop –Loop always executed at least once Design Issues: 1. Pretest or posttest? 2. Should this be a special case of the counting loop statement (or a separate statement)?

12 Gotos Requires notion of program point Transfers execution to given program point Basic construct in machine language Implements loops

13 Advance in Computer Science Standard constructs that structure jumps if … then … else … end while … do … end for … { … } case … Modern style –Group code in logical blocks –Avoid explicit jumps except for function return –Cannot jump into middle of block or function body But there may be situations when “jumping” is the right thing to do!

14 Exceptions: Structured Exit Terminate part of computation –Jump out of construct –Pass data as part of jump –Return to most recent site set up to handle exception –Unnecessary activation records may be deallocated May need to free heap space, other resources Two main language constructs –Declaration to establish exception handler –Statement or expression to raise or throw exception Often used for unusual or exceptional condition, but not necessarily.

15 Subprograms 1.A subprogram has a single entry point 2.The caller is suspended during execution of the called subprogram 3.Control always returns to the caller when the called subprogram’s execution terminates Functions or Procedures? Procedures provide user-defined statements Functions provide user-defined operators

16 Subprogram As Abstraction Subprograms encapsulate local variables, specifics of algorithm applied –Once compiled, programmer cannot access these details in other programs Application of subprogram does not require user to know details of input data layout (just its type) –Form of information hiding

17 Subprogram Parameters Formal parameters: names (and types) of arguments to the subprogram used in defining the subprogram body Actual parameters: arguments supplied for formal parameters when subprogram is called Actual/Formal Parameter Correspondence: –attributes of variables are used to exchange information Name – Call-by-name Memory Location – Call-by reference Value –Call-by-value (one way from actual to formal parameter) –Call-by-value-result (two ways between actual and formal parameter) –Call-by-result (one way from formal to actual parameter)

18 Design Considerations for Parameter Passing 1.Efficiency 2.One-way or two-way - These two are in conflict with one another! –Good programming  limited access to variables, which means one-way whenever possible –Efficiency  pass by reference is fastest way to pass structures of significant size –Also, functions should not allow reference parameters