[erlang-questions] Analysing an Erlang file

Joe Armstrong <>
Wed Jun 17 16:10:15 CEST 2009

This should get you started ...

Use epp:file to parse the erlang, then a list comprehension to yank
out the stuff you
are interested in.

Like this:

test() ->
    {ok, L} = epp:parse_file("elib1_misc.erl",[],[]),
    [{{Name,Arity},length(Clauses)} || {function,_,Name,Arity,Clauses} <- L].

> test().

This is a bit "quick and dirty" since you need to know the internal
syntax of the abstract forms -

I suggest you define

dump(File, Term) ->
    Out = File ++ ".tmp",
    io:format("** dumping to ~s~n",[Out]),
    {ok, S} = file:open(Out, [write]),
    io:format(S, "~p.~n",[Term]),

then evaluate

dump("foo", epp:parse_file(File, [], [])).

Then stare hard at foo.tmp and all the secrets of the Erlang masters
will be revealed :-)

(note dump/2 is *very* useful for inspecting large data structures - I
use it all the time)


On Wed, Jun 17, 2009 at 12:00 AM, Torben
Hoffmann<> wrote:
> Thanks to both Christian and Ricard for pointing out the epp_dodger module -
> it has exactly what I need!
> I have been reading the erl_syntax documentation and it is definitely what I
> need to use... I just couldn't get started.
> Cheers,
> Torben
> On Tue, Jun 16, 2009 at 11:02 PM, Richard Carlsson <>wrote:
>> Torben Hoffmann wrote:
>>> Or should I approach this problem from a different angle?
>> I suggest you read up on the syntax_tools application, mainly
>> the erl_syntax module and the epp_dodger module; the latter
>> lets you parse most source files, even those that contain include
>> directives, macros, and other preprocessor features. The former
>> (and the erl_syntax_lib companion module) make it a lot easier to
>> handle the parsed syntax trees.
>>    /Richard
> --
> http://www.linkedin.com/in/torbenhoffmann

More information about the erlang-questions mailing list