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 8825017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:46:38+00:00 2026-06-14T06:46:38+00:00

I’m writing a parser for a language that is sufficiently simple for Genlex +

  • 0

I’m writing a parser for a language that is sufficiently simple for Genlex + camlp4 stream parsers to take care of it. However, I’d still be interested in having a more or less precise location (i.e. at least a line number) in case of parsing error.

My idea is to use an intermediate stream between the original char Stream and the token Stream of Genlex, that takes care of line counts, like in the code below, but I’m wondering whether there’s a simpler solution?

let parse_file s =
  let num_lines = ref 1 in
  let bol = ref 0 in
  let print_pos fmt i =
    (* Emacs-friendly location *)
    Printf.fprintf fmt "File %S, line %d, characters %d-%d:" 
      s !num_lines (i - !bol) (i - !bol)
  in
  (* Normal stream *)
  let chan = 
    try open_in s
    with
      Sys_error e -> Printf.eprintf "Cannot open %s: %s\n%!" s e; exit 1
  in
  let chrs = Stream.of_channel chan in
  (* Capture newlines and move num_lines and bol accordingly *)
  let next i =
    try
      match Stream.next chrs with
       | '\n' -> bol := i; incr num_lines; Some '\n'
       | c -> Some c
   with Stream.Failure -> None
  in
  let chrs = Stream.from next in
  (* Pass that to the Genlex's lexer *)
  let toks = lexer chrs in
  let error s =
    Printf.eprintf "%a\n%s %a\n%!"
      print_pos (Stream.count chrs) s print_top toks;
    exit 1
  in
  try
    parse toks
  with
    | Stream.Failure -> error "Failure"
    | Stream.Error e -> error ("Error " ^ e)
    | Parsing.Parse_error -> error "Unexpected symbol"
  • 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-06-14T06:46:39+00:00Added an answer on June 14, 2026 at 6:46 am

    A much simpler solution is to use Camlp4 grammars.

    Parsers built this way allow one to get decent error messages “for free”, unlike the case with stream parsers (which are a low level tool).

    It could be that there is no need to define your own lexer, because OCaml’s lexer suits your needs already. But if you really need your own lexer, then you can easily plug in a custom one:

    module Camlp4Loc = Camlp4.Struct.Loc
    module Lexer = MyLexer.Make(Camlp4Loc)
    module Gram = Camlp4.Struct.Grammar.Static.Make(Lexer)
    
    open Lexer
    
    let entry = Gram.Entry.mk "entry"
    
    EXTEND Gram
      entry: [ [ ... ] ];
    END
    
    let parse str =
       Gram.parse rule (Loc.mk file) (Stream.of_string str)
    

    If you are new to OCaml, then all this module system trickery might seem at first like black voodoo magic 🙂 The fact that Camlp4 is a severely underdocumented beast might also contribute to the surreality of experience.

    So never hesitate to ask a question (even a stupid one) on the mailing list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.