Upcoming SlideShare
×

# Unit 3 principles of programming language

12,306 views

Published on

Published in: Education
9 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
12,306
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
995
0
Likes
9
Embeds 0
No embeds

No notes for slide

### Unit 3 principles of programming language

1. 1. By-Garima Jain
2. 2.  Sequence control with expressions Conditional Statements, Loops Exception Handling Subprogram definition and activation Simple and Recursive Subprogram Subprogram Environment
3. 3. Control of the order of execution of the operationsboth primitive and user defined.Implicit : determined by the order of the statementsin the source program or by the built-in executionmodelExplicit : the programmer uses statements to changethe order of execution (e.g. uses If statement)
4. 4. Expressions: How data are manipulated usingprecedence rules and parentheses.Statements: conditional and iteration statements changethe sequential execution. Declarative programming: an executionmodel that does not depend on the order of thestatements in the source program.Subprograms: transfer control from one program toanother.
5. 5. What is the sequence of performing the operations?How is the sequence defined, and how is it represented?Functional composition : Basic sequence-controlmechanism:Given an operation with its operands, the operands maybe:· Constants· Data objects· Other operations
6. 6. Example 1: 3 * (var1 + 5)operation - multiplication, operator: *, arity - 2 operand 1: constant (3) operand 2: operation addition operand1: data object (var1) operand 2: constant (5)
7. 7. Example 2: 3* var1 +5Question: is the example equivalent to the above one?Example 3: 3 + var1 +5Question: is this equivalent to (3 + var1) + 5, or to 3 + (var1 + 5) ?
8. 8. Precedence concerns the order of applyingoperationsAssociativity deals with the order of operations ofsame precedence. Precedence and associativity are defined when thelanguage is defined - within the semantic rules forexpressions.
9. 9. Linear representation of the expression tree: Prefix notation· Postfix notation· Infix notationPrefix and postfix notations are parentheses-free.
10. 10.  Machine code sequence Tree structures - software simulation Prefix or postfix form - requires stack, executed by aninterpreter.
11. 11. Eager evaluation - evaluate all operands beforeapplying operators.Lazy evaluation
12. 12. Side effects - some operations may change operands ofother operations.Error conditions - may depend on the evaluationstrategy (eager or lazy evaluation)Boolean expressions - results may differ depending onthe evaluation strategy.
13. 13. if expression then statement1 elsestatement2 if expression then statement1 a choice among many alternatives nested if statements case statementsImplementation:  jump and branch machineinstructions, jump table implementation for casestatements
14. 14. Simple  repetition (for loop) Specifies a count of the number of times to execute a loop: perform statement K times; for loop -Examples: for I=1 to 10 do statement; for(I=0;I<10; I++) statement;
15. 15. while expression do statement;Evaluate expression and if true execute statement, thenrepeat process. repeat statement until expression; Execute statement and then evaluate expression.Repeat if expression is not true.C++ for loop functionally is equivalent to repetitionwhile condition holds
16. 16.  Multiple exit loops Exceptional conditions Do-while-do structureSolutions vary with languages, e.g. in C++ - breakstatement, assert for exceptions.
17. 17. Exception Handlers are subprograms that are not invoked by explicit callsSpecial situations, called exceptions: Error conditions Unpredictable conditions Tracing and monitoring
18. 18. Exception handlers typically contain only: • A set of declarations of local variables • A sequence of executable statementsException Handlers can be - predefined in the language - programmer defined
19. 19. Languages provide methods for raising (throwing) and testing for exceptions.  try { statement1; statement2; …  if badCondition throw ExceptionName; }  catch ExceptionName{ ……….// do something for exception…….}
20. 20. Operating system exceptions - raised directlyby hardware interrupts.Programmer defined -the translator inserts code to handle theexceptions.
21. 21. Subprogram Control :interaction among subprogramshow subprograms pass data among themselves
22. 22. Simple subprogram call return Copy rule view of subprograms: the effect of a call statement is the same as if the subprogram were copied and inserted into the main program.
23. 23. • Subprograms cannot be recursive• Explicit call statements are required• Subprograms must execute completely at each call• Immediate transfer of control at point of call• Single execution sequence
24. 24. CALL RETURN
25. 25. Execution of subprograms Subprogram definition. Subprogram activation.
26. 26. The definition is translated into a template, usedto create an activation each time a subprogram iscalled.
27. 27. a code segment (the invariant part) - executable code and constants,an activation record (the dynamic part) - local data, parameters. created a new each time the subprogram is called, destroyed when the subprogram returns.
28. 28. • Current-instruction pointer – CIPaddress of the next statement to be executed• Current-environment pointer – CEPpointer to the activation record.
29. 29.  An activation record is created Current CIP and CEP are saved in the created activation record as return point CEP is assigned the address of the activation record. CIP gets the address of the first instruction in the code segment The execution continues from the address in CIP
30. 30.  The old values of CIP and CEP are retrieved.  The execution continues from the address in CIPRestrictions of the model: at most one activation of any subprogram
31. 31. Allocate storage for a single activation record staticallyas an extension of the code segment.Used in FORTRAN and COBOL.The activation record is not destroyed - only reinitializedfor each subprogram execution.Hardware support - CIP is the program counter,CEP is not used, simple jump executed on return.
32. 32. The simplest run-time storage management techniquecall statements : push CIP and CEPreturn statements : pop CIP and CEP off of the stack.Used in most C implementationsLISP: uses the stack as an environment.
33. 33. Specification Syntactically - no difference Semantically - multiple activations of the same subprogram exist simultaneously at some point in the execution.E.G. the first recursive call creates a secondactivation within the lifetime of the first activation.
34. 34. Stack-based -CIP and CEP are stored in stack, forming adynamic chain of links.A new activation record is created for each calland destroyed on return.The lifetimes of the activation records cannotoverlap - they are nested.
35. 35. Data control features determine the accessibility of data atdifferent points during program execution.Central problem:the meaning of variable names, i.e. the correspondencebetween names and memory locations.
36. 36. Two ways to make a data object available as an operandfor an operationDirect transmissionReferencing through a named data object
37. 37. A data object computed at one point as the result ofan operation may be directly transmitted to anotheroperation as an operandExample: x = y + 2*z;The result of multiplication is transmitted directly asan operand of the addition operation
38. 38. A data object may be given a name when it iscreated, the name may then be used to designate itas an operand of an operation.
39. 39. VariablesFormal parametersSubprogramsDefined typesDefined constantsLabelsException namesPrimitive operationsLiteral constants
40. 40. Association: binding identifiers to particular dataobjects and subprogramsReferencing environment: the set of identifierassociations for a given subprogram.Referencing operations during program execution:determine the particular data object or subprogramassociated with an identifier
41. 41. Subprogram Environment The set of associations created on entry to a subprogram formal parameters, local variables, and subprograms defined only within that subprogram.Non-local referencing environment The set of associations for identifiers • used within a subprogram • not created on entry to itGlobal referencing environment:associations created at the start of execution of the main program, availableto be used in a subprogram.Predefined referencing environments:predefined associations in the language definition.