You need to construct a data structure that will hold all function
calls and their corresponding stacks. Pascal/C/C++ will have a tree
of stacks as the AST for procedure calls. Each node in a tree will
contain a stack of all variables defined within the function. If a
variable outside the scope of the function is refered, then we look at
the parent node 's stack and so on. if in the while loop, you are
defining variables, then its like a nested function call. A *while*
will be a node that has an expression evvaluation subtree and on
evaluation that will lead to loop/end-of-loop .