Aha

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

using System;

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

namespace TransitionTable
{
class Program
{
static void Main(string[] args)
{
//rows cols and values of transition table are inptted through user.
//also initial state and final state is also InputString through user.
//also the a string is inputted to check for validation.

Console.WriteLine("Entre No. of rows or states");


int states = 4; //Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Entre No. of Cols or patterns");
int vars = 3; //Convert.ToInt16(Console.ReadLine());

//int[,] tt = new int[states,vars];

Console.WriteLine(/*Entre values of Transition Table*/"We have a


hardcoded graph of Identifier check");
int[,] tt = new int[,] { { 1, 2, 3 }, { 1, 1, 2 }, { 2, 2, 2 }, { 1, 1,
2 } };
/*for (int row = 0; row < states; row++)
{
for (int cols = 0; cols < vars; cols++)
{
tt[row, cols] = Convert.ToInt16(Console.ReadLine());
}
}*/
Console.WriteLine("Final states are hard coded");
int[] Fs = { 1 };
Console.WriteLine("Entre Initial State");
int Is = 0;//Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Now your table is created");
//Console.WriteLine("Entre the InputString string to check for
validation.");

// string InputString = Console.ReadLine();


string[] pattInput;
Console.WriteLine("Entre Number of Patterns");
int nop = Convert.ToInt16(Console.ReadLine());//nop= no of pattern
pattInput = new String[nop];
Console.WriteLine("Entre Patterns.");
//for (int i = 0; i < nop; i++)
//{
// pattInput[i] = Console.ReadLine();
// pattInput[i] = "^" + pattInput[i];
// pattInput[i] = pattInput[i] + "$";
//}
pattInput[0] = "^[A-Za-z]$";
pattInput[1] = "^[0-9]$";
pattInput[2] = "^[_]$";
Console.WriteLine("Entre String To match.");
string strMatch = Console.ReadLine();
TransitionTable ttable = new
TransitionTable(tt,Is,Fs,strMatch,pattInput);

if (ttable.validate()) { Console.WriteLine("Input string is valid,"); }


else Console.WriteLine("Input string or inputed pattern is not
valid.");
}
}
public class TransitionTable
{
int [,] tt;//transition table
int []Fs;//final states
int Is;//initial state
string InputString;//no of variables or letters
string[] pattInput;

public TransitionTable(int [,] tt ,int Is,int []Fs,string InputString,


string [] pattInput)
{
this.tt = tt;
this.Is = Is;
this.Fs = Fs;
this.InputString = InputString;
this.pattInput = pattInput;
}
public bool validate()
{
int st = Is;//st means current state and Is means initial state
for (int i = 0; i < InputString.Length; i++)
{
st = transition(st, InputString[i]);
if (st == -1) return false;
}
//we reach here when the InputString string is over
int j;
for ( j = 0; j < Fs.Length ; j++)
{
if (st == Fs[j])
break;
}
//agar loop pori dafa chale that means k st!=any value of Final state.
if (j == Fs.Length) { return false; }
else return true;
}
//By default private...
int transition(int st, char ch)
{
string str = Convert.ToString(ch);
for (int i = 0; i < InputString.Length; i++)
{
Regex patt = new Regex(pattInput[i]);
if (patt.Match(str).Success) { return tt[st,i]; }
}
return -1;
}
}
}

You might also like