编译原理词法分析器-扫描器的设计与实现的代码Java实现
时间: 2023-10-21 13:07:14 浏览: 98
编译原理——词法分析器设计与实现
以下是使用Java实现的一个简单词法分析器:
```java
import java.io.*;
// 定义Token的类型
enum TokenType {
TOK_INT, // 整数
TOK_OP, // 运算符
TOK_EOF, // 文件结束
}
// 定义Token的类
class Token {
public TokenType type; // Token类型
public int value; // Token的值
public Token(TokenType type, int value) {
this.type = type;
this.value = value;
}
}
// 词法分析器的类
class Lexer {
private BufferedReader reader;
private int currentChar;
public Lexer(String filename) throws FileNotFoundException {
reader = new BufferedReader(new FileReader(filename));
currentChar = getNextChar();
}
private int getNextChar() {
try {
return reader.read();
} catch (IOException e) {
e.printStackTrace();
return -1;
}
}
private void skipWhitespace() {
while (Character.isWhitespace(currentChar)) {
currentChar = getNextChar();
}
}
private Token getIntToken() {
int value = 0;
while (Character.isDigit(currentChar)) {
value = value * 10 + (currentChar - '0');
currentChar = getNextChar();
}
return new Token(TokenType.TOK_INT, value);
}
private Token getOpToken() {
Token token = new Token(TokenType.TOK_OP, currentChar);
currentChar = getNextChar();
return token;
}
public Token getNextToken() {
while (currentChar != -1) {
if (Character.isWhitespace(currentChar)) {
skipWhitespace();
continue;
} else if (Character.isDigit(currentChar)) {
return getIntToken();
} else if (currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/') {
return getOpToken();
} else {
System.out.println("Unknown character: " + (char)currentChar);
currentChar = getNextChar();
}
}
return new Token(TokenType.TOK_EOF, 0);
}
}
// 测试词法分析器的主函数
public class Main {
public static void main(String[] args) throws FileNotFoundException {
Lexer lexer = new Lexer("input.txt");
Token token = lexer.getNextToken();
while (token.type != TokenType.TOK_EOF) {
if (token.type == TokenType.TOK_INT) {
System.out.println("Integer: " + token.value);
} else if (token.type == TokenType.TOK_OP) {
System.out.println("Operator: " + (char)token.value);
}
token = lexer.getNextToken();
}
}
}
```
该词法分析器可以识别整数和四种基本运算符(加、减、乘、除)。它从文件中读取字符并逐个处理,将字符序列转换为一系列Token。
阅读全文