this post was submitted on 26 Oct 2024
21 points (100.0% liked)

Programming

17432 readers
244 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS
 

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!

you are viewing a single comment's thread
view the rest of the comments
[–] FourPacketsOfPeanuts@lemmy.world 9 points 3 weeks ago (1 children)

A good article that includes symbol resolution in context

https://www.linuxjournal.com/article/6463

[–] ericbomb@lemmy.world 4 points 3 weeks ago

An article with actual examples instead of an endless string of jargon! Thank you!