Reputation: 53
We are writing a compiler in OCaml for our own domain specific language. So far, we have working scanner, parser and ast.
What is the best way to test scanner/parser at this point? I know it is possible to pass a sequence of tokens to the parser/scanner and see if it gets accepted/rejected by the scanner/parser. (such as, echo "FLOAT ID" | menhir --interpret --interpret-show-cst parser.mly
).
But, is there a way to pass the actual program written in our own language to the scanner/parser and see whether it gets accepted?
I have to add that I am very new to OCaml and I know very little about compilers.
Upvotes: 0
Views: 672
Reputation: 4441
If what you want to do is to give a string to your parser and see if it works, you could do this (supposing your starting point in the parser is prog)
main.ml :
let () =
(* Taking the string given as a parameter or the program *)
let lb = Lexing.from_string Sys.argv.(1) in
(* if you want to parse a file you should write :
let ci = open_in filename in
let lb = Lexing.from_channel ci in
*)
try
let p = Parser.prog Lexer.token lb in
Printf.printf "OK\n"
with _ -> Printf.printf "Not OK\n"
Did I help ? ;-)
Upvotes: 2