open System open Parser type Expr = | Terms of Term list and Term = | AAA | BBB | Brace of Expr let expr, exprImpl = recparser() let aaa = pstr "aaa" |>> (fun _ -> AAA) "aaa" let bbb = pstr "bbb" |>> (fun _ -> BBB) "bbb" let brace = pbetween (pchar '{') (pchar '}') expr |>> (fun e -> Brace e) "brace" let term = (aaa <|> bbb <|> brace) "term" exprImpl := pmany1 term |>> Terms "expr" let grammar: Parser = pleft expr peof let parse input : Expr option = let input' = debug input match grammar input' with | Success(res,_) -> Some res | Failure(pos, rule) -> printfn "Invalid expression." let msg = sprintf "Cannot parse input at pos %d in rule '%s':" pos rule let diag = diagnosticMessage 20 pos input msg printf "%s" diag None let rec repl() = printf "Enter an expression (or 'quit' to quit): " let input = System.Console.ReadLine() if input = "quit" then printfn "Goodbye!" exit 0 else let ast_opt = parse input match ast_opt with | Some ast -> printfn "%A" ast | None -> () repl() [] let main argv = repl() 0 // return an integer exit code