Syntax error beyond end of program

Posted by a_m0d on Stack Overflow See other posts from Stack Overflow or by a_m0d
Published on 2010-03-24T12:57:23Z Indexed on 2010/03/24 13:23 UTC
Read the original article Hit count: 275

Filed under:

I am experimenting with writing a toy compiler in ocaml. Currently, I am trying to implement the offside rule for my lexer. However, I am having some trouble with the ocaml syntax (the compiler errors are extremely un-informative). The code below (33 lines of it) causes an error on line 34, beyond the end of the source code. I am unsure what is causing this error.

open Printf

let s = (Stack.create():int Stack.t);

let rec check x =
    (
        if Stack.is_empty s then
            Stack.push x s
        else if Stack.top s < x then
            (
                Stack.push x s;
                printf "INDENT\n";
            )
        else if Stack.top s > x then
            (
                printf "DEDENT\n";
                Stack.pop s;
                check x;
            )
        else
            printf "MATCHED\n";
     );

let main () =
    (
        check 0;
        check 4;
        check 6;
        check 8;
        check 5;
    );

let _ = Printexc.print main ()

Ocaml output:

File "lexer.ml", line 34, characters 0-0:
Error: Syntax error

Can someone help me work out what the error is caused by and help me on my way to fixing it?

© Stack Overflow or respective owner

Related posts about ocaml