Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.AreaAveragingScaleFilter;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.Scanner;
- public class compilerLab {
- public static HashSet <String> Func, Arithmetic, Relational, Logical, StateChange, Textual, keys, varOp, Literals;
- public static HashSet <String> outputFunctions, outputKeys, ArithmeticOp, RelationalOp, LogicalOp, StateChangeOp, TextualOp, literalsOut;
- public static void initialize()
- {
- Literals = new HashSet<>();
- literalsOut = new HashSet<>();
- varOp = new HashSet<>();
- keys = new HashSet<>();
- Func = new HashSet<>();
- Arithmetic = new HashSet<>();
- Relational = new HashSet<>();
- Logical = new HashSet<>();
- StateChange = new HashSet<>();
- Textual = new HashSet<>();
- Collections.addAll(Literals, "{", "}", ";", ",");
- Collections.addAll(keys, "int", "double", "float", "char", "for", "if", "else","break", "goto", "const");
- Collections.addAll(Func, "printf", "scanf", "cin", "cout", "main");
- Collections.addAll(Arithmetic, "+", "-", "*", "/", "%");
- Collections.addAll(Relational, "==", "!=", "<", ">", "<=", ">=");
- Collections.addAll(Logical, "!", "&&", "||");
- Collections.addAll(StateChange, "++", "--", "=", "+=", "-=", "/=", "%=");
- Collections.addAll(Textual, "%d", "%f", "lf", "c");
- outputKeys = new HashSet<>();
- ArithmeticOp = new HashSet<>();
- Relational = new HashSet<>();
- Logical = new HashSet<>();
- LogicalOp = new HashSet<>();
- StateChange = new HashSet<>();
- Textual = new HashSet<>();
- TextualOp = new HashSet<>();
- outputFunctions = new HashSet<>();
- RelationalOp = new HashSet<>();
- Literals = new HashSet<>();
- literalsOut = new HashSet<>();
- StateChangeOp = new HashSet<>();
- }
- public static void checkIfFunc(String data)
- {
- if(Func.contains(data))
- {
- outputFunctions.add(data);
- }
- }
- public static void checkLiterals(String data)
- {
- if(Literals.contains(data))
- literalsOut.add(data);
- }
- public static void checkVariable(String data)
- {
- if(data.contains("()"))
- {
- outputFunctions.add(data);
- return;
- }
- varOp.add(data);
- }
- public static void checkIfKeys(String data)
- {
- if(keys.contains(data))
- {
- outputKeys.add(data);
- }
- }
- public static void checkIfops(String data)
- {
- if(Arithmetic.contains(data))
- ArithmeticOp.add(data);
- else if(Relational.contains(data))
- RelationalOp.add(data);
- else if(Logical.contains(data))
- LogicalOp.add(data);
- else if(StateChange.contains(data))
- StateChangeOp.add(data);
- else if(Textual.contains(data))
- TextualOp.add(data);
- }
- public static void main(String args[])
- {
- initialize();
- String data;
- Scanner scan = new Scanner(System.in);
- data = scan.nextLine();
- String[] input = data.split(
- "\\s+"
- );
- for(int i=0; i<input.length; i++)
- {
- String out = input[i];
- checkIfFunc(out);
- checkIfKeys(out);
- checkIfops(out);
- checkVariable(out);
- checkLiterals(out);
- }
- if(outputFunctions.isEmpty()!=true)
- {
- System.out.println("Functions:");
- outputFunctions.forEach(e -> System.out.print(e + " "));
- System.out.println();
- }
- System.out.println("Operators");
- if(ArithmeticOp.isEmpty()!=true)
- {
- System.out.println("Arithmetic:");
- ArithmeticOp.forEach(e -> System.out.print(e + " "));
- System.out.println();
- }
- if(RelationalOp.isEmpty()!=true)
- {
- System.out.println("Relational:");
- RelationalOp.forEach(e -> System.out.print(e + " "));
- System.out.println();
- }
- if(LogicalOp.isEmpty()!=true)
- {
- System.out.println("Logical:");
- LogicalOp.forEach(e -> System.out.print(e + " "));
- System.out.println();
- }
- if(StateChangeOp.isEmpty()!=true)
- {
- System.out.println("State Change:");
- StateChangeOp.forEach(e -> System.out.print(e + " "));
- System.out.println();
- }
- if(TextualOp.isEmpty()!=true)
- {
- System.out.println("Textual:");
- TextualOp.forEach(e -> System.out.print(e + " "));
- System.out.println();
- }
- if(literalsOut.isEmpty()!=true)
- {
- System.out.println("Literals:");
- literalsOut.forEach(e -> System.out.print(e + " "));
- System.out.println();
- }
- if(outputKeys.isEmpty()!=true)
- {
- System.out.println("Keyword:");
- outputKeys.forEach(e -> System.out.print(e + " "));
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement