예제 #1
0
        private Nonterminal Map(NonterminalExpr rule)
        {
            if (!Nonterminals.TryGetValue(rule, out var mapped))
            {
                mapped = new Nonterminal(rule.Name);
                Nonterminals.Add(rule, mapped);

                foreach (var chain in rule.Body)
                {
                    var body = new List <Symbol>();

                    foreach (var symbol in chain)
                    {
                        switch (symbol)
                        {
                        case TerminalExpr terminal:
                            body.Add(Map(terminal));
                            break;

                        case NonterminalExpr nonterminal:
                            body.Add(Map(nonterminal));
                            break;
                        }
                    }

                    Productions.Add(new Production(mapped, body));
                }
            }

            return(mapped);
        }
예제 #2
0
        public void ReadFromFile(string filePath)
        {
            int number = 1;

            string[] lines = System.IO.File.ReadAllLines(@filePath);
            foreach (string line in lines)
            {
                string[] parts = line.Split("-");
                Nonterminals.Add(parts[0]);
                string[] fileProductions = parts[1].Split("|");
                var      havePoductions  = Productions.TryGetValue(parts[0], out var productions);
                if (!havePoductions)
                {
                    productions = new List <string>();
                }
                foreach (string production in fileProductions)
                {
                    productions.Add(production);
                    ProductionNumber.Add(Tuple.Create(parts[0], production), number);
                    number++;
                }
                Productions.Add(parts[0], productions);
            }
        }