1

我有一个前端,menhir它试图解析一个表达式:从字符串到表达式 AST。Parser_e.main在我的 OCaml 代码中,在几个不同的地方调用了前端的入口点。所以我希望能够在前端内部而不是外部捕获可能的错误。捕获错误时,我要显示的一个特别重要的信息是前端无法解析的整个输入字符串。(来自词法分析器的错误非常罕见,因为前端几乎可以读取所有内容)。

所以我尝试关注这个线程,并在出现错误时打印更多信息。在parser_e.mly中,我添加了

exception LexErr of string
exception ParseErr of string

let error msg start finish  = 
  Printf.sprintf "(line %d: char %d..%d): %s" start.pos_lnum 
       (start.pos_cnum - start.pos_bol) (finish.pos_cnum - finish.pos_bol) msg

let parse_error msg nterm =
  raise (ParseErr (error msg (rhs_start_pos nterm) (rhs_end_pos nterm)))

e_expression:
/* empty */ { EE_empty }
| INTEGER { EE_integer $1 }
| DOUBLE { EE_double $1 }
...
| error { parse_error "e_expression" 1; ERR "" }

但它仍然没有输入字符串作为信息。如果我缺少任何功能来获得它,有人吗?

4

1 回答 1

2

在错误的上下文中,您可以使用两个位置的格式提取失败词位的位置,使用Parsing.symbol_start_posParsing.symbol_end_pos函数。不幸的是Parsing,模块并没有真正提供对词位作为字符串的访问,但如果输入存储在文件中,则可以手动提取它或以编译器样式打印错误,下降 IDE 将理解并突出显示它手动。下面是一个模块Parser_error。它定义Parser_error.throw了会引发Parser_error.T异常的函数。异常包含诊断消息和失败词位的位置。提供了几个方便的函数来从文件中提取此词位,或生成文件位置消息。如果您的输入未存储在文件中,则可以使用string_of_exn将输入作为字符串接受的函数,并且Parser_error.T异常,并从中提取有问题的子字符串。这是一个使用此异常报告错误的解析器示例。

open Lexing

(** T(message,start,finish) parser failed with a [message] on an 
    input specified by [start] and [finish] position.*)
exception T of (string * position * position)

(** [throw msg] raise a [Parser_error.T] exception with corresponding
    message. Must be called in a semantic action of a production rule *)
let throw my_unique_msg =
  let check_pos f = try f () with _ -> dummy_pos in
  Printexc.(print_raw_backtrace stderr (get_raw_backtrace ()));
  let sp = check_pos Parsing.symbol_start_pos in
  let ep = check_pos Parsing.symbol_end_pos  in
  raise (T (my_unique_msg,sp,ep))

(** [fileposition start finish] creates a string describing a position 
    of an lexeme specified by [start] and [finish] file positions. The
    message has the same format as OCaml and GNU compilers, so it is
    recognized by most IDE, e.g., Emacs. *)
let fileposition err_s err_e =
  Printf.sprintf
    "\nFile \"%s\", line %d, at character %d-%d\n"
    err_s.pos_fname err_s.pos_lnum err_s.pos_cnum err_e.pos_cnum

(** [string_of_exn line exn] given a [line] in a file, extract a failed 
    lexeme form the exception [exn] and create a string denoting the  
    parsing error in a format similar to the format used by OCaml 
    compiler, i.e., with fancy underlying. *) 
let string_of_exn line (msg,err_s,err_e) =
  let b = Buffer.create 42 in
  if err_s.pos_fname <> "" then
    Buffer.add_string b (fileposition err_s err_e);
  Buffer.add_string b
    (Printf.sprintf "Parse error: %s\n%s\n" msg line);
  let start = max 0 (err_s.pos_cnum - err_s.pos_bol)  in
  for i=1 to start  do
    Buffer.add_char b ' '
  done;
  let diff = max 1 (err_e.pos_cnum - err_s.pos_cnum) in
  for i=1 to diff do
    Buffer.add_char b '^'
  done;
  Buffer.contents b

(** [extract_line err] a helper function that will extract a line from 
     a file designated by the parsing error exception *)
let extract_line err =
  let line = ref "" in
  try
    let ic = open_in err.pos_fname in
    for i=0 to max 0 (err.pos_lnum - 1) do
      line := input_line ic
    done;
    close_in ic;
    !line
  with exn -> !line

(** [to_string exn] converts an exception to a string *)
let to_string ((msg,err,_) as exn) =
  let line = extract_line err in
  string_of_exn line exn

这是一个示例,它显示了在没有文件的情况下如何使用,并且输入来自流或交互式(类似 shell)源:

let parse_command line =
  try
    let lbuf = Lexing.from_string line in
    `Ok Parser.statement Lexer.tokens lbuf
  with
  | Parsing.Parse_error -> `Fail "Parse error"
  | Parser_error.T exn -> `Fail (Parser_error.string_of_exn line exn)
于 2016-07-21T14:24:37.217 回答