-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglc.java
123 lines (95 loc) · 3.63 KB
/
glc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* @author Gabriel de Castro Michelassi - 11208162
* @author Guilherme Balog Gardino - 11270649
*/
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class glc {
public static void main(String[] args) {
// abrir arquivo
String specificationFilePath = "inp-glc.txt";
String testCasesFilePath = "inp-cadeias.txt";
if(args.length == 2){
specificationFilePath = args[0];
testCasesFilePath = args[1];
}
String outputFileFilePath = "out-status.txt";
Scanner specificationFile = null;
Scanner testCasesFile = null;
PrintWriter outputFile = null;
try{
specificationFile = new Scanner (new BufferedReader(new FileReader(specificationFilePath)));
testCasesFile = new Scanner (new BufferedReader(new FileReader(testCasesFilePath)));
outputFile = new PrintWriter(outputFileFilePath);
validateInputsWithCfg(specificationFile, testCasesFile, outputFile);
} catch(FileNotFoundException ex){
System.out.println("File not found");
}finally{
if(specificationFile != null){
specificationFile.close();
}
if(testCasesFile != null){
testCasesFile.close();
}
if(outputFile != null){
outputFile.close();
}
}
}
private static void validateInputsWithCfg(Scanner specificationFile, Scanner testCasesFile, PrintWriter outputFile) {
int numberOfGrammars = specificationFile.nextInt();
for (int i = 0; i < numberOfGrammars; i++) {
ContextFreeGrammar grammar = buildCfgFromFile(specificationFile);
String[] testCases = getTestCases(testCasesFile);
boolean[] testsResults = new boolean[testCases.length];
for (int j = 0; j < testCases.length; j++) {
testsResults[j] = grammar.validate(testCases[j]);
}
outputFile.println(buildReportLine(testsResults));
}
}
public static ContextFreeGrammar buildCfgFromFile(Scanner file) {
int n_variables = file.nextInt();
int n_symbols = file.nextInt();
int n_rules = file.nextInt();
file.nextLine();
String[] variables = file.nextLine().split(" ");
String[] symbols = file.nextLine().split(" ");
@SuppressWarnings("unchecked")
List<String>[] rules = new LinkedList[n_rules];
for (int i = 0; i < n_rules; i++) {
rules[i] = new LinkedList<String>();
String[] rule = file.nextLine().split(" ");
for(String productionMember : rule){
if(productionMember.equals("=>"))
continue;
rules[i].add(productionMember);
}
}
return new ContextFreeGrammar(variables, symbols, rules);
}
public static String[] getTestCases(Scanner file) {
int n_tests = file.nextInt();
file.nextLine();
String[] tests = new String[n_tests];
for (int i = 0; i < n_tests; i++) {
tests[i] = file.nextLine();
}
return tests;
}
public static String buildReportLine(boolean[] results) {
String resp = "";
for (boolean i : results) {
if (i)
resp += "1 ";
else
resp += "0 ";
}
return resp.trim();
}
}