java编写带界面的词法分析器
时间: 2023-10-17 14:10:22 浏览: 80
java编写的词法分析器
Java中可以使用JavaFX来编写带界面的词法分析器。下面是一个示例代码:
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LexicalAnalyzer extends Application {
private final String KEYWORDS = "\\b(int|float|double|String|if|else|while|for|do|switch|case|break|continue|return|void)\\b";
private final String OPERATORS = "[\\+\\-\\*/%]";
private final String PUNCTUATION = "[\\{\\}\\(\\)\\[\\];,]";
private final String INT_CONST = "\\b\\d+\\b";
private final String FLOAT_CONST = "\\b\\d+\\.\\d+\\b";
private final String STRING_CONST = "\"[^\"]*\"";
private final String IDENTIFIER = "\\b[A-Za-z_]\\w*\\b";
private TextArea textArea = new TextArea();
private TextArea resultArea = new TextArea();
@Override
public void start(Stage primaryStage) {
BorderPane borderPane = new BorderPane();
HBox hbox = new HBox();
Button btnOpen = new Button("Open");
Button btnAnalyze = new Button("Analyze");
hbox.getChildren().addAll(btnOpen, btnAnalyze);
borderPane.setTop(hbox);
borderPane.setLeft(textArea);
borderPane.setRight(resultArea);
Scene scene = new Scene(borderPane, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
btnOpen.setOnAction(e -> {
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(primaryStage);
if(file != null) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
textArea.appendText(line + "\n");
}
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
btnAnalyze.setOnAction(e -> {
String text = textArea.getText();
String[] lines = text.split("\n");
StringBuilder resultBuilder = new StringBuilder();
Pattern pattern = Pattern.compile(KEYWORDS + "|" + OPERATORS + "|" + PUNCTUATION + "|" + INT_CONST + "|" + FLOAT_CONST + "|" + STRING_CONST + "|" + IDENTIFIER);
for (String line : lines) {
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String token = matcher.group();
if (token.matches(KEYWORDS)) {
resultBuilder.append("<Keyword, " + token + ">\n");
} else if (token.matches(OPERATORS)) {
resultBuilder.append("<Operator, " + token + ">\n");
} else if (token.matches(PUNCTUATION)) {
resultBuilder.append("<Punctuation, " + token + ">\n");
} else if (token.matches(INT_CONST)) {
resultBuilder.append("<IntConst, " + token + ">\n");
} else if (token.matches(FLOAT_CONST)) {
resultBuilder.append("<FloatConst, " + token + ">\n");
} else if (token.matches(STRING_CONST)) {
resultBuilder.append("<StringConst, " + token + ">\n");
} else if (token.matches(IDENTIFIER)) {
resultBuilder.append("<Identifier, " + token + ">\n");
}
}
}
resultArea.setText(resultBuilder.toString());
});
}
public static void main(String[] args) {
launch(args);
}
}
```
这个程序使用JavaFX来创建界面,其中包含一个文本框和一个按钮。用户可以在文本框中输入代码,点击按钮后程序将分析代码并在另一个文本框中输出结果。程序使用正则表达式来识别代码中的关键字、运算符、标点符号、常量和标识符,并将它们转换成相应的词法单元。
阅读全文