open System open Parser (* AST *) type Term = | AAA | BBB | Parens of Expr and Expr = | Terms of Term list (* grammar *) // forward ref let expr, exprImpl = recparser() let parens = pbetween (pchar '{') (pchar '}') expr |>> (fun e -> Parens e) "parens" let aaa = pstr "aaa" |>> (fun _ -> AAA) "aaa" let bbb = pstr "bbb" |>> (fun _ -> BBB) "bbb" let term = aaa <|> bbb <|> parens "term" exprImpl := pmany1 term |>> (fun ts -> Terms ts) "expr" let grammar = pleft expr peof "top" (* parser function: Returns Some AST on success, otherwise None. *) let parse input : Expr option = match grammar (prepare input) with | Success(res,_) -> Some res | Failure -> None (* A simple read-eval-print loop. Actually, it's so simple that there's no eval. Note that, in the homework, I want you to handle input differently; the REPL is just for fun. *) let rec repl() : unit = printf "Enter an expression: " let input = System.Console.ReadLine() if input = "quit" then printfn "Goodbye!" exit 0 else let asto = parse input match asto with | Some ast -> printfn "%A" ast | None -> printfn "Invalid expression." repl() [] let main argv = repl() 0 // return an integer exit code