Programming Languages Janyl Jumadinova September 24, 2020 Janyl - - PowerPoint PPT Presentation

programming languages
SMART_READER_LITE
LIVE PREVIEW

Programming Languages Janyl Jumadinova September 24, 2020 Janyl - - PowerPoint PPT Presentation

Programming Languages Janyl Jumadinova September 24, 2020 Janyl Jumadinova Programming Languages September 24, 2020 1 / 17 Name, Scope, and Binding Janyl Jumadinova Programming Languages September 24, 2020 2 / 17 Name, Scope, and Binding


slide-1
SLIDE 1

Programming Languages

Janyl Jumadinova September 24, 2020

Janyl Jumadinova Programming Languages September 24, 2020 1 / 17

slide-2
SLIDE 2

Name, Scope, and Binding

Janyl Jumadinova Programming Languages September 24, 2020 2 / 17

slide-3
SLIDE 3

Name, Scope, and Binding

A name is exactly what you think it is – Most names are identifiers – symbols (like ‘+’) can also be names

Janyl Jumadinova Programming Languages September 24, 2020 2 / 17

slide-4
SLIDE 4

Name, Scope, and Binding

A name is exactly what you think it is – Most names are identifiers – symbols (like ‘+’) can also be names A binding is an association between two things, such as a name and the thing it names.

Janyl Jumadinova Programming Languages September 24, 2020 2 / 17

slide-5
SLIDE 5

Name, Scope, and Binding

A name is exactly what you think it is – Most names are identifiers – symbols (like ‘+’) can also be names A binding is an association between two things, such as a name and the thing it names. The scope of a binding is the part of the program (textually) in which the binding is active.

Janyl Jumadinova Programming Languages September 24, 2020 2 / 17

slide-6
SLIDE 6

Binding

Binding Time: the point at which a binding is created or, more generally, the point at which any implementation decision is made.

Janyl Jumadinova Programming Languages September 24, 2020 3 / 17

slide-7
SLIDE 7

Binding

Binding Time: the point at which a binding is created or, more generally, the point at which any implementation decision is made. language design time program structure, possible type language implementation time

  • I/O, arithmetic overflow, type equality (if unspecified in manual)

Janyl Jumadinova Programming Languages September 24, 2020 3 / 17

slide-8
SLIDE 8

Other Implementation Decisions

program writing time – algorithms, names

Janyl Jumadinova Programming Languages September 24, 2020 4 / 17

slide-9
SLIDE 9

Other Implementation Decisions

program writing time – algorithms, names compile time – plan for data layout

Janyl Jumadinova Programming Languages September 24, 2020 4 / 17

slide-10
SLIDE 10

Other Implementation Decisions

program writing time – algorithms, names compile time – plan for data layout link time –layout of whole program in memory

Janyl Jumadinova Programming Languages September 24, 2020 4 / 17

slide-11
SLIDE 11

Other Implementation Decisions

program writing time – algorithms, names compile time – plan for data layout link time –layout of whole program in memory load time

Janyl Jumadinova Programming Languages September 24, 2020 4 / 17

slide-12
SLIDE 12

More Implementation Decisions

run time – value/variable bindings, sizes of strings – NOTE: run time includes

program start-up time module entry time elaboration time (point at which a declaration is first “seen”) procedure entry time

Janyl Jumadinova Programming Languages September 24, 2020 5 / 17

slide-13
SLIDE 13

Binding

The terms STATIC and DYNAMIC are generally used to refer to things bound before run time and at run time, respectively. – “static is a coarse term; so is “dynamic”

Janyl Jumadinova Programming Languages September 24, 2020 6 / 17

slide-14
SLIDE 14

Binding

The terms STATIC and DYNAMIC are generally used to refer to things bound before run time and at run time, respectively. – “static is a coarse term; so is “dynamic” Binding Times are very important in programming languages!

Janyl Jumadinova Programming Languages September 24, 2020 6 / 17

slide-15
SLIDE 15

Binding

In general, early binding times are associated with greater efficiency Later binding times are associated with greater flexibility

Janyl Jumadinova Programming Languages September 24, 2020 7 / 17

slide-16
SLIDE 16

