361 views

$Q_1 \Rightarrow$ For synthesized attribute, value is only computed when symbol is on the _____ side of production. For inherited attribute, value is computed in productions where symbol is on the _____ side.

$1.$ Left, Right   $2.$ Right, Right    $3.$ Right, Left     $4.$ Left, Left

$Q_2 \Rightarrow$ In static scoping undefined variable

$a).$ Search in upper block
$b).$ Search where the function is called
$c).$ Refers to global variable
$d).$ None of these

edited | 361 views
0
0
+4

what synthesized says

Left side take prodution take value from right side of pruction

E.value $\rightarrow$ E.num + Enum

here value is synthesizes attribute. value is calculate based on num.

what inherited says

right side take prodution take value from right side of pruction

E.num $\rightarrow$ E.value + Ecount

here count is inherited  attribute. value(inherited) takes values from num and count . count take values from value and num .

+1

@mcjoshi, C language is lexical (static scope), right ?

If in a scope, variable is not defined, it Refers to global variable.

0

@Kapil, In static scope If variable is not present in local scope, we then refer to outer block and so-on. And if not present in outer block, we go to global if it now found anywhere. rt?? See this

@Anirudh What do they mean by symbol and value in question?

+1

A -->  A1 * B { A.val = A1.val * B.val } - For synthesized

+1
IN STATIC SCOPING UNDEFINED VARIABLE IS REFERED FROM GLOBAL VALUE.

IN DYNAMIC SCOPNG UNDEFINED VARIABLE IS REFERED FROM UPPER SEARCH BLOCK
+2
@mcjoshi, yes exactly. This is how it is different from dynamic scoping (Perl or Lisp), in which if variable is not defined then it takes the value of the variable which is present in the scope of calling function.
+1
And, In dynamic scope we go on tracing calling function till we find the variable.

@Kapil So, In $Q_2$ (C) is correct because We go on expanding the scope block by block and make sure that it is no-where present only after we see all global variables. rt??
+1
Yes, right !!

For Ex --> What is the output ?

#include <stdio.h>

int main(void) {
int x = 1, y =2, z=3;
printf("%d %d %d",x,y,z);
{
int x=10,y=20;
printf("%d %d %d",x,y,z);
{
int z =100;
printf("%d %d %d",x,y,z);
}
}
return 0;
}
+1
1 2 3
10 20 3
10 20 100
+1
@mcjoshi @Kapil

Q2. D is the Ans
0
Okkk !

@mcjoshi, what is the answer provided by them ?
+1
Option (C). Refers to Global variable
0
Any reason for that ?
0
No solution given.
0
If undefined variable referred to global variable (int a)then that variable never called as undefined variable.

If the question would asked wrt to static scoping  .if a variable is referred and u want to find its scope Then:~)if we can't find its scope in local then we move ancestor blocks gradually and if we fail to find there also we last take the defined global variable.
0

explain pls

0

what synthesized says

Left side take prodution take value from right side of pruction

E.value →→ E.num + Enum

here value is synthesizes attribute. value is calculate based on num.

what inherited says

right side take prodution take value from right side of pruction

E.num →→ E.value + Ecount

here count is inherited  attribute. value(inherited) takes values from num and count . count take values from value and num .

0

same thing also told in prashant  comment. But m not getting

Left side take prodution take value from right side of production

pls give some examples

0

I simply copppied his comment :

Left side take production take value from right side of production

E.value (Left side) → E.num + Enum  (Right side)

here value is synthesizes attribute. value is calculate based on num.

Now got it

Q2. Ans. Option D

Undefined Variable:->>  variable that is accessed in the code but has not been previously declared by that code.

In any scoping result will produce compilation error for undefined variables.

Note:->C uses static(lexical) scoping.

Ex:-foo.c
int main()
{
int y = x;//here x is used without declaration so called undefined variable.
return 0;
}

foo.c: In function main':
foo.c:2: error: x' undeclared (first use in this function)
foo.c:2: error: (Each undeclared identifier is reported only once
foo.c:2: error: for each function it appears in.)


https://en.wikipedia.org/wiki/Undefined_variable#C

## Another Example

//const int b = 5;

int foo() {
int a = b + 5;
printf("%d",a);
}

int bar() {
int b = 2;
return foo();
}

int main()
{
foo();
bar();
return 0;
}

O/P of Static Scoping :->> Compiler error 'b' is undeclared.

If we remove first line comment then o/p:->> 10,10

by Boss (24k points)