获取在解析器内部引发解析错误的输入字符串

柔音

我写的一个前端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 "" }

但是它仍然没有输入字符串作为信息。是否有人缺少我想要的功能?

静脉血

在发生错误的情况下,您可以使用Parsing.symbol_start_posParsing.symbol_end_pos函数以两个位置的格式提取失败的词素的位置不幸的是,Parsing模块并没有真正以字符串的形式提供对lexeme的访问,但是如果输入存储在文件中,则可以手动提取它或以编译器样式打印错误,下降的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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章