Professional Documents
Culture Documents
TPL Assignment 3 New
TPL Assignment 3 New
5. After language design and implementation, what are the four times
bindings can take place in a program?
Ans: Compile time, load time, link time, run time.
(ii) Stack-dynamic:
Which its storage bindings are created when their declaration statements
are expanded but whose types are statically related.
Advantages:
maintains memories, recurrence.
Disadvantages:
In comparison to static, stack-dynamic is less effective in terms of running
time. There are no subprograms that are adaptive to the user's background.
x = 21;
int x;
x = 42;
Run the program and explain the results. Rewrite the same code in
C++ and Java and compare the results
Ans: main(void)
{
x = 21;
int x;
x = 42;
}
C++:
main(void)
{
x = 21;
int x;
x = 42;
}
Q2: Write three functions in C or C++: one that declares a large array
statically, one that declares the same large array on the stack, and one
that creates the same large array from the heap. Call each of the
subprograms a large number of times (at least 100,000) and output the
time required by each. Explain the results.
Ans:
#include <iostream>
#include <time.h>
void static_array(void)
{
static int my_array1[200000];
}
void stack_array(void)
{
int my_array2[200000];
}
void heap_array(void)
{
int *my_arr3 = (int *) malloc(200000 * sizeof(int));
}
start = clock();
end = clock();
double time_req_2 = ((double) (end - start)) / CLOCKS_PER_SEC;
start = clock();
Q3: Given the following program of a language called crypt. What is the
output of the program if dynamic scoping is used? What is the output
if static scoping is used? Main is the entry point in the program:
Program Output dynamic Output Static
scoping scoping
procedure main
{ In sub1 X is 31 In sub1 X is 21
integer X=10;
procedure sub1 In sub2 X is 32 In sub2 X is 32
{
X = X + 1; In sub1 X is 21 In sub1 X is 21
print (“In sub1 X is “, X);
X = 5; In main X is 20 In main X is 20
}
procedure sub2
{
integer X = 30;
Call sub1;
X=X+2;
print (“In sub2 X is “, X);
}
X = 20;
Call sub2
Call sub1
print (“In main X is “, X);
}
Q4: Consider the following grammar and remove the direct/indirect left
recursion (if any) with all intermediate steps:
Ans:
A → B | a | CBD
B→ C|b
C → bEF | aEF | cEF
E → BDE | epsilon
F → EF | epsilon
D→ d