Example #1
0
        private void CollectAllElementsRecursive(GrammarTerm element)
        {
            Terminal terminal = element as Terminal;

            if (terminal != null && !_data.Terminals.Contains(terminal) && !_grammar.IsPseudoTerminal(terminal))
            {
                _data.Terminals.Add(terminal);
                return;
            }

            NonTerminal nonTerminal = element as NonTerminal;

            if (nonTerminal == null || _data.NonTerminals.Contains(nonTerminal))
            {
                return;
            }

            if (nonTerminal.Name == null)
            {
                if (nonTerminal.Rule != null && !string.IsNullOrEmpty(nonTerminal.Rule.Name))
                {
                    nonTerminal.Name = nonTerminal.Rule.Name;
                }
                else
                {
                    nonTerminal.Name = "NT" + (_unnamedCount++);
                }
            }

            _data.NonTerminals.Add(nonTerminal);

            if (nonTerminal.Rule == null)
            {
                Cancel(string.Format("Non-terminal {0} has uninitialized Rule property.", nonTerminal.Name));
            }

            foreach (BnfTermList elemList in nonTerminal.Rule.Data)
            {
                for (int i = 0; i < elemList.Count; i++)
                {
                    GrammarTerm child = elemList[i];
                    if (child == null)
                    {
                        Cancel(string.Format("Rule for NonTerminal {0} contains null as an operand in position {1} in one of productions.", nonTerminal, i));
                    }

                    GrammarExpression expr = child as GrammarExpression;
                    if (expr != null)
                    {
                        child = new NonTerminal(null)
                        {
                            Rule = expr
                        };
                        elemList[i] = child;
                    }

                    CollectAllElementsRecursive(child);
                }
            }
        }
Example #2
0
        public GrammarExpression Q()
        {
            GrammarExpression q = Grammar.Empty | this;

            q.Name = this.Name + "?";
            return(q);
        }
Example #3
0
        internal static GrammarExpression Op_Plus(GrammarTerm term1, GrammarTerm term2)
        {
            GrammarExpression expr1 = term1 as GrammarExpression;

            if (expr1 == null || expr1.Data.Count > 1) //either not expression at all, or Pipe-type expression (count > 1)
            {
                expr1 = new GrammarExpression(term1);
            }
            expr1.Data[expr1.Data.Count - 1].Add(term2);
            return(expr1);
        }
Example #4
0
        internal static GrammarExpression Op_Pipe(GrammarTerm term1, GrammarTerm term2)
        {
            GrammarExpression expr1 = term1 as GrammarExpression;

            if (expr1 == null)
            {
                expr1 = new GrammarExpression(term1);
            }

            GrammarExpression expr2 = term2 as GrammarExpression;

            if (expr2 != null && expr2.Data.Count == 1)
            {
                expr1.Data.Add(expr2.Data[0]);
                return(expr1);
            }
            expr1.Data.Add(new BnfTermList());
            expr1.Data[expr1.Data.Count - 1].Add(term2);
            return(expr1);
        }
Example #5
0
    internal static GrammarExpression Op_Pipe(GrammarTerm term1, GrammarTerm term2)
    {
      GrammarExpression expr1 = term1 as GrammarExpression;
      if (expr1 == null) 
        expr1 = new GrammarExpression(term1);

      GrammarExpression expr2 = term2 as GrammarExpression;

      if (expr2 != null && expr2.Data.Count == 1)
      {
        expr1.Data.Add(expr2.Data[0]);
        return expr1;
      }
      expr1.Data.Add(new BnfTermList());
      expr1.Data[expr1.Data.Count - 1].Add(term2); 
      return expr1;
    }
Example #6
0
 internal static GrammarExpression Op_Plus(GrammarTerm term1, GrammarTerm term2)
 {
   GrammarExpression expr1 = term1 as GrammarExpression;
   if (expr1 == null || expr1.Data.Count > 1) //either not expression at all, or Pipe-type expression (count > 1)
     expr1 = new GrammarExpression(term1);
   expr1.Data[expr1.Data.Count - 1].Add(term2);
   return expr1;
 }