Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

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

2 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 )‏

3 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?

4 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.htmhttp://www.unix.org.ua/orelly/perl/prog3/ch29_01.htm –http://www.unix.org.ua/orelly/perl/prog3/ch29_02.htm

5 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.)‏

6 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?

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

8 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?

9 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?

10 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)‏

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

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

13 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?

14 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)‏

15 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?

16 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

17 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

18 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

19 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.)‏

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

21 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?

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


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

Similar presentations


Ads by Google