Added fileparser
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
B0
|
||||
E1
|
||||
0>a>1
|
||||
0>b>1
|
||||
1>a>0
|
||||
1>b>1
|
||||
@@ -0,0 +1,8 @@
|
||||
B0
|
||||
E1
|
||||
0>a>1
|
||||
0>b>1
|
||||
0>$>1
|
||||
1>a>0
|
||||
1>b>1
|
||||
1>$>0
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.imegumii;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Created by kenny on 8-6-2017.
|
||||
*/
|
||||
public class FileParser {
|
||||
|
||||
public static DFA<String> readDFA(String filename)
|
||||
{
|
||||
File f = new File("input/" + filename + ".dot");
|
||||
|
||||
Character[] chars = {'a', 'b'};
|
||||
DFA<String> dfa = new DFA<String>(chars);
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
if(line.startsWith("B")) {
|
||||
String[] states = line.substring(1,line.length()).split(",");
|
||||
for(String s : states) { dfa.defineAsStartState(s); }
|
||||
}
|
||||
|
||||
else if(line.startsWith("E")) {
|
||||
String[] states = line.substring(1,line.length()).split(",");
|
||||
for(String s : states) { dfa.defineAsEndState(s); }
|
||||
}
|
||||
|
||||
else {
|
||||
String[] trans = line.split(">");
|
||||
dfa.addTransition(new Transition<String>(trans[0], trans[1].toCharArray()[0], trans[2]));
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return dfa;
|
||||
}
|
||||
|
||||
public static NDFA<String> readNDFA(String filename)
|
||||
{
|
||||
File f = new File("input/" + filename + ".dot");
|
||||
|
||||
Character[] chars = {'a', 'b'};
|
||||
NDFA<String> ndfa = new NDFA<String>(chars);
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
if(line.startsWith("B")) {
|
||||
String[] states = line.substring(1,line.length()).split(",");
|
||||
for(String s : states) { ndfa.defineAsStartState(s); }
|
||||
}
|
||||
|
||||
else if(line.startsWith("E")) {
|
||||
String[] states = line.substring(1,line.length()).split(",");
|
||||
for(String s : states) { ndfa.defineAsEndState(s); }
|
||||
}
|
||||
|
||||
else {
|
||||
String[] trans = line.split(">");
|
||||
|
||||
if(trans[1].equals("$")) {
|
||||
ndfa.addTransition(new Transition<String>(trans[0], Transition.EPSILON, trans[2]));
|
||||
}
|
||||
else
|
||||
ndfa.addTransition(new Transition<String>(trans[0], trans[1].toCharArray()[0], trans[2]));
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return ndfa;
|
||||
}
|
||||
|
||||
public static RegExp readRegex(String filename)
|
||||
{
|
||||
File f = new File("input/" + filename + ".dot");
|
||||
|
||||
BufferedReader br = null;
|
||||
String line = "Error";
|
||||
try {
|
||||
br = new BufferedReader(new FileReader(f));
|
||||
line = br.readLine();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
RegExp regex = new RegExp("a"); //RegExp.parse(line);
|
||||
|
||||
return regex;
|
||||
}
|
||||
}
|
||||
@@ -390,6 +390,15 @@ public class Main {
|
||||
System.out.println(Graph.generateGraphString(of));
|
||||
}
|
||||
|
||||
public static void FileReadTest()
|
||||
{
|
||||
DFA<String> dfa1 = FileParser.readDFA("dfa1");
|
||||
System.out.println(Graph.generateGraphString(dfa1));
|
||||
|
||||
NDFA<String> ndfa1 = FileParser.readNDFA("ndfa1");
|
||||
System.out.println(Graph.generateGraphString(ndfa1));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Practicum1();
|
||||
|
||||
@@ -403,6 +412,8 @@ public class Main {
|
||||
|
||||
// Hopcroft();
|
||||
|
||||
TupleConstructie();
|
||||
// TupleConstructie();
|
||||
|
||||
FileReadTest();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user