Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Dec 21st, 2012  |  syntax: OCaml  |  size: 0.88 KB  |  hits: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. let boolean_expression_of_string str =
  2.   let lexify str =
  3.     let keywords = ["("; ")"; "¬"; "∧"; "∨"] in
  4.     make_lexer keywords (Stream.of_string str)
  5.   and parse lexer =
  6.     let rec binary_expression =
  7.       let rec loop left_expr =
  8.         parser
  9.           | [< 'Kwd "∧"; right_expr = unary_expression; s >] -> loop (And [left_expr; right_expr]) s (* Syntax error here characters 10-11 *)
  10.           | [< 'Kwd "∨"; right_expr = unary_expression; s >] -> loop (Or [left_expr; right_expr]) s
  11.           | [< >] -> left_expr
  12.       in
  13.       parser [< left_expr = unary_expression; s >] -> loop left_expr s
  14.     and unary_expression =
  15.       parser
  16.         | [< 'Kwd "¬"; expr = unary_expression >] -> Not expr
  17.         | [< 'Kwd "("; expr = binary_expression; 'Kwd ")" >] -> expr
  18.         | [< 'Int n >] -> Var n
  19.     in
  20.     binary_expression lexer
  21.   in
  22.   parse (lexify str)
  23. ;;