first commit
This commit is contained in:
44
src/FiniteAutomaton.java
Normal file
44
src/FiniteAutomaton.java
Normal file
@@ -0,0 +1,44 @@
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
class FiniteAutomaton {
|
||||
private Set<Character> Q;
|
||||
private Set<Character> Sigma;
|
||||
private Map<Character, Map<Character, Character>> delta;
|
||||
private char q0;
|
||||
private Set<Character> F;
|
||||
|
||||
public FiniteAutomaton(Set<Character> Q, Set<Character> Sigma, Map<Character, Map<Character, Character>> delta,
|
||||
char q0, Set<Character> F) {
|
||||
this.Q = Q;
|
||||
this.Sigma = Sigma;
|
||||
this.delta = delta;
|
||||
this.q0 = q0;
|
||||
this.F = F;
|
||||
}
|
||||
|
||||
public boolean stringBelongToLanguage(final String inputString) {
|
||||
char currentState = q0;
|
||||
for (char c : inputString.toCharArray()) {
|
||||
if (!Sigma.contains(c)) {
|
||||
return false;
|
||||
}
|
||||
if (!delta.get(currentState).containsKey(c)) {
|
||||
return false;
|
||||
}
|
||||
currentState = delta.get(currentState).get(c);
|
||||
}
|
||||
return F.contains(currentState);
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
66
src/Grammar.java
Normal file
66
src/Grammar.java
Normal file
@@ -0,0 +1,66 @@
|
||||
import java.util.*;
|
||||
|
||||
public class Grammar {
|
||||
private Set<Character> VN;
|
||||
private Set<Character> VT;
|
||||
private Map<Character, List<String>> P;
|
||||
private char S;
|
||||
private Random random;
|
||||
|
||||
public Grammar(Set<Character> VN, Set<Character> VT, Map<Character, List<String>> P, char S) {
|
||||
this.VN = VN;
|
||||
this.VT = VT;
|
||||
this.P = P;
|
||||
this.S = S;
|
||||
this.random = new Random(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public String generateString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
generateStringHelper(S, sb);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void generateStringHelper(char symbol, StringBuilder sb) {
|
||||
if (VT.contains(symbol)) {
|
||||
sb.append(symbol);
|
||||
} else {
|
||||
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() {
|
||||
Set<Character> Q = new HashSet<>(VN);
|
||||
Q.addAll(VT);
|
||||
Set<Character> Sigma = new HashSet<>(VT);
|
||||
char q0 = S;
|
||||
Set<Character> F = new HashSet<>();
|
||||
for (char vn : VN) {
|
||||
if (P.get(vn).contains("ε")) {
|
||||
F.add(vn);
|
||||
}
|
||||
}
|
||||
Map<Character, Map<Character, Character>> delta = new HashMap<>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,28 @@
|
||||
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
|
||||
// then press Enter. You can now see whitespace characters in your code.
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// Press Alt+Enter with your caret at the highlighted text to see how
|
||||
// IntelliJ IDEA suggests fixing it.
|
||||
System.out.printf("Hello and welcome!");
|
||||
Set<Character> VN = new HashSet<>(Arrays.asList('S', 'A', 'B'));
|
||||
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';
|
||||
|
||||
// Press Shift+F10 or click the green arrow button in the gutter to run the code.
|
||||
Grammar grammar = new Grammar(VN, VT, P, S);
|
||||
|
||||
FiniteAutomaton finiteAutomaton = grammar.toFiniteAutomaton();
|
||||
|
||||
System.out.println("Generated Strings: ");
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
|
||||
// Press Shift+F9 to start debugging your code. We have set one breakpoint
|
||||
// for you, but you can always add more by pressing Ctrl+F8.
|
||||
System.out.println("i = " + i);
|
||||
String generated = grammar.generateString();
|
||||
System.out.println(i + " " + generated);
|
||||
// System.out.println("Accepted by automaton? " + finiteAutomaton.stringBelongToLanguage(generated));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user