Advanced Functional Prog.

back - up - cont


Parsing expressions

To conclude, the parse function can be defined as follow.
Next, all the functions defined on the (syntax) tree can be used (eval, print, simplify, etc.).

open System;;

let parse s = 
  match exp s with
  | [(r,"")] -> r
  | _        -> failwith "Bad syntax";;

parse "(12+23)*45";;

Nb. It is possible to summarize the elements presented as follow:

type Parser<'t> = string -> (('t * string) list);;

sym  : Parser<char>;;
(&&) : Parser<'t1> -> ('t1-> Parser<'t2>) -> Parser<'t2>;;
(||) : Parser<'t>->Parser<'t>->Parser<'t>
rep  : Parser<'t> -> Parser<'t list>;;

num' : Parser<int>
exp  : Parser<Exp>

18 - 18