0% found this document useful (0 votes)
8 views

Runtime Environment Part 3 L12

Compiler design

Uploaded by

Addition Add
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Runtime Environment Part 3 L12

Compiler design

Uploaded by

Addition Add
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Runtime Environment

Part 3
Prepared By:
Dr. D. P. Singh
Graphic Era Deemed to be University, Dehradun
Overview Scoping
• Scope:
Scope of a variable (v) in a program is the region of the
program in which use of v refers to its value of declaration.

• Generally, short names for variables are used in the


modules/functions of the program, that’s why same name of the
variable is used in various segment of the program.

• To make variables distinct in different parts of the program scoping


is used.
Types of Scoping
1. Static or Lexical Scoping

• A variable always refers to its top level environment

• Binding of a variable can be determined by program text and


is independent of the run-time function call stack.

• Compiler first searches in current block, then in global


variables, then in successively smaller scopes.

• e.g. in C, C++, JAVA variables are statically scoped.


Static Scoping cntd…
e.g.
int p=1;
int main()
Output:
{
3
p=2;
2
{
int p;
{
p=3;
}
printf(“%d”,p);
}
printf(“\n%d”, p);
}
Static Scoping cntd…
int x = 5;
int foo()
{
return x; Output:
} 5
int too()
{
int x = 10;
return foo();
}

int main()
{
printf("%d", too());
return 0;
}
Types of Scoping cntd…
2. Dynamic Scoping

• A global identifier refers to the identifier associated with the


most recent environment

• In dynamic scoping the compiler first searches the current


block and then successively all the calling functions.

• The value of identifier depends upon, how the code


executes

• Keyword ‘local’ defines dynamically scoped local variables in


PERL
Dynamic Scoping cntd…
int p;
int main() {
p=1;
foo();
too(); Static Scoping
} Output:
void foo() { 1
int p=2;
1
hoo();
}
void too(){

int p=3;
Dynamic Scoping
hoo();
} Output:
void hoo(){ 2
printf("%d\n",p); 3
}
Parameter Passing Techniques
• Parameter Association:
• Positional
• Arguments associated with formals one by one.
• e.g. C, PASCAL, JAVA, C++
• Keyword
• e.g. Ada uses mixture
• procedure name(x,y: in real; z: in boolean)
• name(1.1,1.2, z=> true)
• name( z=>true,x=>1.1, y=>1.2)
Parameter Passing Techniques cntd…
1. Call by Value:
• At runtime prior to the call of procedure, the
parameter is evaluated and its actual value is put in a
location private to called procedure.

• Thus there is no way to change the actual parameters

• Copying of value takes execution time and space.

• e.g. C, PASACL, Ada, Algol68


Call by Value cntd…
{ a: array [1..10] of integer;
p,q : integer; 10 20
procedure foo (i,j : integer)
12 22
begin
i := i+2;
j := j+2;
end foo;
...
p := 10; Output: 10 20
q := 20;
foo(p,q);
write p,q;
}
Parameter Passing Techniques cntd…
2. Call by Reference:

• At runtime, prior to the call, the parameter is


evaluated and put in a temporary location (if it is
not a variable)

• The Address of the variable (or the temporary) is


passed to the called procedure.

• e.g. FROTRAN, PASCAL


Call by Reference cntd…
{ a: array [1..10] of integer;
p,q : integer;
procedure foo (i,j : integer)
begin
i := i+2;
10 20
j := j+2; Value updates happen in
end foo; (p) (q) storage of the caller while
callee is executing
...
p := 10; 12 22

q := 20; Output: 12 22
foo(p,q);
write p,q;
}
Parameter Passing Techniques cntd…
3. Call by Value Result

• It is hybrid of Call by Value and Call by Reference

• Actual parameters are copied to a local location of called


procedure

• Actual parameter’s values are not affected during the


execution of called procedure

• At return from the called procedure, the values of the formal


parameter are copied to the actual parameters.

• e.g. FORTRAN and Ada


Call by Value Result cntd…
e.g.
{ a: array [1..10] of integer;
p,q : integer;
procedure foo (i,j : integer) 10 20
begin
12 22
i := i+2;
j := j+2;
end foo;
...
p := 10; Output: 12 22
q := 20;
foo(p,q);
write p,q;
}
Parameter Passing Techniques cntd…

4. Call by Name

• Textual substitution of formal parameters name by


the actual parameters

• e.g. Algol
Call by Name cntd…
e.g.
procedure foo(X,I: integer)
begin
I=3;X=8;I=6;X=2;
end
The above procedure is called by the statement foo(P, Q[k]), this would
result in
begin
Q[k]=3; P=8; Q[k]=6; P=2;
end
Thank You

You might also like