Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing C to Ocaml

I would like to get the Abstract Syntax Tree (AST) from a C code, into an OCaml value, so that I can further process the parsed code with a plain OCaml program.

I had in mind to use GCC, get the AST (in GIMPLE) with a hook, and convert the GIMPLE code to Ocaml.

But I wonder if there is another way, or if someone did something similar already. (I haven't found much actually on that...)

I don't want to resort to using CIL. It is an OCaml parser for C code, but it doesn't contain all optimizations that GCC has. (I especially need a deeper alias analysis than the one implemented in CIL).

Can LLVM be a good idea to look at? Already done maybe?

Any better idea?

like image 353
Daz Avatar asked Mar 04 '11 15:03

Daz


2 Answers

If your problem with CIL is the precision of the provided alias analysis, take a look at Frama-C. It is based on CIL but provides a precise value analysis that works for pointers. The value analysis makes its results available inside a modular architecture.

like image 137
Pascal Cuoq Avatar answered Nov 14 '22 20:11

Pascal Cuoq


An other option to parse C to Ocaml would be FrontC. Its description says :

FrontC is an OCAML library providing a C parser and lexer. The result is a syntactic tree easy to process with usual OCAML tree management.

It provides support for ANSI C syntax, old-C K&R style syntax and the standard GNU CC attributes.

It provides also a C pretty printer as an example of use.

like image 34
Nestor Demeure Avatar answered Nov 14 '22 20:11

Nestor Demeure