用java来实现词法分析器
时间: 2024-02-17 18:01:04 浏览: 121
好的,以下是一个简单的基于Java的词法分析器的实现示例:
```java
import java.io.*;
import java.util.*;
// 定义单词符号类型枚举
enum TokenType {
IDENTIFIER, NUMBER, RESERVED_WORD, OPERATOR, SEPARATOR, ERROR
}
// 定义单词符号类
class Token {
public TokenType type; // 单词符号类型
public String value; // 单词符号值
public Token(TokenType type, String value) {
this.type = type;
this.value = value;
}
public String toString() {
return "<" + type.name() + ", " + value + ">";
}
}
// 定义词法分析器类
class Lexer {
private String input; // 输入源代码
private int pos; // 当前扫描位置
// 定义保留字集合
private static final Set<String> RESERVED_WORDS = new HashSet<>(Arrays.asList(
"if", "else", "while", "do", "for", "break", "continue", "int", "float", "char", "void", "return"
));
// 定义运算符集合
private static final Set<Character> OPERATORS = new HashSet<>(Arrays.asList(
'+', '-', '*', '/', '%', '=', '>', '<', '!', '&', '|'
));
// 定义分隔符集合
private static final Set<Character> SEPARATORS = new HashSet<>(Arrays.asList(
'(', ')', '[', ']', '{', '}', ',', ';', ':'
));
public Lexer(String input) {
this.input = input;
this.pos = 0;
}
// 判断一个字符是否是字母
private boolean isLetter(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
// 判断一个字符是否是数字
private boolean isDigit(char c) {
return (c >= '0' && c <= '9');
}
// 读取下一个字符
private char nextChar() {
if (pos >= input.length()) {
return '\0'; // 如果已经到达输入字符串的末尾,返回空字符
} else {
return input.charAt(pos++); // 否则返回下一个字符并将扫描位置向后移动一位
}
}
// 将扫描位置向前回退一位
private void back() {
pos--;
}
// 识别保留字或标识符
private Token scanIdentifier() {
StringBuilder sb = new StringBuilder();
char c = nextChar();
while (isLetter(c) || isDigit(c)) {
sb.append(c);
c = nextChar();
}
back();
String value = sb.toString();
if (RESERVED_WORDS.contains(value)) {
return new Token(TokenType.RESERVED_WORD, value);
} else {
return new Token(TokenType.IDENTIFIER, value);
}
}
// 识别数字
private Token scanNumber() {
StringBuilder sb = new StringBuilder();
char c = nextChar();
while (isDigit(c)) {
sb.append(c);
c = nextChar();
}
if (c == '.') {
sb.append(c);
c = nextChar();
while (isDigit(c)) {
sb.append(c);
c = nextChar();
}
}
back();
return new Token(TokenType.NUMBER, sb.toString());
}
// 识别运算符或分隔符
private Token scanOperatorOrSeparator() {
char c = nextChar();
if (OPERATORS.contains(c)) {
return new Token(TokenType.OPERATOR, Character.toString(c));
} else if (SEPARATORS.contains(c)) {
return new Token(TokenType.SEPARATOR, Character.toString(c));
} else {
return new Token(TokenType.ERROR, Character.toString(c));
}
}
// 扫描下一个单词符号
public Token nextToken() {
char c = nextChar();
while (Character.isWhitespace(c)) { // 跳过空白字符
c = nextChar();
}
if (isLetter(c)) {
back();
return scanIdentifier();
} else if (isDigit(c)) {
back();
return scanNumber();
} else {
return scanOperatorOrSeparator();
}
}
}
// 测试词法分析器
public class LexerTest {
public static void main(String[] args) {
String input = "int x = 123; float y = 3.14; if (x > y) { return x; } else { return y; }";
Lexer lexer = new Lexer(input);
Token token;
do {
token = lexer.nextToken();
System.out.println(token);
} while (token.type != TokenType.ERROR);
}
}
```
以上代码实现了一个简单的基于Java的词法分析器,可以识别保留字、标识符、数字、运算符和分隔符等单词符号,并将其编码输出。您可以将其复制到您的Java IDE中进行调试和运行。
阅读全文