Chapter 55 Semantic Issues of Variables

Chapter 55 Semantic Issues of Variables

Introduction This chapter introduces the fundamental ChapterChapter 55 semantic issues of variables. – It covers the nature of names and special words in programming languages, attributes of Variables: variables, concepts of binding and binding times. – It investigates type checking, strong typing and Names, Bindings, type compatibility rules. – At the end it discusses named constraints and Type Checking and Scope variable initialization techniques. CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 1 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 2 On Names Names • Humans are the only species to have the concept of a name. There are some basic design issues involving names: • It’s given philosophers, writers and computer scientists lots to do for many years. – The logician Frege’s famous morning star and evening – Maximum length? star example. – What characters are allowed? – "What's in a name? That which we call a rose by any – Are names case sensitive? other word would smell as sweet." -- Romeo and Juliet, – Are special words reserved words or keywords? W. Shakespeare – Do names determine or suggest attributes – The semantic web proposes using URLs as names for everything. • In programming languages, names are character strings used to refer to program entities – e.g., labels, procedures, parameters, storage locations, etc. CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 3 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 4 Names: length limitations? Names: What characters are allowed? • Some examples: •Typical scheme: – FORTRAN I: maximum 6 – Names must start with an alpha and contain a mixture – COBOL: maximum 30 of alpha, digits and connectors – FORTRAN 90 and ANSI C: maximum 31 – Some languages (e.g., Lisp) are even more relaxed – C++: no limit, but implementers often impose one •Connectors: – Java, Lisp, Prolog, Ada: no limit, and all are significant – Connectors might include underscore, hyphen, dot, … • The trend has been for programming languages to allow - Fortran 90 allowed spaces as connectors longer or unlimited length names - Languages with infix operators typically only allow _, • Is this always good? reserving ., -, + as operators – What are some advantages and disadvantages of very - LISP: first-name long names? - C: first_name -“Camel notation” is popular in C, Java, C#... - Using upper case characters to break up a long name, e.g. firstName CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 5 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 6 Names: case sensitivity Special words • Foo = foo? •Def: A keyword is a word that is special only in certain • The first languages only had upper case (why?) contexts • Case sensitivity was probably introduced by Unix and – Virtually all programming languages have keywords hence C (when?) •Def: A reserved word is a special word that cannot be used • Disadvantage: as a user-defined name • Poor readability, since names that look alike to a human are different; worse in Modula-2 because – Some PLs have reserved words and others do not predefined names are mixed case (e.g. WriteCard) – PLs try to minimize the number of reserved words • Advantages: • For languages which use keywords rather than reserved • Larger namespace, ability to use case to signify words classes of variables (e.g., make constants be in – Disadvantage: poor readability thru possible confusion uppercase) about the meaning of symbols • C, C++, Java, and Modula-2 names are case sensitive but – Advantage: flexibility – the programmer has fewer the names in many other languages are not constraints on choice of names CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 7 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 8 Reserved words in C Reserved words in Common Lisp Programming languages try to minimize the number of Programming languages try to minimize the number of reserved words. Here are all of C’s reserved words. reserved words. Here are all of Lisp’s reserved words. auto enum signed break extern sizeof case float static char for struct const goto switch continue if typedef default int union do long unsigned double register void else Return volatile entry short while CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 9 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 10 Names: implied attributes Examples of implied attributes • LISP: global variables begin and end with an asterisk, e.g., • We often use conventions that associate attributes (if (> t *time-out-in-seconds*) (blue-screen)) with name patterns. • JAVA: class names begin with an upper case character, field • Some of these are just style conventions while others and method names with a lower case character are part of the language spec and are used by for(Student s : theClass) s.setGrade(“A”); compilers • PERL: scalar variable names begin with a $, arrays with @, and hashtables with %, subroutines begin with a &. $d1 = “Monday”; $d2=“Wednesday”; $d3= “Friday”; @days = ($d1, $d2, $d3); • FORTRAN: default variable type is float unless it begins with one of {I,J,K,L,M,N} in which case it’s integer DO 100 I = 1, N 100 ISUM = ISUM + I CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 11 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 12 Variables Attributes of Variables •A variable is an abstraction of a memory cell Variables can be characterized as a • Can represent complex data structures with lots of 6-tuple of attributes: structure (e.g., records, arrays, objects, etc.) –Name: identifier used to refer to the variable –Address: memory location(s) holding the variables value –Value: particular value at a moment current_student –Type: range of possible values –Lifetime: when the variable can be accessed –Scope: where in the program it can be accessed CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 13 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 14 Variables: names and addresses A variable which shall remain nameless • Many languages allow one to create new data structures with • Name - not all variables have them! only a pointer to refer to them. This is like a variable that does not really have a name (“that thing over there”) , e.g.: • Address - the memory address with which it is Int *intNode; . associated intNode = new int; . – A variable may have different addresses at Delete intNode; different times during execution • Some languages (e.g., shell scripts) assume you reference – A variable may have different addresses at parameters not with names but by position (e.g., $1, $2) different places in a program • Some languages don’t have named variables at all! • If two variable names can be used to access the – E.g., if every function takes exactly one argument, we can same memory location, they are called aliases dispense with the variable name – Aliases are harmful to readability, but they are – Arguments that naturally take several arguments (e.g., +) can be reduced to functions that take only a single useful under certain circumstances argument. More on this when we talk of lisp, maybe. CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 15 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 16 Aliases Variables Type • When two names refer to the same memory • Typing is a rich subject we will talk about later. location we can them aliases. • Most languages associate a variable with a single • Aliases can be created in many ways, depend- type ing on the language. • The type determines the range of values and the • Pointers, reference variables, Pascal variant records, call by operations allowed for a variable name, Prolog’s unification, C and C++ unions, and • In the case of floating point, type usually also Fortran’s equivalence statemenr determines the precision (e.g., float vs. double) • Aliases can be trouble. (how?) • In some languages (e.g., Lisp, Prolog) a variable can • Some of the original justifications for aliases are no longer take on values of any type. valid; e.g. memory reuse in FORTRAN • OO languages (e.g. Java) have a few primitive types • Aliases can be powerful. (int, float, char) and everything else is a pointer to an • Prolog’s unification offers new paradigms object. CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 17 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 18 Variable Value lvalue and rvalue Are the two occurrences of “a” in this expression • The value is the contents of the memory the same? location with which the variable is associated. a := a + 1; • Think of an abstract memory location, rather In a sense, than a physical one. • The one on the left of the assignment refers to the – Abstract memory cell - the physical cell or location of the variable whose name is a; collection of cells associated with a variable • The one on the right of the assignment refers to the • A variable’s type will determine how the bits value of the variable whose name is a; in the cell are interpreted to produce a value. We sometimes speak of a variable’s lvalue and • We sometimes talk about lvalues and rvalues. rvalue • The lvalue of a variable is its address • The rvalue of a variable is its value CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 19 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 20 Binding Possible binding times • Def: A binding is an association, such as • Language design time, e.g., bind operator between an attribute and an entity, or between symbols to operations an operation and a symbol • Language implementation time, e.g., bind • It’s like assignment, but more general floating point type to a representation • We often talk of binding –a variable to a value, as in • Compile time, e.g., bind a variable to a type in classSize is bound to the number of C or Java students • Link time –A symbol to an operator • Load time, e.g., bind a FORTRAN 77 variable + is bound to the inner product operation to memory cell (or a C static variable) • Def: Binding time is the time at which a • Runtime, e.g., bind a nonstatic local variable to binding takes place.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    14 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us