All pastes #19639 Raw Edit

Stuff

public text v1 · immutable
#19639 ·published 2005-08-09 19:24 UTC
rendered paste body
using System; 

using System.Collections;

using System.IO;

using System.Text;



public class Init {



	public static void Main (string[] args) {

		if (args.Length < 1) {

			Console.WriteLine ("Bad Usage\nUsage: AutomataPila.exe GrammarFile [Input String]");

			return;

		}

		AutomataPila automata = new AutomataPila ();

		try {

			FileStream file = File.OpenRead (args[0]);

			StreamReader reader = new StreamReader (file);

			while (true) {

				string LineAct = reader.ReadLine ();

				if (LineAct[0] == '-') {

					automata.SetInitialState (LineAct.Substring (1));

					break;

				}

				string[] tokens = LineAct.Split (':');

				OperationType type; 

				if (tokens[3] == "NULL")

					type = OperationType.NullOperation;

				else if (tokens[3] == "PUSH")

					type = OperationType.PushOperation;

				else if (tokens[3] == "POP")

					type = OperationType.PopOperation;

				else

					throw new Exception ("BAD FORMAT, OPERATION TYPE MUST BE NULL, PUSH OR POP");

				automata.AddRule (tokens[0], tokens[1], tokens[2][0], type, tokens[4][0]);

			}

		}

		catch (IOException) {

			Console.WriteLine ("File not found or unexpected EOF");

			return;

		}

		catch (Exception ex) {

			Console.WriteLine (ex.Message);

			return;

		}

		string input = "";

		if (args.Length > 1)

		{

			StringBuilder builder = new StringBuilder ();

			int i = 1;

			while (true) {

				builder.Append (args[i]);

				i++;

				if (i == args.Length)

					break;

				builder.Append (" ");

			}

			input = builder.ToString ();

		} else {

			Console.Write ("Input String: ");

			input = Console.ReadLine ();

		}

		automata.Input = input;

		if (automata.Run ())

			Console.WriteLine ("The input stream is OK with the given grammar");

		else

			Console.WriteLine ("The input stream is NOT OK with the given grammar");

	}

}





public class AutomataPila {

	

	Hashtable states = new Hashtable ();



	Stack Pila = new Stack ();



	AutomataPilaState ActualState;

	

	public void SetInitialState (string State) {

		ActualState = GetState (State);

		if (ActualState == null)

			throw new Exception ("There is no connection from the intial state to the other ones");

	}

	

	string input;



	public string Input {

		get {

			return input;

		}

		set {

			input = value;

		}

	}



	public void AddRule (string State, string NextState, char Input, OperationType Operation, char Value) {

		if (!states.ContainsKey (State))

			states[State] = new AutomataPilaState (State);

		if (!states.ContainsKey (NextState))

			states[NextState] = new AutomataPilaState (NextState);

		GetState (State).AddRule (NextState, Input, Operation, Value);

	}



	private AutomataPilaState GetState (string State) {

		return states[State] as AutomataPilaState;

	}



	public bool Run () {

		foreach (char CarAct in input) {

			Rule ruleAct = ActualState.GetRule (CarAct);

			if (ruleAct == null)

				return false;

			switch (ruleAct.Operation) {

				case OperationType.PushOperation :

					Pila.Push (ruleAct.Value);

					break;

				case OperationType.PopOperation :

					if (Pila.Count == 0)

						return false;

					char popedValue = (char) Pila.Pop ();

					if (popedValue != ruleAct.Value)

						return false;

					break;

			}

			ActualState = GetState (ruleAct.NextState);

		}

		if (Pila.Count != 0)

			return false;

		return true;

	}

}







public class AutomataPilaState {



	Hashtable rules = new Hashtable ();



	string name;



	public AutomataPilaState (string Name) {

		name = Name;

	}

	

	public string Name {

		get {

			return name;

		}

	}

	

	public void AddRule (string NextState, char Input, OperationType Operation, char Value) {

		rules[Input] = new Rule (NextState, Input, Operation, Value);

	}

	

	public Rule GetRule (char Input) {

		return rules[Input] as Rule;

	}



}





public class Rule {

	

	string nextState;

	OperationType operation;

	char val;

	char input;



	public Rule (string NextState, char Input, OperationType Operation, char Value) {

		nextState = NextState;

		operation = Operation;

		val = Value;

		input = Input;

	}



	public char Input {

		get {

			return input;

		}

	}

	

	public string NextState {

		get {

			return nextState;

		}

	}



	public OperationType Operation {

		get {

			return operation;

		}

	}



	public char Value {

		get {

			return val;

		}

	}

	

}





public enum OperationType {

	PushOperation,

	PopOperation,

	NullOperation

}