示例#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);
                }
            }
        }
示例#2
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);
        }
示例#3
0
文件: Grammar.cs 项目: singba/SSharp
 public GrammarExpression MakePlusRule(NonTerminal listNonTerminal, GrammarTerm delimiter, GrammarTerm listMember)
 {
     listNonTerminal.SetOption(TermOptions.IsList);
     if (delimiter == null)
     {
         listNonTerminal.Rule = listMember | listNonTerminal + listMember;
     }
     else
     {
         listNonTerminal.Rule = listMember | listNonTerminal + delimiter + listMember;
     }
     return(listNonTerminal.Rule);
 }
示例#4
0
文件: Grammar.cs 项目: singba/SSharp
        public GrammarExpression MakeStarRule(NonTerminal listNonTerminal, GrammarTerm delimiter, GrammarTerm listMember)
        {
            if (delimiter == null)
            {
                //it is much simpler case
                listNonTerminal.SetOption(TermOptions.IsList);
                listNonTerminal.Rule = Empty | listNonTerminal + listMember;
                return(listNonTerminal.Rule);
            }
            NonTerminal tmp = new NonTerminal(listMember.Name + "+");

            MakePlusRule(tmp, delimiter, listMember);
            listNonTerminal.Rule = Empty | tmp;
            listNonTerminal.SetOption(TermOptions.IsStarList);
            return(listNonTerminal.Rule);
        }
示例#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);
        }
示例#6
0
        private void CalculateTailFirsts()
        {
            foreach (Production prod in _data.Productions)
            {
                StringSet accumulatedFirsts = new StringSet();
                bool      allNullable       = true;

                for (int i = prod.LR0Items.Count - 1; i >= 0; i--)
                {
                    LR0Item item = prod.LR0Items[i];
                    if (i >= prod.LR0Items.Count - 2)
                    {
                        item.TailIsNullable = true;
                        item.TailFirsts.Clear();
                        continue;
                    }
                    GrammarTerm term   = prod.RValues[item.Position + 1];
                    NonTerminal ntElem = term as NonTerminal;
                    if (ntElem == null || !ntElem.Nullable)
                    {
                        accumulatedFirsts.Clear();
                        allNullable         = false;
                        item.TailIsNullable = false;
                        if (ntElem == null)
                        {
                            item.TailFirsts.Add(term.Key);
                            accumulatedFirsts.Add(term.Key);
                        }
                        else
                        {
                            item.TailFirsts.AddRange(ntElem.Firsts);
                            accumulatedFirsts.AddRange(ntElem.Firsts);
                        }
                        continue;
                    }
                    accumulatedFirsts.AddRange(ntElem.Firsts);
                    item.TailFirsts.AddRange(accumulatedFirsts);
                    item.TailIsNullable = allNullable;
                }
            }
        }
示例#7
0
        private Dictionary <string, LR0ItemList> GetStateShifts(ParserState state)
        {
            Dictionary <string, LR0ItemList> shifts = new Dictionary <string, LR0ItemList>();
            LR0ItemList list;

            foreach (LRItem item in state.Items)
            {
                GrammarTerm term = item.Core.NextElement;
                if (term == null)
                {
                    continue;
                }
                LR0Item shiftedItem = item.Core.Production.LR0Items[item.Core.Position + 1];
                if (!shifts.TryGetValue(term.Key, out list))
                {
                    shifts[term.Key] = list = new LR0ItemList();
                }
                list.Add(shiftedItem);
            }
            return(shifts);
        }
示例#8
0
 public GrammarExpression MakeStarRule(NonTerminal listNonTerminal, GrammarTerm delimiter, GrammarTerm listMember)
 {
   if (delimiter == null)
   {
     //it is much simpler case
     listNonTerminal.SetOption(TermOptions.IsList);
     listNonTerminal.Rule = Empty | listNonTerminal + listMember;
     return listNonTerminal.Rule;
   }
   NonTerminal tmp = new NonTerminal(listMember.Name + "+");
   MakePlusRule(tmp, delimiter, listMember);
   listNonTerminal.Rule = Empty | tmp;
   listNonTerminal.SetOption(TermOptions.IsStarList);
   return listNonTerminal.Rule;
 }
示例#9
0
 public GrammarExpression MakePlusRule(NonTerminal listNonTerminal, GrammarTerm delimiter, GrammarTerm listMember)
 {
   listNonTerminal.SetOption(TermOptions.IsList);
   if (delimiter == null)
     listNonTerminal.Rule = listMember | listNonTerminal + listMember;
   else
     listNonTerminal.Rule = listMember | listNonTerminal + delimiter + listMember;
   return listNonTerminal.Rule;
 }
示例#10
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);
        }
    }
示例#11
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;
    }
示例#12
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;
 }