用java写一段词法分析的程序
时间: 2024-06-08 19:05:58 浏览: 7
以下是一个简单的 Java 词法分析器示例,它可以将输入的字符串分解为标记或词法单元:
```java
import java.util.ArrayList;
public class Lexer {
private String input;
private ArrayList<Token> tokens;
private int position;
public Lexer(String input) {
this.input = input;
this.tokens = new ArrayList<Token>();
this.position = 0;
}
public ArrayList<Token> tokenize() throws Exception {
while (position < input.length()) {
char currentChar = input.charAt(position);
if (Character.isDigit(currentChar)) {
tokens.add(new Token(TokenType.INTEGER, readInteger()));
} else if (Character.isLetter(currentChar)) {
tokens.add(new Token(TokenType.IDENTIFIER, readIdentifier()));
} else if (currentChar == '+') {
tokens.add(new Token(TokenType.PLUS, "+"));
position++;
} else if (currentChar == '-') {
tokens.add(new Token(TokenType.MINUS, "-"));
position++;
} else if (currentChar == '*') {
tokens.add(new Token(TokenType.MULTIPLY, "*"));
position++;
} else if (currentChar == '/') {
tokens.add(new Token(TokenType.DIVIDE, "/"));
position++;
} else if (currentChar == '(') {
tokens.add(new Token(TokenType.LPAREN, "("));
position++;
} else if (currentChar == ')') {
tokens.add(new Token(TokenType.RPAREN, ")"));
position++;
} else if (currentChar == '=') {
tokens.add(new Token(TokenType.ASSIGNMENT, "="));
position++;
} else if (currentChar == ';') {
tokens.add(new Token(TokenType.SEMICOLON, ";"));
position++;
} else if (currentChar == ' ') {
position++;
} else {
throw new Exception("Invalid character: " + currentChar);
}
}
return tokens;
}
private String readInteger() {
String result = "";
while (position < input.length() && Character.isDigit(input.charAt(position))) {
result += input.charAt(position);
position++;
}
return result;
}
private String readIdentifier() {
String result = "";
while (position < input.length() && Character.isLetterOrDigit(input.charAt(position))) {
result += input.charAt(position);
position++;
}
return result;
}
}
```
上述代码中,我们定义了 `Lexer` 类,它接受一个字符串作为输入,并将其分解为标记或词法单元。我们使用 `TokenType` 枚举类来表示不同类型的标记,例如整数、标识符、加号、减号等。
在 `tokenize()` 方法中,我们使用一个循环来遍历输入字符串中的每个字符。根据当前字符的类型,我们使用不同的方法来读取标记。例如,如果当前字符是数字,则我们使用 `readInteger()` 方法来读取整数标记。如果当前字符是字母,则我们使用 `readIdentifier()` 方法来读取标识符标记。如果当前字符是运算符或括号,则我们直接将其添加到标记列表中。
在 `readInteger()` 和 `readIdentifier()` 方法中,我们使用另一个循环来读取连续的数字或字母字符,直到遇到非数字或字母字符为止。
最后,我们返回一个标记列表,其中包含输入字符串中的所有标记或词法单元。如果输入字符串中包含无法识别的字符,则我们抛出一个异常。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)