[erlang-questions] why does the preprocessor sometimes care about -ifdef'd out code?
Thu Mar 8 20:02:07 CET 2007
Matthias Lang wrote:
> A co-worker asked me: Why does this module not compile?
> tmp >erlc -Wall mod1.erl
> ./mod1.erl:5: unterminated '-ifdef'
> Whereas this module does:
The main difference from the C preprocessor is that the Erlang
preprocessor only reads entire "forms" at a time (that is, it
uses the normal tokenizer function for reading a form). A "form"
is (for scanning purposes) a token sequence ending with a "full
stop" token, i.e., a period followed by whitespace (or comment,
Hence, the preprocessor only recognizes "-ifdef(X)." etc. if they
occur at the beginning of a form. In the first example above,
the third form begins "f, -endif.", so the endif is hidden. In
the second example, the "f." is a form in itself, and the "-endif."
is detected (and the "f." is then discarded, so the program will
This is also why you can't put ifdef/endif sections within a function
or any other declaration. They can only occur around complete "forms".
This is different from C, where the preprocessor does not care at all
about the grammar of the underlying token sequence. If you want to get
the same effect, you have to put the ifdef around a macro definition,
and then use the macro in the function body.
Similarly, the Erlang shell keeps asking for input until you have
entered a fullstop token; only then will it attempt to parse the
given token sequence.
As usual, this is all easy when you know what is happening...
More information about the erlang-questions