# How to get information about symbol scoping in Julia while compilation?

**URL:** https://discourse.julialang.org/t/how-to-get-information-about-symbol-scoping-in-julia-while-compilation/107643
**Category:** General Usage
**Tags:** compilation, scope
**Created:** [December 15, 2023, 6:54am UTC](https://discourse.julialang.org/t/how-to-get-information-about-symbol-scoping-in-julia-while-compilation/107643 "2023-12-15T06:54:51Z")
**Posts on this page:** 4
**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: [December 15, 2023, 6:54am UTC](https://discourse.julialang.org/t/how-to-get-information-about-symbol-scoping-in-julia-while-compilation/107643/1 "2023-12-15T06:54:51Z")

</div>

I’m interested in understanding how Julia determines the scope of each symbol during compilation. I’d like to get detailed information about the scope of every symbol instance in the code, ideally as output from the compilation process itself.

1. Is there a built-in way in Julia to get information about symbol scoping during compilation?
2. Are there any flags or compiler options that can be used to generate this information as output?
3. If not, are there any recommended approaches or tools for analyzing symbol scope within the Julia compiler pipeline?
4. Any insights or references on how the Julia compiler handles symbol resolution and scope determination would be greatly appreciated!

Here’s what I’ve tried:

- I searched the Julia documentation and online resources, but I couldn’t find any built-in features to achieve this.

---

<div class="post-metadata">

### Author: ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)
#### Post date: [December 15, 2023, 8:48am UTC](https://discourse.julialang.org/t/how-to-get-information-about-symbol-scoping-in-julia-while-compilation/107643/2 "2023-12-15T08:48:06Z")

</div>

@caleb-allen asked a very similar question on Slack the other day. To surmise:

Scoping rules are defined here [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)

Roughly the pipeline looks like:

1. Parsing, implemented in JuliaSyntax.jl and `src/julia-parser.scm`
2. Lowering, currently implemented in `src/julia-syntax.scm`
3. Type inference
4. Optimizations

So take a look at `src/jlfrontend.scm` and in particular `resolve-scoped` in `src/julia-syntax.scm`

I don’t think there are currently any tools to give you symbol scope information. The current infrastructure is based on transforming the AST to implement the scope rules.

---

<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: [December 15, 2023, 8:50am UTC](https://discourse.julialang.org/t/how-to-get-information-about-symbol-scoping-in-julia-while-compilation/107643/3 "2023-12-15T08:50:52Z")

</div>

I’m trying to test the Julia parser using the `flisp` frontend in the Julia project. I followed the documentation and ran the following commands:

> cd src  
> flisp/flisp  
> (load “jlfrontend.scm”)  
> (jl-parse-file “filename”)  
> ;;Example: (jl-parse-file “test.jl”)

However, the last command returns `#f` instead of parsing the file. The file `<filename>` exists in the current directory (src) where `jlfrontend.scm` is also present. I’m not sure why `jl-parse-file` is failing.

---

<div class="post-metadata">

### Author: ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)
#### Post date: [December 15, 2023, 4:01pm UTC](https://discourse.julialang.org/t/how-to-get-information-about-symbol-scoping-in-julia-while-compilation/107643/4 "2023-12-15T16:01:50Z")

</div>

@Akhil_Akkapelli have you considered using the [JuliaSyntax.jl](https://github.com/JuliaLang/JuliaSyntax.jl) frontend to test the parser? It is now the default frontend and is simple to use.

I’m still exploring Julia’s scoping, but as @vchuravy mentioned, the scoping happens at lowering time (rather than parse time). I’d be very interested in any insights you gain, if you’re willing to share!
