Commits

Anonymous committed 2c925c6

parsing

  • Participants
  • Parent commits 863cffd

Comments (0)

Files changed (5)

-  
 {
 (*  open Term        (* The type token is defined in parser.mli *)*)
-  type t = LAMBDA | DOT | Ident of string | OPEN | CLOSE
-| INT of int | EOL
+  type token = Lambda | Dot | Ident of string | Open | Close
+| Int of int | EOL
   exception Eof
 }
 
 rule token = parse
   [' ' '\t']     { token lexbuf }     (* skip blanks *)
 | ['\n' ]        { EOL }
-| "lambda"       { LAMBDA }
-| '\\'            { LAMBDA }
-| '.'            { DOT }
-| '('            { OPEN }
-| ')'            { CLOSE }
+| "lambda"       { Lambda }
+| '\\'           { Lambda }
+| '.'            { Dot }
+| '('            { Open }
+| ')'            { Close }
 | alpha (digit|alpha)*
     { Ident (Lexing.lexeme lexbuf) }
-| ['0'-'9']+     { INT(int_of_string(Lexing.lexeme lexbuf)) }
+| ['0'-'9']+     { Int (int_of_string(Lexing.lexeme lexbuf)) }
 | eof            { raise Eof }
 
   $ ocamlc lexer.ml main.ml
 **)
 
+open Util
 open Lexer
+open Parser
+open Term
 
 let stoken = function
-  | LAMBDA -> "LAMBDA"
-  | DOT -> "DOT"
+  | Lambda -> "LAMBDA"
+  | Dot -> "DOT"
   | Ident s -> "var:" ^ s
-  | OPEN -> "OPEN"
-  | CLOSE -> "CLOSE"
-  | INT i -> "int:" ^ string_of_int i
+  | Open -> "OPEN"
+  | Close -> "CLOSE"
+  | Int i -> "int:" ^ string_of_int i
   | EOL -> "EOL"
 
 let _ =
   try
     let lexbuf = Lexing.from_channel stdin in
-    while true do
+    let rec tokens store =
+      try tokens (Lexer.token lexbuf :: store) with
+      | Lexer.Eof -> List.rev store
+    in
+    let ts = tokens [] in
+    let tm = Parser.parse ts in
+    prerr_endline @@ "parse: " ^ sterm tm;
+(*    while true do
       let t =  Lexer.token lexbuf in
       prerr_endline (stoken t);
       flush stderr;
     done
+*)
   with
   | Lexer.Eof ->
       prerr_endline "eof"; exit 0
   | App of term * term
 *)
 
-type token =
-  | Ident of string
-  | Lambda
-  | Dot
-  | Open
-  | Close
-
-(*open Lexer*)
+open Lexer
 open ParserUtil
 open Term
 
   | _ -> Inr "token"
 
 let rec term1 () : term parser =
-    ident >>= fun s -> return (Var s)
-	<|> (token Lambda >> ident >>= fun s -> token Dot >> term1() >>= fun t -> return (Abs (s, t)))
+    (ident >>= fun s -> return (Var s))
+	<|> (token Lambda >> ident >>= fun s -> token Dot >> term >>= fun t -> return (Abs (s, t)))
+	<|> (token Open >> term >>= fun t -> token Close >> return t)
+and term_app t1 : term parser =
+    (term1() >>= fun t -> term_app (App (t1, t)))
+	<|> return t1
+and term = fun code -> (term1() >>= fun t1 -> term_app t1) code
 
-and term_app t1 : term parser =
-    term1() >>= fun t -> term_app (App (t1, t))
+let parse ts =
+  match term ts with
+  | Inl (t, []) -> t
+  | Inl (t, _) -> t
+  | Inr err -> failwith err
+
+  
   | Var of var
   | Abs of var * term
   | App of term * term
+
+let rec sterm = function
+  | Var v -> v
+  | Abs (x, t) -> "^" ^ x ^ ". " ^ sterm t
+  | App ((Abs _ as t1), (Abs _ as t2)) -> "(" ^ sterm t1 ^ ") (" ^ sterm t2 ^ ")"
+  | App ((Abs _ as t1), t2) -> "(" ^ sterm t1 ^ ") " ^ sterm t2
+  | App (t1, (Abs _ as t2)) -> sterm t1 ^ " (" ^ sterm t2 ^ ")"
+  | App (t1, t2) -> sterm t1 ^ " " ^ sterm t2
-
+let (@@) f x = f x