January 23, 2014

To parse a CoreErlang file in Erlang and get a corresponding AST, the modules cerl, core_scan, and core_parse from the standard library can be used.

We write a function that reads a CoreErlang file and produces an AST:

parse_ce(File) ->
    case file:read_file(File) of
        {ok,Bin} ->
            case core_scan:string(binary_to_list(Bin)) of
                {ok,Toks,_} ->
                    case core_parse:parse(Toks) of
                        {ok, Mod} ->
                            {ok, Mod};
                        {error,E} ->
                            {error, {parse, E}}
                    end;
                {error,E,_} ->
                    {error, {scan, E}}
            end;
        {error,E} ->
            {error,{read, E}}
    end.
(See the Stackoverflow question for the original author.)

Then we can do the following:

$ erl
Eshell V5.5.3  (abort with ^G)
1> c(test.erl).
2> c(test.erl,to_core).
** Warning: No object file created - nothing loaded **
ok
3> parse_ce("test.core").
{ok,{c_module,
        [],
        ...}}
comments powered by Disqus