SHARE
TWEET

Untitled

a guest Sep 11th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package br.ufpe.cin.if688.table;
  2.  
  3.  
  4. import br.ufpe.cin.if688.parsing.analysis.*;
  5. import br.ufpe.cin.if688.parsing.grammar.*;
  6.  
  7. import java.util.*;
  8.  
  9. import static br.ufpe.cin.if688.parsing.analysis.SpecialSymbol.EPSILON;
  10.  
  11.  
  12. public final class Table {
  13.     private Table() {    }
  14.  
  15.     public static Map<LL1Key, List<GeneralSymbol>> createTable(Grammar g) throws NotLL1Exception {
  16.         if (g == null) throw new NullPointerException();
  17.  
  18.         Map<Nonterminal, Set<GeneralSymbol>> first =
  19.                 SetGenerator.getFirst(g);
  20.         Map<Nonterminal, Set<GeneralSymbol>> follow =
  21.                 SetGenerator.getFollow(g, first);
  22.  
  23.         Map<LL1Key, List<GeneralSymbol>> parsingTable =
  24.                 new HashMap<LL1Key, List<GeneralSymbol>>();
  25.  
  26.         /*
  27.             Implemente sua table aqui.
  28.          */
  29.  
  30.         for(Production p: g.getProductions()){
  31.             if(hasEmptyWord(p)){
  32.                 for(GeneralSymbol gs: follow.get(p.getNonterminal())){
  33.                     parsingTable.put(new LL1Key(p.getNonterminal(), new Terminal(gs.toString())), p.getProduction());
  34.                 }
  35.             } else {
  36.                 if(SetGenerator.isTerminal(g, p.getProduction().get(0))){
  37.                     parsingTable.put(new LL1Key(p.getNonterminal(), new Terminal(p.getProduction().get(0).toString())), p.getProduction());
  38.                 } else {
  39.                     for(GeneralSymbol gs: first.get(p.getNonterminal())){
  40.                         parsingTable.put(new LL1Key(p.getNonterminal(), new Terminal(gs.toString())), p.getProduction());
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.  
  46.         // ------------------------------ Aqui já tem os prints para voce ------------
  47.  
  48.         System.out.print(g);
  49.         System.out.println(first);
  50.         System.out.println(follow);
  51.         System.out.println(sortTable(parsingTable));
  52.  
  53.         return sortTable(parsingTable);
  54.     }
  55.  
  56.     static boolean hasEmptyWord(Production p){
  57.         for(GeneralSymbol t: p.getProduction()){
  58.             if(t.toString().equals("^")){
  59.                 return true;
  60.             }
  61.         }
  62.         return false;
  63.     }
  64.  
  65.  
  66.     static private Map<LL1Key, List<GeneralSymbol>> sortTable( Map<LL1Key, List<GeneralSymbol>> parsingTable) {
  67.         // This sorts only the key, as the values in the set must be in the order of the rule
  68.         Map<LL1Key, List<GeneralSymbol>> sortedMap = new TreeMap<LL1Key, List<GeneralSymbol>>(new Comparator<LL1Key>() {
  69.             @Override
  70.             public int compare(LL1Key t1, LL1Key t2) {
  71.                 return t1.toString().compareTo(t2.toString());
  72.             }
  73.         });
  74.  
  75.         parsingTable.forEach((k,v)->{
  76.             sortedMap.put(k, v);
  77.         });
  78.  
  79.         return sortedMap;
  80.     }
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top