Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package labos2.ppj.zemris.fer.hr;
- import java.io.*;
- import java.util.*;
- import java.util.Map.Entry;
- public class Generator {
- private HashMap<String, String> actionMap;
- private HashMap<String, String> gotoMap;
- private ArrayList<String> nonterminalSymbols;
- private HashMap<String, HashSet<String>> grammarProductions;
- private HashSet<String> grammarSymbols;
- private List<Node> DFA;
- private HashMap<String, HashSet<String>> zapocinjeZnakom = new HashMap<String, HashSet<String>>();
- private HashSet<String> bio = new HashSet<String>();
- private HashSet<String> prazniZnakovi = new HashSet<String>();
- public Generator() throws IOException {
- ParserDatoteke2 parser = new ParserDatoteke2();
- grammarProductions = parser.getProdukcijeGramatike();
- grammarSymbols = ParserDatoteke2.getGrammarSymbols();
- actionMap = new HashMap<String, String>();
- gotoMap = new HashMap<String, String>();
- nonterminalSymbols = ParserDatoteke2.getNezavrsniZnakovi();
- for (Entry<String, HashSet<String>> entry : grammarProductions
- .entrySet()) {
- String nezavrsniZnak = entry.getKey();
- HashSet<String> desnaStranaProdukcije = entry.getValue();
- izracunajZapocinjeZnakom(nezavrsniZnak, desnaStranaProdukcije);
- }
- for (Entry<String, HashSet<String>> entry : grammarProductions
- .entrySet()) {
- HashSet<String> tempSet = new HashSet<String>();
- for (String s : entry.getValue()) {
- tempSet.add(" " + s + " ");
- }
- grammarProductions.put(entry.getKey(), tempSet);
- }
- // ispisiMapu(zapocinjeZnakom);
- DFA = generateItems();
- System.out.println("DKA izgeneriran...");
- System.out.println(DFA.size());
- System.out.println();
- generateTables();
- // System.out.println("Action MAP:");
- // for (Entry<String, String> e : actionMap.entrySet()) {
- // System.out.println(e.getKey() + "\t" + e.getValue());
- // }
- // System.out.println("Reduction MAP:");
- // for (Entry<String, String> e : reductionMap.entrySet()) {
- // System.out.println(e.getKey() + "\t" + e.getValue());
- // }
- // System.out.println("GoTo MAP:");
- // for (Entry<String, String> e : gotoMap.entrySet()) {
- // System.out.println(e.getKey() + "\t" + e.getValue());
- // }
- //
- // System.out.println(actionMap.size() + " " + reductionMap.size() + " "
- // + gotoMap.size());
- // upis tablica u datoteku
- BufferedWriter writer = new BufferedWriter(new FileWriter(
- "actionMap.txt"));
- for (String key : actionMap.keySet()) {
- writer.write(key + "&" + actionMap.get(key) + '\n');
- }
- writer.close();
- writer = new BufferedWriter(new FileWriter("gotoMap.txt"));
- for (String key : gotoMap.keySet()) {
- writer.write(key + "&" + gotoMap.get(key) + '\n');
- }
- writer.close();
- writer = new BufferedWriter(new FileWriter(new File("izlaz.txt")));
- for (Node n : DFA) {
- writer.write(n.toString());
- }
- writer.flush();
- writer.close();
- }
- private void generateTables() {
- int i = 0;
- for (Node node : DFA) {
- actionTable(node);
- // System.out.println(i);
- gotoTable(node);
- // System.out.println(i);
- i++;
- }
- }
- private void gotoTable(Node node) {
- for (Entry<String, Integer> entry : node.getTransitionMap().entrySet()) {
- String nonTerminalSymbol = entry.getKey();
- Integer nodePosition = entry.getValue();
- if (nonTerminalSymbol.startsWith("<")) {
- gotoMap.put(node.getID() + "#" + nonTerminalSymbol, Integer
- .toString(nodePosition));
- }
- }
- // for (String nonTerminalSymbol : nonterminalSymbols) {
- //
- // Node newNode = GOTO(nonTerminalSymbol, node, -1);
- //
- // int nodePosition = DFA.indexOf(newNode);
- //
- // if (nodePosition != -1) {
- // gotoMap.put(node.getID() + "#" + nonTerminalSymbol, Integer
- // .toString(nodePosition));
- // }
- // }
- }
- private void actionTable(Node node) {
- Set<Production> nodeProductions = node.getProductionSet();
- for (Production p : nodeProductions) {
- // System.out.println(p.getContext()); //aj da vidimo
- String futureSymbols = p.getRightSide().split("\\*", 2)[1];
- if (!futureSymbols.startsWith("<")) {
- // String terminalSymbol = futureSymbols.split(" ", 2)[0];
- //
- // Node newNode = GOTO(terminalSymbol, node, -1);
- for (Entry<String, Integer> entry : node.getTransitionMap()
- .entrySet()) {
- String terminalSymbol = entry.getKey();
- int nodePosition = entry.getValue();
- if (!terminalSymbol.startsWith("<")) {
- if (actionMap.containsKey(node.getID() + "#"
- + terminalSymbol)
- && !actionMap
- .containsValue("s#" + nodePosition)) {
- System.out.println("Proturjecje! "
- + node.getID()
- + "#"
- + terminalSymbol
- + " "
- + actionMap.get(node.getID() + "#"
- + terminalSymbol) + " != " + "s#"
- + nodePosition);
- if (actionMap.get(
- node.getID() + "#" + terminalSymbol)
- .startsWith("s#")) {
- System.out.println("WTF?");
- } else if (actionMap.get(
- node.getID() + "#" + terminalSymbol)
- .startsWith("r#")) {
- actionMap.put(node.getID() + "#"
- + terminalSymbol, "s#" + nodePosition);
- System.out
- .println("Redukcija zamijenjena pomakom!");
- }
- } else {
- actionMap.put(node.getID() + "#" + terminalSymbol,
- "s#" + nodePosition);
- }
- }
- }
- // int nodePosition = DFA.indexOf(newNode);
- // if (nodePosition != -1) {
- // actionMap.put(node.getID() + "#" + terminalSymbol, "s"
- // + nodePosition);
- // }
- }
- if (futureSymbols.length() == 0) {
- // kako to da kontekst vraca samo jedan zavrsni znak? trebalo bi ih biti vise?
- if (!p.getLeftSide().equals("<S0>")) {
- if (actionMap.containsKey(node.getID() + "#"
- + p.getContext())
- && !actionMap.containsValue("r#"
- + p.getLeftSide()
- + "->"
- + p.getRightSide().split("\\*")[0]
- .substring(1))) {
- System.out
- .println("Proturjecje! "
- + node.getID()
- + "#"
- + p.getContext()
- + " "
- + actionMap.get(node.getID() + "#"
- + p.getContext())
- + " != "
- + "r#"
- + p.getLeftSide()
- + "->"
- + p.getRightSide().split("\\*")[0]
- .substring(1));
- if (actionMap.get(node.getID() + "#" + p.getContext())
- .startsWith("s#")) {
- System.out.println("Ne diraj! s > r");
- } else if (actionMap.get(
- node.getID() + "#" + p.getContext())
- .startsWith("r#")) {
- // actionMap.put(node.getID() + "#" +
- // p.getContext(), "r#"
- // + p.getLeftSide()
- // + "->"
- // + p.getRightSide().split("\\*")[0].substring(1));
- System.out
- .println("Kako najlakse odluciti koju redukciju ostaviti?");
- }
- } else {
- actionMap
- .put(node.getID() + "#" + p.getContext(), "r#"
- + p.getLeftSide()
- + "->"
- + p.getRightSide().split("\\*")[0]
- .substring(1));
- }
- }
- else {
- actionMap.put(node.getID() + "#@", "accept");
- }
- }
- }
- }
- private List<Node> generateItems() {
- List<Node> nodeContainer = new ArrayList<Node>();
- HashSet<String> initialSet = grammarProductions.get("<S0>");
- Production initialProduction = createProduction("<S0>->*"
- + ((String) (initialSet.toArray())[0]).substring(1), "@");
- int i = 0;
- Node initialNode = new Node(i);
- initialNode.addProduction(initialProduction);
- Node node = closure(initialNode);
- nodeContainer.add(node);
- Stack<Node> stackOfNodes = new Stack<Node>();
- stackOfNodes.push(node);
- while (!stackOfNodes.empty()) {
- Node stackedNode = stackOfNodes.pop();
- for (String grammarSymbol : grammarSymbols) {
- i++;
- Node newNode = GOTO(grammarSymbol, stackedNode, i);
- if (newNode.getProductionSet().size() != 0) {
- if (!nodeContainer.contains(newNode)) {
- stackOfNodes.push(newNode);
- stackedNode.addTransition(grammarSymbol, i);
- nodeContainer.add(newNode);
- } else {
- stackedNode.addTransition(grammarSymbol, nodeContainer
- .indexOf(newNode));
- i--;
- }
- } else
- i--;
- }
- }
- return nodeContainer;
- }
- private Node GOTO(String grammarSymbol, Node node, int i) {
- Node newNode = new Node(i);
- for (Production p : node.getProductionSet()) {
- String[] tempRightSide = p.getRightSide().split("\\*", 2);
- if (tempRightSide[1].length() > 0) {
- String X = tempRightSide[1].split(" ", 2)[0];
- if (grammarSymbol.equals(X)) {
- String newRightSide = tempRightSide[1].replaceFirst(" ",
- "\\*");
- if (!newRightSide.endsWith("\\*")) {
- newNode.addProduction(createProduction(p.getLeftSide()
- + "->" + tempRightSide[0] + " " + newRightSide,
- p.getContext()));
- } else {
- newNode.addProduction(createProduction(p.getLeftSide()
- + "->" + tempRightSide[0] + " " + newRightSide
- + " ", p.getContext()));
- }
- }
- }
- }
- return closure(newNode);
- }
- private Node closure(Node node) {
- Stack<Production> stackOfProductions = new Stack<Production>();
- for (Production production : node.getProductionSet()) {
- stackOfProductions.push(production);
- }
- while (!stackOfProductions.empty()) {
- Production stackedProduction = stackOfProductions.pop();
- String[] productionItems = stackedProduction.getRightSide().split(
- "\\*", 2);
- // String alfa = productionItems[0].trim();
- String[] productionSubItems = productionItems[1].split(" ", 2);
- String B = "";
- // System.out.println(stackedProduction.getRightSide());
- String beta = "";
- if (productionSubItems.length > 1) {
- B = productionSubItems[0];
- beta = productionSubItems[1];
- }
- String a = stackedProduction.getContext();
- // System.out.println("alfa = " + alfa + " B = " + B + " beta = "
- // + beta + " a = " + a);
- Production production = null;
- if (grammarProductions.get(B) != null)
- for (String rightSide : grammarProductions.get(B)) {
- for (String terminal : first(beta, a)) {
- String productionName = B + "->*"
- + rightSide.substring(1);
- production = createProduction(productionName, terminal);
- if (!node.containsProduction(production)) {
- node.addProduction(production);
- stackOfProductions.push(production);
- }
- }
- }
- }
- return node;
- }
- private HashSet<String> first(String beta, String a) {
- if (beta.length() == 0) {
- HashSet<String> tempSet = new HashSet<String>();
- tempSet.add(a);
- return tempSet;
- }
- if (!beta.split(" ", 2)[0].startsWith("<")) {
- HashSet<String> tempSet = new HashSet<String>();
- beta = beta.replace("<", " ");
- tempSet.add(beta.split(" ", 2)[0]);
- return tempSet;
- }
- int position = beta.indexOf(">");
- return zapocinjeZnakom.get(beta.substring(0, position + 1));
- }
- private Production createProduction(String productionName, String context) {
- String leftSide = productionName.split("->")[0];
- String rightSide = productionName.split("->")[1];
- Production production = new Production(leftSide, rightSide, context);
- return production;
- }
- // private static void ispisiMapu(HashMap<String, HashSet<String>> mapa) {
- // for (Entry<String, HashSet<String>> entry : mapa.entrySet()) {
- // String kljuc = entry.getKey();
- // HashSet<String> vrijednost = entry.getValue();
- // // System.out.println(kljuc + " => " + vrijednost);
- // }
- // }
- public boolean prazniZnak(String nezavrsniZnak) {
- return prazniZnakovi.contains(nezavrsniZnak);
- }
- private void izracunajZapocinjeZnakom(String lijevaStranaProdukcije,
- HashSet<String> desnaStranaProdukcije) {
- for (String s : desnaStranaProdukcije) {
- String[] svi = s.split(" ");
- String first = svi[0];
- HashSet<String> zapocinjeTmp = new HashSet<String>();
- if (first.startsWith("<") && !bio.contains(first)) {
- bio.add(first);
- izracunajZapocinjeZnakom(first, grammarProductions.get(first));
- zapocinjeTmp.addAll(zapocinjeZnakom.get(first));
- int i = 0;
- while (grammarProductions.get(first).contains("$")
- && i < svi.length - 1) {
- first = svi[++i];
- if (!first.startsWith("<")) {
- zapocinjeTmp.add(first);
- break;
- }
- bio.add(first);
- izracunajZapocinjeZnakom(first, grammarProductions
- .get(first));
- zapocinjeTmp.addAll(zapocinjeZnakom.get(first));
- }
- } else {
- if (!first.equals("$")) {
- if (!first.startsWith("<"))
- zapocinjeTmp.add(first);
- } else
- prazniZnakovi.add(lijevaStranaProdukcije);
- }
- int i = 0;
- while (first.startsWith("<") && i < svi.length - 1
- && prazniZnakovi.contains(first)) {
- first = svi[++i];
- }
- if (first == svi[svi.length - 1] && first.startsWith("<"))
- prazniZnakovi.add(lijevaStranaProdukcije);
- HashSet<String> set = zapocinjeZnakom.get(lijevaStranaProdukcije);
- if (set == null)
- zapocinjeZnakom.put(lijevaStranaProdukcije, zapocinjeTmp);
- else {
- set.addAll(zapocinjeTmp);
- }
- }
- }
- public HashMap<String, String> getActionMap() {
- return actionMap;
- }
- public void setActionMap(HashMap<String, String> actionMap) {
- this.actionMap = actionMap;
- }
- public HashMap<String, String> getGotoMap() {
- return gotoMap;
- }
- public void setGotoMap(HashMap<String, String> gotoMap) {
- this.gotoMap = gotoMap;
- }
- public ArrayList<String> getNonterminalSymbols() {
- return nonterminalSymbols;
- }
- public void setNonterminalSymbols(ArrayList<String> nonterminalSymbols) {
- this.nonterminalSymbols = nonterminalSymbols;
- }
- public HashMap<String, HashSet<String>> getGrammarProductions() {
- return grammarProductions;
- }
- public void setGrammarProductions(
- HashMap<String, HashSet<String>> grammarProductions) {
- this.grammarProductions = grammarProductions;
- }
- public HashSet<String> getGrammarSymbols() {
- return grammarSymbols;
- }
- public void setGrammarSymbols(HashSet<String> grammarSymbols) {
- this.grammarSymbols = grammarSymbols;
- }
- public List<Node> getDFA() {
- return DFA;
- }
- public void setDFA(List<Node> dFA) {
- DFA = dFA;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement