示例#1
0
        private void CollectTermsRecursive(BnfTerm term)
        {
            // Do not add pseudo terminals defined as static singletons in Grammar class (Empty, Eof, etc)
            //  We will never see these terminals in the input stream.
            //   Filter them by type - their type is exactly "Terminal", not derived class.
            if (term.GetType() == typeof(Terminal))
            {
                return;
            }
            if (_grammarData.AllTerms.Contains(term))
            {
                return;
            }
            _grammarData.AllTerms.Add(term);
            NonTerminal nt = term as NonTerminal;

            if (nt == null)
            {
                return;
            }

            if (nt.Name == null)
            {
                if (nt.Rule != null && !string.IsNullOrEmpty(nt.Rule.Name))
                {
                    nt.Name = nt.Rule.Name;
                }
                else
                {
                    nt.Name = "NT" + (_unnamedCount++);
                }
            }
            if (nt.Rule == null)
            {
                _language.Errors.Add("Non-terminal " + nt.Name + " has uninitialized Rule property.");
                return;
            }
            //check all child elements
            foreach (BnfTermList elemList in nt.Rule.Data)
            {
                for (int i = 0; i < elemList.Count; i++)
                {
                    BnfTerm child = elemList[i];
                    if (child == null)
                    {
                        _language.Errors.Add("Rule for NonTerminal " + nt.Name + " contains null as an operand in position " + i.ToString() + " in one of productions.");
                        continue; //for i loop
                    }
                    //Check for nested expression - convert to non-terminal
                    BnfExpression expr = child as BnfExpression;
                    if (expr != null)
                    {
                        child       = new NonTerminal(null, expr);
                        elemList[i] = child;
                    }
                    CollectTermsRecursive(child);
                } //for i
            }
        }         //method
    private void CollectTermsRecursive(BnfTerm term) {
      // Do not add pseudo terminals defined as static singletons in Grammar class (Empty, Eof, etc)
      //  We will never see these terminals in the input stream.
      //   Filter them by type - their type is exactly "Terminal", not derived class. 
      if (term.GetType() == typeof(Terminal)) return;
      if (_grammarData.AllTerms.Contains(term)) return;
      _grammarData.AllTerms.Add(term);
      NonTerminal nt = term as NonTerminal;
      if (nt == null) return;

      if (nt.Name == null) {
        if (nt.Rule != null && !string.IsNullOrEmpty(nt.Rule.Name))
          nt.Name = nt.Rule.Name;
        else
          nt.Name = "NT" + (_unnamedCount++);
      }
      if (nt.Rule == null) {
        _language.Errors.Add("Non-terminal " + nt.Name + " has uninitialized Rule property.");
        return;
      }
      //check all child elements
      foreach (BnfTermList elemList in nt.Rule.Data)
        for (int i = 0; i < elemList.Count; i++) {
          BnfTerm child = elemList[i];
          if (child == null) {
            _language.Errors.Add("Rule for NonTerminal " + nt.Name + " contains null as an operand in position " + i.ToString() + " in one of productions.");
            continue; //for i loop 
          }
          //Check for nested expression - convert to non-terminal
          BnfExpression expr = child as BnfExpression;
          if (expr != null) {
            child = new NonTerminal(null, expr);
            elemList[i] = child;
          }
          CollectTermsRecursive(child);
        }//for i
    }//method