CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

22
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    1

Transcript of CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Page 1: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

CS 330Programming Languages

10 / 16 / 2008

Instructor: Michael Eckmann

Page 2: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Today’s Topics• Questions / comments?• Perl

– implementing a recursive descent parser in Perl

• Names, Variables, Binding ( Chapter 5 )

Page 3: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Perl

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Let's continue to implement a recursive descent parser for the language in the text book (p. 179)

• What are the features of a recursive decent parser again?

Page 4: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Perl

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• There's so much more about Perl that I haven't gone over. You should now be able to learn more on your own. There are many functions I haven't gone over that are useful

– Substring

– Split

– Etc.

– Find out about the functions (with examples) at these 2 pages:

– http://www.unix.org.ua/orelly/perl/prog3/ch29_01.htm

– http://www.unix.org.ua/orelly/perl/prog3/ch29_02.htm

Page 5: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Textbook concepts

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• When we discuss the concepts in the textbook

– we should keep in mind that we are to be thinking as language designers not necessarily as programmers

– and when we evaluate language criteria we are considering their effect on the language as a whole (how they affect the compilation process, how they affect the programmer's ability to write and read code, etc.)

Page 6: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Names

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• What's a name?

• A name (aka identifier) is associated with variables, labels, subprograms, parameters, etc...

• What possible design criteria are there for names in a language? Any ideas?

Page 7: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Names

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Case sensitive? (Java, C++)• Does it allow spaces or other whitespace? If so, are they ignored?• What's the allowable length?• What characters does it allow?• What can it start with?• Are words that are part of the language allowed as names?

– those allowed as names are called keywords

– those not allowed as names are called reserved words• predefined names are related to these ideas

– e.g. Ada has Integer and Float types which are allowed to be redefined by an Ada program

– Java packages, C++ libraries when imported the predefined names within those are now visible to a program and not able to be redefined.

Page 8: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Names

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Why might there be a restriction on length of names? Does it matter to

the compiler?

– some allow any length but only the first x characters are significant.

• any problems or advantages to this?• What about readability, writability, reliability, cost?

– Which of these ideas about names enhance or are a detriment to these language evaluation criteria?

Page 9: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Variables

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• What's a variable?

• Is it the name of a memory location? Yes. But it is more.

• what else?

Page 10: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Variables

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• What's a variable?

• Is it the name of a memory location? Yes. But it is more.

• Name, address, value, type, size

• And two more things:

– lifetime (how long the name is attached to the memory location),

– scope (where in the code can it be referenced)

Page 11: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Variables

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Memory address

– may be possible for same name to be associated with different addresses

• at different times during execution

• at different places in the code

– can anyone give an example of these?

– the address of a variable is sometimes called its l-value• Aliases

– anyone want to define alias in terms of names and addresses?

Page 12: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Variables

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Aliases

– more than one name associated to the same memory address.

– example: 2 variables x and y both refer to same address, so if I change the value that x refers to, the value that y refers to changes too because it contains the same address

– effects on readability or writability?

– An example of aliases in Java are that they are created when passing certain kinds of parameters (e.g. a reference to a class, or an array reference) Can aliases occur anywhere else in Java?

• Type

– What does a type do?

• Size

• Value

– sometimes referred to as a variable's r-value. Why do you think?

Page 13: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Variables

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Type

– determines the kind and range of possible values

– and the operations that can be performed on variables of the type

• Size

– how much memory --- determined by the type

• Value

– contents of the variable at any given time during run-time

– sometimes referred to as a variable's r-value. Why do you think?

Page 14: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Binding is an association between • an attribute and an entity• an operation and a symbol

• Times when binding takes place• Language (compiler) design time• Language implementation time• Compile time• Link time (when your compiled code is linked to other

libraries)• Load time (when the program is executed it gets loaded into

memory)• Run time (when the program starts executing after being

loaded into memory)

Page 15: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Example from the text illustrates some of these ideas:

int count;

// ...

count = count + 5;

– The text discusses these assuming the code is in C. Even if we don't know C we should be able narrow down when the bindings below occur.

• type of count is bound when?

• set of possible values of an int is bound when?

• meaning of + operator is bound when?

• internal representation of literal 5 is bound when?

• the value of count is bound when?

Page 16: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• The answers for the C language are:• type of count is bound when? compile time• set of possible values of int is bound when? compiler-

implementation time – what does that imply?

• meaning of + operator is bound when? compile-time – what does that imply?

• internal representation of literal 5 is bound when? compiler-design time

• the value of count is bound when? run-time

Page 17: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Examples of bindings that occur at – link time

• a method/function call bound to the specific code in the library in which it is defined

– load time• global variable may be bound to specific memory

storage at load time

Page 18: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Binding attributes to variables• Static binding – first occurs before run time and stays same

throughout program execution

• note: load-time is considered before run-time• Dynamic binding

• Either it first occurs during run time

• OR it can change during execution

Page 19: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Type bindings• Variable declaration

• Explicit (what we're used to -- declare a variable by giving it a name

and a type.)

• Implicit – associate a type based on naming convention and

declaration happens on first appearance

– I, J, ..., N are implicitly Integer types in Fortran

– (sort of occurs in Perl, where the first char of a variable name specifies its “kind”, e.g. @ is for arrays, $ for scalars, % for keyed arrays (aka hashes). But scalars can hold integers, floats, or strings.)

Page 20: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Type bindings• Variable declaration

• Declaration vs. definition

– In C & C++ there is a difference between declaration and definition

– declarations specify types but do not allocate memory

– definitions specify type and allocate memory

– because C & C++ have a keyword extern which declares a var but does not allocate storage --- it is assumed to be done elsewhere. So, in that way one can declare a var without defining it.

– The var needs to be defined somewhere though.

Page 21: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Dynamic Type Binding (JavaScript and PHP)

• Type is specified through an assignment statement

• e.g., JavaScript list = [2, 4.33, 6, 8];list = 17.3;

– Advantage: flexibility (generic program units)– Disadvantages:

• High cost at run-time (dynamic type checking and interpretation)

• Type error detection by the compiler is difficult or impossible, why?

Page 22: CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.

An aside about memory

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• How is memory divided up and categorized for executing programs? A typical

setup is as follows. I'll draw a diagram on the board.

– executable code

– static data area

• used for statically declared objects like globals and constants

– stack (grows one way)

• used for local variable allocation during “procedure / method / subroutine / function” calls. Note: I will constantly use these four words interchangeably.

– heap (grows the other way)

• used for dynamic objects

– objects that are allocated at runtime, may change and size not known until run time

• e.g. linked lists with unknown number of nodes, trees with unknown number of nodes, etc.