UNIT 3

Similar documents
Chapter 7 Control I Expressions and Statements

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Problem with Scanning an Infix Expression

G Programming Languages - Fall 2012

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Problem with Scanning an Infix Expression

Programming Languages Third Edition. Chapter 7 Basic Semantics

G Programming Languages - Fall 2012

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Programming Languages, Summary CSC419; Odelia Schwartz

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

3. Java - Language Constructs I

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

ORACLE: PL/SQL Programming

Chapter 6 Control Flow. June 9, 2015

NOTE: Answer ANY FOUR of the following 6 sections:

Special Topics: Programming Languages

Implementing Subprograms

Lecture 13: Expression Evaluation

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

0. Overview of this standard Design entities and configurations... 5

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Absolute C++ Walter Savitch

CS 314 Principles of Programming Languages

Imperative Programming Languages (IPL)

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Draw a diagram of an empty circular queue and describe it to the reader.

CGS 2405 Advanced Programming with C++ Course Justification

Introduction to Programming Using Java (98-388)

Expressions and Statements. Department of CSE, MIT, Manipal

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

LECTURE 18. Control Flow

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Implementing Subprograms

An Introduction to Trees

Fachhochschule Wedel Technical Report Nr Implementing the Forth Inner Interpreter in High Level Forth

Chapter 5. Names, Bindings, and Scopes

Linear Data Structure

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

Stacks and their Applications

CS 3360 Design and Implementation of Programming Languages. Exam 1

Functional Programming. Pure Functional Programming

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

COP4020 Fall 2006 Final Exam

CSE 3302 Programming Languages Lecture 5: Control

This book is licensed under a Creative Commons Attribution 3.0 License

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Programming Languages: Lecture 12

Lecture 9: Control Flow

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

SMURF Language Reference Manual Serial MUsic Represented as Functions

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Module 27 Switch-case statements and Run-time storage management

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Today's Topics. CISC 458 Winter J.R. Cordy

Chapter 8 Statement-Level Control Structures

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

The PCAT Programming Language Reference Manual

Chapter 10. Implementing Subprograms ISBN

Lexical Considerations

G Programming Languages - Fall 2012

Programming for Engineers Iteration

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

Flow Control. CSC215 Lecture

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Model Viva Questions for Programming in C lab

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Implementing Subroutines. Outline [1]

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Programming Languages

Formal Languages and Automata Theory, SS Project (due Week 14)

Parsing Scheme (+ (* 2 3) 1) * 1

Data Structures and Algorithms

Introduction to Computer Science and Business

Algorithms and Data Structures

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

Run-time Environments

Pace University. Fundamental Concepts of CS121 1

Run-time Environments

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

The role of semantic analysis in a compiler

CHAPTER 8: Central Processing Unit (CPU)

Lexical Considerations


Konzepte von Programmiersprachen

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Run-time Environments - 2

Chapter 10. Implementing Subprograms

Transcription:

UNIT 3

Presentation Outline Sequence control with expressions Conditional Statements, Loops Exception Handling Subprogram definition and activation Simple and Recursive Subprogram Subprogram Environment

Sequence control Control of the order of execution of the operations both primitive and user defined. Implicit : determined by the order of the statements in the source program or by the built-in execution model Explicit : the programmer uses statements to change the order of execution (e.g. uses If statement)

Levels of sequence control Expressions: How data are manipulated using precedence rules and parentheses. Statements: conditional and iteration statements change the sequential execution. Declarative programming: an execution model that does not depend on the order of the statements in the source program. Subprograms: transfer control from one program to another.

Sequencing with expressions What is the sequence of performing the operations? How is the sequence defined, and how is it represented? Functional composition : Basic sequence-control mechanism: Given an operation with its operands, the operands may be: Constants Data objects Other operations

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

More examples and questions Example 2: 3* var1 +5 Question: is the example equivalent to the above one? Example 3: 3 + var1 +5 Question: is this equivalent to (3 + var1) + 5, or to 3 + (var1 + 5)?

Precedence and associativity Precedence concerns the order of applying operations Associativity deals with the order of operations of same precedence. Precedence and associativity are defined when the language is defined - within the semantic rules for expressions.

Arithmetic operations / expressions Linear representation of the expression tree: Prefix notation Postfix notation Infix notation Prefix and postfix notations are parentheses-free.

