4. Principle of Programming Language is a set of rules and norms
governed to communicate instructions (high level instruction or
assemble level instruction) to a machine or particularly a
computer.”
Types of Programming Language that are classified are:
• Procedural Programming Language.
• Functional Programming Language.
• Scripting Programming Language.
• Logic Programming Language.
• Object-Oriented Programming Language.
5. Why Study Programming Languages?
Increased capacity to express ideas
Improved background for choosing appropriate language
Increased ability to learn new languages
Better understanding of the significance of implementation
Better use of languages that are already known
Overall advancement of computing
6. PROGRAMMING PARADIGMS
A Programming Paradigm is a pattern of problem-solving
thoughts that underlies a particular genre of programs and
languages.
Types:-
Imperative Programming
Object-Oriented Programming
Functional Programming
Logic Programming
7. Syntax
Syntax is the study of sentence structure and the rules of
grammar
Syntax helps common users of a language understand how
to organize words so that they make the most sense.
e.g "The through pasture the chased a dog rabbit."
Using normal rules of syntax, this sentence means nothing.
But rearrange those exact words in a new order and they
make perfect syntactical sense:
"The dog chased a rabbit through the pasture."
8. Semantics
Semantics, on the other hand, is the study of the meaning of
sentences.
For instance, there's a world of difference in these two sentences:
"I robbed a bank;" and, "A bank robbed me." In the former, I
took money from a bank against their will. In the latter, they took
money from me.
e.g"The squirrel sang bumper cars."
On a pure syntax level, this sentence "makes sense" with a noun-
verb-noun structure, right?
It's only semantics that you think, how the heck does a squirrel
sing bumper cars?
10. Semantics vs. Syntax vs. Pragmatics
1. Syntax is what we use to do our best to communicate on the
most basic level.
2. Syntax is the study of sentences and phrases, and the rules of
grammar that sentences obey.
1. Semantics helps us determine if there's any meaning to be found
2. Semantics is the study of sentence meaning;
1. Pragmatics enables us to apply the correct meaning to the correct
situation.
2.pragmatics is the study of sentence meaning in context.
11. Translation Models
Grammars
– Terminal and non terminal symbols
– Grammar rules
– BNF notation
– Derivations, Parse trees and Ambiguity
Grammars for programming languages
– Types of grammars
– Regular expressions and Context-free grammars
12. Grammars
Syntax is concerned with the structure of programs. The
formal description of the syntax of a language is called
grammar
Grammars consist of rewriting rules and may be used for
both recognition and generation of sentences (statements).
-Grammars are independent of the syntactic analysis.
13. More about grammars
Word categories ,Constituents
- The boy is reading a book.
- The boy is reading an interesting book.
- The boy is reading a book by Mark Twain.
Terminal and non-terminal symbols, grammar rules
-Terminal to represent the words
- Non-terminal to represent categories of words and
constituents.
Starting symbol S represents "sentence"
These symbols are used in grammar rules
14. Example
Rule Meaning
N ->boy N is the non-terminal symbol
for "noun", "boy" is a terminal
"symbol“
D ->the | a | an D is the non-terminal symbol
for definite or indefinite articles.
NP ->D N This rule says that a noun phrase
NP may consist of an article
followed by a noun
15. BNF notation
Grammars for programming languages use a special
notation called BNF (Backus-Naur form)
The non-terminal symbols are enclosed in < > Instead of
->the symbol ::= is used
The vertical bar is used in the same way - meaning choice.
[] are used to represent optional constituents.
BNF notation is equivalent to the first notation in the
examples above.
16. BNF Example
The rule
<assignment statement>::=
<variable>=<arithmetic expression>
says that an assignment statement has
-a variable name on its left-hand side
-followed by the symbol "=",
-followed by an arithmetic expression
17. Derivations, Parse trees and Ambiguity
Using a grammar, we can generate sentences. The process
is called derivation
Example : S ->SS | (S) | ( )
generates all sequences of paired parentheses.
18. Derivation example
The rules of the grammar can be written separately:
Rule1: S ->SS
Rule2: S ->(S)
Rule3: S -> ( )
Derivation of (( )( ))
S (S) by Rule2
(SS) by Rule1
(( )S) by Rule3
- (( )( )) by Rule3
19. Parsing
Sentential forms - strings obtained at each derivation step.
May contain both terminal and non-terminal symbols.
Sentence - the last string obtained in the derivation, contains
only terminal symbols.
Parsing - determines whether a string is a correct sentence or
not. It can be displayed in the form of a parse tree
20. Ambiguity
Grammar rules can generate two possible parse trees for one
and the same sequence of terminal symbols.
Example
Rule1: If_statement if Exp then S else S
Rule2: If_statement if Exp then S
if a < b then if c < y then write(yes) else write(no);
If a < b then if c < y then write(yes) else write(no); Rule 2
If a < b then if c < y then write(yes)
else write(no); Rule 1
21. Grammars for programming languages
Four types of grammars
Regular grammars: (Type 3 )
Rule format:
A a
A aB
Context-free grammars (Type 2)
Rule format:
A any string consisting of terminals and non-terminals
23. Regular grammars and Regular expressions
Operations on strings of symbols:
concatenation - appending two strings
Kleene star operation - any repetition of the string. e.g. a* can be
a, or aa, or aaaaaaa, etc
Regular expressions: a form of representing regular grammars
Regular expressions on alphabet
∑ string concatenations combined with the symbols U and *,
possibly using '(' and ')’.
the empty expression: Ø
24. Examples
Let ∑ = {0,1}.
Examples of regular expressions are: 0,1, 010101, any
combination of 0s and 1s
generated strings:
0 U 1 0, 1
(0 U 1)1* 0, 01, 011, 0111,…, 1, 11, 111..
(0 U 1)*01 01, 001, 0001,… 1101, 1001,
(any strings that end in 01)
25. Regular languages
Context-free languages
Regular languages are languages whose sentences can be
described by a regular expression.
Regular expressions are used to describe identifiers in
programming languages and arithmetic expressions.
Context-free grammars generate context-free languages.
They are used to describe programming languages.
26. Variables
• A variable is an abstraction of a memory cell
• Variables can be characterized as a 6-tuple of
attributes:
Name: identifier
Address: memory location(s)
Value: particular value at a moment
Type: range of possible values
Lifetime: when the variable accessible
Scope: where in the program it can be accessed
27. Variables
Name - not all variables have them (examples?)
Address - the memory address with which it is
associated
A variable may have different addresses at different
times during execution
A variable may have different addresses at different
places in a program
If two (or more) variable names can be used to
access the same memory location, they are called
aliases
Aliases are harmful to readability, but they are useful
under certain circumstances
28. Variables Type and Value
Type - determines the range of values of variables and the
set of operations that are defined for values of that type;
in the case of floating point, type also determines the
precision
Value - the contents of the location with which the variable
is associated
Abstract memory cell - the physical cell or collection of
cells associated with a variable
29. lvalue and rvalue
Are the two occurrences of “a” in this expression the same?
a := a + 1;
In a sense,
• The one on the left of the assignment refers to the location
of the variable whose name is a;
• The one on the right of the assignment refers to the value
of the variable whose name is a;
• The lvalue of a variable is its address
• The rvalue of a variable is its value
30. Expressions and Assignment statement
Expressions are the fundamental means of specifying
computations in a programming language
To understand expression evaluation, need to be familiar
with the orders of operator and operand evaluation
Essence of imperative languages is dominant role of
assignment statements
31. Arithmetic Expressions
Arithmetic evaluation was one of the motivations for the
development of the first programming languages
Arithmetic expressions consist of operators, operands,
parentheses, and function calls
Design issues for arithmetic expressions
Operator precedence rules?
Operator associativity rules?
Order of operand evaluation?
Operand evaluation side effects?
Operator overloading?
Type mixing in expressions?
33. Arithmetic 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
** (if the language supports it)
*, /
+, -
34. Arithmetic Expressions: Operator Associativity
Rule
The operator associativity rules for expression evaluation
define the order in which adjacent operators with the same
precedence level are evaluated
Typical associativity rules
Left to right, except **, which is right to left
Sometimes unary operators associate right to left (e.g.,
in FORTRAN)
APL is different; all operators have equal precedence and
all operators associate right to left
Precedence and associativity rules can be overriden with
parentheses
35. 1-35
Ruby Expressions
All arithmetic, relational, and assignment operators, as well as
array indexing, shifts, and bit-wise logic operators, are
implemented as methods
- One result of this is that these operators can all
be overriden by application programs
36. Arithmetic Expressions: Conditional
Expressions
Conditional Expressions
C-based languages (e.g., C, C++)
An example:
average = (count == 0)? 0 : sum / count
Evaluates as if written like
if (count == 0)
average = 0
else
average = sum /count
37. Arithmetic Expressions: Operand Evaluation
Order
Operand evaluation order
1. Variables: fetch the value from memory
2. Constants: sometimes a fetch from memory;
sometimes the constant is in the machine language
instruction
3. Parenthesized expressions: evaluate all operands and
operators first
4. The most interesting case is when an operand is a
function call
38. Arithmetic Expressions: Potentials for
Side Effects
Functional side effects: when a function changes a two-
way parameter or a non-local variable
Problem with functional side effects:
When a function referenced in an expression alters
another operand of the expression; e.g., for a parameter
change:
a = 10;
/* assume that fun changes its parameter */
b = a + fun(&a);
39. Functional Side Effects
Two possible solutions to the problem
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
40. Overloaded Operators
Use of an operator for more than one purpose is called
operator overloading
Some are common (e.g., + for int and float)
Some are potential trouble (e.g., * in C and C++)
Loss of compiler error detection (omission of an
operand should be a detectable error)
Some loss of readability
41. Overloaded Operators (continued)
C++ and C# allow user-defined overloaded operators
Potential problems:
Users can define nonsense operations
Readability may suffer, even when the operators make
sense
42. Type Conversions
A narrowing conversion is one that converts an object to a
type that cannot include all of the values of the original
type e.g., float to int
A widening conversion is one in which an object is
converted to a type that can include at least
approximations to all of the values of the original type
e.g., int to float
43. Type Conversions: Mixed Mode
A mixed-mode expression is one that has operands of
different types
A coercion is an implicit type conversion
Disadvantage of coercions:
They decrease in the type error detection ability of the
compiler
In most languages, all numeric types are coerced in
expressions, using widening conversions
In Ada, there are virtually no coercions in expressions
44. Explicit Type Conversions
Called casting in C-based languages
Examples
C: (int)angle
Ada: Float (Sum)
Note that Ada’s syntax is similar to that of function
calls
45. Type Conversions: Errors in Expressions
Causes
Inherent limitations of arithmetic e.g.,
division by zero
Limitations of computer arithmetic e.g.
overflow
Often ignored by the run-time system
46. Relational and Boolean Expressions
Relational Expressions
Use relational operators and operands of various types
Evaluate to some Boolean representation
Operator symbols used vary somewhat among
languages (!=, /=, ~=, .NE., <>, #)
JavaScript and PHP have two additional relational
operator, === and !==
- Similar to their cousins, == and !=, except that they do
not coerce their operands
47. Relational and Boolean Expressions
Boolean Expressions
Operands are Boolean and the result is Boolean
Example operators
FORTRAN 77 FORTRAN 90 C Ada
.AND. and && and
.OR. or || or
.NOT. not ! not
xor
48. Relational and Boolean Expressions: No
Boolean Type in C
C89 has no Boolean type--it uses int type with 0 for false
and nonzero for true
One odd characteristic of C’s expressions: a < b < c
is a legal expression, but the result is not what you might
expect:
Left operator is evaluated, producing 0 or 1
The evaluation result is then compared with the third
operand (i.e., c)
49. Short Circuit Evaluation
An expression in which the result is determined without
evaluating all of the operands and/or operators
Example: (13*a) * (b/13–1)
If a is zero, there is no need to evaluate (b/13-1)
Problem with non-short-circuit evaluation
index = 1;
while (index <= length) && (LIST[index] != value)
index++;
When index=length, LIST [index] will cause an
indexing problem (assuming LIST has length -1
elements)
50. Short Circuit Evaluation (continued)
C, C++, and Java: use short-circuit evaluation for the usual
Boolean operators (&& and ||), but also provide bitwise
Boolean operators that are not short circuit (& and |)
Ada: programmer can specify either (short-circuit is specified
with and then and or else)
Short-circuit evaluation exposes the potential problem of side
effects in expressions
e.g. (a > b) || (b++ / 3)
51. Assignment Statements
The general syntax
<target_var> <assign_operator> <expression>
The assignment operator
= FORTRAN, BASIC, the C-based languages
:= ALGOLs, Pascal, Ada
= can be bad when it is overloaded for the relational
operator for equality (that’s why the C-based languages
use == as the relational operator)
53. Assignment Statements: Compound
Operators
A shorthand method of specifying a commonly needed form of
assignment
Introduced in ALGOL; adopted by C
Example
a = a + b
is written as
a += b
54. Assignment Statements: Unary
Assignment Operators
Unary assignment operators in C-based languages
combine increment and decrement operations with
assignment
Examples
sum = ++count (count incremented, added to sum)
sum = count++ (count incremented, added to sum)
count++ (count incremented)
-count++ (count incremented then negated)
55. Assignment as an Expression
In C, C++, and Java, the assignment statement
produces a result and can be used as operands
An example:
while ((ch = getchar())!= EOF){…}
ch = getchar() is carried out; the result (assigned to
ch) is used as a conditional value for the while
statement
56. Mixed-Mode Assignment
Assignment statements can also be mixed-mode
In Fortran, C, and C++, any numeric type value can be assigned to
any numeric type variable
In Java, only widening assignment coercions are done
In Ada, there is no assignment coercion
57. 57
Binding
A binding is an association, such as between an attribute and an
entity, or between an operation and a symbol
Binding time is the time at which a binding takes place.
Possible binding times:
Language design time -- e.g., bind operator symbols to
operations
Language implementation time -- e.g., bind floating point type
to a representation
Compile time -- e.g., bind a variable to a type in C or Java
Link time
Load time--e.g., bind a FORTRAN 77 variable to memory
cell (or a C static variable)
Runtime -- e.g., bind a nonstatic local variable to a memory
cell
58. Type Bindings
• A binding is static if it occurs before run time and remains
unchanged throughout program execution.
• A binding is dynamic if it occurs during execution or can
change during execution of the program.
• Type binding issues
• How is a type specified?
• When does the binding take place?
• If static, type may be specified by either an explicit or an
implicit declaration
59. Variable Declarations
An explicit declaration is a program statement used for declaring
the types of variables
Def: An implicit declaration is a default mechanism for
specifying types of variables (the first appearance of the
variable in the program)
E.g.: in Perl, variables of type scalar, array and hash begin
with a $, @ or %, respectively.
E.g.: In Fortran, variables beginning with I-N are assumed
to be of type integer.
E.g.: ML (and other languages) use sophisticated type
inference mechanisms
Advantages: writability, convenience
Disadvantages: reliability
60. Dynamic Type Binding
• The type of a variable can chance during the course of the
program and, in general, is re-determined on every
assignment.
• Usually associated with languages first implemented via
an interpreter rather than a compiler.
• Specified through an assignment statement, e.g. APL
LIST <- 2 4 6 8
LIST <- 17.3 23.5
• Advantages:
• Flexibility
• Obviates the need for “polymorphic” types
• Development of generic functions (e.g. sort)
• Disadvantages:
• High cost (dynamic type checking and interpretation)
• Type error detection by the compiler is difficult
61. Type Inferencing
Type Inferencing is used in some programming languages,
including ML, Miranda, and Haskell.
Types are determined from the context of the reference,
rather than just by assignment statement.
Legal:
fun circumf(r) = 3.14159 * r * r; // infer r is real
fun time10(x) = 10 * x; // infer x is integer
Illegal:
fun square(x) = x * x; // can’t deduce anything
Fixed
fun square(x) : int = x * x; // use explicit declaration
62. 62
Storage Bindings and Lifetime
• Storage Bindings
• Allocation - getting a cell from some pool of
available cells
• Deallocation - putting a cell back into the pool
• Def: The lifetime of a variable is the time during
which it is bound to a particular memory cell
• Categories of variables by lifetimes
• Static
• Stack dynamic
• Explicit heap dynamic
• Implicit heap dynamic
63. Static Variables
• Static variables are bound to memory cells before
execution begins and remains bound to the same
memory cell throughout execution.
• Examples:
• all FORTRAN 77 variables
• C static variables
Advantage: efficiency (direct addressing),
history-sensitive subprogram support
Disadvantage: lack of flexibility, no recursion!
64. 64
Static Dynamic Variables
Stack-dynamic variables -- Storage bindings are created for
variables when their declaration statements are elaborated.
If scalar, all attributes except address are statically bound
e.g. local variables in Pascal and C subprograms
Advantages:
allows recursion
conserves storage
Disadvantages:
Overhead of allocation and deallocation
Subprograms cannot be history sensitive
Inefficient references (indirect addressing)
65. Constant
Constant is a value that cannot be altered by the program
during normal execution, i.e., the value is constant.
When associated with an identifier, a constant is said to be
“named,” although the terms “constant” and “named constant”
are often used interchangeably.
This is contrasted with a variable, which is an identifier with
a value that can be changed during normal execution, i.e., the
value is variable
66. A constant is a data item whose value cannot change during
the program’s execution. Thus, as its name implies – the value
is constant.
A variable is a data item whose value can change during the
program’s execution. Thus, as its name implies – the value can
vary.
Constants are used in two ways. They are:
1. literal constant
2. defined constant
67. literal constant is a value you type into your program
wherever it is needed.
Examples include the constants used for initializing a
variable and constants used in lines of code:
21
12.34
'A'
"Hello world!"
false
null
68. Named Constants:
A named constant is a variable that is bound to a value only
when it is bound to storage
Advantages: readability and modifiability
Used to parameterize programs
The binding of values to named constants can be either static
(called manifest constants) or dynamic
Languages: – Pascal: literals only
– FORTRAN 90: constant-valued expressions
– Ada, C++, and Java: expressions of any kind
69. Statement-Level Control Structures
A control structure is a control statement and the
statements whose execution it controls.
•Selection Statements
• Iterative Statements
• Unconditional Branching
70. Selection Statements
Selection statement provides the means of choosing between two
or more paths of execution.
Two general categories:
o Two-way selectors
o Multiple-way selectors
Two-Way Selection Statements
The general form of a two-way selector is as follows:
if control_expression then clause else clause
71. Two-Way Selection Statements
General form:
if control_expression
then clause
else clause
Design Issues:
What is the form and type of the control expression?
How are the then and else clauses specified?
How should the meaning of nested selectors be specified?
72. The Control Expression
If the then reserved word or some other syntactic marker is
not used to introduce the then clause, the control
expression is placed in parentheses
In C89, C99, Python, and C++, the control expression can
be arithmetic.
In languages such as Ada, Java, Ruby, and C#, the control
expression must be Boolean
73. Multiple-Way Selection Statements
Allow the selection of one of any number of statements or
statement groups
Design Issues:
•What is the form and type of the control expression?
• How are the selectable segments specified?
• Is execution flow through the structure restricted to include
just a single selectable segment?
• How are case values specified?
• What is done about unrepresented expression values?
•
74. Iterative Statements
The repeated execution of a statement or compound
statement is accomplished either by iteration or recursion
General design issues for iteration control statements
1. How is iteration controlled?
2. Where is the control mechanism in the loop?
75. Counter-Controlled Loops
A counting iterative statement has a loop variable, and a
means of specifying the initial and terminal, and stepsize
values.
Design Issues:
1. What are the type and scope of the loop variable?
2. What is the value of the loop variable at loop termination?
3. Should it be legal for the loop variable or loop parameters to
be changed in the loop body, and if so, does the change
affect loop control?
4. Should the loop parameters be evaluated only once, or once
for every iteration?