Advertisement
Guest User

Untitled

a guest
Jul 14th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.31 KB | None | 0 0
  1. (* Exprssion.mli *)
  2. type term
  3. val str : term -> string
  4.  
  5. (* Expression.ml *)
  6. open String;;
  7. type term =
  8.   | Const of bool
  9.   | Var of string
  10.   | Not of term
  11.   | Imp of (term * term)
  12.   | Or of (term * term)
  13.   | And of (term * term);;
  14. let rec formula_to_string = function
  15.   | Const c -> string_of_bool c
  16.   | Var s -> s
  17.   | Not (Var v) -> "!"^v
  18.   | Not (Const c) -> "!"^ string_of_bool c
  19.   | Not formula -> ("!"^ formula_to_string formula)
  20.   | Or (a,b) -> ((formula_to_string a) ^ " | " ^ (formula_to_string b))
  21.   | And (a,b) -> ((formula_to_string a) ^ " & " ^ (formula_to_string b) )
  22.   | Imp (a , b) -> ( "(" ^ (formula_to_string a) ^ " > " ^ (formula_to_string b)^")" ) ;;
  23.  
  24. (* Parser.mly*)
  25.  
  26. %{
  27.         open Expression
  28. %}
  29. %token <bool> CONST
  30. %token <string> VAR
  31. %token NOT
  32. %token AND
  33. %token OR
  34. %token IMP
  35. %token LPAREN
  36. %token RPAREN
  37. %token EOF
  38.  
  39. %right IMP
  40. %left OR
  41. %left AND
  42. %right NOT
  43.  
  44. %start <Expression.term> prog
  45. %%
  46. prog:
  47.         | e = expr; EOF{ e }
  48.  
  49. expr:
  50.         | cnst = CONST { Const cnst }
  51.         | v = VAR { Var v }
  52.         | NOT; e1 = expr { Not e1 }
  53.         | e1 = expr; AND; e2 = expr { And ( e1 , e2) }
  54.         | e1 = expr; OR; e2 = expr { Or ( e1 , e2) }
  55.         | e1 = expr; IMP; e2 = expr { Imp ( e1 , e2) }
  56.         | LPAREN; e = expr; RPAREN { e }
  57.         ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement