I have the task of creating a lexical analyzer that translates the language into a series of tokens. I use java.util.regex to go through a string that finds different tokens, and I put them in an array through which I will pass, and use this to assign my corresponding tokens to them. Here is part of my program:
public static void main(String args[]) throws FileNotFoundException, IOException{
String[] symbols = {"+","-","*","/","<","<=",">",">=","==","!=","=",";",",",".","(",")","[","]","{","}","/*","*/","//"};
String[] input;
FileInputStream fstream = new FileInputStream("src\\testCode.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
StringBuilder sb = new StringBuilder();
String s;
String ret = "";
while((s = br.readLine()) != null){
sb.append(s);
}
ret = sb.toString();
input = regexChecker("regex goes here",ret);
for (int i = 0; i < input.length; i++) {
System.out.println(input[i]);
}
System.out.println(input.length);
in.close();
}
public static String[] regexChecker(String theRegex, String str2Check){
List<String> allMatches = new ArrayList<String>();
Pattern checkRegex = Pattern.compile(theRegex);
Matcher regexMatcher = checkRegex.matcher(str2Check);
while(regexMatcher.find()){
allMatches.add(regexMatcher.group());
}
String[] regexInput = allMatches.toArray(new String[allMatches.size()]);
return regexInput;
}
: , ? , ? : , . . - . , int, double, if .. , *,/, + ..
, , , 1, .
(?://.*)|(/\\*(?:.|[\\n\\r])*?\\*/) , , , , . ?