Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: [Caml-list] Using Frontc
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-11-24 (06:13)
From: Zhu Ping <zhuping@c...>
Subject: Re: [Caml-list] Using Frontc
Dear Gerd Stolpmann

Thank you very much for recommending GODI. I have successfully installed
GODI, compiled and linked my code with the FrontC library.

My main program ** is as following:
open Frontc
let parse_result = Frontc.parse_file "/home/dreamer/code/sample1.c" stdout;;

The sample C code sample1.c is:
int main(int base, int expon)
int accum = 1;
while (expon > 0) {
accum = base * accum;
expon = expon - 1;

I performed the compilation and linking by executing the following two
ocamlc -c -I /opt/godi/lib/ocaml/pkg-lib/FrontC
ocamlc -o main /opt/godi/lib/ocaml/std-lib/unix.cma -I
/opt/godi/lib/ocaml/pkg-lib/FrontC frontc.cma main.cmo

To display the parsing result explicitly I built my toplevel:
ocamlmktop -o mytoplevel /opt/godi/lib/ocaml/std-lib/unix.cma -I
/opt/godi/lib/ocaml/pkg-lib/FrontC frontc.cma
#directory "/home/dreamer/code/";;
#directory "/opt/godi/lib/ocaml/pkg-lib/FrontC/";;
#use "";;

The output is:

/home/dreamer/code/sample1.c[1] Invalid symbol: int main(int base, int
/home/dreamer/code/sample1.c[2] Invalid symbol: {
/home/dreamer/code/sample1.c[3] Invalid symbol: int accum = 1;
/home/dreamer/code/sample1.c[4] Invalid symbol: while (expon > 0) {
/home/dreamer/code/sample1.c[5] Invalid symbol: accum = base * accum;
/home/dreamer/code/sample1.c[6] Invalid symbol: expon = expon - 1;
/home/dreamer/code/sample1.c[7] Invalid symbol: }
/home/dreamer/code/sample1.c[8] Invalid symbol: return(accum);
/home/dreamer/code/sample1.c[9] Invalid symbol: }
val parse_result : Frontc.parsing_result =
((Cabs.INT (Cabs.NO_SIZE, Cabs.NO_SIGN), Cabs.NO_STORAGE,
(Cabs.INT (Cabs.NO_SIZE, Cabs.NO_SIGN),
[(Cabs.INT (Cabs.NO_SIZE, Cabs.NO_SIGN), Cabs.NO_STORAGE,
("base", Cabs.INT (Cabs.NO_SIZE, Cabs.NO_SIGN), [], Cabs.NOTHING));
(Cabs.INT (Cabs.NO_SIZE, Cabs.NO_SIGN), Cabs.NO_STORAGE,
("expon", Cabs.INT (Cabs.NO_SIZE, Cabs.NO_SIGN), [],
[], Cabs.NOTHING)),
(Cabs.INT (Cabs.NO_SIZE, Cabs.NO_SIGN), Cabs.NO_STORAGE,
[("accum", Cabs.INT (Cabs.NO_SIZE, Cabs.NO_SIGN), [],
Cabs.CONSTANT (Cabs.CONST_INT "1"))])],
(Cabs.BINARY (Cabs.GT, Cabs.VARIABLE "expon",
Cabs.CONSTANT (Cabs.CONST_INT "0")),
(Cabs.BINARY (Cabs.ASSIGN, Cabs.VARIABLE "accum",
Cabs.BINARY (Cabs.MUL, Cabs.VARIABLE "base",
Cabs.VARIABLE "accum"))),
(Cabs.BINARY (Cabs.ASSIGN, Cabs.VARIABLE "expon",
Cabs.BINARY (Cabs.SUB, Cabs.VARIABLE "expon",
Cabs.CONSTANT (Cabs.CONST_INT "1"))))))),
Cabs.RETURN (Cabs.VARIABLE "accum"))))]

Why the parsing result is correct in the presence of lexing errors?

Thank you very much for your help:)

Gerd Stolpmann wrote:

>>The pity now is the CIL format seems too complicated for me. I like the data structure defined in an earlier version of FrontC
>>( better:). However I still have problems in compiling FrontC sources downloaded from the site
>> Did anyone successfullyinstall FrontC in Linux/Unix?
>FrontC is available in GODI, This is probably the simplest way to install it. Furthermore, this also simplifies
>using libraries, as GODI supports findlib for all libraries, i.e. you just say
>ocamlfind ocamlc -package frontc -c
>and get rid of the .cma nightmare.