0
My teacher passed a paper on compilers. He wants me to do a Lexical Parser.
I managed to do a good part of the code, but I can’t show the result.
I’m a beginner in Java and I’m using Eclipse.
The result that the Teacher wants to appear (with any result, does not need to have an input.) :
Example:
Expression: "8 + 8 = 16"
Result: (The code will analyze if this entry is correct, if it is not will say error).
Digito: 8
Operator: +
Digito: 8
Equality: =
Digito: 16
My code:
**package lexico;**
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Lexico {
public static void main (String []args) {
String code = "59-8=51";
int c = 0;
Map<String, String> tokens = new HashMap<>();
tokens.put("[0-9]+", "[Digito:]");
tokens.put("[/+/-]+", "[Operador:]");
tokens.put("[/=]+", "[Igualdade: ]");
while(c < code.length()) {
for(Map.Entry<String, String> entry : tokens.entrySet()) {
Pattern verificador = Pattern.compile(entry.getKey());
Matcher ve = verificador.matcher(code);
if(ve.find() && ve.start() == c) {
System.out.println(entry.getValue() + ve.group());
c = c + ve.group().length();
}
}
}
}
}
The result of my code:
Blz bro. Helped a lot. Vlw
– XxCable