Programming Language Concept

Just another Binusian blog site

Session 3 : Names, Bindings, and Scopes

October7

Introduction

  • Imperative languages are abstractions of von Neumann architecture
  1. Memory
  2. Processor
  • Variables are characterized by attributes

To design a type, must consider scope, lifetime, type checking, initialization, and type compatibility

Names

  • Length
  1. If too short, they cannot be connotative
  2. Language examples:
  • FORTRAN 95: maximum of 31
  • C99: no limit but only the first 63 are significant; also, external names are limited to a maximum of 31
  • C#, Ada, and Java: no limit, and all are significant
  • C++: no limit, but implementers often impose one
  • Special characters
  1. PHP: all variable names must begin with dollar signs
  2. Perl: all variable names begin with special characters, which specify the variable’s type
  3. Ruby: variable names that begin with @ are instance variables; those that begin with @@ are class variables

 

Case sensitivity

Disadvantage: readability (names that look alike are different)

  • Names in the C-based languages are case sensitive
  • Names in others are not
  • Worse in C++, Java, and C# because predefined names are mixed case
  • A keyword is a word that is special only in certain contexts, e.g., in FortranSpecial wordsAn aid to readability; used to delimit or separate statement clauses
  1. Real VarName  (Real is a data type followed with a name, therefore Real is a keyword)
  2. Real = 3.4 (Real is a variable)                                                       
  3. A reserved word is a special word that cannot be used as a user-defined name
  4. Potential problem with reserved words: If there are too many, many collisions occur (e.g., COBOL has 300 reserved words!)

Variables

  • A variable is an abstraction of a memory cell
  • Variables can be characterized as a sextuple of attributes:
  1. Name
  2. Address
  3. Value
  4. Type
  5. Lifetime
  6. Scope

The Concept of Binding

Possible Binding Times

  • Language design time — bind operator symbols to operations
  • Language implementation time– bind floating point type to a representation
  • Compile time — bind a variable to a type in C or Java
  • Load time — bind a C or C++ static variable to a memory cell)
  • Runtime — bind a nonstatic local variable to a memory cell
  • Storage Bindings & Lifetime
  1. Allocation – getting a cell from some pool of available cells
  2. Deallocation – putting a cell back into the pool
  • The lifetime of a variable is the time during which it is bound to a particular memory cell

Categories of Variables by Lifetimes

  • Static–bound to memory cells before execution begins and remains bound to the same memory cell throughout execution, e.g., C and C++ static variables in functions
  • Advantages: efficiency  (direct addressing), history-sensitive subprogram support
  • Disadvantage: lack of flexibility  (no recursion)
  • Stack-dynamic–Storage bindings are created for variables when their declaration statements are elaborated.
  • If scalar, all attributes except address are statically bound
local variables in C subprograms (not declared static) and Java methods
  • Advantage: allows recursion; conserves storage
  • Disadvantages:
  1. Overhead of allocation and deallocation
  2. Subprograms cannot be history sensitive
  3. Inefficient references (indirect addressing)
  • Explicit heap-dynamic –– Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution
  • Referenced only through pointers or references, e.g. dynamic objects in C++ (via new and delete), all objects in Java
  • Advantage: provides for dynamic storage management
  • Disadvantage: inefficient and unreliable
  • Implicit heap-dynamic--Allocation and deallocation caused by assignment statements (all variables in APL; all strings and arrays in Perl, JavaScript, and PHP)
  • Advantage: flexibility (generic code)
  • Disadvantages:
  1. Inefficient, because all attributes are dynamic
  2. Loss of error detection

Variable Attributes: Scope

  • The scope of a variable is the range of statements over which it is visible
  • The local variables of a program unit are those that are declared in that unit
  • The nonlocal variables of a program unit are those that are visible in the unit but not declared there
  • Global variables are a special category of nonlocal variables
  • The scope rules of a language determine how references to names are associated with variables

