fixed folder structure + lab4 less hardcode, cleanup

This commit is contained in:
2024-04-28 20:24:11 +03:00
parent b6ae645808
commit c0efefa1a1
17 changed files with 86 additions and 73 deletions

View File

@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="ClassEscapesItsScope" enabled="false" level="WARNING" enabled_by_default="false" />
</profile>
</component>

View File

@@ -1,4 +1,5 @@
import java.util.HashMap;
package Lab1;
import java.util.Map;
import java.util.Set;
@@ -6,7 +7,7 @@ public class FiniteAutomaton {
private final String SS;
private final Set<String> alphabet;
private final Map<String, Map<String, String>> T;
public FiniteAutomaton(String SS, Set<String> VN, Set<String> VT, Map<String, Map<String, String>> T) {
public FiniteAutomaton(String SS, Set<String> VT, Map<String, Map<String, String>> T) {
this.SS = SS;
this.alphabet = VT;
this.T = T;

View File

@@ -1,18 +1,14 @@
package Lab1;
import java.util.*;
public class Grammar {
private final String SS;
private final Set<String> VN;
private final Set<String> VT;
private final Map<String, List<String>> P;
private final FiniteAutomaton FA;
public Grammar(String SS, Set<String> VN, Set<String> VT, Map<String, List<String>> P) {
public Grammar(String SS, Set<String> VT, Map<String, List<String>> P) {
this.SS = SS;
this.VN = VN;
this.VT = VT;
this.P = P;
this.FA = new FiniteAutomaton(SS, VN, VT, generateTransitions(P));
this.FA = new FiniteAutomaton(SS, VT, generateTransitions(P));
}
public Map<String, Map<String, String>> generateTransitions(Map<String, List<String>> P) {
@@ -31,10 +27,6 @@ public class Grammar {
return transitions;
}
public FiniteAutomaton getFA() {
return this.FA;
}
public boolean isValid(String word) {
return this.FA.isValid(word);
}

View File

@@ -1,3 +1,5 @@
package Lab2;
import java.util.List;
import java.util.Map;

View File

@@ -1,3 +1,5 @@
package Lab2;
import util.Pair;
import java.util.*;
@@ -5,13 +7,11 @@ public class ManualFiniteAutomaton {
private final List<String> stateList;
private final Set<String> alphabet;
private final Set<String> acceptingStates;
private final Map<String, List<Pair>> T;
public ManualFiniteAutomaton(List<String> stateList, Set<String> alphabet, Set<String> acceptingStates, Map<String, List<Pair>> T) {
public ManualFiniteAutomaton(List<String> stateList, Set<String> alphabet, Map<String, List<Pair>> T) {
this.stateList = stateList;
this.alphabet = alphabet;
this.acceptingStates = acceptingStates;
this.T = T;
}
@@ -86,7 +86,7 @@ public class ManualFiniteAutomaton {
}
}
// Method to convert the NFA to Grammar
// Method to convert the NFA to Lab1.Grammar
public void toGrammar() {
Map<String, String> mappedStates = mapStates(); // Map original states to single-character states
Map<String, List<String>> grammar = new HashMap<>(); // Create a map to store the grammar rules

View File

@@ -1,3 +1,5 @@
package Lab3;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
@@ -89,7 +91,7 @@ public class Lexer {
}
public static void main(String[] args) {
String input = "(3 + 5) * sqrt(16)";
String input = "(3 + 5) * loge(16)";
try {
List<Token> tokens = tokenize(input);
for (Token token : tokens) {

View File

@@ -1,3 +1,5 @@
package Lab4;
import java.util.Random;
public class RegexGenerator {
@@ -6,31 +8,22 @@ public class RegexGenerator {
String[] randomStrings = new String[numStrings];
Random random = new Random();
System.out.println("=========================");
for (int i = 0; i < numStrings; i++) {
System.out.println("String Nr. " + (i+1));
StringBuilder sb = new StringBuilder();
sb.append("O");
System.out.println("Putting a fixed 'O'");
int length = random.nextInt(1,5);
for (int j = 0; j < length; j++) {
char randomChar = randomChar("PQR");
sb.append(randomChar);
System.out.println("Putting a random char from \"PQR\": " + randomChar);
}
sb.append("2");
System.out.println("Putting a fixed '2'");
char randomDigit = randomChar("34");
sb.append(randomDigit);
System.out.println("Putting a random digit from \"34\": " + randomDigit);
randomStrings[i] = sb.toString();
System.out.println("=========================");
}
return randomStrings;
@@ -40,38 +33,27 @@ public class RegexGenerator {
String[] randomStrings = new String[numStrings];
Random random = new Random();
System.out.println("=========================");
for (int i = 0; i < numStrings; i++) {
System.out.println("String Nr. " + (i+1));
StringBuilder sb = new StringBuilder();
int length1 = random.nextInt(6);
for (int j = 0; j < length1; j++) {
char randomChar = randomChar("A");
sb.append(randomChar);
System.out.println("Putting a random char from \"A\": " + randomChar);
}
sb.append("B");
System.out.println("Putting a fixed 'B'");
char randomChar = randomChar("CDE");
sb.append(randomChar);
System.out.println("Putting a random char from \"CDE\": " + randomChar);
sb.append("F");
System.out.println("Putting a fixed 'F'");
char chr = randomChar("GHI");
sb.append(chr);
System.out.println("Putting a random char from \"GHI\": " + chr);
sb.append(chr);
System.out.println("Repeating the previous random char: " + chr);
randomStrings[i] = sb.toString();
System.out.println("=========================");
}
return randomStrings;
@@ -81,43 +63,28 @@ public class RegexGenerator {
String[] randomStrings = new String[numStrings];
Random random = new Random();
System.out.println("=========================");
for (int i = 0; i < numStrings; i++) {
System.out.println("String Nr. " + (i+1));
StringBuilder sb = new StringBuilder();
int length1 = random.nextInt(1,6);
for (int j = 0; j < length1; j++) {
sb.append("J");
System.out.println("Putting a fixed 'J'");
}
sb.append("J".repeat(Math.max(0, length1)));
sb.append("K");
System.out.println("Putting a fixed 'K'");
length1 = random.nextInt(6);
for (int j = 0; j < length1; j++) {
char randomChar = randomChar("LMN");
sb.append(randomChar);
System.out.println("Putting a random char from \"LMN\": " + randomChar);
}
length1 = random.nextInt(2);
for (int j = 0; j < length1; j++) {
sb.append("O");
System.out.println("Putting a fixed 'O'");
}
sb.append("O".repeat(length1));
length1 = 3;
char chr = randomChar("PQ");
for (int j = 0; j < length1; j++) {
sb.append(chr);
System.out.println("Putting a random char from \"PQ\": " + chr);
}
sb.append(String.valueOf(chr).repeat(length1));
randomStrings[i] = sb.toString();
System.out.println("=========================");
}
return randomStrings;
@@ -133,22 +100,62 @@ public class RegexGenerator {
String[] randomStrings1 = generateRandomNr1(5);
System.out.println("All Strings Nr1: ");
for (String str : randomStrings1) {
System.out.println(str);
explainRandomNr1(str);
}
System.out.println("=========================");
String[] randomStrings2 = generateRandomNr2(5);
System.out.println("All Strings Nr2: ");
for (String str : randomStrings2) {
System.out.println(str);
explainRandomNr2(str);
}
System.out.println("=========================");
String[] randomStrings3 = generateRandomNr3(5);
System.out.println("All Strings Nr3: ");
for (String str : randomStrings3) {
System.out.println(str);
explainRandomNr3(str);
}
System.out.println("=========================");
}
public static void explainRandomNr1(String s) {
System.out.println("Input: " + s);
System.out.println("O -> O");
int mid = s.indexOf('2');
System.out.println("(P|Q|R)+ -> " + s.substring(1, mid));
System.out.println("2 -> 2");
System.out.println("(3|4) -> " + s.charAt(s.length() - 1));
}
public static void explainRandomNr2(String s) {
System.out.println("Input: " + s);
int idx = 0;
while (s.charAt(idx) == 'A') {
idx++;
}
System.out.println("A* -> " + (idx > 0 ? s.substring(0, idx) : "'' (no A's)"));
System.out.println("B -> B");
char cde = s.charAt(idx + 1);
System.out.println("(C|D|E) -> " + cde);
System.out.println("F -> F");
System.out.println("(G|H|I)^2 -> " + s.substring(idx + 3));
}
public static void explainRandomNr3(String s) {
System.out.println("Input: " + s);
int idx = s.indexOf('K');
System.out.println("J+ -> " + s.substring(0, idx));
System.out.println("K -> K");
int nextPart = idx + 1;
while (nextPart < s.length() && "LMN".contains(s.charAt(nextPart) + "")) {
nextPart++;
}
System.out.println("(L|M|N)* -> " + (nextPart > idx + 1 ? s.substring(idx + 1, nextPart) : "'' (no L, M, N)"));
if (nextPart < s.length() && s.charAt(nextPart) == 'O') {
System.out.println("O? -> O");
nextPart++;
}
System.out.println("(P|Q)^3 -> " + s.substring(nextPart));
}
}

View File

@@ -1,4 +1,4 @@
package ast_parser;
package Lab6;
class BinaryOperatorExpr extends Expr {
Expr left;

View File

@@ -1,4 +1,4 @@
package ast_parser;
package Lab6;
abstract class Expr {
abstract double evaluate();

View File

@@ -1,4 +1,4 @@
package ast_parser;
package Lab6;
import java.util.List;

View File

@@ -1,4 +1,4 @@
package ast_parser;
package Lab6;
import java.util.ArrayList;
import java.util.List;

View File

@@ -1,4 +1,4 @@
package ast_parser;
package Lab6;
import java.util.List;

View File

@@ -1,4 +1,4 @@
package ast_parser;
package Lab6;
class NumberExpr extends Expr {
double value;

View File

@@ -1,4 +1,4 @@
package ast_parser;
package Lab6;
import java.util.ArrayList;
import java.util.List;

View File

@@ -1,4 +1,4 @@
package ast_parser;
package Lab6;
public class Token {
public TokenType type;

View File

@@ -1,4 +1,4 @@
package ast_parser;
package Lab6;
public enum TokenType {
NUMBER,

View File

@@ -1,3 +1,6 @@
import Lab1.Grammar;
import Lab2.ChomskyChecker;
import Lab2.ManualFiniteAutomaton;
import util.Pair;
import java.util.Arrays;
@@ -9,7 +12,7 @@ public class Main {
// LAB 1
public static final String SS = "S";
public static final Set<String> VN = Set.of("S", "B", "C");
//public static final Set<String> VN = Set.of("S", "B", "C");
public static final Set<String> VT = Set.of("a", "b", "c");
public static final Map<String, List<String>> P = Map.of(
"S", List.of("aB"),
@@ -19,7 +22,7 @@ public class Main {
// LAB 2
public static final List<String> stateList = Arrays.asList("q0", "q1", "q2");
public static final Set<String> alphabet = Set.of("a", "b");
public static final Set<String> acceptingStates = Set.of("q2");
//public static final Set<String> acceptingStates = Set.of("q2");
public static final Map<String, List<Pair>> transitions= Map.of(
"q0", List.of(new Pair("a", "q0"), new Pair("a", "q1")),
"q1", List.of(new Pair("b", "q2"), new Pair("a", "q0")),
@@ -29,14 +32,14 @@ public class Main {
public static void main(String[] args) {
String toCheck = "aac";
Grammar grammar = new Grammar(SS, VN, VT, P);
Grammar grammar = new Grammar(SS, VT, P);
grammar.generateWords(5);
System.out.println("===================================");
System.out.println("Is " + toCheck + " valid? " + grammar.isValid(toCheck));
System.out.println("===================================");
System.out.println(ChomskyChecker.classifyGrammar(P));
ManualFiniteAutomaton MFA = new ManualFiniteAutomaton(stateList, alphabet, acceptingStates, transitions);
ManualFiniteAutomaton MFA = new ManualFiniteAutomaton(stateList, alphabet, transitions);
System.out.println("===================================");
MFA.toGrammar();
System.out.println("===================================");