设计、编制并调试一个词法分析程序,实现从源程序中分出各种单词的方法,并能输出词法分析中的各种错误。利用Java语言实现
时间: 2024-06-09 09:06:08 浏览: 10
下面是一个简单的Java词法分析程序,它可以从输入的源程序中分离出各种单词,并输出词法分析中的各种错误。
```java
import java.io.*;
public class LexicalAnalyzer {
private BufferedReader reader;
private char currentChar;
private boolean EOF;
public LexicalAnalyzer(String fileName) {
try {
reader = new BufferedReader(new FileReader(fileName));
EOF = false;
getNextChar();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void getNextChar() {
int nextChar;
try {
nextChar = reader.read();
if (nextChar == -1) {
EOF = true;
} else {
currentChar = (char) nextChar;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public Token getNextToken() {
Token token = new Token();
while (!EOF) {
if (Character.isLetter(currentChar)) {
token.setType(TokenType.IDENTIFIER);
token.setValue(getIdentifier());
return token;
} else if (Character.isDigit(currentChar)) {
token.setType(TokenType.NUMBER);
token.setValue(getNumber());
return token;
} else if (currentChar == '+') {
token.setType(TokenType.PLUS);
getNextChar();
return token;
} else if (currentChar == '-') {
token.setType(TokenType.MINUS);
getNextChar();
return token;
} else if (currentChar == '*') {
token.setType(TokenType.MULTIPLY);
getNextChar();
return token;
} else if (currentChar == '/') {
token.setType(TokenType.DIVIDE);
getNextChar();
return token;
} else if (currentChar == '(') {
token.setType(TokenType.OPEN_PAREN);
getNextChar();
return token;
} else if (currentChar == ')') {
token.setType(TokenType.CLOSE_PAREN);
getNextChar();
return token;
} else if (currentChar == '=') {
token.setType(TokenType.EQUAL);
getNextChar();
return token;
} else if (currentChar == '<') {
getNextChar();
if (currentChar == '=') {
token.setType(TokenType.LESS_THAN_OR_EQUAL);
getNextChar();
} else {
token.setType(TokenType.LESS_THAN);
}
return token;
} else if (currentChar == '>') {
getNextChar();
if (currentChar == '=') {
token.setType(TokenType.GREATER_THAN_OR_EQUAL);
getNextChar();
} else {
token.setType(TokenType.GREATER_THAN);
}
return token;
} else if (Character.isWhitespace(currentChar)) {
getNextChar();
} else {
token.setType(TokenType.ERROR);
token.setValue(Character.toString(currentChar));
getNextChar();
return token;
}
}
token.setType(TokenType.EOF);
return token;
}
public String getIdentifier() {
StringBuilder identifier = new StringBuilder();
while (Character.isLetterOrDigit(currentChar)) {
identifier.append(currentChar);
getNextChar();
}
return identifier.toString();
}
public String getNumber() {
StringBuilder number = new StringBuilder();
while (Character.isDigit(currentChar)) {
number.append(currentChar);
getNextChar();
}
return number.toString();
}
public static void main(String[] args) {
LexicalAnalyzer lexicalAnalyzer = new LexicalAnalyzer("input.txt");
Token token = lexicalAnalyzer.getNextToken();
while (token.getType() != TokenType.EOF) {
System.out.println(token);
token = lexicalAnalyzer.getNextToken();
}
}
}
enum TokenType {
IDENTIFIER,
NUMBER,
PLUS,
MINUS,
MULTIPLY,
DIVIDE,
OPEN_PAREN,
CLOSE_PAREN,
EQUAL,
LESS_THAN,
LESS_THAN_OR_EQUAL,
GREATER_THAN,
GREATER_THAN_OR_EQUAL,
ERROR,
EOF
}
class Token {
private TokenType type;
private String value;
public Token() {
type = null;
value = "";
}
public Token(TokenType type, String value) {
this.type = type;
this.value = value;
}
public TokenType getType() {
return type;
}
public void setType(TokenType type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return type + " " + value;
}
}
```
在上面的程序中,`LexicalAnalyzer` 类负责从输入的源程序中获取下一个单词,它通过 `getNextToken()` 方法实现。该方法使用一个 `while` 循环不断读取字符并根据字符类型构造对应的 Token 对象。
Token 类型通过 `TokenType` 枚举类型表示。在 `getNextToken()` 方法中,对于每个字符,都会判断它是哪种类型的 Token,并构造对应的 Token 对象。如果遇到无法识别的字符,则构造一个 ERROR 类型的 Token。
`getIdentifier()` 和 `getNumber()` 方法分别用于读取标识符和数字。
在 `main()` 方法中,我们创建了一个 `LexicalAnalyzer` 对象并循环调用 `getNextToken()` 方法,将每个 Token 输出到控制台。
这是一个简单的词法分析程序,它可以处理一些基本的 Token,例如标识符、数字、加减乘除、括号、等于、小于等于和大于等于。如果遇到无法识别的字符,它会构造一个 ERROR 类型的 Token。你可以根据需要扩展它,添加更多的 Token 类型。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)