Static Scope

  • Based on program text
  • To connect a name reference to a variable, you (or the compiler) must find the declaration
  • Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name
  • Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent
  • Some languages allow nested subprogram definitions, which create nested static scopes (e.g., Ada, JavaScript, Common LISP, Scheme, Fortran 2003+, F#, and Python)
  • Variables can be hidden from a unit by having a “closer” variable with the same name
  • Ada allows access to these “hidden” variables

Evaluation of Static Scoping

  • Works well in many situations
  • Problems:
  1. In most cases, too much access is possible
  2. As a program evolves, the initial structure is destroyed and local variables often become global; subprograms also gravitate toward become global, rather than nested

Session 2 : Describing Syntax and Semantics

September30

Introduction

  • Syntax: the form or structure of the expressions, statements, and program units
  • Semantics: the meaning of the expressions, statements, and program units
  • Syntax and semantics provide a language’s definition

Users of a language definition

  • Other language designers
  • Implementers
  • Programmers (the users of the language)

The General Problem of Describing Syntax: Terminology

  • A sentence is a string of characters over some alphabet
  • A language is a set of sentences
  • A lexeme is the lowest level syntactic unit of a language
  • A token is a category of lexemes

Formal Definition of Languages

  • Recognizers

A recognition device reads input strings over the alphabet of the language and decides whether the input strings belong to the language

  • Generators

A device that generates sentences of a language

One can determine if the syntax of a particular sentence is syntactically correct by comparing it to the structure of the generator

Attribute Grammars

  • Attribute grammars (AGs) have additions to CFGs to carry some semantic info on parse tree nodes
  • Primary value of AGs:

Static semantics specification

Compiler design (static semantics checking)

Attribute Grammars :

An attribute grammar is a context-free grammar G = (S, N, T, P) with the following additions:

  1. For each grammar symbol x there is a set A(x) of attribute values
  2. Each rule has a set of functions that define certain attributes of the nonterminals in the rule
  3. Each rule has a (possibly empty) set of predicates to check for attribute consistency

Attribute Grammars: An Example

  • Syntax

<assign> -> <var> = <expr>

<expr> -> <var> + <var> | <var>

<var> A | B | C

  • actual_type: synthesized for <var> and <expr>
  • expected_type: inherited for <expr>

Semantics

  • There is no single widely acceptable notation or formalism for describing semantics
  • Several needs for a methodology and notation for semantics:
  1. Programmers need to know what statements mean
  2. Compiler writers must know exactly what language constructs do
  3. Correctness proofs would be possible
  4. Compiler generators would be possible
  5. Designers could detect ambiguities and inconsistencies

Operational Semantics

  • Operational Semantics

–Describe the meaning of a program by executing its statements on a machine, either simulated or actual.  The change in the state of the machine (memory, registers, etc.) defines the meaning of the statement

  • To use operational semantics for a high-level language, a virtual machine is needed
  • Uses of operational semantics:
  1. Language manuals and textbooks
  2. Teaching programming languages
  • Two different levels of uses of operational semantics:
  1.    Natural operational semantics
  2.    Structural operational semantics
  • Evaluation
  1.    Good if used informally (language manuals)
  2.    Extremely complex if used formally

Denotational Semantics

  • Based on recursive function theory
  • The most abstract semantics description method
  • Originally developed by Scott and Strachey (1970)
  • The process of building a denotational specification for a language:
  1. Define a mathematical object for each language entity
  2. Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects
  • The meaning of language constructs are defined by only the values of the program’s variables

Session 1 : Introduction

September30

 

Implementation Methods

  • Compilation

Programs are translated into machine language; includes JIT systems

Use: Large commercial applications

  • Pure Interpretation

Programs are interpreted by another program known as an interpreter

Use: Small programs or when efficiency is not an issue

  • Hybrid Implementation Systems

A compromise between compilers and pure interpreters

Use: Small and medium systems when efficiency is not the first concern

 

Influences on Language Design

  • Computer Architecture

Languages are developed around the prevalent computer architecture, known as the von Neumann architecture

  • Program Design Methodologies

New software development methodologies (object-oriented software development) led to new programming paradigms and by extension, new programming languages

 

Language Categories

  • Imperative
  • Functional
  • Logic
  • Markup/programming hybrid

 

Language Evaluation Criteria

  • Readability: the ease with which programs can be read and understood
  • Writability: the ease with which a language can be used to create programs
  • Reliability: conformance to specifications (i.e., performs to its specifications)
  • Cost: the ultimate total cost

 

 

Programming Domains

  • Scientific applications
  • Business applications
  • Artificial intelligence
  • Systems programming
  • Web Software

 

Reasons for Studying Concepts of Programming Languages

  • Increased ability to express ideas
  • Improved background for choosing appropriate languages
  • Increased ability to learn new languages
  • Better understanding of significance of implementation
  • Better use of languages that are already known
  • Overall advancement of computing

 

 

Newer Entries »