Ejemplo n.º 1
0
    void Start()
    {
        elements         = new Dictionary <GameObject, GrammarElement>();
        this.productions = new Dictionary <GameObject, GrammarProduction>();

        currentProduction = new GrammarProduction(null, new List <GrammarElement>());
    }
Ejemplo n.º 2
0
    public string GetProductionString(GrammarProduction production)
    {
        string productionString = "<";

        if (production.GetLeftSide() != null)
        {
            productionString += production.GetLeftSide().GetSymbol();
        }
        productionString += "> → ";
        if (production.GetRightSide().Count == 0)
        {
            productionString += "λ";
        }
        else
        {
            foreach (GrammarElement element in production.GetRightSide())
            {
                if (element.IsNonTerminal())
                {
                    productionString += "<" + element.GetSymbol() + ">";
                }
                else
                {
                    productionString += element.GetSymbol();
                }
            }
        }
        return(productionString);
    }
Ejemplo n.º 3
0
 public bool EvaluateProductionExistence(GrammarProduction production)
 {
     foreach (KeyValuePair <GameObject, GrammarProduction> p in productions)
     {
         if (p.Value.GetLeftSide() == production.GetLeftSide() && (p.Value.GetRightSide().Count == production.GetRightSide().Count))
         {
             int  c         = 0;
             bool different = false;
             List <GrammarElement> rightSide1 = p.Value.GetRightSide();
             List <GrammarElement> rightSide2 = production.GetRightSide();
             while (c < rightSide1.Count)
             {
                 if (rightSide1[c] != rightSide2[c])
                 {
                     different = true;
                     break;
                 }
                 c++;
             }
             if (!different)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Ejemplo n.º 4
0
 public void ProductionClick(GameObject productionButton)
 {
     currentProductionButton    = productionButton;
     currentProduction          = productions[currentProductionButton];
     currentProductionText.text = GetProductionString(currentProduction);
     newProduction = false;
 }
Ejemplo n.º 5
0
    public void ElementClick(GameObject elementButton)
    {
        GrammarElement currentElement = elements[elementButton];

        if (currentProduction == null)
        {
            currentProduction = new GrammarProduction(null, new List <GrammarElement>());
        }

        if (currentProduction.GetLeftSide() == null)
        {
            if (currentElement.IsNonTerminal())
            {
                currentProduction.SetLeftSide(currentElement);
            }
        }
        else
        {
            currentProduction.GetRightSide().Add(currentElement);
        }

        if (!newProduction)
        {
            currentProductionButton.transform.parent.GetChild(1).GetComponentInChildren <Text>().text = GetProductionString(currentProduction);
        }
        currentProductionText.text = GetProductionString(currentProduction);
    }
Ejemplo n.º 6
0
 internal static void AddProduction(GrammarProduction Prod)
 {
     if (Productions.ItemIndex(Prod) == -1)
     {
         Productions.Add(Prod);
     }
     else
     {
         BuilderApp.Log.Add(SysLogSection.Grammar, SysLogAlert.Warning, "Duplicate production", "The production '" + Prod.Head.Name + "' was redefined.", Conversions.ToString(Prod.Line));
     }
 }
Ejemplo n.º 7
0
        private static void Reduce(GrammarDefinition definition, GrammarProduction production, FullLalrState lalrState)
        {
            List <Node> toBeReduced = new List <Node>();

            for (int i = 0; i < production.Body.Count; i++)
            {
                toBeReduced.Add(lalrState.NodeStack[lalrState.NodeStack.Count - 1]);
                lalrState.NodeStack.RemoveAt(lalrState.NodeStack.Count - 1);
                lalrState.StateStack.RemoveAt(lalrState.StateStack.Count - 1);
            }

            toBeReduced.Reverse();
            lalrState.NodeStack.Add(Node.FromNodes(definition.ProductionNames[production], production, toBeReduced));
        }
Ejemplo n.º 8
0
 public void AddProduction()
 {
     if (newProduction && currentProduction.GetLeftSide() != null)
     {
         if (!EvaluateProductionExistence(currentProduction))
         {
             GameObject newProductionButton = Instantiate(productionButton, productionsContainer.transform);
             newProductionButton.transform.GetChild(0).GetComponentInChildren <Text>().text = (productions.Count + 1).ToString();
             newProductionButton.transform.GetChild(1).GetComponentInChildren <Text>().text = GetProductionString(currentProduction);
             productions.Add(newProductionButton.transform.GetChild(1).gameObject, currentProduction);
         }
     }
     else if (!newProduction)
     {
         newProduction = true;
     }
     currentProduction          = new GrammarProduction(null, new List <GrammarElement>());
     currentProductionText.text = GetProductionString(currentProduction);
 }
Ejemplo n.º 9
0
 private bool IsFinite(GrammarProduction item, HashSet <NonterminalSymbol> finites)
 {
     if (item.Body.Count == 0)
     {
         return(true);
     }
     if (item.Body.All(x => x is TerminalSymbol))
     {
         return(true);
     }
     foreach (var e in item.Body)
     {
         if (e is NonterminalSymbol)
         {
             if (!finites.Contains(e as NonterminalSymbol))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Ejemplo n.º 10
0
    void Start()
    {
        elements         = new Dictionary <GameObject, GrammarElement>();
        this.productions = new Dictionary <GameObject, GrammarProduction>();

        currentProduction = new GrammarProduction(null, new List <GrammarElement>());

        GrammarElement A = new GrammarElement(true, "A");
        GrammarElement B = new GrammarElement(true, "B");
        GrammarElement C = new GrammarElement(true, "C");
        GrammarElement D = new GrammarElement(true, "D");
        GrammarElement E = new GrammarElement(true, "E");
        GrammarElement a = new GrammarElement(false, "a");
        GrammarElement b = new GrammarElement(false, "b");
        GrammarElement c = new GrammarElement(false, "c");
        GrammarElement d = new GrammarElement(false, "d");
        GrammarElement e = new GrammarElement(false, "e");
        GrammarElement f = new GrammarElement(false, "f");

        GrammarProduction one = new GrammarProduction(A, new List <GrammarElement>()
        {
            a, B, C
        });
        GrammarProduction two = new GrammarProduction(A, new List <GrammarElement>()
        {
            D, b, A
        });
        GrammarProduction three = new GrammarProduction(B, new List <GrammarElement>()
        {
        });
        GrammarProduction four = new GrammarProduction(B, new List <GrammarElement>()
        {
            b, A, B
        });
        GrammarProduction five = new GrammarProduction(C, new List <GrammarElement>()
        {
            c, C
        });
        GrammarProduction six = new GrammarProduction(C, new List <GrammarElement>()
        {
            D, d, B
        });
        GrammarProduction seven = new GrammarProduction(D, new List <GrammarElement>()
        {
        });
        GrammarProduction eight = new GrammarProduction(D, new List <GrammarElement>()
        {
            e, E
        });
        GrammarProduction nine = new GrammarProduction(E, new List <GrammarElement>()
        {
            B, D
        });
        GrammarProduction ten = new GrammarProduction(E, new List <GrammarElement>()
        {
            f
        });

        List <GrammarProduction> productions = new List <GrammarProduction>()
        {
            one, two, three, four, five, six, seven, eight, nine, ten
        };

        grammar = new Grammar(productions, new List <GrammarElement>()
        {
            A, B, C, D, E
        });
    }
Ejemplo n.º 11
0
 public static Node FromNodes(string name, GrammarProduction production, IEnumerable <Node> children)
 {
     return(new ReducedNode(name, production, children));
 }