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 …
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?
I tried
A = 10
s = "Z = A + 1"
ex = Meta.parse(s)
eval(ex)
and
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 ?
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.
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?
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?
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
I am wondering how the machine-learning community sees the problem : is there an engine-independent “language” for writing scripts?
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 and try to match some expressions from your syntax tree to the generated syntax tree. Good luck!
I’m not aware of any, but that doesn’t mean much. Never tried to search for something like this.
Thank a lot !