Exemple #1
0
 public Tree(typeNode type, string word, typeNode father)
 {
     this.lexema = type;
     this.value  = word;
     this.father = father;
     this.sons   = new Tree[0];
 }
Exemple #2
0
 public typeNode getFatherType(string word, int index, out typeNode curType)
 {
     curType = getType(word);
     if (curType == typeNode.article)
     {
         if (index == 0)
         {
             return(typeNode.subject);
         }
         if ((index == 3) || (index == 2))
         {
             return(typeNode.obj);
         }
     }
     if (curType == typeNode.noun)
     {
         if ((index == 0) || (index == 1))
         {
             return(typeNode.subject);
         }
         if ((index == 2) || (index == 3) || (index == 4))
         {
             return(typeNode.obj);
         }
     }
     if (curType == typeNode.verb)
     {
         if ((index == 1) || (index == 2))
         {
             return(typeNode.predicate);
         }
     }
     return(typeNode.error);
 }
Exemple #3
0
        public bool findPlace(typeNode myType, string word, typeNode fatherNode, Tree head)
        {
            bool flag = false;

            if (myType == typeNode.error)
            {
                return(false);
            }
            return(addNode(myType, word, fatherNode, head, ref flag));
        }
Exemple #4
0
        static void Main(string[] args)
        {
            string sentence;
            bool   validSent = true;

            Tree lexemTree = new Tree(typeNode.sentence, "-", typeNode.error);

            lexemTree.findPlace(typeNode.subject, "-", typeNode.sentence, lexemTree);
            lexemTree.findPlace(typeNode.predicate, "-", typeNode.sentence, lexemTree);
            lexemTree.findPlace(typeNode.obj, "-", typeNode.predicate, lexemTree);

            Console.WriteLine("Grammar of construction:");
            Console.WriteLine("Sentence -> Subject | Predicate");
            Console.WriteLine("Subject -> article + noun| noun");
            Console.WriteLine("article -> a|an|the");
            Console.WriteLine("Predicate -> verb|object");
            Console.WriteLine("object -> article + noun|noun");
            Console.WriteLine();


            Console.WriteLine("Enter your sentence: ");
            sentence = Console.ReadLine();
            string[] tokens = sentence.Split(' ');

            for (int i = 0; i < tokens.Length; i++)
            {
                typeNode myType;
                typeNode father = lexemTree.getFatherType(tokens[i], i, out myType);
                if (father == typeNode.error)
                {
                    validSent = false;
                    break;
                }
                lexemTree.findPlace(myType, tokens[i], father, lexemTree);
            }

            if (!lexemTree.checkIfRightTree(lexemTree))
            {
                validSent = false;
            }

            if (validSent)
            {
                lexemTree.getUpAnalysis(lexemTree);
                Console.WriteLine();
                lexemTree.getDownAnalysis(lexemTree);
            }
            else
            {
                Console.WriteLine("Incorrect sentence!");
            }

            Console.ReadKey();
        }
Exemple #5
0
 public void search(Tree root, typeNode x, ref string result, ref bool isFound)
 {
     if (isFound)
     {
         return;
     }
     if (root.lexema == x)
     {
         result  = root.value;
         isFound = true;
         return;
     }
     else
     {
         for (int i = 0; i < root.sons.Length; i++)
         {
             search(root.sons[i], x, ref result, ref isFound);
         }
     }
 }
Exemple #6
0
 private bool addNode(typeNode curType, string element, typeNode fatherType, Tree root, ref bool stop)
 {
     if (stop)
     {
         return(true);
     }
     else
     {
         if ((root.sons.Length == 0) && (root.lexema != fatherType))
         {
             return(false);
         }
         if (root.lexema == fatherType)
         {
             int numSons = root.sons.Length + 1;
             Array.Resize(ref root.sons, numSons);
             root.sons[numSons - 1] = new Tree(curType, element, fatherType);
             stop = true;
             return(true);
         }
     }
     for (int i = 0; (i < root.sons.Length) && !stop; i++)
     {
         if (root.sons[i].lexema == fatherType)
         {
             int numSons = root.sons[i].sons.Length + 1;
             Array.Resize(ref root.sons[i].sons, numSons);
             root.sons[i].sons[numSons - 1] = new Tree(curType, element, fatherType);
             stop = true;
             return(true);
         }
         else
         {
             addNode(curType, element, fatherType, root.sons[i], ref stop);
         }
     }
     return(false);
 }
    public typeNode type()
    {
        var token = lexer.GetToken();

        if (token.Id == 1)
        {
            var text = "";
            var arg0 = INT();
            text += arg0.Text;
            var result = new typeNode(text, null, arg0);
            result.INT = arg0;
            result.res = Int;
            token      = lexer.GetToken();
            if (token.Id != -1 && token.Id != 27 && token.Id != 16 && token.Id != 29 && token.Id != 13)
            {
                throw new ParserException("Got unxpected token from lexer");
            }
            return(result);
        }
        if (token.Id == 2)
        {
            var text = "";
            var arg0 = BOOL();
            text += arg0.Text;
            var result = new typeNode(text, null, arg0);
            result.BOOL = arg0;
            result.res  = Bool;
            token       = lexer.GetToken();
            if (token.Id != -1 && token.Id != 27 && token.Id != 16 && token.Id != 29 && token.Id != 13)
            {
                throw new ParserException("Got unxpected token from lexer");
            }
            return(result);
        }
        throw new ParserException("Got unxpected token from lexer");
        //return null
    }