用Java写一个词法分析器
时间: 2023-05-20 12:03:53 浏览: 92
好的,以下是一个简单的Java词法分析器的代码示例:
```java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Lexer {
private String input;
private int currentPosition;
private Token currentToken;
public Lexer(String input) {
this.input = input;
this.currentPosition = 0;
this.currentToken = null;
}
private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z][a-zA-Z0-9]*");
private static final Pattern NUMBER_PATTERN = Pattern.compile("[0-9]+");
private void skipWhitespace() {
while (currentPosition < input.length() && Character.isWhitespace(input.charAt(currentPosition))) {
currentPosition++;
}
}
private Token matchIdentifier() {
Matcher matcher = IDENTIFIER_PATTERN.matcher(input.substring(currentPosition));
if (matcher.find()) {
String identifier = matcher.group();
currentPosition += identifier.length();
return new Token(TokenType.IDENTIFIER, identifier);
} else {
return null;
}
}
private Token matchNumber() {
Matcher matcher = NUMBER_PATTERN.matcher(input.substring(currentPosition));
if (matcher.find()) {
String number = matcher.group();
currentPosition += number.length();
return new Token(TokenType.NUMBER, number);
} else {
return null;
}
}
public Token getNextToken() {
skipWhitespace();
if (currentPosition >= input.length()) {
return null;
}
char currentChar = input.charAt(currentPosition);
if (Character.isLetter(currentChar)) {
return matchIdentifier();
} else if (Character.isDigit(currentChar)) {
return matchNumber();
} else {
currentPosition++;
return new Token(TokenType.OPERATOR, Character.toString(currentChar));
}
}
}
enum TokenType {
IDENTIFIER,
NUMBER,
OPERATOR
}
class Token {
private TokenType type;
private String value;
public Token(TokenType type, String value) {
this.type = type;
this.value = value;
}
public TokenType getType() {
return type;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return String.format("(%s, %s)", type, value);
}
}
```
这个词法分析器可以识别标识符、数字和运算符。你可以使用它来解析简单的算术表达式,例如:
```java
Lexer lexer = new Lexer("x = 42 + y");
Token token;
while ((token = lexer.getNextToken()) != null) {
System.out.println(token);
}
```
这将输出:
```
(IDENTIFIER, x)
(OPERATOR, =)
(NUMBER, 42)
(OPERATOR, +)
(IDENTIFIER, y)
```