A simple Java implementation of a Deterministic Finite Automaton (DFA). This project provides a basic framework to define and run DFAs to recognize specific string patterns based on a given alphabet and a set of state transitions.
The repository includes examples for validating Roman numerals and numeric constants.
automaton.DFAutomaton: The main class that simulates the DFA. It validates input strings against the defined states and transitions.automaton.State: Represents a single state within the automaton, containing its transitions and whether it is a final (accepting) state.
An automaton is defined by an alphabet and an array of states.
- Alphabet: A
String[]. Each string in the array groups characters that trigger the same transition. For example,{"0123456789", ".", "E"}. The index of each string corresponds to a transition path. - States: A
State[]. EachStateobject is initialized with:- An
int[]for transitions. The index of this array corresponds to the index of the symbol in the alphabet. The value at that index is the index of the next state in thestatesarray. Use-1for invalid transitions (trap state). - A
booleanindicating if the state is an accepting (valid) final state.
- An
Instantiate the DFAutomaton class with your alphabet and states array.
DFAutomaton myAutomaton = new DFAutomaton(myAlphabet, myStates);Use the isAccepted(String entry) method to check if a string is recognized by the automaton. It returns true if the string ends in a valid state and false otherwise.
boolean result = myAutomaton.isAccepted("your-test-string");The src/test/DFA.java file contains complete, runnable examples.
This DFA validates a simplified set of Roman numerals.
// Define the alphabet for Roman numerals
String[] romanAlphabet = {"I", "V", "X", "L", "C", "D", "M"};
// Define the state transition table
State[] romanStates = {
new State(new int[]{1, 2, 3, 4, 5, 6, 7}, false),
new State(new int[]{8, 9, 9, -1, -1, -1, -1}, true),
new State(new int[]{1, -1, -1, -1, -1, -1, -1}, true),
new State(new int[]{1, 2, 10, 11, 11, -1, -1}, true),
new State(new int[]{1, 2, 3, -1, -1, -1, -1}, true),
new State(new int[]{1, 2, 3, 4, 12, 13, 13}, true),
new State(new int[]{1, 2, 3, 4, 5, -1, -1}, true),
new State(new int[]{1, 2, 3, 4, 5, 6, 14}, true),
new State(new int[]{9, -1, -1, -1, -1, -1, -1}, true),
new State(new int[]{-1, -1, -1, -1, -1, -1, -1}, true),
new State(new int[]{1, 2, 11, -1, -1, -1, -1}, true),
new State(new int[]{1, 2, -1, -1, -1, -1, -1}, true),
new State(new int[]{1, 2, 3, 4, 13, -1, -1}, true),
new State(new int[]{1, 2, 3, 4, -1, -1, -1}, true),
new State(new int[]{1, 2, 3, 4, 5, 6, 15}, true),
new State(new int[]{1, 2, 3, 4, 5, 6, -1}, false)
};
// Create and test the automaton
DFAutomaton roman = new DFAutomaton(romanAlphabet, romanStates);
System.out.println("Is 'XVI' a valid roman number?: " + roman.isAccepted("XVI")); // true
System.out.println("Is 'IIII' a valid roman number?: " + roman.isAccepted("IIII")); // falseThis DFA validates numeric constants, including integers, decimals, and scientific notation (e.g., 1.5E-2).
// Define the alphabet for numeric constants
String[] numAlphabet = {"1234567890", ".", "E", "+-"};
// Define the state transition table
State[] numStates = {
new State(new int[]{2, 7, -1, 1}, false),
new State(new int[]{2, 7, -1, -1}, false),
new State(new int[]{2, 3, 4, -1}, true),
new State(new int[]{3, -1, 4, -1}, true),
new State(new int[]{6, -1, -1, 5}, false),
new State(new int[]{6, -1, -1, -1}, false),
new State(new int[]{6, -1, -1, -1}, true),
new State(new int[]{3, -1, -1, -1}, false)
};
// Create and test the automaton
DFAutomaton numeric = new DFAutomaton(numAlphabet, numStates);
System.out.println("Is '.E2' a valid numeric constant?: " + numeric.isAccepted(".E2")); // true
System.out.println("Is '1.5E-2' a valid numeric constant?: " + numeric.isAccepted("1.5E-2")); // true
System.out.println("Is '1.E' a valid numeric constant?: " + numeric.isAccepted("1.E")); // falseThe project is a standard Java project (originally configured for NetBeans). You can compile the source files and run the test.DFA class from the command line.
-
Navigate to the
srcdirectory:cd src -
Compile the Java files:
javac automaton/*.java test/*.java
-
Run the main test class:
java test.DFA
You should see the following output:
Is a valid roman number?: true
Is a valid numeric constant?: true
