open Scanf
let scantest fmt v = scanf fmt ((=) v)  (* citeste si testeaza = v *)

type boolexp =
  Id of string
| Not of boolexp
| And of boolexp * boolexp
| Or of boolexp * boolexp

let rec factor() =
  if scantest " %1[(]" "(" then (* un caracter, paranteza ( *)
    let r = expr() in
    if scantest " %1[)]" ")" then r     (* ) inchisa corect *)
    else failwith "missing )"   (* genereaza exceptie cu mesajul dat *)
  else if scantest " %1[~]" "~" then Not(factor())  (* ~, citeste factor *)
  else scanf " %[0-9A-Za-z]" (fun v -> if v <> "" then Id v
    else failwith "literal or ( expected")
                
and term() =
  let rec term1 t1 =    (* a citit t1, cauta mai departe factor *)
    if scantest " %1[*]" "*" then term1 (And(t1, factor())) (* SI *)
    else t1             (* returneaza termenul acumulat *)
  in term1 (factor())   (* citeste termen, pornind de la primul factor *)

and expr() =
  let rec expr1 e1 =    (* a citit e1, cauta mai departe termen *)
    if scantest " %1[+]" "+" then expr1 (Or(e1, term()))    (* SAU *)
    else e1             (* returneaza expresia acumulata *)
  in expr1 (term())     (* citeste expresie, pornind de la primul termen *)

This document was generated using caml2html