Design
Design
CS F 301/ IS F301
D.C.KIRAN
BITS Pilani [email protected]
Pilani Campus
Slides are Mostly From
Text Book 1:
Programming Languages: Concepts & Constructs, 2/e
By
Ravi Sethi and K. V. Viswanatha
Reference Book 1:
Concepts of Programming Languages, 8/e
by Robert W. Sebesta
* To Implement the compiler (start now and continue learning till end of next semester)
• Preprocessors
• Assemblers
• Linkers
• Loaders
4
BITS Pilani, Pilani Campus
Developing Programming Language
• Similar to developing any software.
• Should have knowledge Data used and Environment where program runs.
• Follows all phases of Software Development Life Cycle (SDLC)
SDLC (PL-DLC)
Phase Output
• Requirements analysis • Programming Language
Requirements Specification
(PLRS)
• Design Document,
• Design
Design rules
Grammar / Syntax
Semantic
Back end
• Implementation • Writing compiler.
6
BITS Pilani, Pilani Campus
Jargons
• Language
• Statement
• Alphabets
• Grammar
• Derivation
• Language Recognition / Acceptor
English: {Apple, Ball, Cat, Doll, Egg, Fish, Girl, Him, In, Jump, Kick……….}
C: { int main()
{
printf(“hello world”);
return (0);
}
}
start < =
0 1 2 return(relop, LE)
>
3 return(relop, NE)
other
Pushback(LA)
= 4
*
return(relop, LT)
5 return(relop, EQ)
>
=
6 7 return(relop, GE)
other
8
*
return(relop, GT)
Pushback(LA)
• Language
• Statement
• Alphabets
• Grammar
• Derivation
• Language Recognition / Acceptor
( A )
A A
( ) ( A )
( )
int a=0;
if (0==0) What is the value of a after this fragment
if (0==1) a=1; executes?
else a=2;
Becomes:
<Stmt> → EwithElse | EnoElse
EwithElse → if <Logic_Expr> then EwithElse else EwithElse | other
EnoElse → if <Logic_Expr> then <Stmt>
| if <Logic_Expr> then EwithElse else EnoElse
with
Example 3:
Expression Grammar:
S→E
E → E+T
E→T
T → E-T
T→F
F → E*F
F → id
SaAb / aCb
A aBC / aCa S
B cd
C mn / pq
a A b
a
B C
c m
d n
BITS Pilani, Pilani Campus
Left-Factor
Constructs Issues
Identifiers (Naming, Binding, Scope, Lifetime)
Data Types ( Primitive and User Defined)
Expressions & (Precedence , Associativity and Evaluation)
Assignment Statements
Iterative Statements Condition vs Range
Selection Statements Two Way vs Multiple Selection
<exp>
<exp>
b c
a b
Our grammar generates this tree for a+b*c. In left tree, the addition is performed
before the multiplication, which is not the usual convention for operator precedence.
BITS Pilani, Pilani Campus
Precedence In The Grammar
Expression Grammar
<exp> <exp> + <exp>
| <exp> * <exp>
| (<exp>)
| a | b | c
To fix the precedence problem, we modify the grammar so that it is forced to put *
below + in the parse tree.
Expression Grammar
Modified Grammar
<exp> <exp> + <mulexp> | <mulexp> <exp>
<mulexp> <mulexp> * <rootexp> | <rootexp>
<rootexp> (<exp>) | a | b | c <exp>
+ <mulexp>
<mulexp>
<exp> + <mulexp>
<rootexp>
<mulexp> <rootexp>
c
<rootexp> b
a
Left-associative parse tree.
BITS Pilani, Pilani Campus
Exercise
Expression Grammar
<exp> <exp> + <mulexp> | <mulexp>
<mulexp> <mulexp> * <rootexp> | <rootexp>
<rootexp> (<exp>) | a | b | c
1.) Add a left-associative & operator, at lower precedence than any of the others
2.) Then add a right-associative ^ operator, at higher precedence than any of the
others
52
BITS Pilani, Pilani Campus
Solution
53
BITS Pilani, Pilani Campus
Boolean Expressions
54
BITS Pilani, Pilani Campus
Expression Evaluation
55
BITS Pilani, Pilani Campus
Short Circuit Evaluation
As soon as we can conclude outcome of the evaluation, skip the
remainder of it.
Church-Rosser theorem:
1. If E => F1 and E => F2, then there always exists a G such that F1 => G
and F2 => G.
2. If E has a normal form G, then there is a normal order evaluation E -> G.
Obviously, using = as the assignment operator and overloaded as the relational operator for
equality can lead to many errors and therefore a bad design decision.
<Exp_stmt> <Exp> /* include unary operations like ++count, --count, -count etc*/
<assignment_stmt> id <assignment_opr><Exp>
/ id = id <more>;<statements>
/ cond-assign>/ ε
<assignment_opr> = / *= / += / -= / /=
How do we convert?
– Reinterpret bit sequence
– Build new object
What should be allowed?
• float to int ?
• int to float ?
67
BITS Pilani, Pilani Campus
Type Conversions : Mixed mode Assignment
68
BITS Pilani, Pilani Campus
Type Conversions : Mixed mode Assignment
int m, n;
float x, y;
scanf(“%d %d”,&m,&n);
scanf(“%f %f”,&x,&y);
double double
float float
long long
int int
short byte char char short byte
70
BITS Pilani, Pilani Campus
Check List for Design
Constructs Issues
Identifiers (Naming, Binding, Scope, Lifetime)
Data Types ( Primitive and User Defined)
Expressions &
Assignment Statements
(Precedence , Associativity and Evaluation)
Type conversion √
Iterative Statements Condition vs Range
Selection Statements Two Way vs Multiple Selection
72
BITS Pilani, Pilani Campus
Condition Based
Rules for C Programming language iterative statements
75
BITS Pilani, Pilani Campus
For loop in Database Applications
76
BITS Pilani, Pilani Campus
Check List for Design
Constructs Issues
Identifiers (Naming, Binding, Scope, Lifetime)
Data Types ( Primitive and User Defined)
Expressions &
Assignment Statements
(Precedence , Associativity and Evaluation)
√
Iterative Statements Condition vs Range
√
Selection Statements Two Way vs Multiple Selection
78
BITS Pilani, Pilani Campus
Selection Statements: Evolution
There was much research and many arguments in the late 1960s and early
1970s about this issue.
One important result:
It was proven that all algorithms represented by flowcharts can be coded with only two-
way selection and pretest logical loops.
So the unconditional branch statement and its inherent problems was superfluous.
Other constructs have been added for readability/writability.
• For statement; post test loop…
• Need to balance…
– Increase in writability vs. decrease simplicity, size and readability.
79
BITS Pilani, Pilani Campus
Selection Statements
80
BITS Pilani, Pilani Campus
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?
81
BITS Pilani, Pilani Campus
Two-Way Selection Statements
82
BITS Pilani, Pilani Campus
Two-Way Selection Statements
If x > y :
x = y;
print “case 1”
83
BITS Pilani, Pilani Campus
Nesting Selectors
Java example
if (sum == 0)
if (count == 0)
result = 0;
else result = 1;
Which if gets the else?
Java's static semantics rule: else matches with
the nearest if
84
BITS Pilani, Pilani Campus
Nesting Selectors (continued)
Indentation as in Python..
• Python Example:
if sum == 0 :
if count == 0 :
result = 0
else:
result = 1
end
Use of a special word to mark then end of the selection construct, as in Fortran 95, Ada and Ruby.
• Ruby Example:
if sum == 0 then
if count == 0 then
result = 0
end
else
result = 1
end 86
BITS Pilani, Pilani Campus
Multiple-Way Selection Statements
87
BITS Pilani, Pilani Campus
Multiple-Way Selection: Examples
88
BITS Pilani, Pilani Campus
Multiple-Way Selection: Examples
90
BITS Pilani, Pilani Campus
Multiple-Way Selection: Examples
91
BITS Pilani, Pilani Campus
Multiple-Way Selection Using if
Multiple Selectors can appear as direct extensions to two-way selectors, using else-if clauses.
Ada example:
if ...
then ...
elsif ...
then ...
elsif ...
then ...
else ...
end if
Python example:
if count < 10 :
bag1 = True
elif count < 100 :
bag2 = True
elif count < 1000 :
bag3 = True
Ruby example:
case
when count < 10 then bag1 = True
when count < 100 then bag2 = True
when count < 1000 then bag3 = True
92
BITS Pilani, Pilani Campus
Grammar for Selection In C Language
• Goto label
• Continue
• Break
• Return
Inside the for statement.
Jump Statement
<reversed> reverse / ε
<Compound_stmt> { <statements> } / <statements>
BITS Pilani, Pilani Campus
Modify grammar to add selection Statements
If ( Cond_exp) If ( Cond_exp)
If ( Cond_exp) If ( Cond_exp)
{ {
<statement> <statement>
<statements> <statements>
else else
} }
{ <statement>
else else
<statements>
{ <statement>
}
<statements>
}
If ( Cond_exp)
If ( Cond_exp):
then
<statements>
<statements>
endif
else
else
then
<statements>
<statements>
endelse
BITS Pilani, Pilani Campus
<Selection_stmt> if (cond_exp) { <statements> } else { <statements>}
/ if(cond_exp) { <statements> }
/ if (cond_exp) <statement> else { <statements>}
/ if(cond_exp) <statement>
/ if (cond_exp) { <statements> } else <statement>
/ if (cond_exp) <statement> else <statement>
If ( Cond_exp) If ( Cond_exp)
{ {
<statements> <statements>
} }
else if (Cond_exp)
{ else if (Cond_exp)
<statements> <statement>
}
else if (Cond_exp) else if (Cond_exp)
{ {
<statements> <statements>
} }
else else
{ {
<statements> <statements>
} }
Constructs Issues
Identifiers (Naming, Binding, Scope, Lifetime)
Data Types ( Primitive and User Defined)
Expressions &
Assignment Statements
(Precedence , Associativity and Evaluation)
√
Iterative Statements Condition vs Range
√
Control Statements Control Statements.
√
• Introduction
• Variables (Name, Address, type, value)
• The Concept of Binding
• Scope (Static vs Dynamic)
• Type Checking
• Type Equivalence
• Referencing Environments
• Named Constants
110
BITS Pilani, Pilani Campus
Introduction
111
BITS Pilani, Pilani Campus
Variables Attributes –
(Name, Address, Value, Type, Lifetime, Scope)
– Case sensitivity
112
BITS Pilani, Pilani Campus
Length
115
BITS Pilani, Pilani Campus
Variables Attributes –
(Name, Address, Value, Type, Lifetime, Scope)
116
BITS Pilani, Pilani Campus
Variables Attributes –
(Name, Address, Value, Type, Lifetime, Scope)
Type –
Determines the range of values of variables
and
the set of operations that are defined for values of that type;
Type Binding
How is a type specified?
When does the binding take place?
117
BITS Pilani, Pilani Campus
First -- The Concept of Binding…
118
BITS Pilani, Pilani Campus
Possible Binding Times
119
BITS Pilani, Pilani Campus
Binding Times Static vs Run time
Static
int m, n;
float x, y;
scanf(“%d %d”,&m,&n);
scanf(“%f %f”,&x,&y);
Dynamic Static
void function(m,n,x,y) void function(int m, int n, float x, float y)
{ {
OP1= m+n; OP1= m+n;
OP2 = x*y; OP2 = x*y;
OP3 = x*m; OP3= x*m;
} }
Explicit
int m, n;
float x, y;
scanf(“%d %d”,&m,&n);
scanf(“%f %f”,&x,&y);
124
BITS Pilani, Pilani Campus
Variables Attributes –
(Name, Address, Value, Type, Lifetime, Scope)
Lifetime
• Period of time when location is allocated to program
Scope
• Region of program text where declaration is visible
– Inner declaration of x hides outer: “hole in scope”
127
• The memory manager has one large chunk of memory from the
operating system that it can manage for the application
program
• Allocation – when a program requests memory for a variable or
an object (anything requiring space), the memory manager
gives it the address of a chunk of contiguous heap memory
– if there is no space big enough, can request the operating
system for virtual space
– if out of space, inform the program
• De-allocation – returns de-allocated space to the pool of free
space
– doesn’t reduce the size of space and return to the operating
system
128
130
131
BITS Pilani, Pilani Campus
Variables Attributes –
(Name, Address, Value, Type, Lifetime, Scope)
• Static
Bound to memory cells before program execution begins and
remains bound to the same memory cell throughout execution.
• C and C++ static variables
–Advantages:
• efficiency – static constants in Java
• efficiency - (direct addressing) and no run-time
allocate/deallocate
• history-sensitive support
132
BITS Pilani, Pilani Campus
Variables Attributes –
(Name, Address, Value, Type, Lifetime, Scope)
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.
• Local variables of a method in Java
Advantages
allows recursion
conserves storage
Disadvantages
Overhead of allocation and de-allocation
Subprograms cannot be history sensitive
Inefficient references (indirect addressing)
133
BITS Pilani, Pilani Campus
Variables Attributes –
(Name, Address, Value, Type, Lifetime, Scope)
Explicit heap-dynamic
allocated and de-allocated by explicit directives, specified by the
programmer, which take effect during execution
Referenced only through pointers or references
• dynamic objects in C++ (via new and delete)
int *intnode;
…
intnode = new int;
…
Delete intnode;
• Objects in Java
–Advantage: provides for dynamic storage management
–Disadvantages: inefficient and can be unreliable due to the
complexities of storage management
134
BITS Pilani, Pilani Campus
Variables Attributes –
(Name, Address, Value, Type, Lifetime, Scope)
Implicit heap-dynamic
Bound to heap storage only when assigned a value
• allocation and de-allocation caused by assignment
statements
– all variables in APL; all strings and arrays in Perl,
JavaScript, and PHP
» Highs = [74, 80, 85, 90]
135
BITS Pilani, Pilani Campus
Classify the variables in the program according to their Lifetime
137
BITS Pilani, Pilani Campus
Activation Tree
main()
{ …;
g(x);
main Global area
return(0); }
activation
record for
void g(int m) main
{ … g(2) activation
f(y); … record for
g(2)
g(y);
activation
activation
… f(1) g(1) record
recordforfor
} g(1)
f(1)
activation
void f(int n) record for
g(1) g(1)
{ g(n);
…
}
138
BITS Pilani, Pilani Campus
Finding the factorial of 3
fact(1)
1
fact(2) fact(2) fact(2)
2
fact(3) fact(3) fact(3) fact(3) fact(3)
6
main() main() main() main() main() main()
else return (3 * factorial else return (2 * factorial else return (1 * factorial (0));
(2)); (1));
High address
Parameter &
Heap Return value
Control link & Activation
Saved status Record
Low address
Temporaries temporaries
… [1]
}
[0]
142
BITS Pilani, Pilani Campus
void main() ARI: Activation Record Instance
{
int a,b,c;
c=sum(a,b);
print(c);
}
int f(int p)
{ NIL 1008 sp
int i; return value 1007
for(i=0;i<5;i++) c 1006
{ b 1005
a 1004
ARI of function main
p= p+i;
} NIL 1003
} NIL 1001
fp return global env 1000
Call Sequence in Stack-based Environments
• Calling sequence
– Store return address
– Push fp as control link
– Copy sp to fp
– Push arguments
– Reserve space for local
variables
• Return sequence
– Copy fp to sp
– Load control link into fp
– Jump to return address
– Change sp
144
void main()
{
int a,b,c;
c=sum(a,b);
print(c);
}
NIL 1016 sp
int sum(int m, int n)
return value 1015
{
res 1014
int res;
n 1013
res = m+f(n);
m 1012
return(res);
NIL 1011
} ARI of function sum
1000 1010
NIL 1008
{
return value 1007
int i;
c 1006
for(i=0;i<5;i++)
b 1005
{
a 1004 ARI of function main
p= p+i;
NIL 1003
}
NIL 1002
return(p):
NIL 1001
}
return global env 1000
void main() temp 1023
sp
{ return value 1022
int a,b,c; i 1021
c=sum(a,b); p 1020
print(c); NIL 1019 ARI of function f
} 1009 1018
return(res); m 1012
The non local variables (free variables) of a program unit are those that
are visible but not declared there
147
BITS Pilani, Pilani Campus
Static Scope
151
BITS Pilani, Pilani Campus
For Dynamic Scoping check the visibility of Non-local variable
void main()
fun2
{
int a,b,c,d,e,f; fun1
fun1() main a b c d e f
}
int fun1( )
{
int res, a,b,g,h,i,j;
Fun2()
}
int fun2( )
{
int a,b,g,l,m,n;
Draw run time stack
}
void main()
fun2
{
int a,b,c,d,e,f; fun1 a b g h i j c d e f
fun1() main a b c d e f
}
int fun1( )
{
int res, a,b,g,h,i,j;
Fun2()
}
int fun2( )
{
int a,b,g,l,m,n;
void main()
fun2 a b g l m n c d e f h i j
{
int a,b,c,d,e,f; fun1 a b g h i j c d e f
fun1() main a b c d e f
}
int fun1( )
{
int res, a,b,g,h,i,j;
Fun2()
}
int fun2( )
{
int a,b,g,l,m,n;
void main()
fun2
{
int a,b,c,d,e,f; fun1
fun1() main
fun2()
}
int fun1( )
{
int res, a,b,g,h,i,j;
}
int fun2( )
{
int a,b,g,l,m,n;
Draw run time stack
Fill the visible variables
}
Calculate the address of non local variable
void main()
fun2 a b g l m n c d e f
{
int a,b,c,d,e,f; fun1 a b g h i j c d e f
fun1() main a b c d e f
fun2()
}
int fun1( )
{
int res, a,b,g,h,i,j;
}
int fun2( )
{
int a,b,g,l,m,n;
Draw run time stack
Fill the visible variables
}
Calculate the address of non local variable
int i = 10;
void fun1() {
printf(“Inside fun1…%d\n”, i);
}
Static Scope
void fun2() { Inside fun1… 10
int i = 20;
Inside fun1… 10
fun1();
} Inside fun1… 10
int main() {
fun1(); Dynamic Scope
fun2(); Inside fun1… 10
fun1();
} Inside fun1… 20
Inside fun1… 10
var a : integer;
procedure first
a := 1;
procedure second
var a : integer;
first();
begin
a := 2;
second();
write_integer(a);
end;
158
BITS Pilani, Pilani Campus
Example 2: Static Scope
var a : integer;
procedure first
a := 1; var a : integer;
main()
a := 2;
procedure second second()
var a : integer; var a : integer;
first(); first()
a := 1;
write_integer(a);
begin
a := 2;
second(); The program prints 1
write_integer(a);
end;
159
BITS Pilani, Pilani Campus
Example 2: Dynamic Scope
var a : integer;
procedure first
a := 1; var a : integer;
main()
a := 2;
procedure second second()
var a : integer; var a : integer;
first(); first()
a := 1;
write_integer(a);
begin
a := 2;
second(); The program prints 2
write_integer(a);
end;
160
BITS Pilani, Pilani Campus
Example 3: Static vs. Dynamic Scope
int n = 1;
print_plus_n(int x)
{
Print( x + n);
}
increment_n()
{
n = n + 2;
print_plus_n(n);
}
Static Scoping
main() {
8 6 50
int n;
n = 200;
Dynamic Scoping
print_plus_n(7);
207 104 52
n = 50;
increment_n();
Print( n);
}
void Q() {
x = x + 1;
if (x < 23) Q();
else P();
}
void R() {
int x = 20; Dynamic Scoping
Q(); 23
P(): 23
} 10
void main() {
int x = 10;
R();
P();
}
163
BITS Pilani, Pilani Campus
Using Shallow Access to Implement Dynamic Scoping
void subC() {
int x, z;
x = u + v;
}
void subB() {
int w, x; ...
}
A B
Stacks
void subA() { A C A
int v, w; ...
Main Main B C A
}
int main() { Variables u v x z w
int v, u; ...
}
main calls subA Names in the stack cells indicate the
subA calls subA program units of the variable declaration
subA calls subB
subB calls subC
164
BITS Pilani, Pilani Campus
Blocks
165
BITS Pilani, Pilani Campus
Implementing Blocks
• Two Methods:
1. Treat blocks as parameter-less subprograms that are
always called from the same location
– Every block has its own activation record; an
instance is created every time the block is executed
2. Since the maximum storage required for a block can be
statically determined, this amount of space can be
allocated after the local variables in the activation
record. Blocks that are not active at the same time can
reuse space.
166
BITS Pilani, Pilani Campus
Static Link
void main() {
int x,y,z,m; f, g while 3
while(…) ---1
{ d, e, m while 2
int a,b,c,m;
…………… a ,b, c, m while 1
while(…)-2
{ x, y, z, m main
int d,e,m;
d = b + x + m;
}
Statically visible variables
}
While 3 F G X Y Z M
while(………)-3
{ While 2 D E M A B C X Y Z
int f,g; While 1 A B C M X Y Z
f=b + x + m; Main X Y Z M
}
}
BITS Pilani, Pilani Campus
Static Chain- for Nested Scoping
168
BITS Pilani, Pilani Campus
void main() {
int x,y,z;
while(…)
e
{
int a,b,c; d
…………… Block c
while(…) Variables b and g
{ a and f
int d,e; z
…… Local
y
} Variables
x
}
while(………)
{
int f,g;
}
}
BITS Pilani, Pilani Campus
Nested Subprograms
procedure A is
procedure B is
procedure C is
...
end; -- of C
procedure D is
...
end; -- of D
end; -- of B
end; -- of A
170
BITS Pilani, Pilani Campus
Example Pascal Program
program MAIN_2; Static Structure of the program
var X : integer;
procedure BIGSUB;
var A, B, C : integer;
procedure SUB1;
var A, D : integer;
begin { SUB1 }
A := B + C; ----1
end; { SUB1 }
procedure SUB2(X : integer);
var B, E : integer;
procedure SUB3;
var C, E : integer;
begin { SUB3 }
SUB1; VS
E := B + A: ----- 2
end; { SUB3 }
begin { SUB2 } Call sequence for MAIN_2
SUB3;
A := D + E; --------3
end; { SUB2 } MAIN_2 calls BIGSUB
begin { BIGSUB }
SUB2(7);
BIGSUB calls SUB2
end; { BIGSUB } SUB2 calls SUB3
begin
BIGSUB; SUB3 calls SUB1
end; { MAIN_2 }
171
BITS Pilani, Pilani Campus
Example Pascal Program
program MAIN_2;
var X : integer;
procedure BIGSUB;
var A, B, C : integer;
procedure SUB1; Statically visible variables
var A, D : integer;
begin { SUB1 } SUB 3 C E B A X
A := B + C; ----1
end; { SUB1 } SUB2 B E A C X
procedure SUB2(X : integer);
var B, E : integer; SUB1 A D B C X
procedure SUB3;
var C, E : integer; BIGSUB A B C X
begin { SUB3 }
SUB1; Main_2 X
E := B + A: ----- 2
end; { SUB3 }
begin { SUB2 }
SUB3;
A := D + E; --------3 Call sequence for MAIN_2
end; { SUB2 }
begin { BIGSUB }
SUB2(7); MAIN_2 calls BIGSUB
end; { BIGSUB }
begin BIGSUB calls SUB2
BIGSUB; SUB2 calls SUB3
end; { MAIN_2 }
SUB3 calls SUB1
172
BITS Pilani, Pilani Campus
Example Pascal Program
program MAIN_2;
var X : integer;
procedure BIGSUB; Dynamically visible variables
var A, B, C : integer;
procedure SUB1; SUB 1 A D C E X B
var A, D : integer;
begin { SUB1 }
SUB3 C E B A X
A := B + C; ----1 SUB2 B E A C X
end; { SUB1 }
procedure SUB2(X : integer);
var B, E : integer;
procedure SUB3; BIGSUB A B C X
var C, E : integer;
begin { SUB3 } Main_2 X
SUB1;
E := B + A: ----- 2
end; { SUB3 }
begin { SUB2 } Call sequence for MAIN_2
SUB3;
A := D + E; --------3
end; { SUB2 } MAIN_2 calls BIGSUB
begin { BIGSUB }
SUB2(7);
BIGSUB calls SUB2
end; { BIGSUB } SUB2 calls SUB3
begin
BIGSUB; SUB3 calls SUB1
end; { MAIN_2 }
173
BITS Pilani, Pilani Campus
D
procedure A is
procedure B is Let static depth of main procedure is
0.
procedure C is
A’s static depth is 0
...
B’s static depth is 1
end; -- of C
C’s static depth is 2
procedure D is
D’s static depth is 2
...
end; -- of D
end; -- of B
end; -- of A
175
BITS Pilani, Pilani Campus
Chain_offect/ nesting_depth
procedure A is
procedure B is • If procedure C reference a
procedure C is variable declared in A, The
chain_offcect of the
... reference would be 2.
end; -- of C
• If Procedure C references a
procedure D is variable declared in B, The
... chain_offect of that
reference would be 1.
end; -- of D
• Reference to local is 0
end; -- of B
end; -- of A
177
BITS Pilani, Pilani Campus
D
Solution
a. 33
b. 26
Y= 11 ARI of function F
X=3
Y= 11 ARI of function G
X=2
Y= 13 Block 3
Y= 11
Block 2
X =3
Y= 7
Z= ? Block 1
Y= 13 ARI of function F
X=2
Y= 13 ARI of function G
X=2
Y= 13 Block 3
Y= 11
Block 2
X =3
Y= 7
Z= ? Block 1
}
Statically visible variables
}
While 3 F G X Y Z M
while(………)-3
{ While 2 D E M A B C X Y Z
int f,g; While 1 A B C M X Y Z
f=b + x + m; Main X Y Z M
}
}
BITS Pilani, Pilani Campus
Static Link
void main() {
int x,y,z,m; f, g, m while 3
while(…) ---1
{ d, e, m while 2
int a,b,c,m;
…………… a ,b, c, m while 1
while(…)-2
{ x, y, z, m main
int d,e,m;
d = b + x + m;
}
Static Chain
}
While 3 F(0,1) B X(1,1) M(1,4)
while(………)-3
Static
{
error
int f,g;
While 2 D(0,1) B(1,2) X (2,1) M (0,3)
f=b + x + m;
}
}
BITS Pilani, Pilani Campus
Tutorial 3
int n = 1;
print_plus_n(int x)
{
Print( x + n);
}
increment_n()
{
n = n + 2;
print_plus_n(n);
}
main() {
int n; NIL
n = 200; return value
print_plus_n(7); n=200
n = 50; NIL
increment_n(); NIL
Print( n); return global env Global n=1
} Main
BITS Pilani, Pilani Campus
What is the output when Static Scoping is followed?
What is the output when Dynamic Scoping is followed?
int n = 1;
print_plus_n(int x)
{
Print( x + n);
} NIL
increment_n() return value
{ x =7
n = n + 2; NIL
print_plus_n(n); NIL
} return Main
main() { Print_plus
int n; NIL
n = 200; return value
print_plus_n(7); n= 200
n = 50; NIL
increment_n(); NIL
Print( n); return global env Global n=1
} Main
BITS Pilani, Pilani Campus
What is the output when Static Scoping is followed?
What is the output when Dynamic Scoping is followed?
int n = 1;
print_plus_n(int x)
{
Print( x + n);
}
increment_n()
{
n = n + 2;
print_plus_n(n);
}
main() {
int n; NIL
print_plus_n(7); n =50
n = 50; NIL
increment_n(); NIL
int n = 1;
print_plus_n(int x)
{
Print( x + n);
} NIL
{ n
n = n + 2; NIL
print_plus_n(n); NIL
} return Main
main() { Increment_n
int n; NIL
n = 200; return value
print_plus_n(7); n = 52
n = 50; NIL
increment_n(); NIL
Print( n); return global env Global n=1
} Main
BITS Pilani, Pilani Campus
What is the output when Static Scoping is followed?
What is the output when Dynamic Scoping is followed?
NIL
return value
x = 52
int n = 1; NIL
print_plus_n(int x) NIL
{ return
increment_n
Print( x + n);
Print_plus
} NIL
{ n
n = n + 2; NIL
print_plus_n(n); NIL
} return Main
main() { Increment_n
int n; NIL
n = 200; return value
print_plus_n(7); n= 52
n = 50; NIL
increment_n(); NIL
Print( n); return global env Global n=1
} Main
BITS Pilani, Pilani Campus
What is the output when Static Scoping is followed?
What is the output when Dynamic Scoping is followed?
int n = 1;
print_plus_n(int x)
{
Print( x + n);
} NIL
{ n
n = n + 2; NIL
print_plus_n(n); NIL
} return Main
main() { Increment_n
int n; NIL
n = 200; return value
print_plus_n(7); n= 52
n = 50; NIL
increment_n(); NIL
Print( n); return global env Global n=1
} Main
BITS Pilani, Pilani Campus
What is the output when Static Scoping is followed?
What is the output when Dynamic Scoping is followed?
int n = 1;
print_plus_n(int x)
{
Print( x + n);
}
increment_n()
{
n = n + 2;
print_plus_n(n);
}
main() {
int n; NIL
n = 200; return value
print_plus_n(7); n= 52
n = 50; NIL
increment_n(); NIL
Print( n); return global env Global n=1
} Main
BITS Pilani, Pilani Campus
What is the output when Static Scoping is followed?
What is the output when Dynamic Scoping is followed?
int n = 1;
print_plus_n(int x)
{
Print( x + n);
}
increment_n()
{
n = n + 2; Static Scoping
print_plus_n(n); 8 6 50
}
main() { Dynamic Scoping
int n; 207 104 52
n = 200;
print_plus_n(7);
n = 50;
increment_n();
Print( n);
}
BITS Pilani, Pilani Campus
What is the output when Static Scoping is followed?
What is the output when Dynamic Scoping is followed?
int n = 1;
print_plus_n(int x)
{
Print( x + n);
}
increment_n()
{
n = n + 2;
Main
print_plus_n(n);
n=
}
200
main() {
int n; Print_plus Global
n = 200; x=7 n =1
x n
print_plus_n(7);
n = 50;
increment_n();
Print( n);
}
BITS Pilani, Pilani Campus
What is the output when Static Scoping is followed?
What is the output when Dynamic Scoping is followed?
int n = 1;
print_plus_n(int x)
{
Print( x + n);
}
increment_n()
{
n = n + 2;
Main
print_plus_n(n);
n =50
}
main() { Global n=1
int n;
n = 200; x n
print_plus_n(7);
n = 50;
increment_n();
Print( n);
}
BITS Pilani, Pilani Campus
What is the output when Static Scoping is followed?
What is the output when Dynamic Scoping is followed?
int n = 1;
print_plus_n(int x)
{
Print( x + n);
}
increment_n()
{
n = n + 2;
Main
print_plus_n(n);
n =52
}
main() { Print_plus Global n=1
int n; x =52
n = 200; x
print_plus_n(7); n
n = 50;
increment_n();
Print( n);
}
BITS Pilani, Pilani Campus
What is the output when Static Scoping is followed?
What is the output when Dynamic Scoping is followed?
int n = 1;
print_plus_n(int x)
{
Print( x + n);
}
increment_n()
{
n = n + 2;
Main
print_plus_n(n);
n =52
}
main() { Global n=1
int n;
n = 200; x
print_plus_n(7); n
n = 50;
increment_n();
Print( n);
}
BITS Pilani, Pilani Campus
What is the output when Static Scoping is followed?
What is the output when Dynamic Scoping is followed?
void Q() {
x = x + 1;
if (x < 23) Q();
else P();
}
void R() {
int x = 20; Dynamic Scoping
Q(); 23
P(): 23
} 10
void main() {
int x = 10;
R();
P();
}
Constructs Issues
Identifiers (Naming, Binding, Scope, Lifetime)
√
Data Types ( Primitive and User Defined)
Expressions &
Assignment Statements
(Precedence , Associativity and Evaluation)
√
Iterative Statements Condition vs Range
√
Control Statements Control Statements.
√
E1 E + T
E
ET
TT*F
TF E + T
F id
F(E) T T * F
F F id
id id
•Attributes of Variable
(Names, Address, Value, Type, Scope, Lifetime)
•Types
•Type Binding
• Type Conversion and Coercion
•Type Checking
•Strong Typing vs Weak Typing
•Type Equivalence
203
BITS Pilani, Pilani Campus
Why Typing?
Offer Protection.
E.g. Float value stored in integer location can lead to loss of
data –
can be prevented by compiler / run-time system that checks for
type matching.
E1 E + T
E
ET
TT*F
TF E + T
F id
F(E) T T * F
F F id
id id
E1 E + T
E {24.2, real}
ET
T1 T * F
TF {2,int} E + T {12.2, real}
F id
F(E) {2,int} T {3,int} T * F {4.2,
real}
{place} id id {place}
Functions:
•Treat functions in a programming language as mapping from a
domain type D to a range type R.
•The type of a function can be denoted by the type expression
D→R where D are R type expressions.
•Ex: int→int represents the type of a function which takes an
int value as parameter, and its return type is also int.
Syntax Directed.
• Type equivalence
– When are the types of two values the same?
• Type compatibility
– When can a value of type A be used in a context that
expects type B?
• Type inference
– What is the type of an expression, given the types of the
operands?
• Overloading
– Arithmetic operators
2 + 3 means integer addition
2.0 + 3.0 means floating pt addition
– Language may have an arithmetic operator table,
telling which type of operator will be used based on
the expressions
• Type coercion
– If x is float, is x=3 acceptable?
• Disallow
• Allow and implicitly convert 3 to float.
• "Allow" but require programmer to explicitly convert 3 to float
• How do we convert?
– Reinterpret bit sequence
– Build new object
– What should be allowed?
• float to int ?
• int to float ?
219
BITS Pilani, Pilani Campus
Conversion and Coercion
• The typeEqual comparison is commonly extend to allow
arithmetic expressions of mixed type and for other cases of
types which are compatible, but not equal
– If mixed types are allowed in an arithmetic expression, then
a conversion should be inserted (into the AST or the code)
2 * 3.14 becomes code like
t1 = float ( 2)
t2 = t1 * 3.14
220
BITS Pilani, Pilani Campus
Type Equivalence
• Structural Equivalence
• Name Equivalence
typedef struct {
typedef struct {
int b;
int a;
int a;
int b;
typedef struct { int a, b; } foo1; } foo2; } foo3;
222
BITS Pilani, Pilani Campus
Equivalence Algorithm (equiv):
w.r.t Alias
int a[10],b[10];
int d[10];
a and b are equivalent in pascal and C.
d is not equivalent to a and b in pascal but it is equal in C.
float b; float y;
} structure1; } structure2;
celsius c;
fahrenheit f;
………………
……………..
c=f; /*probably an error*/
……………..
……………..
Constructs Issues
Identifiers (Naming, Binding, Scope, Lifetime)
√
Data Types ( Primitive and User Defined)
√
Expressions &
Assignment Statements
(Precedence , Associativity and Evaluation)
√
Iterative Statements Condition vs Range
√
Control Statements Control Statements.
√