Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 693831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:47:39+00:00 2026-05-14T02:47:39+00:00

I am experimenting with writing a toy compiler in ocaml. Currently, I am trying

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:47:39+00:00Added an answer on May 14, 2026 at 2:47 am

    The trailing ; after the definitions of main, check and s are erroneous.

    Replace these 3 occurences with ;; as follows:

    let s = (Stack.create():int Stack.t);;
    
    let rec check x =
      (
          (* ...sequence of imperative statements... *)
      );;
    
    let main () =
      (
          (* ...sequence of imperative statements... *)
      );;
    

    ; is used in the following cases:

    • to sequence imperative statements
    • as a separator between list elements
    • as a separator between array elements
    • as a separator between record fields

    Some examples:

    let hello_world1 () =
      print_endline "Hello";
      print_endline "World !"
    ;;
    
    let hello_world2 () =
      begin
        print_endline "Hello";
        print_endline "World !"
      end
    ;;
    
    let hello_world3 () =
      (
        print_endline "Hello";
        print_endline "World !";
      )
    ;;
    
    let some_list =
      [1; 2; 3]
    ;;
    
    let some_array =
      [| 'a'; 'b'; 'c' |]
    ;;
    
    type my_process =
      {
        pid: int;
        executable_path: string;
      }
    ;;
    
    let p1 = { pid = 142; executable_path = "./my_exec" };;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple REST service in Node.js (just experimenting), trying to figure out
I am having trouble writing the proper LINQ syntax. I have used it before
Im experimenting with some RSS reader/fetcher im writing at the moment. Everything is going
I have been experimenting with writing applications that use a local SQL Database to
I'm experimenting with writing my own custom MembershipProvider in asp.net and I want to
Experimenting with Scala... I'm trying to define something analogous to the @ hack in
While experimenting with pixel shaders in WPF I decided to draw some pixels onto
I am relatively new to Python, and I am experimenting with writing the following
I'm experimenting with WeakReference, and I'm writing a code that checks if a weak
I've been experimenting with the standard python math module and have come across some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.