Global vs local Scope and their declarations

Regarding the implementation of Scope in the lecture script as map from an identifier to a AST-node of its declaration, I’m confused which class in the project plays the rule of the " AST-node of the identifier’s declaration". More precisely, I am asking, whether (the global scope is mapping to external declaration and the local scope to declaration statements) or (both scopes should be mapping to one class).

Since the class ExternalDeclaration is devoted to global declarartions, they can serve as declarations for global scope. However, I’m wondering, how the local scope would get its declarations?

  • If I use class DeclarationStatement for local scopes, I think, I will face a problem with connecting global scope with local scope since the global scope and local scopes should be implemented as if they are one graph.

  • If I use ExternalDeclaration for both local and global scopes does not match with definition of a global variable. Doing this is as if the variables of a local scope could be global variables, once they are not declared in neasted scopes within this local scope. Nevertheless, I understood from the defintion of a global scope that the global scope is the only one which contains global declarations.

  • If I use DeclarationStatement for both local and global scopes, then why would we have ExternalDeclaration?

Which option is reasonable? if none, how should I thinks about them in some other reasonable way?

To be perfectly honest I don’t see the problem? I believe one way of doing this, is that you make a tree with the top level as as root and every block statement creates a child. Nodes store their declarations and children (in order) and this way you can track declarations and their precedence in a lightweight manner.

In particular I don’t understand why you would want to implement global scope and local scopes as if they are “one stack”, when in reality their structure looks more graph-like.

I am sure I overlooked some glaringly obvious problem with my approach but it seems sound to me as of right now.

1 Like

I just used the word “stack” as it was used to describe the scope implementation during the lecture, but it looklikes a graph.

I modified my post to clarify what is my question about.

Oh, now I get the question. Well, you could use both by letting your external declaration and your local declaration class both implement the same declaration interface (this is important, because you don’t have multiple inheritance in Java). Then you could handle them simultaneously.

1 Like

The project presentation on monday will give many useful hints on these kinds of design question. Concretely, here I would recommend you create an interface Declaration that serves as a parent to all kinds of declarations (I have 5 different implementors)