How to turn a string with a valid Erlang expression into an abstract syntax tree (AST)?

后端 未结 3 1345
醉话见心
醉话见心 2020-12-30 09:38

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

Below is an example of

3条回答
  •  礼貌的吻别
    2020-12-30 10:20

    Zoltan

    This is how we get the AST:

    11> String = "fun() -> io:format(\"blah~n\") end.".
    "fun() -> io:format(\"blah~n\") end."
    12> {ok, Tokens, _} = erl_scan:string(String).     
    {ok,[{'fun',1},
         {'(',1},
         {')',1},
         {'->',1},
         {atom,1,io},
         {':',1},
         {atom,1,format},
         {'(',1},
         {string,1,"blah~n"},
         {')',1},
         {'end',1},
         {dot,1}],
        1}
    13> {ok, AbsForm} = erl_parse:parse_exprs(Tokens). 
    {ok,[{'fun',1,
                {clauses,[{clause,1,[],[],
                                  [{call,1,
                                         {remote,1,{atom,1,io},{atom,1,format}},
                                         [{string,1,"blah~n"}]}]}]}}]}
    14> 
    

提交回复
热议问题