Created
December 2, 2018 16:42
-
-
Save Esarve/d83ef638c6b83736ec0adfa1bffd9cb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Scanner; | |
public class Test { | |
private static ArrayList<String> operators = new ArrayList<>(); | |
private static ArrayList<String> keywords = new ArrayList<>(); | |
private static ArrayList<String> symbols = new ArrayList<>(); | |
private static ArrayList<String> variables = new ArrayList<>(); | |
private static ArrayList<String> constants = new ArrayList<>(); | |
private static List<String> operatorList; | |
private static List<String> keywdsList; | |
private static List<String> symblsList; | |
private static Scanner scanner = new Scanner(System.in); | |
public static void main(String[] args) { | |
initStoredItems(); | |
System.out.print("Input String: "); | |
String input = scanner.nextLine(); | |
String[] tokens = generateTokens(input); | |
for (String str : tokens | |
) { | |
if (operatorList.contains(str)) operators.add(str); | |
else if (keywdsList.contains(str)) keywords.add(str); | |
else if (symblsList.contains(str)) symbols.add(str); | |
else if (str.matches("[\\d]+")) constants.add(str); | |
else variables.add(str); | |
} | |
System.out.println("Operators: "+operators); | |
System.out.println("Symbols: "+symbols); | |
System.out.println("Keywords: "+keywords); | |
System.out.println("Variables: "+variables); | |
System.out.println("Constants: "+constants); | |
} | |
private static void initStoredItems(){ | |
String[] storedKeywds = new String[]{"for","if","while","do","break","int"}; | |
String[] storedOperators = new String[]{"+","-","*","/","="}; | |
String[] storedSymbls = new String[]{";","==","!"}; | |
operatorList = Arrays.asList(storedOperators); | |
keywdsList = Arrays.asList(storedKeywds); | |
symblsList = Arrays.asList(storedSymbls); | |
} | |
private static String[] generateTokens(String input){ | |
String[] tokens = input.split("[\\s]+"); | |
return tokens; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment