# Prevent interpolation when Meta.parse a String to Expr

**URL:** https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242
**Category:** New to Julia
**Tags:** question, metaprogramming, meta
**Created:** [February 14, 2021, 6:38am UTC](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242 "2021-02-14T06:38:16Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Lian\_Yunlong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lian_yunlong/32/14437_2.png) [@Lian\_Yunlong](https://discourse.julialang.org/u/Lian_Yunlong)
#### Post date: [February 14, 2021, 6:38am UTC](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242/1 "2021-02-14T06:38:16Z")

</div>

Hello everyone,  
if I want to parse the following string to expression  
`s = "Z = A + 1"`  
where A represents a variable defined elsewhere in the code, then how can I prevent interpolation when I pass this string to `Meta.parse()` ?  
`s = "Z = \$A + 1"` won’t work …

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [February 14, 2021, 8:20am UTC](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242/2 "2021-02-14T08:20:09Z")

</div>

What is the result you expect here?  
`parse` turns your string into an expression, in which `A` is a symbol that will be looked up in the evaluating scope later. I’m not sure what kind of interpolation you want to prevent at that point. Should `A` be a literal String `"A" `, do you really want an anonymous function with `A` as an argument or did I misunderstand you?

---

<div class="post-metadata">

### Author: ![Lian\_Yunlong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lian_yunlong/32/14437_2.png) [@Lian\_Yunlong](https://discourse.julialang.org/u/Lian_Yunlong)
#### Post date: [February 14, 2021, 8:23am UTC](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242/3 "2021-02-14T08:23:35Z")

</div>

I tried

```julia
A = 10
s = "Z = A + 1"
ex = Meta.parse(s)
eval(ex)

```

and

```julia
A = 10
s = "Z = A + 1"
ex = Meta.parse(s)
Meta.eval(ex)

```

the first one works as expected, while the second one doesn’t work… (`ERROR: UndefVarError: A not defined`)  
It seems that Meta.eval is not the function I want to evaluate the Expr ?

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [February 14, 2021, 8:36am UTC](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242/4 "2021-02-14T08:36:03Z")

</div>

I see, `eval` and `Meta.eval` are doing the same but in different scopes. Each module has it’s own `eval`. What you did was evaluating `A` in the scope of module `Meta` and that has no binding for `A`. What are you trying to do, maybe we can help with that.

---

<div class="post-metadata">

### Author: ![Lian\_Yunlong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lian_yunlong/32/14437_2.png) [@Lian\_Yunlong](https://discourse.julialang.org/u/Lian_Yunlong)
#### Post date: [February 14, 2021, 9:36am UTC](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242/6 "2021-02-14T09:36:46Z")

</div>

Thank you very much! I am trying to write a parser for a self-defined language and finally translate it to Julia code. The motivation is to provide a programming-language-independent, ab-initio-software-independent writing tool for various ab initio calculation scripts. Currently, these scripts are written in bash or Python with various packages such as ASE.

When this language is parsed by Julia, it is not closed. I mean, it can take predefined variables and functions in the same scope where it is parsed.

In order to execute the parsed and translated code in Julia, I think I should use `eval` at the final step. Am I correct?

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [February 14, 2021, 11:24am UTC](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242/7 "2021-02-14T11:24:04Z")

</div>

I’m no expert by any means, but that sound correct.  
Just a ladt question: what do you use `Meta.parse` for? Are you rewriting the DSL into Julia first and then parse it or parse it into AST form directly?

---

<div class="post-metadata">

### Author: ![Lian\_Yunlong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lian_yunlong/32/14437_2.png) [@Lian\_Yunlong](https://discourse.julialang.org/u/Lian_Yunlong)
#### Post date: [February 15, 2021, 8:03am UTC](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242/8 "2021-02-15T08:03:59Z")

</div>

I have a set of recursive grammar rules which defines the language. Then I parse a piece of code into a tree according to these grammar rules. Finally I rebuild the Julia code from the tree and run. It is in this final step where I used `Meta.parse`. I generate a string of Julia code from the tree and parse. Perhaps I should build Julia `Expr` directly from the tree? I am less confident with `Expr` ☹

---

<div class="post-metadata">

### Author: ![Lian\_Yunlong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lian_yunlong/32/14437_2.png) [@Lian\_Yunlong](https://discourse.julialang.org/u/Lian_Yunlong)
#### Post date: [February 15, 2021, 8:47am UTC](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242/9 "2021-02-15T08:47:59Z")

</div>

I am wondering how the machine-learning community sees the problem : is there an engine-independent “language” for writing scripts?

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [February 15, 2021, 9:55am UTC](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242/10 "2021-02-15T09:55:57Z")

</div>

> [@Lian\_Yunlong](#):
>
> Perhaps I should build Julia `Expr` directly from the tree?

That’s probably the most natural thing to do 😃 the AST is a tree already, so the transformations you need are probably easier that way. But you can use whatever you feel comfortable with, ofc. It’s not exactly trivial to build complex expressions “by hand”, but it’s very enlightening if you have the time to play around with it a little. Why not start with generating normal julia code and then [inspect it](https://docs.julialang.org/en/v1/manual/metaprogramming/#Functions-on-Expressions) and try to [match some expressions](https://docs.julialang.org/en/v1/devdocs/ast/#Surface-syntax-AST) from your syntax tree to the generated syntax tree. Good luck!

> [@Lian\_Yunlong](#):
>
> I am wondering how the machine-learning community sees the problem : is there an engine-independent “language” for writing scripts?

I’m not aware of any, but that doesn’t mean much. Never tried to search for something like this.

---

<div class="post-metadata">

### Author: ![Lian\_Yunlong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lian_yunlong/32/14437_2.png) [@Lian\_Yunlong](https://discourse.julialang.org/u/Lian_Yunlong)
#### Post date: [February 15, 2021, 11:04pm UTC](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242/11 "2021-02-15T23:04:15Z")

</div>

Thank a lot !
