How can I parse in memory strings (of type String) in OCaml using ocamlyacc?

92 Views Asked by At

I have a parser which parses the std input using Ocamlyacc and lex. How can I trigger the start parse rule on a string in OCaml?

1

There are 1 best solutions below

0
On

Without seeing your code, it's hard to answer but assuming your start rule is called start, and your generated parser module is called Parser.ml and yout generated lexer module is called Lexer.ml, you should do something like :

let parse_from_string s =
  let lex = Lexing.from_string s in
    try
      Lexing.(lex.lex_curr_p <- {lex.lex_curr_p with pos_cnum = 0});
      Parser.start Lexer.token lex
    with
    | Failure s ->
        Printf.eprintf "Error near %s\n\n"
        (string_of_position lex.lex_start_p)