Beispiel #1
0
        public override CharFA <TAccept> ToFA <TAccept>(TAccept accept)
        {
            var ranges = new List <CharRange>();

            for (int ic = Entries.Count, i = 0; i < ic; ++i)
            {
                var entry = Entries[i];
                var crc   = entry as RegexCharsetCharEntry;
                if (null != crc)
                {
                    ranges.Add(new CharRange(crc.Value, crc.Value));
                }
                var crr = entry as RegexCharsetRangeEntry;
                if (null != crr)
                {
                    ranges.Add(new CharRange(crr.First, crr.Last));
                }
                var crcl = entry as RegexCharsetClassEntry;
                if (null != crcl)
                {
                    ranges.AddRange(CharFA <TAccept> .CharacterClasses[crcl.Name]);
                }
            }
            if (HasNegatedRanges)
            {
                return(CharFA <TAccept> .Set(CharRange.NotRanges(ranges), accept));
            }
            return(CharFA <TAccept> .Set(ranges, accept));
        }
Beispiel #2
0
        private void RenderTimer_Tick(object sender, EventArgs e)
        {
            RenderTimer.Enabled = false;
            CharFA <string> fa = null;

            try
            {
                var ast = RegexExpression.Parse(Regex.Text);
                fa = ast.ToFA("Accept");
            }
            catch (Exception ex)
            {
                Status.Text = string.Format("Error Parsing Regex: {0}", ex.Message);
            }
            if (null != fa)
            {
                // mark our states for displaying the DFA later
                foreach (var ffa in fa.FillClosure())
                {
                    ffa.Tag = ffa;
                }

                try
                {
                    var kvp = fa.Lex(Input.Text.GetEnumerator(), "#ERROR");
                    if (kvp.Value.Length != Input.Text.Length)
                    {
                        Status.Text = string.Format("Input error at {0}", kvp.Value.Length);
                    }
                    else
                    {
                        Status.Text = "Successfully Lexed.";
                    }
                }
                catch (Exception eex)
                {
                    Status.Text = string.Format("Input error: {0}", eex.Message);
                }
                var options = new CharFA <string> .DotGraphOptions();

                options.DebugString = Input.Text;
                var dfa = fa.ToDfa();
                dfa.TrimDuplicates();
                try
                {
                    using (var stm = fa.RenderToStream("jpg", false, options))
                        NfaGraph.Image = Image.FromStream(stm);
                } catch
                {
                }
                options.StatePrefix    = @"Q";
                options.DebugSourceNfa = fa;
                try
                {
                    using (var stm = dfa.RenderToStream("jpg", false, options))
                        DfaGraph.Image = Image.FromStream(stm);
                }
                catch { }
            }
        }
Beispiel #3
0
        public override CharFA <TAccept> ToFA <TAccept>(TAccept accept)
        {
            var left  = (null != Left) ? Left.ToFA(accept) : null;
            var right = (null != Right) ? Right.ToFA(accept) : null;

            return(CharFA <TAccept> .Or(new CharFA <TAccept>[] { left, right }, accept));
        }
Beispiel #4
0
        static void _Concat(CharFA <TAccept> lhs, CharFA <TAccept> rhs)
        {
            var f = lhs.FirstAcceptingState;

            f.EpsilonTransitions.Add(rhs);
            f.IsAccepting = false;
        }
Beispiel #5
0
        /// <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);
        }
Beispiel #6
0
        /// <summary>
        /// Creates an FA that will match any one of a set of a characters
        /// </summary>
        /// <param name="ranges">The set ranges of characters that will be matched</param>
        /// <param name="accept">The symbol to accept</param>
        /// <returns>An FA that will match the specified set</returns>
        public static CharFA <TAccept> Set(IEnumerable <CharRange> ranges, TAccept accept = default(TAccept))
        {
            var result = new CharFA <TAccept>();
            var final  = new CharFA <TAccept>(true, accept);

            foreach (var ch in CharRange.ExpandRanges(ranges))
            {
                result.Transitions.Add(ch, final);
            }
            return(result);
        }
