将ocamlyacc与sedlex一起使用

将ocamlyacc与sedlex一起使用,ocaml,ocamllex,ocamlyacc,Ocaml,Ocamllex,Ocamlyacc,我正在尝试找出如何将ocamlyacc与sedlex一起使用 lexer.ml(使用sedlex): 我还有一个名为parser.mly的ocamlyacc文件,其中包含parse作为语法规则之一 要分析字符串,我使用以下方法: let lexbuf = Sedlexing.Utf8.from_string s in let parsed = (Parser.parse Lexer.lex) lexbuf in (* ... do things ... *) 但在编译过程中,会出现此错误(由上

我正在尝试找出如何将ocamlyacc与sedlex一起使用

lexer.ml
(使用sedlex):

我还有一个名为
parser.mly
的ocamlyacc文件,其中包含
parse
作为语法规则之一

要分析字符串,我使用以下方法:

let lexbuf = Sedlexing.Utf8.from_string s in
let parsed = (Parser.parse Lexer.lex) lexbuf in
(* ... do things ... *)
但在编译过程中,会出现此错误(由上面的
Lexer.lex
引起):

错误:此表达式的类型为Sedlexing.lexbuf->Parser.token 但表达式的类型应为Lexing.lexbuf->Parser.token 类型Sedlexing.lexbuf与类型Lexing.lexbuf不兼容


据我所知,出现此错误是因为ocamlyacc希望lexer由ocamlex生成,而不是由sedlex生成。因此,问题是:如何将ocamlyacc与sedlex一起使用?

如果您没有非常具体的理由使用ocamlyacc而不是Menhir,那么使用Menhir并将解析函数转换为修改后的API可能要简单得多,因为只需要
unit->token*position*position
类型的令牌生成器函数:

 let provider lexbuf () =
    let tok = generated_lexer lexbuf in
    let start, stop =  Sedlexing.lexing_positions lexbuf in
    tok, start, stop

 let parser_result = MenhirLib.Convert.Simplified.traditional2revised
     generated_parser_entry_point
     (provider lexbuf)
否则,您需要从您的
Sedlexing.lexbuf->token
中创建函数
Lexing.lexbuf->token
,该函数将伪lexbuf作为输入,在sedlex缓冲区上应用真正的Lexing函数,将位置信息复制到伪
Lexing.lexbuf
,然后返回该令牌

 let provider lexbuf () =
    let tok = generated_lexer lexbuf in
    let start, stop =  Sedlexing.lexing_positions lexbuf in
    tok, start, stop

 let parser_result = MenhirLib.Convert.Simplified.traditional2revised
     generated_parser_entry_point
     (provider lexbuf)