Binding

In general, early binding times are associated with greater efficiency Later binding times are associated with greater flexibility Compiled languages tend to have early binding times Interpreted languages tend to have later binding times

Janyl Jumadinova Programming Languages September 24, 2020 7 / 17

slide-17
SLIDE 17

Scope Rules - Control Bindings

Fundamental to all programming languages is the ability to name data

  • i.e., to refer to data using symbolic identifiers rather than addresses

Janyl Jumadinova Programming Languages September 24, 2020 8 / 17

slide-18
SLIDE 18

Scope Rules - Control Bindings

Fundamental to all programming languages is the ability to name data

  • i.e., to refer to data using symbolic identifiers rather than addresses

Not all data is named! For example, dynamic storage in C or Pascal is referenced by pointers, not names

Janyl Jumadinova Programming Languages September 24, 2020 8 / 17

slide-19
SLIDE 19

Scope Rules - Control Bindings

Fundamental to all programming languages is the ability to name data

  • i.e., to refer to data using symbolic identifiers rather than addresses

Not all data is named! For example, dynamic storage in C or Pascal is referenced by pointers, not names double *d = (double *)malloc(8); *d = 3.14; /* No name is bound to the value 3.14 */ /* The name ‘‘d’’ is bound to the ADDRESS containing 3.14 */

Janyl Jumadinova Programming Languages September 24, 2020 8 / 17

slide-20
SLIDE 20

Lifetime and Storage Management

The period of time from creation to destruction is called the LIFETIME of a binding.

Janyl Jumadinova Programming Languages September 24, 2020 9 / 17

slide-21
SLIDE 21

Lifetime and Storage Management

The period of time from creation to destruction is called the LIFETIME of a binding. If object outlives binding it’s garbage. If binding outlives object it’s a dangling reference.

Janyl Jumadinova Programming Languages September 24, 2020 9 / 17

slide-22
SLIDE 22

Lifetime and Storage Management

The period of time from creation to destruction is called the LIFETIME of a binding. If object outlives binding it’s garbage. If binding outlives object it’s a dangling reference. The textual region of the program in which the binding is active is its scope.

Janyl Jumadinova Programming Languages September 24, 2020 9 / 17

slide-23
SLIDE 23

Lifetime and Storage Management

Storage Allocation mechanisms Static Stack Heap

Janyl Jumadinova Programming Languages September 24, 2020 10 / 17

slide-24
SLIDE 24

Lifetime and Storage Management

Storage Allocation mechanisms Static Stack Heap Static allocation for code globals static or own variables

Janyl Jumadinova Programming Languages September 24, 2020 10 / 17

slide-25
SLIDE 25

Static Example

In C, variables can be global (visible to any function)

Janyl Jumadinova Programming Languages September 24, 2020 11 / 17

slide-26
SLIDE 26

Static Example

When we compile this, i is stored in a fixed location, while j is allocated

  • n the stack

Janyl Jumadinova Programming Languages September 24, 2020 12 / 17

slide-27
SLIDE 27

Two Types of Scoping

Static scoping (also called “lexical scoping”) most familiar (Java, C) scope of variables known at compile time

Janyl Jumadinova Programming Languages September 24, 2020 13 / 17

slide-28
SLIDE 28

Two Types of Scoping

Static scoping (also called “lexical scoping”) most familiar (Java, C) scope of variables known at compile time Dynamic scoping scope depends on order of function calls at execution time pretty rare nowadays

Janyl Jumadinova Programming Languages September 24, 2020 13 / 17

slide-29
SLIDE 29

Static Scope Example (Java)

Janyl Jumadinova Programming Languages September 24, 2020 14 / 17

slide-30
SLIDE 30

What Happens Here? (Java)

Janyl Jumadinova Programming Languages September 24, 2020 15 / 17

slide-31
SLIDE 31

What Happens in Dynamic Scoping?

Janyl Jumadinova Programming Languages September 24, 2020 16 / 17

slide-32
SLIDE 32

In-Class Exercise

JavaScript Case Study

– Pull activity4 in your class activities repository

Janyl Jumadinova Programming Languages September 24, 2020 17 / 17