简体   繁体   中英

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

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM