Get the input string that raises parsing error inside the parser
<p>I have a frontend written in <code>menhir</code> which tries to parse an expression: from a string to an expression AST. The entry point of the frontend <code>Parser_e.main</code> is called in several different places in my OCaml code. So I would like to be able to catch possible errors inside the frontend rather than outside. When catching an error, a particular important information I want to show is the entire <strong>input string</strong> that the frontend cannot parse. (Errors from the lexer are very rare, because the frontend can almost read everything).</p>

<p>So I tried to follow <a href="https://stackoverflow.com/a/14048768/702977">this thread</a>, and to print more information when there is an error. In <code>parser_e.mly</code>, I have added</p>

<pre><code>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 "" }
</code></pre>

<p>But it still does not have the <strong>input string</strong> as information. Does anyone if there is any function I am missing to get that?</p>