S -> aaAdS | b
A -> bA | a 


type token = At | Bt | Dt;;

type s = Prod1 of (a*s) 
          | B
and a = Prod3 of a | 
         A;;

let rec s_parse tokens = 
    match tokens with
       At::At::tk -> (match (a_parse tk) with
                           (a, after_a) -> (match after_a with
                                      Dt::after_d -> (match (s_parse after_d) with
                                                     (s, after_s) -> (Prod1 (a,s), after_s)
                                                   | _ -> failwith "Error 2")    
                                    | _ -> failwith "Wrror 3") 
                          | _ -> failwith "Error 4")
   | Bt::tk -> (B, tk)
   | _ -> failwith "Error 5"
and a_parse tokens = 
    match tokens with
       Bt::tk -> (match (a_parse tk) with
                   (b, after_b) -> (Prod3  b, after_b)
                  | _ -> failwith "Error")
     |  At::tk -> (A, tk)  
     | _ -> failwith "Wrror 6";;    

[At;At;Bt;At;Dt;Bt]

[At;At;Bt;At;Dt;At;At;Bt;Bt;Bt;Bt;Bt;At;Dt;Bt]