COS 301 Programming Languages

COS 301 Programming Languages

COS 301 Topics Programming Languages • Names • Variables • The Concept of Binding Sebesta Chapter 5 • Scope Names, Binding and Scope • Scope and Lifetime • Referencing Environments The beginning of wisdom is to call • Named Constants things by their right names Chinese Proverb Abstraction and the Von Neumann Machine Language groups • Imperative languages are abstractions of the • We will consider a broad division of imperative von Neumann architecture and imperative/OO languages in discussing – Variables are an abstraction for memory; a one- names because the treatment of names falls dimensional array of cells broadly into two classes: – Some abstractions are close to the machine, for – C-Like: C, C++, Java, etc (AKA the curly-brace example an n-bit integer in an n-bit machine languages) – Others require a mapping function, such as two or – Pascal-Like: Pascal, Ada, Modula, etc. higher dimensional arrays • Other major language families • Variables are characterized by attributes – Fortran – One of the most important is the type – Scripting – To design a type, must consider scope, lifetime, type checking, initialization, and type compatibility Names Names - Length • The terms name and identifier are roughly • Length synonymous – If too short, they cannot be connotative – Names are associated with variables, subprograms, – Language examples: modules, classes, parameters, types and other • FORTRAN 95: maximum of 31 constructs • C99: no limit but only the first 63 are significant; also, • Design issues for names: external names are limited to a maximum of 31 • C#, Ada, and Java: no limit, and all are significant – Allowable length • C++: no limit, but implementers often impose one – Are names case sensitive? – Are special words reserved words or keywords? 1 Underscores Special Characters • Underscores • Most languages allow a few special characters – Many languages allow underscores (and possibly a few other in names (_,!,@,$, etc.) characters) – Some languages allow variable names to contain only • Some languages require them underscores – PHP: all variable names must begin with $. Names • You can write a complete program with variables _, __, ___, without $ are constants _____, etc – Often used to separate words in meaningful variable names: – Perl: all variable names begin with $, @, or %, ex. grand_total_score which specify the variable’s type – The way that names are formed is often part of a language – Ruby: variable names that begin with @ are instance culture variables; those that begin with @@ are class • GrandTotalScore (camel case) grand_total_score variables • GRANDTOTALSCORE GRAND_TOTAL_SCORE • In C one convention is to use uppercase names for constants, lower or mixed case names for variables Special Characters Implicit Typing • Languages typically allow only a few characters • Many BASICs use type declaration chars: foo$ is other A-Z,a-z, 0-9, with syntactic restrictions a string, foo! is a single precision float – Cobol allows hyphens in names (what sort of parsing • In FORTRAN 77 variables beginning with I,J,K,L, problem does this present?) M are implicitly integers, otherwise REAL is – C allows unrestricted use of the underscore assumed – Fortran prior to Fortran 90 allowed embedded – COUNT is a real spaces – KOUNT is an integer • Grand Total Score is the same as GrandTotalScore – IMPLICIT statement allows mods to this rule • QuickBasic has DEFINT, DEFSNG, etc. statements to define implicit typing Character Case Keywords and Reserved Words • Case sensitivity • Keywords – Languages that allow only upper-case are less – An aid to readability; used to delimit or separate readable; no longer an issue statement clauses • A keyword is a word that is special only in certain – Case sensitive languages have a readability issue: contexts, e.g., in Fortran names that look alike are different – Real VarName (Real is a data type followed with a name, • Names in the C-based languages are case sensitive therefore Real is a keyword) – In Fortran this is legal: • Names in many others are not Real Integer • PHP has case-sensitive variable names, case-insensitive Integer Real function names – Pascal: const true = false – Case sensitive languages that do not require – PL/I had no reserved words! If if = then then else = if else if = else variable declarations also have a writability issue: it’s too easy to accidentally create a new variable 2 Keywords and Reserved Words The Concept of Binding • Reserved Words • A binding is an association between an entity (such as – A reserved word is a special word that cannot be a variable) and a property (such as its value). used as a user-defined name – A binding is static if the association occurs before run-time. – Potential problem with reserved words: If there are – A binding is dynamic if the association occurs at run-time. too many, many collisions occur (e.g., COBOL has • Static binding is used by most compiled languages 300 reserved words) • Interpreted languages often delay name resolution until runtime Variables Variable Names • A variable is an abstraction of a memory cell • Name - not all variables have them – Those that do not are typically “heap-dynamic” variables • Variables can be characterized as a tuple of • A variable name is a binding of a name to a memory attributes: address – Name – Address – Value – Type – Lifetime – Scope Variable Addresses Variable Type • Address - the memory address with which it is • Type - determines the range of values of variables and associated the set of operations that are defined for values of – A variable may have different addresses at different times that type during execution – A variable may have different addresses at different places in a program – If two variable names can be used to access the same memory location, they are called aliases – Aliases are created via pointers, reference variables, C and C++ unions • Aliases decrease readability (program readers must remember all of them) 3 Variable Value L-values and R-values • The contents of the memory cell associated • A distinction in the use of variable names first with the variable introduced in Algol 68 – Note that “memory cell” is an abstract concept • L-value - use of a variable name to denote its – Double-precision floats may occupy 8 bytes of address. physical storage in most implementations but we think of them as occupying 1 memory cell • R-value - use of a variable name to denote its value. Ex: x = x + 1 – Store into memory at address of x the value of x plus one. • On the LHS x denotes an address while on the RHS it denotes a value Explicit Dereferencing and Pointers The Concept of Binding • Some languages support/require explicit A binding is an association, such as between an dereferencing. attribute and an entity, or between an ML: x := !y + 1 operation and a symbol C: x = *y + 1 • Binding time is the time at which a binding • C pointers takes place. int x,y; int *p; ; p is a pointer to int x = *p; *p = y; • Note that C is liberal about whitespace: x *y; /* y is a pointer to type x */ x * y; /* same as above */ Possible Binding Times Example: Java Assignment Statement • Language design time -- bind operator symbols • In the simple statement to operations count = count + 1; • Language implementation time-- bind floating • Some of the bindings and binding times are point type to a representation – Type of count is bound at compile time – Set of possible values is bound at design time (but • Compile time -- bind a variable to a type in C or in C, at implementation time) Java – Meaning of + is bound at compile time, when types • Load time -- bind a C or C++ static variable of operands are known to a memory cell) – Internal representation of the literal 1 is bound at • Runtime -- bind a nonstatic local variable to a compiler design time memory cell – Value of count is bound at runtime with this statement 4 Static and Dynamic Binding Type Binding • A binding is static if it first occurs before run • How is a type specified? time and remains unchanged throughout • When does the binding take place? program execution. • If static, the type may be specified by either • A binding is dynamic if it first occurs during an explicit or an implicit declaration execution or can change during execution of the program Explicit/Implicit Declaration Dynamic Type Binding • An explicit declaration is a program statement • Perl symbols $, @, % divide variables into three used for declaring the types of variables namespaces: scalars, arrays and hashes • An implicit declaration is a default mechanism – Within each namespace type binding is dynamic for specifying types of variables (the first • JavaScript and PHP use dynamic type binding appearance of the variable in the program) • Specified through an assignment statement • FORTRAN and BASIC provide implicit e.g., JavaScript declarations list = [2, 4.33, 6, 8]; – Advantage: writability list = 17.3; – Disadvantage: reliability – Advantage: flexibility (generic program units) • Vestiges in modern languages: – Disadvantages: – Fortran: Implicit None • High cost (dynamic type checking and interpretation) – Visual Basic: Option Explicit • Type error detection by the compiler is difficult Cost of Dynamic Type Binding Type Inferencing • Type checking at run time • In ML types are determined (by the compiler) • Every variable needs a runtime descriptor to from the context of the reference fun circumference(r) = 3.14159 * r * r; maintain current type • Here the compiler can deduce type real and • Storage for variable is of varying size produce a

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 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