Advertisement
Guest User

MiM

a guest
Nov 12th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.38 KB | None | 0 0
  1. import java.awt.image.AreaAveragingScaleFilter;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.HashSet;
  5. import java.util.Scanner;
  6.  
  7. public class compilerLab {
  8.     public static HashSet <String> Func, Arithmetic, Relational, Logical, StateChange, Textual, keys, varOp, Literals;
  9.     public static HashSet <String> outputFunctions, outputKeys, ArithmeticOp, RelationalOp, LogicalOp, StateChangeOp, TextualOp, literalsOut;
  10.     public static void initialize()
  11.      {
  12.          Literals = new HashSet<>();
  13.          literalsOut = new HashSet<>();
  14.          varOp = new HashSet<>();
  15.         keys = new HashSet<>();
  16.         Func = new HashSet<>();
  17.         Arithmetic = new HashSet<>();
  18.         Relational = new HashSet<>();
  19.         Logical = new HashSet<>();
  20.         StateChange = new HashSet<>();
  21.         Textual = new HashSet<>();
  22.         Collections.addAll(Literals, "{", "}", ";", ",");
  23.          Collections.addAll(keys, "int", "double", "float", "char", "for", "if", "else","break", "goto", "const");
  24.          Collections.addAll(Func, "printf", "scanf", "cin", "cout", "main");
  25.          Collections.addAll(Arithmetic, "+", "-", "*", "/", "%");
  26.          Collections.addAll(Relational, "==", "!=", "<", ">", "<=", ">=");
  27.          Collections.addAll(Logical, "!", "&&", "||");
  28.          Collections.addAll(StateChange, "++", "--", "=", "+=", "-=", "/=", "%=");
  29.          Collections.addAll(Textual, "%d", "%f", "lf", "c");
  30.          outputKeys = new HashSet<>();
  31.          ArithmeticOp = new HashSet<>();
  32.          Relational = new HashSet<>();
  33.          Logical = new HashSet<>();
  34.          LogicalOp = new HashSet<>();
  35.          StateChange = new HashSet<>();
  36.          Textual = new HashSet<>();
  37.          TextualOp = new HashSet<>();
  38.          outputFunctions = new HashSet<>();
  39.          RelationalOp = new HashSet<>();
  40.          Literals = new HashSet<>();
  41.          literalsOut = new HashSet<>();
  42.          StateChangeOp = new HashSet<>();
  43.      }
  44.      public static void checkIfFunc(String data)
  45.      {
  46.          if(Func.contains(data))
  47.          {
  48.              outputFunctions.add(data);
  49.          }
  50.  
  51.      }
  52.      public  static void checkLiterals(String data)
  53.      {
  54.          if(Literals.contains(data))
  55.              literalsOut.add(data);
  56.      }
  57.      public static void checkVariable(String data)
  58.      {
  59.              if(data.contains("()"))
  60.                 {
  61.                     outputFunctions.add(data);
  62.                     return;
  63.                 }
  64.  
  65.          varOp.add(data);
  66.      }
  67.      public static void checkIfKeys(String data)
  68.      {
  69.          if(keys.contains(data))
  70.          {
  71.                 outputKeys.add(data);
  72.          }
  73.      }
  74.      public static void checkIfops(String data)
  75.      {
  76.          if(Arithmetic.contains(data))
  77.              ArithmeticOp.add(data);
  78.          else if(Relational.contains(data))
  79.              RelationalOp.add(data);
  80.          else if(Logical.contains(data))
  81.              LogicalOp.add(data);
  82.          else if(StateChange.contains(data))
  83.              StateChangeOp.add(data);
  84.          else if(Textual.contains(data))
  85.              TextualOp.add(data);
  86.      }
  87.      public static void main(String args[])
  88.      {
  89.          initialize();
  90.          String data;
  91.          Scanner scan = new Scanner(System.in);
  92.          data = scan.nextLine();
  93.          String[] input = data.split(
  94.                  "\\s+"
  95.          );
  96.          for(int i=0; i<input.length; i++)
  97.          {
  98.              String out = input[i];
  99.              checkIfFunc(out);
  100.              checkIfKeys(out);
  101.              checkIfops(out);
  102.              checkVariable(out);
  103.              checkLiterals(out);
  104.          }
  105.          if(outputFunctions.isEmpty()!=true)
  106.          {
  107.              System.out.println("Functions:");
  108.              outputFunctions.forEach(e -> System.out.print(e + " "));
  109.              System.out.println();
  110.          }
  111.          System.out.println("Operators");
  112.          if(ArithmeticOp.isEmpty()!=true)
  113.          {
  114.              System.out.println("Arithmetic:");
  115.              ArithmeticOp.forEach(e -> System.out.print(e + " "));
  116.              System.out.println();
  117.          }
  118.          if(RelationalOp.isEmpty()!=true)
  119.          {
  120.              System.out.println("Relational:");
  121.              RelationalOp.forEach(e -> System.out.print(e + " "));
  122.              System.out.println();
  123.          }
  124.          if(LogicalOp.isEmpty()!=true)
  125.          {
  126.              System.out.println("Logical:");
  127.              LogicalOp.forEach(e -> System.out.print(e + " "));
  128.              System.out.println();
  129.          }
  130.          if(StateChangeOp.isEmpty()!=true)
  131.          {
  132.              System.out.println("State Change:");
  133.              StateChangeOp.forEach(e -> System.out.print(e + " "));
  134.              System.out.println();
  135.          }
  136.          if(TextualOp.isEmpty()!=true)
  137.          {
  138.              System.out.println("Textual:");
  139.              TextualOp.forEach(e -> System.out.print(e + " "));
  140.              System.out.println();
  141.          }
  142.          if(literalsOut.isEmpty()!=true)
  143.          {
  144.              System.out.println("Literals:");
  145.              literalsOut.forEach(e -> System.out.print(e + " "));
  146.              System.out.println();
  147.          }
  148.          if(outputKeys.isEmpty()!=true)
  149.          {
  150.              System.out.println("Keyword:");
  151.              outputKeys.forEach(e -> System.out.print(e + " "));
  152.              System.out.println();
  153.          }
  154.      }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement