Question: Consider the following pseudocode: x: integer - - global procedure set _ x ( n: integer ) x: = n procedure print _ x (

Consider the following pseudocode:
x: integer -- global
procedure set_x(n: integer)
x:= n
procedure print_x()
write_integer(x)
procedure first(n: integer)
set_x(1)
x:= n
print_x()
procedure second()
x: integer
set_x(2)
first(x)
print_x()
set_x(0)
first(x)
print_x()
second()
print_x()
What does this program print if the language uses static scoping? What does it print with dynamic scoping? Explain why.
Answer:
---------------------------------------------------------
5.(2 marks) Consider the following code written in a language that implements nested subroutines and static scoping rules:
procedure main()
a : integer :=5
b: integer :=10
procedure middle()
a: integer := b -1
b: integer :=1
a := a + b
print a, b
procedure inner()
b: integer := a -1
middle()
print a, b
--body of main
inner()
print a, b
What does the program print (or will the compiler identify static semantic errors) if we assume that names can be declared in any order and their scope is the entire block in which they are declared?
Answer:
----------------------------------------------------------------------------------------
3.(5 marks) Consider the following grammar given in BNF notation:
::=
::= a
: a | b | b
::= c b | b
Which of the following sentences are in the language generated by this grammar? For sentences in the language, draw the corresponding parse tree. Otherwise, explain why the sentence cannot be generated using the rules of the grammar.
Is the grammar LL(1)? Explain why or why not.
a) baba
b) abab
c) cbaba
d) bbbcba
Answer:
----------------------------------------------------------------------------------
Consider the following program written in generic pseudocode:
int h =1;
int a[4]={-4,-6,0,-3};
void foo(int u, int v){
v = u;
h =0;
a[1]= a[h];
h =2;
u =0;
print u;
print v;
print h;
}
int main(){
int h =3;
foo(a[3], a[h]);
print h;
print a[0];
print a[1];
print a[2];
print a[3];
}
What is the output produced by the above program if we assume call by name as the underlying mechanism for passing parameters. Explain your answer.
Answer:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!