Beispiel #7
0
        /// <summary>
        /// Creates an FA that will match any one of a set of a characters
        /// </summary>
        /// <param name="set">The set of characters that will be matched</param>
        /// <param name="accept">The symbol to accept</param>
        /// <returns>An FA that will match the specified set</returns>
        public static CharFA <TAccept> Set(IEnumerable <char> set, TAccept accept = default(TAccept))
        {
            var result = new CharFA <TAccept>();
            var final  = new CharFA <TAccept>(true, accept);

            foreach (var ch in set)
            {
                result.Transitions.Add(ch, final);
            }
            return(result);
        }
Beispiel #8
0
        public CharFA <string> ToLexer(IProgress <FAProgress> progress = null)
        {
            var result = new CharFA <string>();

            for (int ic = Rules.Count, i = 0; i < ic; ++i)
            {
                var rule = Rules[i];
                result.EpsilonTransitions.Add(rule.Right.ToFA(rule.Left));
            }
            return(result as CharFA <string>);
        }
Beispiel #9
0
 public override CharFA <TAccept> ToFA <TAccept>(TAccept accept)
 {
     if (null == Left)
     {
         return((null != Right) ? Right.ToFA(accept) : null);
     }
     else if (null == Right)
     {
         return(Left.ToFA(accept));
     }
     return(CharFA <TAccept> .Concat(new CharFA <TAccept>[] { Left.ToFA(accept), Right.ToFA(accept) }, accept));
 }
Beispiel #10
0
        /// <summary>
        /// Creates an FA that matches a literal string
        /// </summary>
        /// <param name="string">The string to match</param>
        /// <param name="accept">The symbol to accept</param>
        /// <returns>A new FA machine that will match this literal</returns>
        public static CharFA <TAccept> Literal(IEnumerable <char> @string, TAccept accept = default(TAccept))
        {
            var result  = new CharFA <TAccept>();
            var current = result;

            foreach (var ch in @string)
            {
                current.IsAccepting = false;
                var fa = new CharFA <TAccept>(true, accept);
                current.Transitions.Add(ch, fa);
                current = fa;
            }
            return(result);
        }
Beispiel #11
0
        /// <summary>
        /// Creates a new FA that matches any one of the FA expressions passed
        /// </summary>
        /// <param name="exprs">The expressions to match</param>
        /// <param name="accept">The symbol to accept</param>
        /// <returns>A new FA that will match the union of the FA expressions passed</returns>
        public static CharFA <TAccept> Or(IEnumerable <CharFA <TAccept> > exprs, TAccept accept = default(TAccept))
        {
            var result = new CharFA <TAccept>();
            var final  = new CharFA <TAccept>(true, accept);

            foreach (var fa in exprs)
            {
                if (null != fa)
                {
                    var nfa = fa.Clone();
                    result.EpsilonTransitions.Add(nfa);
                    var nffa = nfa.FirstAcceptingState;
                    nffa.IsAccepting = false;
                    nffa.EpsilonTransitions.Add(final);
                }
                else if (!result.EpsilonTransitions.Contains(final))
                {
                    result.EpsilonTransitions.Add(final);
                }
            }
            return(result);
        }
Beispiel #12
0
        /// <summary>
        /// Creates a new FA that is a concatenation of two other FA expressions
        /// </summary>
        /// <param name="exprs">The FAs to concatenate</param>
        /// <param name="accept">The symbol to accept</param>
        /// <returns>A new FA that is the concatenation of the specified FAs</returns>
        public static CharFA <TAccept> Concat(IEnumerable <CharFA <TAccept> > exprs, TAccept accept = default(TAccept))
        {
            CharFA <TAccept> left = null;
            var right             = left;

            foreach (var val in exprs)
            {
                if (null == val)
                {
                    continue;
                }
                var nval = val.Clone();
                if (null == left)
                {
                    left = nval;
                    continue;
                }
                else if (null == right)
                {
                    right = nval;
                }
                else
                {
                    _Concat(right, nval);
                }

                _Concat(left, right);
            }
            if (null != right)
            {
                right.FirstAcceptingState.AcceptSymbol = accept;
            }
            else
            {
                left.FirstAcceptingState.AcceptSymbol = accept;
            }
            return(left);
        }
Beispiel #13
0
 public override CharFA <TAccept> ToFA <TAccept>(TAccept accept)
 => CharFA <TAccept> .Literal(new char[] { Value }, accept);
Beispiel #14
0
 public override CharFA <TAccept> ToFA <TAccept>(TAccept accept)
 => null != Expression ? CharFA <TAccept> .Optional(Expression.ToFA(accept), accept) : null;
Beispiel #15
0
        } = -1;                                          // kleene by default

        public override CharFA <TAccept> ToFA <TAccept>(TAccept accept)
        => null != Expression ? CharFA <TAccept> .Repeat(Expression.ToFA(accept), MinOccurs, MaxOccurs, accept) : null;
Beispiel #16
0
        /// <summary>
        /// Creates a new FA that will match a repetition of the specified FA expression
        /// </summary>
        /// <param name="expr">The expression to repeat</param>
        /// <param name="minOccurs">The minimum number of times to repeat or -1 for unspecified (0)</param>
        /// <param name="maxOccurs">The maximum number of times to repeat or -1 for unspecified (unbounded)</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, int minOccurs = -1, int maxOccurs = -1, TAccept accept = default(TAccept))
        {
            expr = expr.Clone();
            if (minOccurs > 0 && maxOccurs > 0 && minOccurs > maxOccurs)
            {
                throw new ArgumentOutOfRangeException(nameof(maxOccurs));
            }
            switch (minOccurs)
            {
            case -1:
            case 0:
                switch (maxOccurs)
                {
                case -1:
                case 0:
                    var result = new CharFA <TAccept>();
                    var final  = new CharFA <TAccept>(true, accept);
                    final.EpsilonTransitions.Add(result);
                    foreach (var afa in expr.FillAccepting())
                    {
                        afa.IsAccepting = false;
                        afa.EpsilonTransitions.Add(final);
                    }
                    result.EpsilonTransitions.Add(expr);
                    result.EpsilonTransitions.Add(final);
                    return(result);

                case 1:
                    return(Optional(expr, accept));

                default:
                    var l = new List <CharFA <TAccept> >();
                    expr = Optional(expr);
                    l.Add(expr);
                    for (int i = 1; i < maxOccurs; ++i)
                    {
                        l.Add(expr.Clone());
                    }
                    return(Concat(l, accept));
                }

            case 1:
                switch (maxOccurs)
                {
                case -1:
                case 0:
                    var result = new CharFA <TAccept>();
                    var final  = new CharFA <TAccept>(true, accept);
                    final.EpsilonTransitions.Add(result);
                    foreach (var afa in expr.FillAccepting())
                    {
                        afa.IsAccepting = false;
                        afa.EpsilonTransitions.Add(final);
                    }
                    result.EpsilonTransitions.Add(expr);
                    return(result);

                case 1:
                    return(expr);

                default:
                    return(Concat(new CharFA <TAccept>[] { expr, Repeat(expr.Clone(), 0, maxOccurs - 1) }, accept));
                }

            default:
                switch (maxOccurs)
                {
                case -1:
                case 0:
                    return(Concat(new CharFA <TAccept>[] { Repeat(expr, minOccurs, minOccurs, accept), Repeat(expr, 0, 0, accept) }, accept));

                case 1:
                    throw new ArgumentOutOfRangeException(nameof(maxOccurs));

                default:
                    if (minOccurs == maxOccurs)
                    {
                        var l = new List <CharFA <TAccept> >();
                        l.Add(expr);
                        for (int i = 1; i < minOccurs; ++i)
                        {
                            l.Add(expr.Clone());
                        }
                        return(Concat(l, accept));
                    }
                    return(Concat(new CharFA <TAccept>[] { Repeat(expr.Clone(), minOccurs, minOccurs, accept), Repeat(Optional(expr.Clone()), maxOccurs - minOccurs, maxOccurs - minOccurs, accept) }, accept));
                }
            }
            // should never get here
            throw new NotImplementedException();
        }