-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpa1.java
118 lines (99 loc) · 3.93 KB
/
pa1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Automata PA 1
* Taylor Coury [email protected]
* Melinda Grad [email protected]
* This program will read in file with DFA spcifications
* and will simulate the DFA on sample strings.
* The program will print out a message indicating whether each
* string has been accepted or rejected.
*/
import java.io.*;
import java.util.Scanner;
public class pa1{
public static void main (String[] args){
// Open and read input from the file specifying the DFA
File file = new File(args[0]);
Scanner file_sc = null;
try{
file_sc = new Scanner(file);
}
catch(FileNotFoundException e){
System.out.println(e);
}
// Get the number of states
String s = file_sc.nextLine();
int num_states = Integer.parseInt(s);
//Get the alphabet
String alphabet = file_sc.nextLine();
//Get number transitions
int num_alpha = alphabet.length();
int num_transitions = num_alpha * num_states;
//Array to hold transitions
int[][] transition_array = new int[num_alpha][num_states];
populateArray(num_transitions, file_sc, num_alpha, alphabet, transition_array);
int start_state = Integer.parseInt(file_sc.next());
//Get the set of accept states
String temp = file_sc.nextLine();
String accept_temp = file_sc.nextLine();
String[] accept_states = accept_temp.split(" ");
int num_accept = accept_states.length;
//Get the input strings to simulate then check against the array
while(file_sc.hasNextLine()){
String input_string = file_sc.nextLine();
int length = input_string.length();
int current = start_state;
for(int i = 0; i < length; i++){
for(int j = 0; j < num_alpha; j++){
if(input_string.charAt(i) == alphabet.charAt(j)){
current = transition_array[j][current-1];
break;
}
}
}
checkResult(num_accept, current, accept_states);
}
file_sc.close();
}
/* Function to check if DFA accepts or rejects
@param num_accept number of accept states
@param current current state
@param accept states set of accept states
*/
public static void checkResult(int num_accept, int current, String[] accept_states){
boolean accept = false;
for(int i = 0; i < num_accept; i++){
if(current == Integer.parseInt(accept_states[i])){
accept = true;
break;
}
}
if(accept)
System.out.println("Accept");
else
System.out.println("Reject");
}
/* Function to populate the array of transitions
@param num_transitions number of transitions
@param file_sc scanner
@param num_alpha number of chars in the alphabet
@param alphabet String containing the alphabet
@param transition array array to hold transitions
*/
public static void populateArray(int num_transitions, Scanner file_sc, int num_alpha,
String alphabet, int[][] transition_array){
//Loop to populate the array of transitions
for(int i=0; i<num_transitions; i++){
//Read in the transition
int curr_state = Integer.parseInt(file_sc.next());
String input = file_sc.next();
int next_state = Integer.parseInt(file_sc.next());
//Get rid of ''
String clean_input = input.replace("'", "");
for(int j=0; j< num_alpha; j++){
if(clean_input.compareTo(Character.toString(alphabet.charAt(j)))==0){
transition_array[j][curr_state-1] = next_state;
break;
}
}
}
}
}