# Idea for a AST frontend for parsing between Julia and Python

**URL:** <https://discourse.julialang.org/t/idea-for-a-ast-frontend-for-parsing-between-julia-and-python/57963>\
**Category:** Tooling\
**Tags:** question\
**Created:** [March 25, 2021, 6:41pm UTC](https://discourse.julialang.org/t/idea-for-a-ast-frontend-for-parsing-between-julia-and-python/57963 "2021-03-25T18:41:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aliG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alig/32/23306_2.png) [@aliG](https://discourse.julialang.org/u/aliG)\
**Post date:** [March 25, 2021, 6:41pm UTC](https://discourse.julialang.org/t/idea-for-a-ast-frontend-for-parsing-between-julia-and-python/57963/1 "2021-03-25T18:41:06Z")

</div>

Hi, I am interested in parsing projects between Julia and Python.  
There is a good use case for converting Python code in a symbolic computational library like `sympy` into equivalent Julia forms.  
Example would be converting this `inverse_fourier_transform(sqrt(pi)*exp(-(pi*k)**2), k, x)` into a AST , then a low level IR that adds optimizations etc finally resulting in efficient code recognizable using Symbolics.jl or so on.  
The Ast representation of Julia is pretty good, to make this work in Sympy, a wrapper can be made since there isn’t any Python equivalent library.  
An example of a AST library is this [ast](https://docs.lfortran.org/AST%20and%20ASR/#ast) in `lfortran` which does the same for Fortran variant.

I’m not sure if this a right place to post. Is this a good feature to add related to Julia or are there already good tools for this?

Examples of various language AST’s for sympy ( [sympy/sympy/parsing at master · sympy/sympy (github.com)](https://github.com/sympy/sympy/tree/master/sympy/parsing))

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 25, 2021, 6:48pm UTC](https://discourse.julialang.org/t/idea-for-a-ast-frontend-for-parsing-between-julia-and-python/57963/2 "2021-03-25T18:48:26Z")

</div>

> [@aliG](#):
>
> There is a good use case for converting Python code in a symbolic computational library like `sympy` into equivalent Julia forms.

See [lambdify in SymPy.jl](https://juliahub.com/docs/SymPy/KzewI/1.0.41/Tutorial/basic_operations/#lambdify-1) for converting `sympy` expressions into Julia. And using SymPy.jl you can also take a Julia expression and evaluate it into a `sympy` expression just by making one or more of the inputs symbolic variables.

Is there a different problem you are trying to solve?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [March 25, 2021, 6:58pm UTC](https://discourse.julialang.org/t/idea-for-a-ast-frontend-for-parsing-between-julia-and-python/57963/3 "2021-03-25T18:58:44Z")

</div>

[diojit](https://github.com/thautwarm/diojit) uses Julia as a JIT backend for Python via [DIO.jl](https://github.com/thautwarm/DIO.jl), so it’s doing something like Python → Julia → AST → IR, but I don’t think it’ll be particularly easy to add Symbolics.jl into the mix.

---

<div class="post-metadata">

**Author:** ![aliG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alig/32/23306_2.png) [@aliG](https://discourse.julialang.org/u/aliG)\
**Post date:** [March 27, 2021, 2:17pm UTC](https://discourse.julialang.org/t/idea-for-a-ast-frontend-for-parsing-between-julia-and-python/57963/4 "2021-03-27T14:17:57Z")

</div>

@stevengj@stillyslalom those look quite interesting, thanks for the suggestions.

For an [example](https://docs.sympy.org/latest/modules/parsing.html#sympy.parsing.sym_expr.SymPyExpression) on how code parsing is done in `sympy.parsing`:

```julia
from sympy.parsing.sym_expr import SymPyExpression
>>> src2 = ''' ... integer :: a, b, c, d ... real :: p, q, r, s ... '''
>>> p = SymPyExpression()
>>> p.convert_to_expr(src2, 'f')
>>> p.convert_to_c()
['int a = 0', 'int b = 0', 'int c = 0', 'int d = 0', 'double p = 0.0', 'double q = 0.0', 'double r = 0.0', 'double s = 0.0']

```

So it will process source language into code in a SympyExpression and convert it to any target language as supported. For that there are AST wrappers of those languages creating representations under the hood, a Python library wrapping Julia AST would be fitting here.

**diojit** would be great feature in code generation, though there will be an extra step of execution with Julia runtime.
