Exemplo n.º 1
0
        public Grammar ReadGrammar()
        {
            try
            {
                var grammar = new Grammar();
                _reader = new StreamReader(_filePath);

                grammar.SetNotTerminals(ReadNotTerminals());
                grammar.SetTerminals(ReadTerminals());
                grammar.SetRules(ReadRules(grammar));
                grammar.SetStart(ReadStart());

                _reader.Close();
                return grammar;
            }
            catch (Exception e)
            {
                return null;
            }
        }
Exemplo n.º 2
0
 private Rule CreateRule(Grammar grammar, string str)
 {
     Rule rule = null;
     string res = "";
     bool flag = false;
     foreach (var c in str)
     {
         if (c != ' ')
             res += c;
         else
         {
             if (!flag)
             {
                 rule = new Rule(new NotTerminal(res));
                 flag = true;
             }
             else
                 rule.AddRightPart(grammar.GetSymbol(res));
             res = "";
         }
     }
     rule.AddRightPart(grammar.GetSymbol(res));
     return rule;
 }
Exemplo n.º 3
0
        private List<Rule> ReadRules(Grammar grammar)
        {
            var rules = new List<Rule>();
            int count = int.Parse(_reader.ReadLine());

            for (int i = 0; i < count; i++)
            {
                string str = _reader.ReadLine();
                rules.Add(CreateRule(grammar, str));
            }
            return rules;
        }
Exemplo n.º 4
0
 public LexicalAnalyzer(Grammar grammar, string output)
 {
     _grammar = grammar;
     _output = output;
 }