本文介绍了如何将带有有效Erlang表达式的字符串转换为抽象语法树(AST)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将包含有效的Erlang表达式的字符串转换为其抽象语法树表示形式,到目前为止没有成功。

I would like to convert a string containing a valid Erlang expression to its abstract syntax tree representation, without any success so far.

下面是我的一个示例想做。编译后,所有 z:z()。生成模块 zed ,该模块通过调用 zed :zed()。返回在给定列表上应用 lists:reverse 的结果。

Below is an example of what I would like to do. After compiling, alling z:z(). generates module zed, which by calling zed:zed(). returns the result of applying lists:reverse on the given list.

-module(z).
-export([z/0]).

z() ->
  ModuleAST = erl_syntax:attribute(erl_syntax:atom(module),
                                   [erl_syntax:atom("zed")]),

  ExportAST = erl_syntax:attribute(erl_syntax:atom(export),
                                   [erl_syntax:list(
                                    [erl_syntax:arity_qualifier(
                                     erl_syntax:atom("zed"),
                                     erl_syntax:integer(0))])]),

  %ListAST = ?(String),  % This is where I would put my AST
  ListAST = erl_syntax:list([erl_syntax:integer(1), erl_syntax:integer(2)]),

  FunctionAST = erl_syntax:function(erl_syntax:atom("zed"),
                                    [erl_syntax:clause(
                                     [], none,
                                     [erl_syntax:application(
                                        erl_syntax:atom(lists),
                                        erl_syntax:atom(reverse),
                                        [ListAST]
                    )])]),

  Forms = [erl_syntax:revert(AST) || AST <- [ModuleAST, ExportAST, FunctionAST]],

  case compile:forms(Forms) of
    {ok,ModuleName,Binary}           -> code:load_binary(ModuleName, "z", Binary);
    {ok,ModuleName,Binary,_Warnings} -> code:load_binary(ModuleName, "z", Binary)
  end.

字符串可能是 [1,2,3]。 从A = 4,B = 2 + 3,[A,B]开始。 或类似的东西。

String could be "[1,2,3].", or "begin A=4, B=2+3, [A,B] end.", or anything alike.

(请注意,这只是我想做的一个例子,因此请评估 String 不是我的选择。)

(Note that this is just an example of what I would like to do, so evaluating String is not an option for me.)

编辑

以下指定ListAST会生成一个巨大的dict-digraph-error-monster,并说 lint_module内部错误。

Specifying ListAST as below generates a huge dict-digraph-error-monster, and says "internal error in lint_module".

String = "[1,2,3].",
{ok, Ts, _} = erl_scan:string(String),
{ok, ListAST} = erl_parse:parse_exprs(Ts),






EDIT2

此解决方案适用于简单的条款:

This solution works for simple terms:

{ok, Ts, _} = erl_scan:string(String),
{ok, Term} = erl_parse:parse_term(Ts),
ListAST = erl_syntax:abstract(Term),


推荐答案

在您的EDIT示例中:

In your EDIT example:

String = "[1,2,3].",
{ok, Ts, _} = erl_scan:string(String),
{ok, ListAST} = erl_parse:parse_exprs(Ts),

ListAST实际上是一个列表的AST:s(因为顾名思义parse_exprs,它解析多个表达式(每个表达式都以句点终止)。由于您的字符串包含单个表达式,因此您获得了一个元素的列表。您所需要做的就是将其匹配:

the ListAST is actually a list of AST:s (because parse_exprs, as the name indicates, parses multiple expressions (each terminated by a period). Since your string contained a single expression, you got a list of one element. All you need to do is match that out:

{ok, [ListAST]} = erl_parse:parse_exprs(Ts),

所以它与erl_syntax无关(它接受所有erl_parse树);只是您在ListAST周围有一个额外的列表包装器,这导致了编译器呕吐。

so it has nothing to do with erl_syntax (which accepts all erl_parse trees); it's just that you had an extra list wrapper around the ListAST, which caused the compiler to puke.

这篇关于如何将带有有效Erlang表达式的字符串转换为抽象语法树(AST)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 13:11