Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Stefan Heimann <lists@stefanheimann.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Syntax error
Date: Wed, 4 Jun 2003 19:25:12 +0200	[thread overview]
Message-ID: <20030604172511.GB3658@kunz.ratzer> (raw)
In-Reply-To: <Pine.LNX.4.44.0306041105490.22712-100000@zodiac.mimuw.edu.pl>

Hi,

On Wed, Jun 04, 2003 at 11:19:47AM +0200, Lukasz Lew wrote:
> Hi.
> 
> I am using Genlex and ocaml streams and parsers. And when I catch 
> exception Stream.Error "", I can not generate other message than "syntax error".
> 
> How can I obtain informations about line number or character number, 
> where the error ocurred ?
> 
> In ocaml examples at http://caml.inria.fr/Examples/oc.tar.gz, function 
> pos_in is used, but it returns always number of last character in stream.

I don't have a good solution for this, just a hack. 

I have something like 'let lineno = ref 1' in the lexer and increment
the lineno everytime a '\n' is read. In the parser I have written the
parse_error function like that:

let parse_error s = raise Parse_error

My main program looks like that:

(* raises End_of_file if i is too large *)
let rec get_line i cin =
  begin
    assert (i >= 1);
    let l = input_line cin in
      if i == 1 then l
      else get_line (i - 1) cin
  end

let _ =
  let fname = Sys.argv.(1) in
  let lexbuf = Lexing.from_channel (open_in fname) in
    try
      let gr = Ebnf_parser.grammar Ebnf_lexer.token lexbuf in
        print_string (Grammar.string_of_grammar gr)
    with Parsing.Parse_error ->
      begin
        prerr_string (fname ^ ":" ^ (string_of_int !Ebnf_lexer.lineno)
                      ^ ": ");
        prerr_string ("Syntax error on token `" 
                      ^ (Lexing.lexeme lexbuf) ^ "'.");
        prerr_newline ();
        try let l = get_line !Ebnf_lexer.lineno (open_in fname) in
          prerr_string l;
          prerr_newline ();
        with End_of_file -> ()
      end

Maybe this helps...

Bye,
  Stefan

-- 
Stefan Heimann
http://www.stefanheimann.net :: personal website.
http://cvsshell.sf.net       :: CvsShell, a console based cvs client.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


      reply	other threads:[~2003-06-04 17:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-28 21:03 [Caml-list] Ocamllex/Ocamlyacc feature request Brian Hurt
2003-05-29 10:16 ` Ken Wakita
2003-05-29 12:03   ` Damien
2003-05-29 13:07     ` Luc Maranget
2003-06-04  9:19   ` [Caml-list] Syntax error Lukasz Lew
2003-06-04 17:25     ` Stefan Heimann [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030604172511.GB3658@kunz.ratzer \
    --to=lists@stefanheimann.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox