So I’m a database engineer taking some computer science courses and got an assignment to write about symbol resolution. The only reference to it I could find was this https://binarydodo.wordpress.com/2016/07/01/symbol-resolution-during-link-editing/ then a stack over flow of someone asking a similar question to this. I took that to mean “Linking names of any variable, object, etc. in a program to the object in memory” and rolled with that. Hoping someone can clarify if my understanding is correct! Would ask teacher, but weekend and wanting to get this done today.

Here is what I wrote so far.

Symbol resolution is the task of taking something that was referenced by name in a program, and connecting it to the specific item in memory. In a program you can call functions in the program, functions in other programs, objects already created, variables, or even variables created by other objects. All those items are going to exist in memory, on the hard drive, or might be moved to the registrar of the CPU. Symbol resolution is the converting of the names of the items in the program to pointers the computer can use to modify it whenever that name is referenced. When you update a variable, it will need to know where in RAM it is stored, and converting the name of the variable to that address everywhere it is referenced is the process of finding it. Effectively binding an address in memory to that reference.

By doing this, software can work with the exact same variables multiple times, as it it looking in the same place. If a variable is updated, it will know as it’s looking in the proper place in RAM. When working with Object Oriented Programming, it is what defines the differently named objects of the same class as separate parts of memory. Object A of Class Object1 will be bound to a different bit of memory than Object B of Class Object2.

When it goes through resolving symbols, it has to do it in the same order every time, otherwise the programs would run inconsistently. In python for example, it follows the LEGB rule (Scope Resolution in Python | LEGB Rule, 2018). So when trying to find if a variable is the same as another variable, it goes in order of Local, Enclosed, Global, Built In. So it tries to match the reference to anything local, anything in the function, anything global, then tries to match any reference to built in keywords or functions.

As an example:


# Global variable
greeting = "Hello"

# Function that uses global variable
def say_hello(name):
    return greeting + ' ' + name

# Another function that has its own local variable with the same name
def say_hello2(name):
    # Local variable 'greeting' shadows the global variable
    greeting = "Good day"
    # Here, 'greeting' resolves to the local variable instead of the global one
    return greeting + ' ' + name

# Main block
if __name__ == "__main__":
    print(say_hello("Alice"))       # Resolves 'greeting' as global
    print(say_hello2("Bob"))         # Resolves 'greeting' as local within greet_formally

We can see we have two variables both called “greeting”, but they hold different values. In this case using symbol resolution it is able to resolve the “greeting” inside of say_hello2 as having the value “Good Day”. Then it resolves “Greeting” inside of “say_hello” as “Hello”, because when looking for where to link it followed the LEGB rule. In say_hello2, we had a local “greeting”, so it connected to that one and stopped looking for a connection, never making it to the Global “greeting”. If we had an external file we connected with “import” it would then check inside the imported file to try to resolve the names of any variables, functions, or objects. By doing it in this order it will always get the same result and have consistent outcomes, which is essential for programs.

Scope Resolution in Python | LEGB Rule. (2018, November 27). GeeksforGeeks. https://www.geeksforgeeks.org/scope-resolution-in-python-legb-rule/

I know it’s a longer post, but thank you for your time!

  • @solrize
    link
    39 hours ago

    Basically the variables like “greeting” in the program occupy memory locations, like “location 3”. Symbol resolution is when the compiler sees a name and figures out the associated location. Normally that is done with something like a Python dictionary (in the old days you’d have to implement the dictionary yourself, which was an exercise in its own right).

    Slightly complicating the python example, there can be local and global variables in separate locations but with the same name. So the compiler has to figure out from context which one you meant. That too is an exercise.