Fixed grammar and transitions, + chose wrong variant

This commit is contained in:
2024-02-26 00:34:22 +02:00
parent 0b0181c58b
commit a9fe32831f
4 changed files with 121 additions and 107 deletions

View File

@@ -1,44 +1,36 @@
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
class FiniteAutomaton { public class FiniteAutomaton {
private Set<Character> Q; private final Set<String> ALPHABET; // Set of symbols in the alphabet
private Set<Character> Sigma; private final Map<String, Map<String, String>> P; // Transition function represented as a map of maps
private Map<Character, Map<Character, Character>> delta; private final String SS; // Starting state
private char q0;
private Set<Character> F;
public FiniteAutomaton(Set<Character> Q, Set<Character> Sigma, Map<Character, Map<Character, Character>> delta, // Constructor to initialize the FiniteAutomaton with the alphabet, transition function, and starting state
char q0, Set<Character> F) { public FiniteAutomaton(Set<String> ALPHABET, Map<String, Map<String, String>> P, String SS) {
this.Q = Q; this.ALPHABET = ALPHABET; // Initialize the alphabet
this.Sigma = Sigma; this.P = P; // Initialize the transition function
this.delta = delta; this.SS = SS; // Initialize the starting state
this.q0 = q0;
this.F = F;
} }
public boolean stringBelongToLanguage(final String inputString) { // Method to validate an input string against the finite automaton
char currentState = q0; public boolean isValid(String input) {
for (char c : inputString.toCharArray()) { String currentState = SS; // Start from the initial state
if (!Sigma.contains(c)) {
return false; // Iterate over each symbol in the input string
} for (char symbol : input.toCharArray()) {
if (!delta.get(currentState).containsKey(c)) { String symbolStr = String.valueOf(symbol); // Convert the symbol to a string
return false;
} // Check if the symbol is in the alphabet
currentState = delta.get(currentState).get(c); if (!ALPHABET.contains(symbolStr)) return false; // If not, the input is invalid
// Check if there is a transition defined for the current state and symbol
if (P.containsKey(currentState) && P.get(currentState).containsKey(symbolStr))
currentState = P.get(currentState).get(symbolStr); // Move to the next state
else
return false; // If no transition is defined, the input is invalid
} }
return F.contains(currentState); // Check if the final state is the "OK" state, indicating the input is valid
return currentState.equals("OK");
} }
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("States (Q): ").append(Q).append("\n");
sb.append("Alphabet (Sigma): ").append(Sigma).append("\n");
sb.append("Transition Function (delta): ").append(delta).append("\n");
sb.append("Initial State (q0): ").append(q0).append("\n");
sb.append("Accepting States (F): ").append(F);
return sb.toString();
}
}

View File

