/// <summary> /// Creates a new FA that matches the specified FA expression or empty /// </summary> /// <param name="expr">The expression to make optional</param> /// <param name="accept">The symbol to accept</param> /// <returns>A new FA that will match the specified expression or empty</returns> public static CharFA <TAccept> Optional(CharFA <TAccept> expr, TAccept accept = default(TAccept)) { var result = expr.Clone(); var f = result.FirstAcceptingState; f.AcceptSymbol = accept; result.EpsilonTransitions.Add(f); return(result); }
/// <summary> /// Creates a new FA that will match a repetition of one or more of the specified FA expression /// </summary> /// <param name="expr">The expression to repeat</param> /// <param name="accept">The symbol to accept</param> /// <returns>A new FA that matches the specified FA one or more times</returns> public static CharFA <TAccept> Repeat(CharFA <TAccept> expr, TAccept accept = default(TAccept)) { var result = new CharFA <TAccept>(); var final = new CharFA <TAccept>(true, accept); var e = expr.Clone(); var afa = e.FirstAcceptingState; afa.IsAccepting = false; afa.EpsilonTransitions.Add(final); afa.EpsilonTransitions.Add(result); result.EpsilonTransitions.Add(e); return(result); }