Skip to content
Snippets Groups Projects
Commit ed1aa282 authored by Noah Heuser's avatar Noah Heuser
Browse files

convert grammar to JSONObject String

parent 7d493a0a
No related branches found
No related tags found
No related merge requests found
......@@ -2,14 +2,52 @@ package json;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONArray;
import org.json.JSONObject;
import type3.Type3GrammarGenerator;
public class GrammarToJSON {
private String createJSONGrammar(Map<String, List<List<String>>> rules) {
JSONArray var = new JSONArray();
return var.toString();
private static String createJSONGrammar(String[] vars, int alphaSize, Map<String, List<List<String>>> rules,
String start) {
JSONObject grammarJSON = new JSONObject();
JSONArray varsJSON = new JSONArray();
for (String v : vars) {
varsJSON.put(v);
}
JSONArray termsJSON = new JSONArray();
for (int i = 97; i < 97 + alphaSize; i++) {
termsJSON.put(((char) i) + "");
}
JSONArray rulesJSON = new JSONArray();
int ruleNo = 1;
for (Entry<String, List<List<String>>> rule : rules.entrySet()) {
String premise = rule.getKey();
for (List<String> conclusio : rule.getValue()) {
JSONObject ruleJSON = new JSONObject();
JSONArray conclusioJSON = new JSONArray(conclusio);
ruleJSON.put("ruleNo", ruleNo++);
ruleJSON.put("premise", premise);
ruleJSON.put("conclusio", conclusioJSON);
rulesJSON.put(ruleJSON);
}
}
grammarJSON.put("variables", varsJSON);
grammarJSON.put("terminals", termsJSON);
grammarJSON.put("rules", rulesJSON);
grammarJSON.put("start", start);
return grammarJSON.toString();
}
public static void main(String[] args) {
Type3GrammarGenerator g = new Type3GrammarGenerator();
String[] vars = { "S", "A", "B" };
Map<String, List<List<String>>> rules = g.genType3GrammarRuleSet(vars, 3, 3, "S");
System.out.println(rules);
System.out.println(createJSONGrammar(vars, 3, rules, "S"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment