Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control."— Presentation transcript:

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

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

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

4 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 5 Example What is the result for: 3 + 4 * 5 + 6 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, 3 + 4 * 5 + 6 = 29 … but it depends on the precedence of operators

6 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 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 8 Control of Statement Execution Sequential Conditional Selection Looping Construct Must have all three to provide full power of a Computing Machine

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

10 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 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 12 Gotos Requires notion of program point Transfers execution to given program point Basic construct in machine language Implements loops

13 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 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 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 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 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 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


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

Similar presentations


Ads by Google