コード例 #1
0
ファイル: Grammar.cs プロジェクト: odasm/Fiesta_Utils
 public BnfExpression MakePlusRule(NonTerminal listNonTerminal, BnfTerm delimiter, BnfTerm listMember)
 {
     listNonTerminal.SetOption(TermOptions.IsList);
     if (delimiter == null)
     {
         listNonTerminal.Rule = listMember | listNonTerminal + listMember;
     }
     else
     {
         listNonTerminal.Rule = listMember | listNonTerminal + delimiter + listMember;
     }
     return(listNonTerminal.Rule);
 }
コード例 #2
0
ファイル: Grammar.cs プロジェクト: odasm/Fiesta_Utils
        public BnfExpression MakeStarRule(NonTerminal listNonTerminal, BnfTerm delimiter, BnfTerm 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);
        }
コード例 #3
0
        }                     //method

        #endregion

        #region Calculating Tail Firsts
        private void CalculateTailFirsts()
        {
            foreach (Production prod in Data.Productions)
            {
                StringSet accumulatedFirsts = new StringSet();
                bool      allNullable       = true;
                //We are going backwards in LR0Items list
                for (int i = prod.LR0Items.Count - 1; i >= 0; i--)
                {
                    LR0Item item = prod.LR0Items[i];
                    if (i >= prod.LR0Items.Count - 2)
                    {
                        //Last and before last items have empty tails
                        item.TailIsNullable = true;
                        item.TailFirsts.Clear();
                        continue;
                    }
                    BnfTerm     term   = prod.RValues[item.Position + 1]; //Element after-after-dot
                    NonTerminal ntElem = term as NonTerminal;
                    if (ntElem == null || !ntElem.Nullable)               //term is a terminal or non-nullable NonTerminal
                    //term is not nullable, so we clear all old firsts and add this term
                    {
                        accumulatedFirsts.Clear();
                        allNullable         = false;
                        item.TailIsNullable = false;
                        if (ntElem == null)
                        {
                            item.TailFirsts.Add(term.Key);//term is terminal so add its key
                            accumulatedFirsts.Add(term.Key);
                        }
                        else
                        {
                            item.TailFirsts.AddRange(ntElem.Firsts); //nonterminal
                            accumulatedFirsts.AddRange(ntElem.Firsts);
                        }
                        continue;
                    }
                    //if we are here, then ntElem is a nullable NonTerminal. We add
                    accumulatedFirsts.AddRange(ntElem.Firsts);
                    item.TailFirsts.AddRange(accumulatedFirsts);
                    item.TailIsNullable = allNullable;
                } //for i
            }     //foreach prod
        }         //method
コード例 #4
0
        }//method

        private ShiftTable GetStateShifts(ParserState state)
        {
            ShiftTable  shifts = new ShiftTable();
            LR0ItemList list;

            foreach (LRItem item in state.Items)
            {
                BnfTerm 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);
            } //foreach
            return(shifts);
        }     //method
コード例 #5
0
ファイル: BnfTerm.cs プロジェクト: skeleten/FiestaShark
        //Pipe/Alternative
        internal static BnfExpression Op_Pipe(BnfTerm term1, BnfTerm term2)
        {
            //Check term1 and see if we can use it as result, simply adding term2 as operand
            BnfExpression expr1 = term1 as BnfExpression;

            if (expr1 == null) //either not expression at all, or Pipe-type expression (count > 1)
            {
                expr1 = new BnfExpression(term1);
            }
            //Check term2; if it is an expression and is simple sequence (Data.Count == 1) then add this sequence directly to expr1
            BnfExpression expr2 = term2 as BnfExpression;

            //1. term2 is a simple expression
            if (expr2 != null && expr2.Data.Count == 1) // if it is simple sequence (plus operation), add it directly
            {
                expr1.Data.Add(expr2.Data[0]);
                return(expr1);
            }
            //2. term2 is not a simple expression
            expr1.Data.Add(new BnfTermList());           //add a list for a new OR element (new "plus" sequence)
            expr1.Data[expr1.Data.Count - 1].Add(term2); // and put  term2 there if it is not Empty pseudo-element
            return(expr1);
        }
コード例 #6
0
        private void CollectAllElementsRecursive(BnfTerm element)
        {
            //Terminal
            Terminal term = element as Terminal;

            // 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 != null && !Data.Terminals.Contains(term) && term.GetType() != typeof(Terminal))
            {
                Data.Terminals.Add(term);
                return;
            }
            //NonTerminal
            NonTerminal nt = element as NonTerminal;

            if (nt == null || Data.NonTerminals.Contains(nt))
            {
                return;
            }

            if (nt.Name == null)
            {
                if (nt.Rule != null && !string.IsNullOrEmpty(nt.Rule.Name))
                {
                    nt.Name = nt.Rule.Name;
                }
                else
                {
                    nt.Name = "NT" + (_unnamedCount++);
                }
            }
            Data.NonTerminals.Add(nt);
            if (nt.Rule == null)
            {
                AddError("Non-terminal {0} has uninitialized Rule property.", nt.Name);
                Data.AnalysisCanceled = true;
                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)
                    {
                        AddError("Rule for NonTerminal {0} contains null as an operand in position {1} in one of productions.", nt, i);
                        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;
                    }
                    CollectAllElementsRecursive(child);
                }
            }
        }//method
コード例 #7
0
ファイル: BnfExpression.cs プロジェクト: odasm/Fiesta_Utils
 public BnfExpression(BnfTerm element) : base(null)
 {
     Data = new BnfExpressionData();
     Data.Add(new BnfTermList());
     Data[0].Add(element);
 }