用Java编写一个词法分析器
时间: 2023-05-15 20:05:03 浏览: 155
好的,我可以为您提供一个基本的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 boolean isWhitespace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
private boolean isDigit(char c) {
return c >= '0' && c <= '9';
}
private boolean isLetter(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
private Token getNextToken() {
if (currentPosition >= input.length()) {
return null;
}
char currentChar = input.charAt(currentPosition);
if (isWhitespace(currentChar)) {
currentPosition++;
return getNextToken();
}
if (isDigit(currentChar)) {
StringBuilder value = new StringBuilder();
while (isDigit(currentChar)) {
value.append(currentChar);
currentPosition++;
if (currentPosition >= input.length()) {
break;
}
currentChar = input.charAt(currentPosition);
}
return new Token(TokenType.NUMBER, value.toString());
}
if (isLetter(currentChar)) {
StringBuilder value = new StringBuilder();
while (isLetter(currentChar) || isDigit(currentChar)) {
value.append(currentChar);
currentPosition++;
if (currentPosition >= input.length()) {
break;
}
currentChar = input.charAt(currentPosition);
}
return new Token(TokenType.IDENTIFIER, value.toString());
}
switch (currentChar) {
case '+':
currentPosition++;
return new Token(TokenType.PLUS, "+");
case '-':
currentPosition++;
return new Token(TokenType.MINUS, "-");
case '*':
currentPosition++;
return new Token(TokenType.MULTIPLY, "*");
case '/':
currentPosition++;
return new Token(TokenType.DIVIDE, "/");
case '(':
currentPosition++;
return new Token(TokenType.LEFT_PAREN, "(");
case ')':
currentPosition++;
return new Token(TokenType.RIGHT_PAREN, ")");
default:
throw new RuntimeException("Invalid character: " + currentChar);
}
}
public Token getCurrentToken() {
if (currentToken == null) {
currentToken = getNextToken();
}
return currentToken;
}
public Token getNextNonWhitespaceToken() {
Token token = getNextToken();
while (token != null && token.getType() == TokenType.WHITESPACE) {
token = getNextToken();
}
currentToken = token;
return token;
}
}
enum TokenType {
NUMBER,
IDENTIFIER,
PLUS,
MINUS,
MULTIPLY,
DIVIDE,
LEFT_PAREN,
RIGHT_PAREN,
WHITESPACE
}
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 "Token{" +
"type=" + type +
", value='" + value + '\'' +
'}';
}
}
```
这是一个简单的词法分析器,它可以识别数字、标识符和一些基本的运算符。您可以根据需要进行修改和扩展。
阅读全文