ocaml - Feed ocamlyacc parser from explicit token list? -
is possible feed ocamlyacc-generated parser explicit token list analysis?
i'd use ocamllex explicitly generate token list analyze using yacc-generated parser later. however, standard use case generates parser calls lexer implicitly next token. here tokens computed during yacc analysis rather before. conceptually parser should work on tokens yacc-generated parser provides interface relies on lexer in case don't need.
if have list of tokens, can go ugly way , ignore lexing buffer altogether. after all, parse-from-lexbuf function parser expects non-pure function :
let my_tokens = ref [ (* whatever *) ] let token lexbuf = match !my_tokens | [] -> eof | h :: t -> my_tokens := t ; h let ast = parser.parse token (lexbuf.from_string "") on other hand, looks comments have function of type lexing.lexbuf -> token list you're trying fit lexing.lexbuf -> token signature of parser. if case, can use queue write converter between 2 types:
let deflate token = let q = queue.create () in fun lexbuf -> if not (queue.is_empty q) queue.pop q else match token lexbuf | [ ] -> eof | [tok] -> tok | hd::t -> list.iter (fun tok -> queue.add tok q) t ; hd let ast = parser.parse (deflate my_lexer) lexbuf
Comments
Post a Comment