java实现词法分析程序
时间: 2024-05-06 20:15:10 浏览: 67
Java实现词法分析程序可以使用正则表达式和有限状态自动机(DFA)来识别和处理输入的代码。
以下是一个简单的Java程序,用于从输入代码中提取标识符、关键字和运算符等词汇元素:
```java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Lexer {
private static final String[] KEYWORDS = {"int", "float", "double", "if", "else", "for", "while", "do", "switch", "case", "break", "continue", "return"};
private static final String IDENTIFIER_REGEX = "[a-zA-Z][a-zA-Z0-9]*";
private static final String INTEGER_REGEX = "\\d+";
private static final String FLOAT_REGEX = "\\d+\\.\\d+";
private static final String OPERATOR_REGEX = "[+\\-*/%]|==|!=|<=|>=|<|>";
private static final String WHITESPACE_REGEX = "\\s+";
public static void main(String[] args) {
String code = "int x = 10;\nfloat y = 3.14;\nif (x < y) {\n System.out.println(\"x is less than y\");\n} else {\n System.out.println(\"x is greater than or equal to y\");\n}";
Pattern pattern = Pattern.compile(IDENTIFIER_REGEX + "|" + INTEGER_REGEX + "|" + FLOAT_REGEX + "|" + OPERATOR_REGEX + "|" + WHITESPACE_REGEX);
Matcher matcher = pattern.matcher(code);
while (matcher.find()) {
String token = matcher.group();
if (token.matches(IDENTIFIER_REGEX)) {
if (isKeyword(token)) {
System.out.println(token + " is a keyword");
} else {
System.out.println(token + " is an identifier");
}
} else if (token.matches(INTEGER_REGEX)) {
System.out.println(token + " is an integer");
} else if (token.matches(FLOAT_REGEX)) {
System.out.println(token + " is a float");
} else if (token.matches(OPERATOR_REGEX)) {
System.out.println(token + " is an operator");
} else if (token.matches(WHITESPACE_REGEX)) {
// do nothing
} else {
System.out.println("Unknown token: " + token);
}
}
}
private static boolean isKeyword(String token) {
for (String keyword : KEYWORDS) {
if (keyword.equals(token)) {
return true;
}
}
return false;
}
}
```
该程序使用正则表达式将输入代码按照标识符、整数、浮点数、运算符和空格进行匹配,并对每个匹配到的词汇元素进行分类和输出。其中,关键字列表通过数组 `KEYWORDS` 存储,识别标识符时需要判断是否为关键字。
阅读全文