Hugs with C preprocessor

Alastair Reid alastair at
Wed Jan 7 19:37:21 EST 2004

> I want to use Haskell files with #ifdef, #else and #endif preprocessor
> directives.  I tried hugs -F "cpp -P" <source_file_name> but cpp complains
> about unterminated character constants.  What is wrong?

ANSI C preprocessors tend to get confused by the quotes in
 lines like the following:

   f x = x' where x' = ...
   g x = x'
      x' = ...

You can often avoid this by putting cpp in 'K&R mode'.
For example, GNU cpp accepts '-traditional'.

I'm attaching a shell script you might use (from end of hugs98/Install in the 
Hugs98 distribution).  (Not tested recently but ought to work.)

Alastair Reid

     eval "exec perl -S $0 $*"
          if $running_under_some_random_shell;
     # Reads CPP output and turns #line things into appropriate Haskell
     # pragmas.  This program is derived from the "hscpp" script
     # distributed with the Glasgow Haskell Compiler.
     $Cpp = 'gcc -E -xc -traditional';
     open(INPIPE, "$Cpp @ARGV |") || die "Can't open C pre-processor pipe\n";
     while (<INPIPE>) {
     # line directives come in flavo[u]rs:
     #   s/^#\s*line\s+\d+$/\{\-# LINE \-\}/;   IGNORE THIS ONE FOR NOW
         s/^#\s*line\s+(\d+)\s+(\".+\")$/\{\-# LINE \1 \2 \-\}/;
         s/^#\s*(\d+)\s+(\".*\").*/\{\-# LINE \1 \2 \-\}/;
         print $_;
     close(INPIPE) || exit(1); # exit is so we reflect any errors.

More information about the Haskell mailing list