@@ -1,66 +1,91 @@
import java.util.*; import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
public class Grammar { public class Grammar {
private Set<Character> VN; // Instance variables to hold the grammar components
private Set<Character> VT; private final String SS; // Starting symbol
private Map<Character, List<String>> P; private final Set<String> VN; // Set of non-terminal symbols
private char S; private final Map<String, List<String>> P; // Production rules
private Random random; private final FiniteAutomaton FA; // Finite Automaton representation of the grammar
public Grammar(Set<Character> VN, Set<Character> VT, Map<Character, List<String>> P, char S) { // Constructor to initialize the Grammar object with the provided grammar components.
this.VN = VN; public Grammar(String SS, Set<String> VN, Set<String> VT, Map<String, List<String>> P) {
this.VT = VT; this.SS = SS; // Initialize the starting symbol
this.P = P; this.VN = VN; // Initialize the set of non-terminal symbols
this.S = S; this.P = P; // Initialize the production rules
this.random = new Random(System.currentTimeMillis()); // Convert the grammar to a Finite Automaton for validation.
// The Finite Automaton will use the terminal symbols and transitions built from the production rules.
this.FA = new FiniteAutomaton(VT, buildTransitions(P), SS);
} }
// Method to generate and print a specified number of strings and their validity.
public void generateNeededStrings(int num) {
System.out.println("Strings + Validity:");
// Loop to generate and check validity for each string
for (int i = 0; i < num; i++) {
// Generate a random string according to the grammar.
String generatedString = generateString();
// Check if the generated string is valid and print its validity.
System.out.println((i + 1) + ": " + generatedString + " " + (isValid(generatedString) ? "is valid" : "is not valid"));
}
}
// Method to check if a given word is valid according to the grammar.
public boolean isValid(String word) {
// Delegate the validation to the Finite Automaton representation of the grammar.
return FA.isValid(word);
}
// Method to generate a random string according to the grammar.
public String generateString() { public String generateString() {
StringBuilder sb = new StringBuilder(); // Start with the starting symbol.
generateStringHelper(S, sb); String result = getRandomProduction(SS);
return sb.toString(); boolean containsNonTerminal = true;
// Continue replacing non-terminals with their productions until no more non-terminals remain.
while (containsNonTerminal) {
containsNonTerminal = false;
// Iterate over each character in the current string.
for (char entry : result.toCharArray()) {
String entryString = String.valueOf(entry);
// If the character is a non-terminal, replace it with a random production.
if (VN.contains(entryString)) {
result = result.replaceFirst(entryString, getRandomProduction(entryString));
containsNonTerminal = true;
}
}
}
return result;
} }
private void generateStringHelper(char symbol, StringBuilder sb) { // Method to get a random production for a given non-terminal symbol.
if (VT.contains(symbol)) { private String getRandomProduction(String nonTerminal) {
sb.append(symbol); // Get the list of productions for the given non-terminal and select one randomly.
} else { return P.get(nonTerminal).get(ThreadLocalRandom.current().nextInt(P.get(nonTerminal).size()));
List<String> productions = P.get(symbol);
String chosenProduction = productions.get(random.nextInt(productions.size()));
for (char c : chosenProduction.toCharArray()) {
generateStringHelper(c, sb);
}
}
} }
public FiniteAutomaton toFiniteAutomaton() { // Method to build transitions for the Finite Automaton representation of the grammar.
Set<Character> Q = new HashSet<>(VN); private Map<String, Map<String, String>> buildTransitions(Map<String, List<String>> productions) {
Q.addAll(VT); // Initialize a map to hold the transitions for each state (non-terminal symbol).
Set<Character> Sigma = new HashSet<>(VT); Map<String, Map<String, String>> transitions = new HashMap<>();
char q0 = S; // Iterate over each production rule.
Set<Character> F = new HashSet<>(); for (Map.Entry<String, List<String>> entry : productions.entrySet()) {
for (char vn : VN) { String state = entry.getKey(); // Get the non-terminal symbol as the state.
if (P.get(vn).contains("ε")) { List<String> productionList = entry.getValue(); // Get the list of productions for the state.
F.add(vn); Map<String, String> stateTransitions = new HashMap<>();
// Iterate over each production.
for (String production : productionList) {
String symbol = production.substring(0, 1); // Get the first character as the symbol.
// Get the next state as the rest of the production, or "OK" if it's empty.
String nextState = production.length() > 1 ? production.substring(1) : "OK";
stateTransitions.put(symbol, nextState); // Add the transition to the state's transitions.
} }
transitions.put(state, stateTransitions); // Add the state and its transitions to the overall transitions map.
} }
Map<Character, Map<Character, Character>> delta = new HashMap<>(); return transitions;
for (char q : Q) {
delta.put(q, new HashMap<>());
for (char c : Sigma) {
delta.get(q).put(c, ' ');
}
}
for (char vn : VN) {
if (!P.containsKey(vn)) {
P.put(vn, Collections.emptyList());
}
for (String production : P.get(vn)) {
char nextState = production.charAt(0);
char inputSymbol = production.length() > 1 ? production.charAt(1) : ' ';
delta.get(vn).put(inputSymbol, nextState);
}
}
return new FiniteAutomaton(Q, Sigma, delta, q0, F);
} }
} }

View File

@@ -1,28 +1,25 @@
import java.util.*; import java.util.*;
public class Main { public class Main {
// Define the starting symbol, non-terminal symbols, terminal symbols, and production rules
public static final String SS = "S"; // Starting symbol
public static final Set<String> VN = Set.of("S", "B", "C"); // Non-terminal symbols
public static final Set<String> VT = Set.of("a", "b", "c"); // Terminal symbols
public static final Map<String, List<String>> P = Map.of( // Production rules
"S", List.of("aB"), // Production rule for S
"B", List.of("aC", "bB"), // Production rule for B
"C", List.of("bB", "c", "aS")); // Production rule for C
public static void main(String[] args) { public static void main(String[] args) {
Set<Character> VN = new HashSet<>(Arrays.asList('S', 'A', 'B')); String toCheck = "aac"; // Word to check
Set<Character> VT = new HashSet<>(Arrays.asList('a', 'b', 'c', 'd'));
Map<Character, List<String>> P = new HashMap<>();
P.put('S', Arrays.asList("bS", "dA"));
P.put('A', Arrays.asList("aA", "dB", "b"));
P.put('B', Arrays.asList("cB", "a"));
char S = 'S';
Grammar grammar = new Grammar(VN, VT, P, S);
FiniteAutomaton finiteAutomaton = grammar.toFiniteAutomaton();
System.out.println("Generated Strings: ");
for (int i = 1; i <= 5; i++) {
String generated = grammar.generateString();
System.out.println(i + " " + generated);
// System.out.println("Accepted by automaton? " + finiteAutomaton.stringBelongToLanguage(generated));
}
// Create a Grammar object with the defined grammar
Grammar grammar = new Grammar(SS, VN, VT, P);
// Generate strings needed for the validation process
grammar.generateNeededStrings(5);
// Check if the given word is valid according to the grammar
System.out.println("Is " + toCheck + " valid? " + grammar.isValid(toCheck));
} }
} }

Binary file not shown.