# Parsing Julia code to separate symbols based on scoping and create a symbol table

**URL:** <https://discourse.julialang.org/t/parsing-julia-code-to-separate-symbols-based-on-scoping-and-create-a-symbol-table/113809>\
**Category:** General Usage\
**Tags:** metaprogramming, scope\
**Created:** [May 3, 2024, 6:15pm UTC](https://discourse.julialang.org/t/parsing-julia-code-to-separate-symbols-based-on-scoping-and-create-a-symbol-table/113809 "2024-05-03T18:15:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akhil\_Akkapelli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akhil_akkapelli/32/202792_2.png) [@Akhil\_Akkapelli](https://discourse.julialang.org/u/Akhil_Akkapelli)\
**Post date:** [May 3, 2024, 6:15pm UTC](https://discourse.julialang.org/t/parsing-julia-code-to-separate-symbols-based-on-scoping-and-create-a-symbol-table/113809/1 "2024-05-03T18:15:59Z")

</div>

I have a quote block in Julia containing functions, modules, and etc. Assuming that quote block is in Main module and Main module does not have these symbols with same names. I want to separate the symbols based on their scoping and create a symbol table that stores their names which reflect their scopes along with their locations in AST where they occur. Specifically, I want to handle cases where symbols with the same name have different scoping.

```julia
exp = Meta.parse("quote
    x = 2
    
   function foo(x)
        y = 2 * x
        z = x + y
    end
    
    function bar(a)
        y = a + x
    end
end")

```

In this quote block, there are multiple symbols named `x` and **`y`** , … each with its own scoping. I want to parse this code and generate a symbol table that reflects this scoping, something like:

```julia
Symbol: x
 ScopeName: x@_1, Location: exp.args[1].args[2].args[1] (or) (1,2,1)
 ScopeName: x@_2, Location: exp.args[1].args[4].args[1].args[2] (or) (1,4,1,2)
 ScopeName: x@_2, Location: exp.args[1].args[4].args[2].args[3].args[2].args[3] (or) (1,4,2,3,2,3)
 ScopeName: x@_2, Location: exp.args[1].args[4].args[2].args[5].args[2].args[2] (or) (1,4,2,5,2,2)
 ScopeName: x@_3, Location: exp.args[1].args[6].args[2].args[3].args[2].args[3] (or) (1,6,2,3,2,3)
Symbol: y
 ScopeName: y@_1, Location: exp.args[1].args[4].args[2].args[3].args[1] (or) (1,4,2,3)
 ScopeName: y@_1, Location: exp.args[1].args[4].args[2].args[5].args[2].args[3] (or) (1,4,2,5,2,3)
 ScopeName: y@_2, Location: exp.args[1].args[6].args[2].args[3].args[1] (or) (1,6,2,3,1)
.
.
.

```

Could someone provide guidance on how to achieve this in Julia? Additionally, are there any existing packages or libraries that could simplify this task?

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [May 3, 2024, 7:00pm UTC](https://discourse.julialang.org/t/parsing-julia-code-to-separate-symbols-based-on-scoping-and-create-a-symbol-table/113809/2 "2024-05-03T19:00:39Z")

</div>

I had the same question on slack some time ago (February, so the slack thread will disappear sometime soon):

> **[Slack is your productivity platform](https://julialang.slack.com/?redir=%2Farchives%2FC688QKS7Q%2Fp1708900671381629%3Fname%3DC688QKS7Q%26perma%3D1708900671381629)**
>
> Slack is a new way to communicate with your team. It’s faster, better organized, and more secure than email.

What I understood from @vchuravy and @Mason 's guidance was that this is not currently possible in an easy way.

The parsing of julia code was recently moved from lisp to actual Julia code thanks to @c42f 's JuliaSyntax.jl. However, you need to introspect the lowering step to achieve what you want, which happens after parsing. That still happens in lisp, so we can not introspect it. Claire is also working on [GitHub - c42f/JuliaLowering.jl: Julia code lowering with precise provenance](https://github.com/c42f/JuliaLowering.jl) which might help, but that is still at a very embryonic stage.

Of course, you can write some AST crawers yourself and do all of this yourself, but then you probably miss some edge cases.
