Feedjit

Articles for you

Saturday, June 1, 2013

Optimized Transition Diagrams Pridictive Parser in C# .nET

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Optimized_tds
{
    class Program
    {
        string[] input = new string[30];
        string get_input = "";
        int input_length = 0;
        int i = 0;
        string accept = "";
        ///////////////////////////////////////////////////////////////////
        public void read()
        {
            get_input = Console.ReadLine();
            get_input += " ";
            input = get_input.Split(' ');
            foreach (string word in input)
            {
                input_length++;

           }
           
        }
        ///////////////////////////////////////////////////////////////////
        bool match(string str)
        {
            if (str == input[i])
            {
                i++;
                return true;
            }
            return false;
        }
        ///////////////////////////////////////////////////////////////////
        public void E()
       
        {
            T();
            if (match("+"))
            {
                accept += "+";

                E();
            }
        }
        ///////////////////////////////////////////////////////////////////
        public void T()
        {
            F();
            if (match("*"))
            {
                accept += "*";

                T();
            }
        }
        ///////////////////////////////////////////////////////////////////
        public void F()
        {
            if (match("("))
            {
                accept += "(";

                E();
                if (match(")"))
                {
                    accept += ")";


                }
                else
                {
                    Console.WriteLine(")" + "is missing");
                }
            }
            else if (match("id"))
            {
                accept += "id";

            }
           
        }
        ///////////////////////////////////////////////////////////////////
        static void Main()
        {
           
            Program obj = new Program();
            obj.accept = "";
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Clear();
                Console.WriteLine("    ┌════════════════════════════════════════════════════════╖");
                Console.WriteLine("    │--------------------------------------------------------║");
                Console.WriteLine("    │MAIN MENU FOR PRIDICTIVE PARSER IN COMPILER CONSTRUCTION║");
                Console.WriteLine("    │--------------------------------------------------------║");
                Console.WriteLine("    ╘════════════════════════════════════════════════════════╝");
                Console.WriteLine();
                Console.WriteLine("INSERT DATA INPUT SEQUENCE");
                Console.WriteLine("═══════════════════════");
              
               
              

                        Console.WriteLine("Write in a line separated by space");
                        obj.read();
                   
                


                        obj.E();
                        if (obj.i == obj.input.Length-1)
                        {
                            Console.WriteLine("════════════════════════════════════════════");
                            Console.WriteLine("════════════════════════════════════════════");
                            Console.WriteLine(obj.accept + " is a valid input sequence");
                            Console.WriteLine("═══════════════════════════════════════════");
                            Console.WriteLine("═══════════════════════════════════════════");
                        }
                        else
                           
                        {
                            Console.Write("According to Grammer ");
                            Console.WriteLine("Invalid-Input Sequence");
                        }
                        Console.WriteLine("Press any key to exit");
                        Console.ReadLine();
        }
    }
        }
   
Download here

Read More

Articles for you