帮我写一个用java判断关键字、标识符、判断符、数字、界符的语法分析器
时间: 2023-07-10 19:14:55 浏览: 51
好的,根据你的要求,我可以为你提供一个基于Java语言的简单的语法分析器,用于判断关键字、标识符、判断符、数字、界符。代码如下:
```java
import java.util.Arrays;
import java.util.List;
public class SyntaxAnalyzer {
private static final List<String> KEYWORDS = Arrays.asList("if", "else", "while", "for", "int", "float", "double", "boolean", "char", "string");
private static final List<String> OPERATORS = Arrays.asList("+", "-", "*", "/", "%", "=", "==", "!=", ">", "<", ">=", "<=");
private static final List<String> DELIMITERS = Arrays.asList("(", ")", "{", "}", ";", ",");
private static final String LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String DIGITS = "0123456789";
public static void analyze(String expression) {
String[] tokens = expression.split("\\s+");
for (String token : tokens) {
if (KEYWORDS.contains(token)) {
System.out.println(token + " is a keyword");
} else if (OPERATORS.contains(token)) {
System.out.println(token + " is an operator");
} else if (DELIMITERS.contains(token)) {
System.out.println(token + " is a delimiter");
} else if (token.matches("[a-zA-Z]+[a-zA-Z0-9]*")) {
System.out.println(token + " is an identifier");
} else if (token.matches("\\d+")) {
System.out.println(token + " is an integer");
} else if (token.matches("\\d+\\.\\d+")) {
System.out.println(token + " is a floating point number");
} else {
System.out.println(token + " is not recognized");
}
}
}
public static void main(String[] args) {
String expression = "int x = 5; double y = 3.14; if (x < y) { System.out.println(\"x is less than y\"); }";
SyntaxAnalyzer.analyze(expression);
}
}
```
在这个语法分析器中,我们使用了一些常量来表示关键字、运算符、分隔符等。在 `analyze` 方法中,我们首先将输入的表达式分割成单个的词语,然后对每个词语进行分类判断,如果是关键字,则输出 `token is a keyword`;如果是运算符,则输出 `token is an operator`;如果是分隔符,则输出 `token is a delimiter`;如果是标识符,则输出 `token is an identifier`;如果是整数,则输出 `token is an integer`;如果是浮点数,则输出 `token is a floating point number`;否则输出 `token is not recognized`。
在 `main` 方法中,我们定义了一个表达式,并将其传递给 `analyze` 方法进行分析。你可以根据需要修改表达式,或者从其他输入源获取表达式。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)