5 votes 5 votes Indicate the result of the following program if the language uses (i) static scope rules and (ii) dynamic scope rules. var x, y:integer; procedure A (var z:integer); var x:integer; begin x:=1; B; z:= x end; procedure B; begin x:=x+1 end; begin x:=5; A(y); write (y) ...end. Compiler Design gate1989 descriptive compiler-design runtime-environment + – makhdoom ghaya asked Dec 3, 2016 makhdoom ghaya 1.7k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
3 votes 3 votes Using static rules, we get 1. Using dynamic rules, we get 2 kapilbk1996 answered Sep 25, 2018 kapilbk1996 comment Share Follow See all 6 Comments See all 6 6 Comments reply Show 3 previous comments HitechGa commented Jan 14, 2022 reply Follow Share Note that these questions are PASCAL-like. If you see the PASCAL semantics, you shall see that if the formal parameter for a procedure has the keyword $\text{var}$ written before it, then PASCAL considers that the parameter to be call-by-reference. 5 votes 5 votes Argharupa Adhikary commented Aug 26, 2022 reply Follow Share Nice 1 votes 1 votes rajveer43 commented Dec 29, 2023 reply Follow Share FOR STATIC SCOPING: Global Variables: x and y are declared globally. Procedure A: Declares a local variable x. Calls procedure B. Updates z with the value of the local x (which is 1). Procedure B: Updates the global x by incrementing it. Now, when the write(y) statement is executed in the main block: x in the main block is 5. y in the main block was modified by procedure A, so it's 1. So, the output under static scope rules would be 1. FOR DYNAMIC SCOPING: Global Variables: x and y are declared globally. Procedure A: Declares a local variable x. Calls procedure B. Updates z with the value of the local x (which is 1). Procedure B: Updates the global x by incrementing it. Now, when the write(y) statement is executed in the main block: x in the main block is 6 (global x was incremented by procedure B). y in the main block was not modified by any procedure, so it's still 0. So, the output under dynamic scope rules would be 0. Static Scope: Output is 1. Dynamic Scope: Output is 0 0 votes 0 votes Please log in or register to add a comment.