Based on our grammar, the expression 10 - 4 + 3 can be parsed either as (10 - 4) + 3, or as 10 - (4 + 3) type regex = | String of string | Plus of regex * regex | Concat of regex * regex | Star of regex let rec matches str regex = match regex with | String(s) -> str = s | Plus(r1, r2) -> (matches str r1) || (matches str r2) | Concat(r1, r2) -> Split str in all possible ways into two strings, str = str1 . str2 Check if (matches str1 r1) and (matches str2 r2) Return yes if such a split exists. | Star(r1) -> str = empty or (matches str r1) Problem: What about iteration? str = empty, or split str in all possible ways into two strings, str = str1 . str2, with str1 non-empty Check if (matches str1 r1) and (matches str2 regex) Note: regex refers to the full regular expression originally passed as argument Return yes if such a split exists.