我正在努力弄清楚如何使用ocamlyacc与库克斯。
lexer.ml (使用sedlex):
let rec lex (lexbuf: Sedlexing.lexbuf) =
match%sedlex lexbuf with
| white_space -> lex lexbuf
(* ... other lexing rules ... *)
| _ -> failwith "Unrecognized."我还有一个名为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希望由ocamllex而不是由sedlex生成lexer。所以问题是:我怎样才能使用ocamlyacc与香吗?
发布于 2018-09-24 16:16:04
如果您没有非常具体的理由使用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)否则,您需要从您的Lexing.lexbuf -> token创建函数Sedlexing.lexbuf -> token,该函数以一个虚拟的词汇表作为输入,将真正的lexing函数应用于sedlex缓冲区,将位置信息复制到虚拟Lexing.lexbuf,然后返回令牌。
https://stackoverflow.com/questions/52474076
复制相似问题