# AST from Julia source code?

**URL:** https://discourse.julialang.org/t/ast-from-julia-source-code/101430
**Category:** New to Julia
**Created:** [July 10, 2023, 12:40pm UTC](https://discourse.julialang.org/t/ast-from-julia-source-code/101430 "2023-07-10T12:40:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [July 10, 2023, 12:40pm UTC](https://discourse.julialang.org/t/ast-from-julia-source-code/101430/1 "2023-07-10T12:40:40Z")

</div>

While browsing the devdocs for [ast](https://docs.julialang.org/en/v1/devdocs/ast/), the first table shows a transformation from `f(x)` to `(call f x)`.

Was there a function in Julia that performs this transformation, or was the documentation merely showing how an AST might be represented as such?

---

<div class="post-metadata">

### Author: ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)
#### Post date: [July 10, 2023, 12:49pm UTC](https://discourse.julialang.org/t/ast-from-julia-source-code/101430/2 "2023-07-10T12:49:47Z")

</div>

From the paragraph above the table

> For example `(call f x)` corresponds to `Expr(:call, :f, :x)` in Julia.

You can use `Meta.parse` to convert a string to an `Expr`.

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [July 10, 2023, 12:50pm UTC](https://discourse.julialang.org/t/ast-from-julia-source-code/101430/3 "2023-07-10T12:50:22Z")

</div>

Ok, but there is no way to turn Julia into S-expressions then?

---

<div class="post-metadata">

### Author: ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)
#### Post date: [July 10, 2023, 12:57pm UTC](https://discourse.julialang.org/t/ast-from-julia-source-code/101430/4 "2023-07-10T12:57:38Z")

</div>

I don’t know of any builtin function in Base that does this.  
But it should not be too difficult to cook up one, given that `Expr` contains all relevant data already.  
IIUC then `Expr` is just a different representation/just differently printed than a s-expression.

---

<div class="post-metadata">

### Author: ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)
#### Post date: [July 10, 2023, 6:09pm UTC](https://discourse.julialang.org/t/ast-from-julia-source-code/101430/5 "2023-07-10T18:09:20Z")

</div>

I don’t really understand the distinction that you’re trying to draw. `Expr` is an s-expression representation. Are you just looking for printing that doesn’t explicitly print out `Expr`? In that case

```julia
julia> Meta.show_sexpr(Meta.parse("f(x)"))
(:call, :f, :x)

```

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [July 10, 2023, 6:28pm UTC](https://discourse.julialang.org/t/ast-from-julia-source-code/101430/6 "2023-07-10T18:28:07Z")

</div>

Thanks!
