Question

Assignment: Show the symbol table for the following C programs at the printf lines (a) using lexi...

Assignment:

Show the symbol table for the following C programs at the printf lines

(a) using lexical scope and (b) using dynamic scope.

What does the program print using each kind of scope rule?

Program 1:

const int b = 10;

int foo()

{

   int a = b + 10;

   return a;

}

int bar()

{

   int b = 4;

   return foo();

}

int main()

{

   printf(“foo = %d\n”,foo());

   printf(“bar = %d\n”,bar());

   return 0;

}

Program 2:

int a;

                void first()
{
                a = 12
}


void second()

                {
                int a = 6;
                first();
}

                void main()

                {
                a = 4;

                                second();

                                printf("%d\n",a);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Lexical(Static) scoping determines the value of a variable by the structure of the code that refers to variable..
while In dynamic scoping runtime stack of the program will decide the value by reference of a variable.

Program 1:-

const int b = 10;//b1
int foo()
{
int a = b + 10;//b2*+10
return a;
}

int bar()
{
int b = 4;//b2=4
return foo();
}
int main()
{
printf(“foo = %d\n”,foo());
printf(“bar = %d\n”,bar());
return 0;
}

Using lexical scoping for understanding
for global variable assign a label b1 and for local b2,
So, foo() will return 20.
But in bar() we have local b=4 and it is structured in foo()
while running foo() it will consider b2 instead of b1.
So final awnser will be
foo = 20
bar = 14

Using dynamic scoping
foo() will same as lexical scoping.
But for bar() we have b2=4 and then it will return foo() from the bar()
but as soon as it enters in the foo() value of b2 will be out of scope
and foo() will use b1=10 and will count a=20 and returns.
So, this time awnser will be
foo = 20
bar = 20


Program 2:-
int a;//a1
void first()
{
a=12;//a2*
}
void second()

{
  
int a = 6;//a2
first();
}
void main()

{
  
a = 4;
//a1
second();
  
printf("%d\n",a);//a1
}


Using lexical scoping,
Here also for understanding let us take label a1 and a2 for global and local variable.
In lexical while calling second() by structure of program in
first a assigned to 12 is local variable(a2) second().
So, a1 is not affected still.
So it will print 4.

Using Dynamic scoping,
difference is second() will call first().
This time a of second go out of scope and stack of a will refer to the global variable
which will be affected by first() and it will be 12.
So, it will print 12.

Like, please.

and comment if you have any queries.

Add a comment
Know the answer?
Add Answer to:
Assignment: Show the symbol table for the following C programs at the printf lines (a) using lexi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Consider the following programs (using C's syntax): #include <stdio.h> int a- 1, b-2; int foo...

    2. Consider the following programs (using C's syntax): #include <stdio.h> int a- 1, b-2; int foo (int x) 1 return (x+a); void ba r () { printf("%d, %d\n",a,b); void foobar) } printf("%d, %d\n", a, b) ; int a -4; bar bfoo (b); bar int main)( int b 8; foobar printf ("%d, %d\n", a, b) ; return 0; (a) What does the program print under static scoping? (b) What does the program print under dynamic scoping?

  • Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void)...

    Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void) { x = x + 2; y = y + 4; } void bar(void) { int x = 10; y = y + 3; foo( ); cout << x << endl; cout << y << endl; } void baz(void) { int y = 7; bar( ); cout << y << endl; } void main( ) { baz( ); } What output does this program...

  • 7.9 Using the second organization of the symbol table described in the text (a stack of...

    7.9 Using the second organization of the symbol table described in the text (a stack of tables), show the symbol table for the following Ada program at the three points indicated by the comments (a) using lexical scope and (b) using dynamic scope. What does the program print using cach kind of scope rule? procedure scope2 is a, b: integer; function p return integer is a: integer; begin -- point 1 a :- 0; b:- 1; return 2; end P:...

  • B) The following code is given. Determine the outputs if scoping is static and dynamic. static...

    B) The following code is given. Determine the outputs if scoping is static and dynamic. static scope output dynamic scope output #include <stdio.h> void function1(int); void function(int); int function(void); intx=10; main() { functioni(x); function2x); void function1(int y) { int x=y+5; printf("x=%d in function1 \n",function()); void function(int y){ int x=y+10; printf("x=%d in function1 \n", function()); int function(){ return x;

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • This is done in c programming and i have the code for the programs that it wants at the bottom i ...

    This is done in c programming and i have the code for the programs that it wants at the bottom i jut dont know how to call the functions Program 2:Tip,Tax,Total int main(void) {    // Constant and Variable Declarations    double costTotal= 0;    double taxTotal = 0;    double totalBill = 0;    double tipPercent = 0;    // *** Your program goes here ***    printf("Enter amount of the bill: $");    scanf("%lf", &costTotal);    printf("\n");    // *** processing ***    taxTotal = 0.07 * costTotal;    totalBill...

  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include...

    How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include void program_one (); void program_two (); void program_three (); void program_four(); void program_five(); int main() { int menu_option = 0; while (menu_option != 9) { printf(" = 1\n"); //Change this to your first program name. Nothing else. printf(" = 2\n"); //Change this to your second program name. Nothing else. printf(" = 3\n"); //Change this to your third program name. Nothing else. printf(" =...

  • 6) Show the output of the following program. You mus trace the code to show how...

    6) Show the output of the following program. You mus trace the code to show how you reached the answer. (5 points) #include <stdio.h> int x 18; int y = 5; int z ·e; void my first function) void my_second_function); int main() int ys y = x; if (z) my_first functionO else my-second-function(); x++ : if (z) my_first_function(); else my_second_function(); return e; void my_first_function) x+y); is %d\n", printf("The value of x+y in my-first-function() void my_second_function) ( x=100; is %d\n", x);...

  • I need help on this Systems review please! it's due by midnight monday. Question 1 Not...

    I need help on this Systems review please! it's due by midnight monday. Question 1 Not yet answered Points out of 1.00 Flag question Question text Using these declarations: int * numberPointers[3]; int * pointer; int number; Which of the following statements would generate a warning or error? Select one: a. number = pointer; b. *pointer = number; c. pointer = numberPointers; d. numberPointers[2] = &number; e. a., b., and d. f. a. and c. Question 2 Not yet answered...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT