public RegexResult Regex([FromBody] RegexRequest request)
        {
            RegexResult result = new RegexResult();

            Compiler.RegularExpressionEngine.SyntaxTreeNode syntaxTree = RegexToSyntaxTreeConverter.Convert(request.Regex);
            result.SyntaxTree += "digraph C {\r\n";
            result.SyntaxTree += syntaxTree.TopParent().ToString();
            result.SyntaxTree += "}\r\n";

            Node dfa = RegexToDFAConverter.Convert(request.Regex);

            result.DFA += "digraph D {\r\n";
            result.DFA += dfa.ToDot();
            result.DFA += "}\r\n";

            result.Matches = RegexEngine.Simulate(request.Regex, request.Input) == SimulationState.Accepting;

            return(result);
        }
        private Node DoConvert(string regex)
        {
            SyntaxTreeNode result = RegexToSyntaxTreeConverter.Convert(regex);

            SyntaxTreeNode s         = result;
            SyntaxTreeNode topParent = s;

            while (s != null)
            {
                if (s.Parent != null)
                {
                    topParent = s.Parent;
                }
                s = s.Parent;
            }

            TopSyntaxTreeNode = topParent;
            Alphabet          = topParent.LeafNodes().Distinct().ToList();

            Dictionary <Node, SyntaxTreeNodeSet> DStates = new Dictionary <Node, SyntaxTreeNodeSet>();
            Node startNode = new Node();

            startNode.IsEntry = true;
            DStates.Add(startNode, new SyntaxTreeNodeSet(topParent.FirstPos()));
            List <Node> MarkedStates         = new List <Node>();
            List <(Node, char, Node)> DTrans = new List <(Node, char, Node)>();

            while (DStates.Keys.Any(x => !MarkedStates.Contains(x)))
            {
                Node S = DStates.Keys.First(x => !MarkedStates.Contains(x));
                MarkedStates.Add(S);
                foreach (char a in Alphabet)
                {
                    SyntaxTreeNodeSet U = new SyntaxTreeNodeSet(DStates[S].Where(x => x.Character == a).SelectMany(x => x.FollowPos).Distinct().ToList());
                    bool isAccepting    = U.Any(x => x.Character == '#');
                    if (U.Any())
                    {
                        Node targetNode = DStates.FirstOrDefault(x => x.Value.IsEqualTo(U)).Key;
                        if (targetNode == null)
                        {
                            targetNode = new Node();
                            if (isAccepting)
                            {
                                targetNode.IsAccepting = true;
                            }
                            DStates.Add(targetNode, U);
                        }
                        else
                        {
                            targetNode.IsAccepting = true;
                        }

                        if (a != '#')
                        {
                            DTrans.Add((S, a, targetNode));
                            new Edge(a, S, targetNode);
                        }
                    }
                }
            }

            return(startNode);
        }
Esempio n. 3
0
        private SyntaxTreeNode GetNode(SubExpression expression)
        {
            if (expression is RepeatedExpression repeatedExpression)
            {
                if (repeatedExpression.RepetitionOperator == '*')
                {
                    SyntaxTreeNode node   = GetNode(repeatedExpression.Expression);
                    SyntaxTreeNode parent = CreateNode();
                    parent.Type = SyntaxTreeNodeType.Star;
                    node.Parent = parent;
                    parent.Children.Add(node);
                    return(parent);
                }
                else if (repeatedExpression.RepetitionOperator == '+')
                {
                    SyntaxTreeNode node   = GetNode(repeatedExpression.Expression);
                    SyntaxTreeNode parent = CreateNode();
                    parent.Type = SyntaxTreeNodeType.Plus;
                    node.Parent = parent;
                    parent.Children.Add(node);
                    return(parent);
                }
            }
            else if (expression is OrExpression orExpression)
            {
                SyntaxTreeNode left       = null;
                SyntaxTreeNode right      = null;
                SyntaxTreeNode lastParent = null;

                foreach (SubExpression subExpression in orExpression.SubExpressions)
                {
                    if (left == null)
                    {
                        left = GetNode(subExpression);
                        continue;
                    }
                    else if (right == null)
                    {
                        right = GetNode(subExpression);

                        SyntaxTreeNode parent = CreateNode();
                        parent.Type  = SyntaxTreeNodeType.Or;
                        left.Parent  = parent;
                        right.Parent = parent;
                        parent.Children.Add(left);
                        parent.Children.Add(right);
                        lastParent = parent;
                        left       = parent;

                        right = null;
                    }
                }

                return(lastParent);
            }
            else if (expression is AndExpression andExpression)
            {
                SyntaxTreeNode left       = null;
                SyntaxTreeNode right      = null;
                SyntaxTreeNode lastParent = null;

                foreach (SubExpression subExpression in andExpression.SubExpressions)
                {
                    if (left == null)
                    {
                        left = GetNode(subExpression);
                        continue;
                    }
                    else if (right == null)
                    {
                        right = GetNode(subExpression);

                        SyntaxTreeNode parent = CreateNode();
                        parent.Type  = SyntaxTreeNodeType.And;
                        left.Parent  = parent;
                        right.Parent = parent;
                        parent.Children.Add(left);
                        parent.Children.Add(right);
                        lastParent = parent;
                        left       = parent;

                        right = null;
                    }
                }

                return(lastParent);
            }
            else if (expression is SingleCharacterExpression singleCharacter)
            {
                return(new SyntaxTreeNode {
                    Character = singleCharacter.Character, Type = SyntaxTreeNodeType.Leaf
                });
            }

            return(null);
        }