Exemplo n.º 1
0
        public bool Parse(IParser parser)
        {
            dynamic result = parser.Parse();

            try
            {
                ClassTable = result.ClassTable;
                Nodes      = new NodeCollection
                {
                    Axiom         = result.Axiom,
                    SortedNodes   = result.SortedNodes,
                    UnsortedNodes = result.UnsortedNodes,
                };
                HasLeftRecursion = result.HasLeftRecursion;

                UnclassifiedTerminals = result.UnclassifiedTerminals;

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }
        }
Exemplo n.º 2
0
 public FiniteAutomatonParser(IClassTable classTable, INodeCollection nodes, IEnumerable <string> unclassifiedTerminals)
 {
     this.classTable            = classTable;
     this.nodes                 = nodes;
     this.unclassifiedTerminals = unclassifiedTerminals;
 }
Exemplo n.º 3
0
        public bool Load()
        {
            throw new NotImplementedException();

            try
            {
                var doc = XDocument.Load(Configuration.Path.GrammarXml);

                var root = doc.Element("grammar");

                UnclassifiedTerminals = root.Element("unclassified-terminals").Elements()
                                        .Select(n => n.Value).ToList();

                var classTable = root.Element("class-table");

                var whiteDelimiters = classTable.Attribute("white-delimiters").Value
                                      .Replace("\\n", "\n")
                                      .Replace("\\r", "\r")
                                      .Replace("\\n", "\n");

                var unclassifiedTokenClass = classTable.Attribute("unclassified-token-class-name").Value;
                var undefinedTokenClass    = classTable.Attribute("undefined-token-class-name").Value;

                var symbolClasses = classTable.Element("symbol-classes").Elements()
                                    .Select(n => (n.Attribute("name").Value, n.Attribute("symbols").Value))
                                    .ToDictionary(n => n.Item1, n => n.Item2);

                var tokenClasses = classTable.Element("token-classes").Elements()
                                   .Select(n => (n.Attribute("name").Value, int.Parse(n.Attribute("id").Value)));

                ClassTable = new GrammarParser.ClassTable
                {
                    WhiteDelimiters            = whiteDelimiters,
                    UnclassifiedTokenClassName = unclassifiedTokenClass,
                    UndefinedTokenClassName    = undefinedTokenClass,
                    SymbolClasses = symbolClasses,
                    TokenClasses  = new Map <string, int>(tokenClasses),
                };

                HasLeftRecursion = bool.Parse(root.Attribute("has-left-recursion").Value);

                var nodesElement = root.Element("nodes");


                var nodes = new Dictionary <int, INode>();


                var terminals = nodesElement.Elements("t");

                var classes = nodesElement.Elements("nt")
                              .Where(n => n.Attribute("is-class") != null);

                var definedTokens = nodesElement.Elements("nt")
                                    .Where(n => n.Attribute("is-defined-token") != null);

                var mediums = nodesElement.Elements("nt")
                              .Except(classes)
                              .Except(definedTokens);

                foreach (var terminal in terminals)
                {
                    int id = int.Parse(terminal.Attribute("id").Value);

                    nodes[id] = new Terminal
                    {
                        Id        = id,
                        IsControl = bool.Parse(terminal.Attribute("is-control").Value),
                        Name      = terminal.Attribute("name").Value,
                    };
                }

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }

            throw new NotImplementedException();
        }
Exemplo n.º 4
0
        //private static void SaveFiniteAutomaton(string path, IFiniteAutomaton automaton)
        //{
        //    var states = automaton.States.Select(n =>
        //    {
        //        var links = n.Value.Links.Select(m => $"{m.Key,-20} {m.Value,-20}");
        //        $"{n.Key,-20} {string.Join("",links)}"
        //        })
        //}

        private static void SaveClassTable(string path, IClassTable classTable)
        {
            WriteAllLines(path, classTable.SymbolClasses.Select(n => $"{n.Key,-20} {n.Value,-20}"));
        }
Exemplo n.º 5
0
 public TokenParser(INodeCollection nodes, IClassTable classTable, IFiniteAutomaton finiteAutomaton)
 {
     this.nodes           = nodes;
     this.classTable      = classTable;
     this.finiteAutomaton = finiteAutomaton;
 }