Language.C and non-pretty-printing

Alexander Bernauer bernauer at
Thu May 5 11:20:28 BST 2011

Hi Benedikt,

thank you for your work. I have a question, though.

The Show instances for the C operators do not return valid Haskell
syntax. Instead they return the C syntax of those operators. For

$ cat foo.c
int foo(int i, int k)
	return i < k;	

$ runhaskell DumpAst.hs foo.c
CTranslUnit [CFDefExt (CFunDef [CTypeSpec (CIntType _)] (CDeclr (Just `foo') [CFunDeclr (Right ([CDecl [CTypeSpec (CIntType _)] [(Just (CDeclr (Just `i') [] Nothing [] _),Nothing,Nothing)] _,CDecl [CTypeSpec (CIntType _)] [(Just (CDeclr (Just `k') [] Nothing [] _),Nothing,Nothing)] _],False)) [] _] Nothing [] _) [] (CCompound [] [CBlockStmt (CReturn (Just (CBinary < (CVar `i' _) (CVar `k' _) _)) _)] _) _)] _

Instead of the string "<" there should be the string "CLeOp". Is there a
specific reason for this? Similarily, why isn't the Show instance for
Ident using regular " quotes?

The reason why I would appreciate having valid Haskell syntax is that in
my daily work with laguage-c it is very helpfull to be able to dump the
AST of a sample program so that I can figure out the correct match

To this end I am using the Haskell parser module to parse and pretty
print the AST dump from language-c. For the above example this looks
like this:

     (CFunDef [CTypeSpec (CIntType _)]
        (CDeclr (Just "foo")
                 ([CDecl [CTypeSpec (CIntType _)]
                     [(Just (CDeclr (Just "i") [] Nothing [] _), Nothing, Nothing)] _,
                   CDecl [CTypeSpec (CIntType _)]
                     [(Just (CDeclr (Just "k") [] Nothing [] _), Nothing, Nothing)] _],
              [] _]
           Nothing [] _)
        (CCompound []
              (CReturn (Just (CBinary CLeOp (CVar "i" _) (CVar "k" _) _)) _)]

So, would it be possible that
1) the Show instance for Ident uses " instead of ' and `?
2) the Show instances for the C operators return the name of the
   respective data constructors?
3) the Pretty instances for the C operators don't rely on their Show
   instances any more but return the correct C syntax string on their own?

If you say yes, I will go ahead and make a patch for that. I would also
add my above mentioned pretty printing util.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <>

More information about the Language-c mailing list