用java写一个词法分析的代码
时间: 2024-06-09 18:08:18 浏览: 104
以下是一个简单的 Java 词法分析器代码,它可以将输入的代码分解为单个标识符,关键字和运算符。
```java
import java.util.HashMap;
public class Lexer {
private String input;
private int position;
private int charPosition;
private HashMap<String, TokenType> keywords;
public Lexer(String input) {
this.input = input;
this.position = 0;
this.charPosition = 1;
// 初始化关键字
keywords = new HashMap<>();
keywords.put("if", TokenType.IF);
keywords.put("else", TokenType.ELSE);
keywords.put("while", TokenType.WHILE);
keywords.put("do", TokenType.DO);
keywords.put("int", TokenType.INT);
keywords.put("float", TokenType.FLOAT);
keywords.put("bool", TokenType.BOOL);
keywords.put("true", TokenType.TRUE);
keywords.put("false", TokenType.FALSE);
}
private char currentChar() {
if (position >= input.length()) {
return '\0';
}
return input.charAt(position);
}
private void advance() {
position++;
charPosition++;
}
private void skipWhitespace() {
while (currentChar() == ' ' || currentChar() == '\t' || currentChar() == '\n') {
advance();
}
}
private String readIdentifier() {
StringBuilder identifier = new StringBuilder();
while (Character.isLetterOrDigit(currentChar()) || currentChar() == '_') {
identifier.append(currentChar());
advance();
}
return identifier.toString();
}
private String readNumber() {
StringBuilder number = new StringBuilder();
while (Character.isDigit(currentChar())) {
number.append(currentChar());
advance();
}
if (currentChar() == '.') {
number.append(currentChar());
advance();
while (Character.isDigit(currentChar())) {
number.append(currentChar());
advance();
}
return number.toString();
} else {
return number.toString();
}
}
private Token readOperator() {
switch (currentChar()) {
case '=':
if (peek() == '=') {
advance();
advance();
return new Token(TokenType.EQUALS, "==");
} else {
advance();
return new Token(TokenType.ASSIGN, "=");
}
case '+':
advance();
return new Token(TokenType.PLUS, "+");
case '-':
advance();
return new Token(TokenType.MINUS, "-");
case '*':
advance();
return new Token(TokenType.MULTIPLY, "*");
case '/':
advance();
return new Token(TokenType.DIVIDE, "/");
case '<':
advance();
return new Token(TokenType.LESS_THAN, "<");
case '>':
advance();
return new Token(TokenType.GREATER_THAN, ">");
default:
return null;
}
}
private char peek() {
if (position + 1 >= input.length()) {
return '\0';
}
return input.charAt(position + 1);
}
public Token getNextToken() {
while (currentChar() != '\0') {
if (currentChar() == ' ' || currentChar() == '\t' || currentChar() == '\n') {
skipWhitespace();
continue;
}
if (Character.isLetter(currentChar()) || currentChar() == '_') {
String identifier = readIdentifier();
TokenType tokenType = keywords.getOrDefault(identifier, TokenType.IDENTIFIER);
return new Token(tokenType, identifier);
}
if (Character.isDigit(currentChar())) {
String number = readNumber();
return new Token(TokenType.NUMBER, number);
}
Token operator = readOperator();
if (operator != null) {
return operator;
}
throw new RuntimeException("Invalid character: " + currentChar());
}
return new Token(TokenType.EOF, "");
}
}
```
该代码通过 `getNextToken()` 方法逐个读取输入代码中的标识符、关键字和运算符,并返回相应的 Token 类型。Token 类型由 `TokenType` 枚举类表示,其中包含了所有可识别的类型。
阅读全文