Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Ocamllex/Ocamlyacc feature request
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Stefan Heimann <lists@s...>
Subject: Re: [Caml-list] Syntax error
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