What Are Programming Languages
What Are Programming Languages
Languages?
Abby S. Paculdo
Concept of Programming
Language
Increased capacity to express ideas. Awareness
of a wider of programming language features can reduce
such limitations in software development thought processes
by learning new language constructs.
Improved background for choosing
appropriate language. Continue to used the
language.
Increased ability to learn new language.
Computer programming is a young discipline , and design
methodologies, software development tools, and
programming languages are still in a state of continuous
evolution.
Better understanding of the significance of
implementation.
Increased ability to design new language. The
form of that interface is designed by the system developer
Programming Domain
Scientific Applications, a simple data structures
but require large numbers of floating-point arithmetic
computations, common data structures are array and
common control structure are counting loops.
Artificial Intelligence, broad area of computer
application characterized by the use of symbolic rather than
numeric computations.
System Programming , also known as System
Software.
The wrong question...
What can this language do?
Implicitly we are comparing the new
language with other languages.
The answer is very simple: all languages
can do exactly the same computations!
(Influence on Language Design)Computer
Architecture (Result of operation)
If they can do all the same
computations... There must be other
reasons for the existence of hundreds of
programming languages.
Definition
A program is a sequence of symbols that
specifies a computation.
A programming language is a set of rules that
specify which sequence of symbols constitute a
program, and what computation the program
describes.
word processor, image processing, pattern classification, data analysis, database access, ...
Output:
1. abcd
2. accc
Usual grammar for
expressions
E E + T | T
T T * P | P
P i | ( E )
Name, Binding,
Type Checking,
And Scopes
Data Structure and algorithm
Algorithm a process or set of rules
used for calculation or problem
solving
Data Structure a series of coded
instructions to control the operation
of a computer or other machine,
Name Forms
A name/identifier is a string of
characters used to identify some
entity in a program.
In some language, notably C, C++,
and Java, uppercase and
lowercase letters in names are
distinct; that is, names in these
languages are case sensitive
Java vs C++/C
Used for class names, method names, and variable names.
Every name is made from the following characters, starting
with a letter:
Letters: a-z, A-Z, and other alphabetic characters from
other languages, Digits: 0-9, Special: _ (underscore)
No names can be the same as a Java keyword (eg, import,
if, ...)
Packages, Class and interface names - Start with
uppercase - Class and interface names start with an
uppercase letter, and continue in lowercase. For multiple
words, use camelcase. Eg, Direction, LogicalLayout,
DebugGapSpacer.
Variable and method names Lowercase - Lowercase is
used for variable and method names. If a name has multiple
words, use camelcase.
Constants - All uppercase, use _ to separate words - The
names of constants (typically declared static final) should be
in all uppercase. For example, BorderLayout.NORTH.
Java vs C++/C
Name Forms
A keyword is a special word of a that is
special certain contexts.
Example: int apple;
int = 3;
A reserved word is a special word of
programming language that cannot be
used as a name. As a language design
choice, reserved words are better than
keywords because the ability to
redefined keywords can lead to
readability problems.
Java vs. C++
Name Forms
A program Variable is an abstraction of a computer
memory cell or collection of cells. A variable can be
characterized as a sextuple of attribute; ( name, address,
value, type, lifetime, scope).
a. Name variable names are most common names in
program.
b. Address address of a variable is the memory address
with which it is associated.
c. Type determines the range of values the variable can
have and the set of operations that are defined for
values of the type.
d. Value is the contents of the memory cell or cells
associated with the variable. It is convenient to think of
computer memory in terms of abstract cells, rather than
physical cells, the cells, or individually addressable
units, of most contemporary computer memories are
byte sized, with a byte usually being eight bits in
length.
Variables in Java
the basic unit of storage in a java program. A
variable is defined by combination of an
identifier, a type, and an optional initialize. It is a
named memory location that may be assigned a
value by the program.
Type identifier[=value][,identifier[=value]
.];
Variables in C++
A symbol that represent a storage
location in the computers memory. The
information that is stored in that location
is called the Value of the variable. One
common way for a variable to obtain a
value is by an assignment. Syntax
Variable = expression;
First the expression is evaluated and then
the resulting value is assigned to the
variable. The equals sign = is the
assignment operator in C++.
The Concept of Binding
In a general sense, a binding is an
association, such as between an attribute
and an entity or between an operation and a
symbol. The time at which a binding takes
place is called binding time. Binding and
binding time are prominent concepts in the
semantics of programming language.
Binding can take place at:
a. Language design time
b. Language Implementation time
c. Compile time
d. Link time
e. Load time / or run time
The Concept of Binding
The asterisk symbol (*) is usually
bound to the multiplication
operation at language design time.
A data type , such as Integer is
bound to range of possible values
at language implementation time.
At compile time, a variable in a C
program is bound to a particular
data type. A call to library
subprogram is bound to the
subprogram code to link time.
The Concept of Binding
int count;
.
count = count + 5
int myFunction() {
static int count =0;
count++;
return count;
}
Static Variables
Advantages:
efficiency: direct addressing, no run-time
overhead for allocation & deallocation.
history-sensitive : maintain values
between successive function calls.
Disadvantages:
lack of flexibility (no recursion).
storage cannot be shared among
variables.
b. Stack-Dynamic
Variables
Stack-dynamic=storage bindings are created
for variables when their declaration
statements are elaborated.
- A declaration is elaborated when the executable code
associated with it is executed (with some exceptions).
- Storage is allocated from the run-time stack.
- If scalar, all attributes except address are statically
bound:
Example: local variables in C subprograms
and Java methods.
int factorial(int n) {
int result =1;
for (int i =2; i n; i++)
result =i;
return result;
Stack-Dynamic Variables
Advantages:
Allows recursion;
Conserves storage.
Disadvantages:
Overhead of allocation and deallocation.
Subprograms cannot be history
sensitive.
Inefficient references (indirect
addressing).
c. Explicit Heap-Dynamic
Variables
Explicit heap-dynamic variables are allocated and
deallocated by explicit directives, specified by the
programmer, which take effect during execution:
Storage is allocated from the heap.
The actual variables are nameless.
Referenced only through pointers or references,
e.g. dynamic objects in C++ (via new and delete),
all objects in Java.
DATA TYPES
Data Types
A data type defines a collection of data objects and
a set of predefined operations on those objects.
Primitive data types are those not defined in
terms of other data types:
Some primitive data types are merely reflections of the
hardware.
Others require only a little non-hardware support for their
implementation.
Design issues:
Is it a primitive type or just a special kind of array?
Should the length of strings be static or dynamic?
String In Programming
Language
C and C++:
Implemented as null terminated char arrays.
A library of functions in string.h that provide string
operations.
Many operations are inherently unsafe (ex: strcpy).
C++ string class from the standard library is safer.
Pattern Matching:
built-in for Perl, JavaScript, Ruby, and PHP, using
regular expressions.
class libraries for C++. Java, Python, C#.
String Length
Static Length set when the string is created:
Java String, C++ STL, Ruby String, C# .NET.
Dynamic
Length varying length with no
maximum:
JavaScript and Perl (overhead of dynamic
allocation/deallocation).
Dynamic length:
need run-time descriptor;
allocation/de-allocation is the biggest
implementation problem.
Character String
Implementation
Array Types
An array is an aggregate of homogeneous
data elements in which an individual
element is identified by its position in the
aggregate, relative to the first element.
Indexing is a mapping from indices to
elements:
array_name[index_value_list] an element
Index range checking:
C, C++, Perl, and Fortran do not specify range
checking.
Java, ML, C# specify range checking.
In Ada, the default is to require range checking,
but it can be turned off.
Array Categories
Static:subscript ranges are statically bound and
storage allocation is static (before run-time)
Advantage: efficiency no dynamic
allocation/deallocation.
Example: arrays declared as static in C/C++
functions.
Single-Dimensional Arrays:
implemented as a block of adjacent memory
cells.
access function for single-dimensioned arrays:
address(list[k])
=address(list[lower_bound]) +
((k lower_bound) *element_size)
Access Function for a Multi-
Dimensioned Array
Compile Time
Descriptors
Record types
A recordis a possibly heterogeneous
aggregate of data elements in which the
individual elements are identified by
names.
A record type in Ada:
type Emp_Rec_Type is record
First: String (1..20);
Mid: String (1..10);
Last: String (1..20);
Hourly_Rate: Float;
end record;
Emp_Rec: Emp_Rec_Type;
Record Type
Control Flow
Structure
Control Flow
Control flow =the flow of control,
or execution sequence, in a
program.
Levels of control flow:
1. Within expressions.
2. Among program statements.
3. Among program units.
Expressions
Expressions are the fundamental
means of specifying computations in
a programming language:
1. Arithmeticexpressions.
2. Relationalexpressions.
3. Booleanexpressions.
The control flow in expression
evaluation is determined by:
1. The order of operator evaluation:
Associativity;
Precedence.
2. The order of operand evaluation
Arithmetic Expressions
Arithmetic evaluation was one of
the motivations for the
development of the first
programming languages.
Arithmetic expressions consist of:
operators;
unary, binary, ternary.
operands;
parentheses;
function calls;
Arithmetic Expression: Design
Issues
Operator precedence rules?
Operator associatively rules?
Operator overloading?
Order of operand evaluation?
Operand evaluation side effects?
Type mixing in expressions?
Operator Precedence
Rules
The operator precedence rules for
expression evaluation define the order
in which adjacent operators of
different precedence levels are
evaluated.
Typical precedence levels:
parentheses;
unary operators;
** (where supported by the language);
*, /
+,
Operator Associatively
Rules
The operator associatively rules for
expression evaluation define the order in
which adjacent operators with the same
precedence level are evaluated.
Typical associatively rules:
Left to right, except **, which is right to left.
Sometimes unary operators associate right
to left (e.g., FORTRAN).
a = 10;
/* assume that fun changes its parameter */
b = a + fun(a);
Functional Side Effects: Possible
Solutions
1. Write the language definition to disallow
functional side effects:
No two-way parameters in functions
No non-local references in functions
Advantage: it works!
Disadvantage: inflexibility of one-way
parameters and lack of non-local references
2. Write the language definition to demand
that operand evaluation order be fixed
Disadvantage: limits some compiler
optimizations
Java requires that operands appear to be
evaluated in left-to-right order
Referential Transparency
Referential Transparency: an expression can be
substituted with its value, without changing the
effects of the program.
Functional side effects violate referential transparency.
Disadvantage of coercions:
They decrease the type error detection ability of the
compiler.
Scenarios:
All numeric types are coerced in expressions, using
widening conversions (most languages).
In Ada, there are virtually no coercions in expressions.
Relational Expression
Relational Expressions
Use relational operators and operands of various types.
Evaluate to some Boolean representation.
Always lower precedence than the arithmetic
operators.
Operator symbols used vary somewhat among
languages (!=, /=, .NE., <>, #).
Disadvantages:
loss in readability.
loss in type error detection
Simple Assignment
Statements
The general syntax:
<target_var> <assign_operator> <expression>
The assignment operator:
=FORTRAN, BASIC, the C-based languages
:=ALGOLs, Pascal, Ada
Note that there is only one statement. To execute more than one
statement conditionally, ablock statementis to be used.
If-else-Statement cont
if-elsestatements can be chained in following form:if(Expression1){
Statement1a
Statement1b
...
}elseif(Expression2){
Statement2a
Statement2b
...
}elseif(Expression3){
Statement3a
Statement3b
...
}else{
Statement4a
Statement4b
...
}
The execution evaluates all conditional expressions beginning fromExpression1until the
first expression is found that evaluates totrue. Then the corresponding statement
sequence is executed, or, if none of the expressions evaluated totrue, the statement
sequence of the finalelsepart.
Do-Statement
Syntax:doStatementwhile(Expression);
Semantics: The statement is executed first and then the expression is
evaluated which must be ofbooleantype.
As long the expression remainstrue, the statement is executed
repeatedly.
if sum == 0 then
if count == 0 then
result = 0
else
result = 1
end
end