Programming Language Concepts
Programming Language Concepts
Concepts
Main Topics
Reasons for studying programming languages
Programming Domains
Language Evaluation Criteria
Influences on Language Design
Language Categories
Language Design Tradeoffs
Implementation Methods
Programming Environments
2
Why Study PLC?
Increased capacity to express ideas
Improved background for choosing
appropriate languages
Increased ability to learn new languages
Better understanding of the significance of
implementation
Increased ability to design new languages
Overall advancement of computing
3
Increased capacity to express ideas
Programming language constrains
Control structures
Data structures
Abstractions that can be used
Awareness of language features reduces
these limitations
Features of one language may be simulated in
another
Study of PLC builds appreciation for language
features and encourages their use
4
Language Evaluation Criteria
Readability
Writability
Reliability
Cost
5
Readability
Overall Simplicity
Control Statements
Data Types and Structures
Syntax Considerations
6
Writability
Support for abstraction
Process abstraction
Data abstraction
Expressivity
APL has powerful operators that accomplish lots of
computation with little coding
for statements for counting loops (instead of
while)
and then, or else Boolean operators in Ada
7
Reliability
Type checking
Subscript ranges: Ada vs. C
Static vs. dynamic type checking
Exception handling
Intercept runtime errors, take action to correct
problem, and continue processing
PL/I, C++, Ada, Java
Aliasing
2 or more ways to reference same memory cell
Possible via pointers, reference parameters, unions
8
Costs
Training programmers
Writing programs
Compiling programs
Executing programs
Language implementation system
Poor reliability
Maintaining programs
9
Language Categories
Imperative
Functional
Logic
Object-oriented
10
Language Design Tradeoffs
Reliability vs. cost of execution
Ada’s runtime type checking adds to execution
overhead
Readability vs. writability
C and APL
Flexibility vs. safety
Pascal variant record is a flexible way to view a
data object in different ways, but no type
checking is done to make it safe
11
Questions to Ponder
What are language design criteria?
What are some design trade-offs?
What are some language design
criteria that are in direct conflict
with each other?
12
Influences on Language Design
Computer architecture
Imperative languages model von Neumann
architecture
Functional programming languages need a non-von
Neumann architecture to be implemented efficiently
Programming methodologies
Top-down design, stepwise refinement
Data-oriented vs. process-oriented design
Object-oriented design
Concurrency (process-oriented)
13
Implementation methods
Compilation
Interpretation
Hybrid implementation systems
Java applets are compiled into byte code
Compiled applets are downloaded and
interpreted by byte code interpreter
14
Evolution of Major
Programming Languages
Evolution Language
15
Lexical and Syntax Analysis
18
Context-Free Grammar
A context-free grammar is one whose
productions take the form A ,
where A is a single non-terminal symbol
and is any string of terminals and/or
non-terminals.
Context-free grammars are used to
describe the syntax of modern
programming languages.
19
Derivation
A derivation is a repeated application of rules,
starting with the start symbol and ending with
a sentence. In each step of a derivation,
exactly one non-terminal is expanded.
Every string of symbols in a derivation is a
sentential form. A sentential form may
contain terminal and non-terminal symbols.
A sentence is a sentential form that has only
terminal symbols.
20
Parse Trees
Grammars describe the hierarchical
syntactic structure of sentences in the
language they define
These hierarchical structures are called
parse trees.
Every internal node is labeled with a non-
terminal symbol
Every leaf is labeled with a terminal symbol
21
Questions to Ponder
What’s syntax and semantics?
What’s syntax analysis?
What’s lexical analysis?
What’s a lexeme and a token?
What are grammars used for?
22
Names, Bindings, Type
Checking, and Scopes
Naming
Naming is the process by which the
programmer associates a name with a
potentially complicated program fragment
The goal is to hide complexity
Programming languages use name to designate
variables, types, classes, methods, operators,…
Naming provides abstraction
E.g. Mathematics is all about the formal notation
(i.e. naming) that lets us explore more and more
abstract concepts
24
Variable Names
Design issues
- What should the maximum length be?
- Are connector characters allowed?
- Are names case sensitive?
- Are special words reserved words or keywords?
Length
- FORTRAN I: maximum 6
- COBOL: maximum 30
- FORTRAN 90 and ANSI C: maximum 31
- Ada: no limit, and all are significant
- C++: no limit, but implementers often impose one
25
Variable Names
Case sensitivity
Disadvantage: readability (names that look alike are different)
Special words
A keyword is a word that is special only in certain contexts
Disadvantage: poor readability
28
Attributes of variables
Type
Determines the range of values of variables
and the set of operations defined for those
values
Value
The contents of the location with which a
variable is associated
29
Some definitions
Binding—an association, such as
between an attribute and an entity, or
between an operation and a symbol
Binding time—the time at which a
binding takes place
30
Binding times
Compile time
Example: binding of a variable to a type in C
Load time
Example: binding of a C static variable to a
memory cell
Run time
Example: binding of a non-static local variable
to a memory cell
31
More on binding
A binding is static if it
occurs before run time and
remains unchanged throughout execution
A binding is dynamic if it
Occurs during execution, or
Can change during execution
32
Scope
The scope of a variable is the range of
statements over which it is visible
The non-local variables of a program unit are
those that are visible but not declared there
The scope rules of a language determine how
references to names are associated with
variables
Scope may be static or dynamic
33
Static scope
Based on the text of a program
34
Dynamic scope
Based on calling sequences of program
units, not their textual layout
35
Scope vs. lifetime
Scope and lifetime are sometimes closely related,
but are different concepts.
Consider a static variable in a C or C++ function:
void fun() {
static int x = 0;
int y = 0;
…
}
Scope of x and y is from point of declaration to end of
function block
Lifetime of x is entire execution of program
Lifetime of y is during execution of fun()
36
Scope and Blocks
Blocks - a method of creating static scopes
inside program units. Example:
C and C++:
for (...) {
int index;
...
}
37
Variable initialization
The binding of a variable to a value at
the time it is bound to storage is called
initialization
Initialization is often done on the
declaration statement:
38
Type Checking
Type checking is the activity of ensuring that the
operands of an operator are of compatible types
A compatible type is one that is either legal for the
operator, or is allowed under language rules to be
implicitly converted, by compiler-generated code, to a
legal type.
A type error is the application of an operator to an
operand of an inappropriate type
A programming language is strongly typed if type
errors are always detected
39
Strong Typing
Allows the detection of the misuses
of variables that result in type
errors.
41
Data Types
Statement-Level Control Structures
Abstract Data Types
43
Ordinal Types ( user defined )
i) Aggregate name;
ii) Index (subscript):
Design Issues:
47
Fundamental Pointer Operations
48
Problems with pointers
49
Levels of Control Flow
the selection
Can a single statement, a sequence of statements, or a
51
Multiple selection constructs
54
Subprograms
Fundamentals of Subprograms
Design Issues for Subprograms
Subprogram Definitions
A subprogram header is the first line of the definition, including the name,
the kind of subprogram, and the formal parameters
Result – same
58
Design Considerations for
Parameter Passing
1. Efficiency
2. One-way or two-way
59
Parameters that are
Subprogram Names
- Possibilities:
a. It is that of the subprogram that enacts it.
- Shallow binding
61
Functions
Design Issues:
62
Questions to Ponder
What are some design issues for
subprograms?
What are some design issues for
parameter passing?
What is polymorphism?
What are some design issues for
functions?
63
Object-Oriented, Functional and
Logical Programming Languages
Object-Oriented Languages
Abstract Data Types are called classes
Class instances are called objects
66
FPLs vs imperative languages
Imperative programming languages
Design is based directly on the von Neumann architecture
Efficiency is the primary concern, rather than the suitability
of the language for software development
Functional programming languages
The design of the functional languages is based on
mathematical functions
A solid theoretical basis that is also closer to the user, but
relatively unconcerned with the architecture of the machines
on which programs will run
67
Lambda expressions
A mathematical function is a mapping of members of
one set, called the domain set, to another set, called
the range set
A lambda expression specifies the parameter(s)
and the mapping of a function in the following form
(x) x * x * x
for the function
cube (x) = x * x * x
Lambda expressions describe nameless functions
68
Fundamentals of FPLs
The objective of the design of a FPL is to mimic
mathematical functions as much as possible
The basic process of computation is fundamentally
different in a FPL than in an imperative language:
In an imperative language, operations are done and the
results are stored in variables for later use
Management of variables is a constant concern and source
of complexity for imperative programming languages
In an FPL, variables are not necessary, as is the case in
mathematics
The evaluation of a function always produces the
same result given the same parameters. This is
called referential transparency
69
LISP
73
Declarative vs. Imperative
Languages used for logic programming are called
declarative languages because programs written in
them consist of declarations rather than assignment
and flow-of-control statements. These declarations
are statements, or propositions, in symbolic logic.
Programming in imperative languages (e.g., Pascal,
C) and functional languages (e.g., Lisp) is
procedural, which means that the programmer knows
what is to be accomplished by the program and
instructs the computer on exactly how the
computation is to be done.
74
Logic Programming
Programming in logic programming languages
is non-procedural.
Programs in such languages do not state how
a result is to be computed. Instead, we
supply the computer with:
relevant information (facts and rules)
a method of inference for computing desired
results.
Logic programming is based on the predicate
calculus.
75
Questions to Ponder
What are some characteristics of
Object-Oriented, Functional and Logical
Programming Languages?
What’s the difference between
Declarative and Imperative
Programming Languages?
76