Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a question about an assignment in which I need to create a DFA on Java. My program needs to be able to take

I have a question about an assignment in which I need to create a DFA on Java. My program needs to be able to take two commandline arguments, the first should be the text file that will be read and the second will be the input string for the DFA. An example on the text file would be

state x accept

state x acceptstart

state x start

state x start accept

transition p y q

The x's, p's and q's could be any number between [0,1000] and the y (an input string) could be any of the characters 0-9 or lowercase a-z. States that are not accept or start states will not have an input line and everything in the input file is tab delimited.

If, after reading the input string, the DFA accepts the input string, it should write (to standard output) accept and then a space and then a list of states the DFA could possibly end in and each state must only be listed once. If, after reading the input string the DFA rejects it, it should write (to standard output) reject and then a space and then a list of states the DFA could possibly end in. If there is no transition for the given state and input symbol then you should assume that the current branch of computation ends in a non accept state.

My code is as follows:

import java.util.Scanner;

import java.util.Map;

import java.util.HashMap;

import java.lang.String;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

public class DFA {

public static void main(String[] args) {

File file = new File(args[0]);

ArrayList states = new ArrayList<>(255);

try {

Scanner scanner = new Scanner(file);

while(scanner.hasNextLine()) {

String stateOrTransition = scanner.next();

if(stateOrTransition.equals("state")) {

int stateNumber= scanner.nextInt();

states.add(stateNumber);

String acceptOrReject= scanner.next();

}else if(stateOrTransition.equals("transition")) {

int transitionFrom = scanner.nextInt();

String transString = scanner.next();

int transitionTo = scanner.nextInt();

}else{

}

}

} catch (FileNotFoundException ex) {

System.out.println("No File Found!");

return;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases A Beginners Guide

Authors: Andy Oppel

1st Edition

007160846X, 978-0071608466

More Books

Students also viewed these Databases questions