Presentation is loading. Please wait.

Presentation is loading. Please wait.

Languages and Compilers (SProg og Oversættere)

Similar presentations


Presentation on theme: "Languages and Compilers (SProg og Oversættere)"— Presentation transcript:

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

2 Sequence control and Subprogram Control
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 … but it depends on the precedence of operators
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, * = 29 … but it depends on the precedence of operators

6 Precedence table for ADA
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 Level Operator Operation Highest ** abs not Exp, abs, negation * / mod rem + - Unary + - & Binary = <= < > => Relations Lowest And or xor Boolean Precedence table for ADA

7 C precedence levels Precedence Operators Operator names
tokens, a[k], f() Literals, subscripting, function call .,-> Selection , Postfix increment/decrement 15* , Prefix inc/dec , -, sizeof Unary operators, storage !,&,* Logical negation, indirection typename Casts *, /, % Multiplicative operators , Additive operators <<, >> Shift <,>,<=, >= Relational ==, != Equality & Bitwise and  Bitwise xor | Bitwise or && Logical and || Logical or ?: Conditional =, +=, -=, *=, Assignment /=, %=, <<=, >>=, &=, =, |= , 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 Gotos Requires notion of program point
Transfers execution to given program point Basic construct in machine language Implements loops

11 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!

12 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.

13 Subprograms A subprogram has a single entry point
The caller is suspended during execution of the called subprogram 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

14 Executing Subprograms
The subprogram call and return operations of a language are together called its subprogram linkage Call Semantics: 1. Save the execution status of the caller 2. Carry out the parameter-passing process 3. Pass the return address 4. Transfer control to the callee Return Semantics: 1. If pass-by-value-result parameters are used, move the current values of those parameters to their corresponding actual parameters 2. If it is a function, move the functional value to a place the caller can get it 3. Restore the execution status of the caller 4. Transfer control back to the caller Required Storage: - Status information of the caller, parameters, returnaddress, and functional value (if it is a function)

15 Code segment generated once at compiled time
Activation Records Code segment generated once at compiled time Activation record instances created at run time Fresh activation record instance created for each call of subprogram Usually put in a stack (top inserted first)

16 Subprogram Implementation
Code segment Activation Record Code to create activation record inst Program code Code to delete activation record inst const 1 const n Return point Static link Dynamic link Result data Input parameters Local parameters

17 Activation Records Static link – pointer to bottom of activation record instance of static parent Used to find non-local variables Dynamic link – pointer to top of activation record instance of the caller Used to delete subprogram activation record instance at completion

18 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)


Download ppt "Languages and Compilers (SProg og Oversættere)"

Similar presentations


Ads by Google