Execution-time representation of expressions Machine code sequence Tree structures - software simulation Prefix or postfix form - requires stack, executed by an interpreter.

Evaluation of tree representation Eager evaluation - evaluate all operands before applying operators. Lazy evaluation

Problems Side effects - some operations may change operands of other operations. Error conditions - may depend on the evaluation strategy (eager or lazy evaluation) Boolean expressions - results may differ depending on the evaluation strategy.

Conditional statements statement2 if expression then statement1 else if expression then statement1 a choice among many alternatives nested if statements case statements Implementation: jump and branch machine instructions, jump table implementation for case statements

Iteration statements Examples: Simple repetition (for loop) Specifies a count of the number of times to execute a loop: perform statement K times; for loop - for I=1 to 10 do statement; for(i=0;i<10; I++) statement;

Repetition while condition holds while expression do statement; Evaluate expression and if true execute statement, then repeat process. repeat statement until expression; Execute statement and then evaluate expression. Repeat if expression is not true. C++ for loop functionally is equivalent to repetition while condition holds

Problems with structured sequence control Multiple exit loops Exceptional conditions Do-while-do structure Solutions vary with languages, e.g. in C++ - break statement, assert for exceptions.

Exceptions and exception handlers Exception Handlers are subprograms that are not invoked by explicit calls Special situations, called exceptions: Error conditions Unpredictable conditions Tracing and monitoring

Exceptions and exception handlers Exception handlers typically contain only: A set of declarations of local variables A sequence of executable statements Exception Handlers can be - predefined in the language - programmer defined

Raising and catching an exception www.getmyuni.com Languages provide methods for raising (throwing) testing for exceptions. and try { statement1; statement2; if badcondition throw ExceptionName; } catch ExceptionName {.// do something for exception.}

Implementation Operating system exceptions - raised directly by hardware interrupts. Programmer defined - the translator inserts code to handle the exceptions.

Subprogram Control www.getmyuni.com Subprogram Control : interaction among subprograms how subprograms pass data among themselves

Subprogram Sequence Control 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.

Assumptions 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

Simple flow of execution CALL RETURN

Simple call-return subprograms Execution of subprograms Subprogram definition. Subprogram activation.

Subprogram definition The definition is translated into a template, used to create an activation each time a subprogram is called.

Subprogram activation 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.

System-defined pointers Current-instruction pointer CIP address of the next statement to be executed Current-environment pointer CEP pointer to the activation record.

On call instruction 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

On return The old values of CIP and CEP are retrieved. The execution continues from the address in CIP Restrictions of the model: at most one activation of any subprogram

The simplest implementation Allocate storage for a single activation record statically as an extension of the code segment. Used in FORTRAN and COBOL. The activation record is not destroyed - only reinitialized for each subprogram execution. Hardware support - CIP is the program counter, CEP is not used, simple jump executed on return.

Stack-based implementation The simplest run-time storage management technique call statements : push CIP and CEP return statements : pop CIP and CEP off of the stack. Used in most C implementations LISP: uses the stack as an environment.

Recursive Subprograms 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 second activation within the lifetime of the first activation.

Implementation Stack-based - CIP and CEP are stored in stack, forming a dynamic chain of links. A new activation record is created for each call and destroyed on return. The lifetimes of the activation records cannot overlap - they are nested.

Attributes of Data Control Data control features determine the accessibility of data at different points during program execution. Central problem: the meaning of variable names, i.e. the correspondence between names and memory locations.

Names and Referencing Environments Two ways to make a data object available as an operand for an operation Direct transmission Referencing through a named data object

Direct transmission A data object computed at one point as the result of an operation may be directly transmitted to another operation as an operand Example: x = y + 2*z; The result of multiplication is transmitted directly as an operand of the addition operation

eferencing through named data object A data object may be given a name when it is created, the name may then be used to designate it as an operand of an operation.

Program elements that may be named Variables Formal parameters Subprograms Defined types Defined constants Labels Exception names Primitive operations Literal constants

Associations and Referencing Environments www.getmyuni.com Association: binding identifiers to particular data objects and subprograms Referencing environment: the set of identifier associations for a given subprogram. Referencing operations during program execution: determine the particular data object or subprogram associated with an identifier

Subprogram Environment Local referencing 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 it Global referencing environment: associations created at the start of execution of the main program, available to be used in a subprogram. Predefined referencing environments: predefined associations in the language definition.

THANK YOU..!!