Advanced Functional Prog.

back - up - cont


Parsing expressions

Answers (2)

EXP = NUM ('+' EXP | '*' EXP | epsilon) 
    | '(' EXP ')'
let rec exp = 
  ((num' 
   || 
   (sym '('   && fun _  -> 
    exp       && fun e  -> 
    sym ')'   && fun _  -> 
    ret e))   && fun n1 -> 
   ((sym '+') && fun _  -> 
    exp       && fun n2 -> 
    ret (n1+n2)) 
   || 
   ((sym '*') && fun _  -> 
    exp       && fun n2 -> 
    ret (n1*n2)) 
   || 
   ret n1);;

exp "(12+34)*56";;

16 - 18