SHARE
TWEET

Untitled

a guest Mar 26th, 2020 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package HW3;
  2.  
  3. import java.util.*;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import java.io.StreamTokenizer;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.StringTokenizer;
  13.  
  14. public class first {
  15.    
  16.     private static final String FILE1 = "src/G1.txt";
  17.     private static final String FILE2 = "src/G2.txt";
  18.     private static ArrayList<Rule> rules = new ArrayList<Rule>();
  19.    
  20.     public static void main(String[] args) throws FileNotFoundException {
  21.        
  22.         File file = new File(FILE1);
  23.         Scanner in = new Scanner(file);
  24.        
  25.         int cur = 0;
  26.         Symbol tempTerm = new Symbol("", false);
  27.         ArrayList<Symbol> tempOrder = new ArrayList<Symbol>();
  28.        
  29.         while(in.hasNextLine()) {
  30.             String nextLine = in.nextLine();
  31.             if(nextLine.equals("$")) {
  32.                 rules.add(new Rule(tempTerm, tempOrder));
  33.                 cur = 0;
  34.             } else if(cur == 0) {
  35.                 tempOrder = new ArrayList<Symbol>();
  36.                 tempTerm = new Symbol(nextLine, isTerminalSymbol(nextLine));
  37.                 cur++;
  38.             } else {
  39.                 tempOrder.add(new Symbol(nextLine, isTerminalSymbol(nextLine)));
  40.             }
  41.         }
  42.        
  43.         System.out.println(rules.get(2).getNTS());
  44.        
  45.        
  46.         for(int i = 0; i < rules.size(); i++) {
  47.             System.out.print(rules.get(i).getNTS() + " = {" );
  48.             findFirst(rules.get(i));
  49.             System.out.print("}");
  50.             System.out.println(" ");
  51.         }
  52.        
  53.     }
  54.    
  55.     public static void findFirst(Rule rule) {
  56.         ArrayList<Symbol> ord = rule.getOrder();
  57.         Symbol firstSymbol = ord.get(0);
  58.         if(firstSymbol.isTerminal()) {
  59.             System.out.print(" " + firstSymbol.getTerm() + " ");
  60.         } else {
  61.             for(int i = 0; i < rules.size(); i++) {
  62.                 if(firstSymbol.getTerm().equalsIgnoreCase(rules.get(i).getNTS())) {
  63.                     if(rule.getNTS().equalsIgnoreCase(rules.get(i).getNTS())) {
  64.                         continue;
  65.                     }
  66.                     findFirst(rules.get(i));
  67.                 }
  68.             }
  69.         }
  70.     }
  71.    
  72.     public static boolean isTerminalSymbol(String s) {
  73.         return !Character.isUpperCase(s.toCharArray()[0]);
  74.     }
  75. }
  76.  
  77. class Rule {
  78.     Symbol term;
  79.     ArrayList<Symbol> order;
  80.     public Rule(Symbol term, ArrayList<Symbol> order) {
  81.         this.term = term;
  82.         this.order = order;
  83.     }
  84.     public String getNTS() {
  85.         return term.getTerm();
  86.     }
  87.     public ArrayList<Symbol> getOrder() {
  88.         return order;
  89.     }
  90. }
  91.  
  92. class Symbol {
  93.     String term;
  94.     boolean terminal;
  95.     public Symbol(String term, boolean terminal) {
  96.         this.term = term;
  97.         this.terminal = terminal;
  98.     }
  99.     public boolean isTerminal() {
  100.         return terminal;
  101.     }
  102.     public String getTerm() {
  103.         return term;
  104.     }
  105. }
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
Top