Exemplo n.º 1
0
        public override FA VisitLexerDescription(CASTElement currentNode)
        {
            int i = 0;
            FA  leftFa = null, rightFa;

            CGraph.CMergeGraphOperation.MergeOptions mergeOptions = CGraph.CMergeGraphOperation.MergeOptions.MO_DEFAULT;
            CSubsetConstructionAlgorithm             subcon;
            CHopcroftAlgorithm hopmin;
            CLexerDescription  lexerDescription = currentNode as CLexerDescription;
            List <CASTElement> rExpStatements = lexerDescription.GetContextChildren(ContextType.CT_LEXERDESCRIPTION_BODY);

            // Preserve labels of RegExp-derived NFA
            if (!m_options.IsSet(ThompsonOptions.TO_STEPS))
            {
                mergeOptions = CGraph.CMergeGraphOperation.MergeOptions.MO_PRESERVELABELS;
            }

            //1. Create FA
            foreach (var rExpStatement in rExpStatements)
            {
                if (i > 0)
                {
                    rightFa = Visit(rExpStatement);
                    //2.Synthesize the two FAs to a new one
                    CThompsonAlternationTemplate alttempSyn = new CThompsonAlternationTemplate(this.GetHashCode());
                    leftFa = alttempSyn.Sythesize(leftFa, rightFa, mergeOptions);

                    // Prefix node elements of the resulting graph with
                    CIt_GraphNodes it = new CIt_GraphNodes(leftFa);
                    for (it.Begin(); !it.End(); it.Next())
                    {
                        leftFa.PrefixElementLabel(leftFa.GetFANodePrefix(it.M_CurrentItem), it.M_CurrentItem);
                    }
                }
                else
                {
                    leftFa = Visit(rExpStatement);
                }
                i++;
            }

            m_NFA = leftFa;
            m_NFA.UpdateAlphabet();
            m_ReportingServices.ExctractThompsonStep(m_NFA, @"merge.dot", this.GetHashCode());
            if (i > 1)
            {
                m_ReportingServices.AddThompsonStepToReporting(m_NFA, this.GetHashCode(), true);
            }
            else
            {
                m_ReportingServices.ThompsonStepsGenerate();
            }

            //return the final-synthesized FA
            return(m_NFA);
        }
        public override StringBuilder Print()
        {
            StringBuilder  graphvizStringBuilder = new StringBuilder(1000);
            string         header             = "digraph G" + m_graph.M_SerialNumber + "{\r\n";
            string         graphedge_operator = "->";
            CIt_GraphNodes itn = new CIt_GraphNodes(m_graph);
            CIt_GraphEdges itg = new CIt_GraphEdges(m_graph);
            FA             fa  = m_graph as FA;

            // Print header if necessary
            // Print the header if the graph is printed alone independently and not
            // in the context for example of a multigraph printing
            if (!m_options.IsSet(ThompsonOptions.TO_COMBINEGRAPHS))
            {
                graphvizStringBuilder.Append(header);
            }

            for (itn.Begin(); !itn.End(); itn.Next())
            {
                if (fa.GetFinalStates().Count != 0 && fa.GetFinalStates().Contains(itn.M_CurrentItem))
                {
                    graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [peripheries=2];\n");
                }
                else if (itn.M_CurrentItem == fa.M_Initial)
                {
                    graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [style=filled,fillcolor=green];\n");
                }
                else if (m_thompsonInfo.IsNodeClosureEntrance(itn.M_CurrentItem))
                {
                    graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [style=filled,fillcolor=red];\n");
                }
                else if (m_thompsonInfo.IsNodeClosureExit(itn.M_CurrentItem))
                {
                    graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [style=filled,fillcolor=purple];\n");
                }
                else
                {
                    graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\";\n");
                }
            }

            // Print all edges of the graph
            for (itg.Begin(); !itg.End(); itg.Next())
            {
                CGraphEdge g = itg.M_CurrentItem;

                string source, target;
                source = g.M_Source.M_Label;
                target = g.M_Target.M_Label;

                graphvizStringBuilder.AppendFormat("\"{0}\"" + graphedge_operator + "\"{1}\"",
                                                   source, target);
                string s = m_FAInfo.Info(g).M_TransitionCharSet?.ToString();
                if (s != null)
                {
                    graphvizStringBuilder.AppendFormat(" [style = bold, label = \"" + m_FAInfo.Info(g).M_TransitionCharSet?.ToString() + "\"]");
                }
                else
                {
                    if (m_thompsonInfo.IsNodeClosureExit(g.M_Source) &&
                        m_thompsonInfo.IsNodeClosureEntrance(g.M_Target))
                    {
                        graphvizStringBuilder.AppendFormat(" [color=red,style = bold, label = \"" + m_FAInfo.Info(g).M_TransitionCharSet?.ToString() + "\"]");
                    }
                }

                graphvizStringBuilder.Append(";\r\n");
            }

            // Print footer if necessary
            // Print the header if the graph is printed alone independently and not
            // in the context for example of a multigraph printing
            if (!m_options.IsSet(ThompsonOptions.TO_COMBINEGRAPHS))
            {
                graphvizStringBuilder.Append("}\r\n");
            }

            return(graphvizStringBuilder);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Prints the graph into a StringBuilder object.
        /// Optionally the header and footer of the .dot file can be ommited for use of the
        /// graph edges in the multi layer graph printer.
        /// </summary>
        /// <param name="onlyedges">if set to <c>true</c> [onlyedges] the graph is printed as a standalone graph.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override StringBuilder Print()
        {
            // Allocate a stringbuiler object and allocate space for 1000 characters
            StringBuilder  graphvizStringBuilder = new StringBuilder(1000);
            string         graphedge_operator    = " ";
            string         header           = "";
            string         headerProperties = "";
            CIt_GraphNodes itn = new CIt_GraphNodes(m_graph);
            CIt_GraphEdges itg = new CIt_GraphEdges(m_graph);
            FA             fa  = m_graph as FA;

            switch (m_graph.M_GraphType)
            {
            case GraphType.GT_UNDIRECTED:
                graphedge_operator = "--";
                header             = "graph G" + m_graph.M_SerialNumber + "{\r\n";
                header            += headerProperties;
                break;

            case GraphType.GT_DIRECTED:
                graphedge_operator = "->";
                header             = "digraph G" + m_graph.M_SerialNumber + "{\r\n";
                header            += headerProperties;
                break;

            case GraphType.GT_MIXED:
                break;
            }

            // Print header if necessary
            // Print the header if the graph is printed alone independently and not
            // in the context for example of a multigraph printing
            if (!m_options.IsSet(ThompsonOptions.TO_COMBINEGRAPHS))
            {
                graphvizStringBuilder.Append(header);
            }


            // Print all  nodes
            for (itn.Begin(); !itn.End(); itn.Next())
            {
                if (fa.GetFinalStates().Count != 0 && fa.GetFinalStates().Contains(itn.M_CurrentItem))
                {
                    graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [peripheries=2];\n");
                }
                else if (itn.M_CurrentItem == fa.M_Initial)
                {
                    graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [style=filled,fillcolor=green];\n");
                }
                else
                {
                    graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\";\n");
                }
            }
            // Print all edges of the graph
            for (itg.Begin(); !itg.End(); itg.Next())
            {
                CGraphEdge g = itg.M_CurrentItem;

                string source, target;
                source = g.M_Source.M_Label;
                target = g.M_Target.M_Label;

                graphvizStringBuilder.AppendFormat("\"{0}\"" + graphedge_operator + "\"{1}\"",
                                                   source, target);
                string s = m_FAInfo.Info(g).M_TransitionCharSet?.ToString();
                if (s != null)
                {
                    graphvizStringBuilder.AppendFormat(" [style = bold, label = \"" + m_FAInfo.Info(g).M_TransitionCharSet?.ToString() + "\"]");
                }
                else
                {
                }

                graphvizStringBuilder.Append(";\r\n");
            }
            // Print footer if necessary
            // Print the header if the graph is printed alone independently and not
            // in the context for example of a multigraph printing
            if (!m_options.IsSet(ThompsonOptions.TO_COMBINEGRAPHS))
            {
                graphvizStringBuilder.Append("}\r\n");
            }

            return(graphvizStringBuilder);
        }