Directory:Derek Elder/Programs/FiniteAutomata

< Directory:Derek Elder‎ | Programs
Revision as of 23:02, 2 November 2009 by Derek Elder (talk | contribs) (+Program)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
//Program 1d
//Professor Pattis, ICS-23 Lab 1
//Programmers: Cameron Ruatta, Derek Elder

import edu.uci.ics.pattis.introlib.Prompt;
import edu.uci.ics.pattis.introlib.TypedBufferReader;
import edu.uci.ics.pattis.ics23.collections.*;
import edu.uci.ics.pattis.ics23.collections.Map.Entry;

import java.util.StringTokenizer;
import java.io.EOFException;

public class FA
{
	public static void main(String[] args)
	{		
		Map<String,Map<String,String>> finiteAutomatonMap = new ArrayMap<String,Map<String,String>>();
		TypedBufferReader tbr = new TypedBufferReader("Enter Finite Automaton Description File");
		//Repeatedly read lines from a file until the EOFException is thrown.
		for(;;)
		{
			try
			{
				String line = tbr.readLine();
				StringTokenizer st = new StringTokenizer(line, ";");
				String initialState = st.nextToken();
				
				while(st.hasMoreTokens())
				{
					String token = st.nextToken();
					String token2 = st.nextToken();

					//Update map for the current state
					Map<String,String> stateMap = finiteAutomatonMap.get(initialState);
					if(stateMap == null)
					{
						stateMap = new ArrayMap<String,String>();
						finiteAutomatonMap.put(initialState,stateMap);
					}
					stateMap.put(token,token2);
				}
			} catch(EOFException e) {break;}
		}

		System.out.println("Finite Automaton");
		List<String> stateList = new ArrayList<String>(finiteAutomatonMap.keys());
		Collections.sort(stateList);
		for(String states : stateList)
		{
			Map<String,String> transitions = finiteAutomatonMap.get(states);
			System.out.println(states + " transitions = " + transitions);
		}	
		
		tbr = new TypedBufferReader("Enter start state/inputs file");
		String initialState = "", finalState = "";
		for(;;)
		{
			try
			{
				String line = tbr.readLine();
				StringTokenizer st = new StringTokenizer(line, ";");
				initialState = st.nextToken();
				String currentState = initialState;
				System.out.println("Finite Automaton");
				System.out.println("Initial state = " + initialState);
				
				while(st.hasMoreTokens())
				{
					String token = st.nextToken();

					currentState = finiteAutomatonMap.get(currentState).get(token);
					System.out.println("input = " + token + "; new state = " + currentState);
					finalState = currentState;
				}
			} catch(EOFException e) {break;}
		}
		System.out.println("Final state = " + finalState);
	}
}