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.
An automaton is defined by an alphabet and an array of states.
This DFA validates a simplified set of Roman numerals.
// Define the alphabet for Roman numeralsString[]romanAlphabet = {"I","V","X","L","C","D","M"};// Define the state transition tableState[]romanStates = {newState(newint[]{1,2,3,4,5,6,7},false),newState(newint[]{8,9,9, -1, -1, -1, -1},true),newState(newint[]{1, -1, -1, -1, -1, -1, -1},true),newState(newint[]{1,2,10,11,11, -1, -1},true),newState(newint[]{1,2,3, -1, -1, -1, -1},true),newState(newint[]{1,2,3,4,12,13,13},true),newState(newint[]{1,2,3,4,5, -1, -1},true),newState(newint[]{1,2,3,4,5,6,14},true),newState(newint[]{9, -1, -1, -1, -1, -1, -1},true),newState(newint[]{-1, -1, -1, -1, -1, -1, -1},true),newState(newint[]{1,2,11, -1, -1, -1, -1},true),newState(newint[]{1,2, -1, -1, -1, -1, -1},true),newState(newint[]{1,2,3,4,13, -1, -1},true),newState(newint[]{1,2,3,4, -1, -1, -1},true),newState(newint[]{1,2,3,4,5,6,15},true),newState(newint[]{1,2,3,4,5,6, -1},false)};// Create and test the automatonDFAutomatonroman =newDFAutomaton(romanAlphabet,romanStates);System.out.println("Is 'XVI' a valid roman number?: " +roman.isAccepted("XVI"));// trueSystem.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 constantsString[]numAlphabet = {"1234567890",".","E","+-"};// Define the state transition tableState[]numStates = {newState(newint[]{2,7, -1,1},false),newState(newint[]{2,7, -1, -1},false),newState(newint[]{2,3,4, -1},true),newState(newint[]{3, -1,4, -1},true),newState(newint[]{6, -1, -1,5},false),newState(newint[]{6, -1, -1, -1},false),newState(newint[]{6, -1, -1, -1},true),newState(newint[]{3, -1, -1, -1},false)};// Create and test the automatonDFAutomatonnumeric =newDFAutomaton(numAlphabet,numStates);System.out.println("Is '.E2' a valid numeric constant?: " +numeric.isAccepted(".E2"));// trueSystem.out.println("Is '1.5E-2' a valid numeric constant?: " +numeric.isAccepted("1.5E-2"));// trueSystem.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 thetest.DFA class from the command line.