All pastes #758323 Raw Edit

Something

public text v1 · immutable
·published 2007-11-02 02:48 UTC
rendered paste body
import java.io.*;
import java.lang.Exception;
import java.util.StringTokenizer;


public class FSM {
    public static void main (String args[]) {
		if(args.length < 1) {
			System.out.println("Usage:  FSM <file.fsm>");
			System.exit(1);
		}
	
		parseFile(args[0]);
    }
	
	public static void parseFile(String filePath) {
		StringTokenizer st;
		String[]	tokens = new String[100];
		String		str = new String("");
		int			i, n = 0, line = 0, ASF = -1;
		boolean		hasAcceptingState = false, eof = false;
		BufferedReader in;
		
		
		//Create buffered reader from filepath
		try {
			in = new BufferedReader(new FileReader(filePath));
		} catch (IOException ioe) {
			System.out.println(ioe);
			System.exit(1);
		}
		
		//read first line
		try {
			str = in.readLine();
		} catch(IOException ioei) {
			System.out.println(ioei);
			System.exit(1);
		}
		
		while (!eof) {

			line++;
			st = new StringTokenizer(str);
			
			while (st.hasMoreTokens()) {
				tokens[n++] = st.nextToken();
			}
			
			for(i = 0; i < n; i++)
				System.out.println(tokens[i]);
				
			
			//Check state_name
			try {	
				Integer.parseInt(tokens[0]);
			} catch (Exception e) {
				System.out.println("Line: "+line+":  The STATE_NAME must be an integer.  \""+tokens[0]+"\" is not an acceptable STATE_NAME.");
				System.exit(1);
			}
			
			//Check for ASF
			if(tokens[1] == "X") {
				System.out.println("Accepting state.");
				
				//Make sure has no other accepting states
				if(hasAcceptingState & (Integer.parseInt(tokens[0]) != ASF)) {
					System.out.println("Line: "+line+":  Only allowed to have 1 accepting state.  "+ASF+" was the first declared accepting state.");
					System.exit(1);
				}
				else {
					hasAcceptingState = true;
				}
			}
			else {
				//while not end of token, parse NSPs
			}
			
			//read next line
			try {
				str = in.readLine();
			} catch(IOException ioei) {
				eof = true;
			}
        }
	}
}