From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA01813; Wed, 4 Jun 2003 19:25:09 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id TAA01902 for ; Wed, 4 Jun 2003 19:25:08 +0200 (MET DST) Received: from carme.kasserver.com (h-62.141.48.121.keyweb.de [62.141.48.121] (may be forged)) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h54HP7H25353 for ; Wed, 4 Jun 2003 19:25:07 +0200 (MET DST) Received: from kunz.ratzer (dclient217-162-174-4.hispeed.ch [217.162.174.4]) by carme.kasserver.com (8.11.6/8.11.6) with ESMTP id h54HP6N23151 for ; Wed, 4 Jun 2003 19:25:06 +0200 Received: from stefan by kunz.ratzer with local (Exim 3.35 #1 (Debian)) id 19Nc0e-000142-00 for ; Wed, 04 Jun 2003 19:25:12 +0200 Date: Wed, 4 Jun 2003 19:25:12 +0200 From: Stefan Heimann To: caml-list@inria.fr Subject: Re: [Caml-list] Syntax error Message-ID: <20030604172511.GB3658@kunz.ratzer> Mail-Followup-To: caml-list@inria.fr References: <941EB0B0-91BE-11D7-B411-000393B9096C@is.titech.ac.jp> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.3.28i X-Spam: no; 0.00; caml-list:01 lukasz:01 lew:01 'let:01 lineno:01 cin:99 argv:01 prerr:01 ocaml:01 caml:01 int:01 token:01 rec:01 0200,:01 raises:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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