实验二:词法分析器的设计
设计一个词法分析器程序,该程序能完成如下功能:输入的字符串中的单词有整数和以字母开头的标识符,分析器能识别出单词并区分是整数还是标识符,标识符中是否有while、do、if、then、else、begin、end、and、or、not等保留字。若单词是整数则输出整数的编码及数值,是标识符则判定是否为保留字,是保留字则将保留字及其对应的编码输出,否则输出标识符号及其编码。要求用图形界面方式编程.(可参考实验指导书P110至P113)
可以有如下约定:保留字if的编码为0,then为1,else为2, while为3, begin为4,do为5,end为6,and为39,or为40,not为41。整数的编码为57,标识符的编码56.
例如:若文本文件中的字符串为 while do at a45 a+ 34 end 则输出为(while, 3)(do, 5),  (at, 56),  (a45, 56),  (a+, 56),  (34, 57),  (end,6)我的程序;import java.io.*;import javafx.application.Application;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;public class Grammer<OutStreamWrite> extends Application{
public static File filesite=null;
String r="no text";
String s=null;
private TextField textfiled1=new TextField();
//主界面
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open txt File");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("txt","*.txt")
);
//进入识别界面
filesite= fileChooser.showOpenDialog(primaryStage);
System.out.println(filesite);
if(filesite != null) {
s=convertStreamToString();
BorderPane pane= new BorderPane();
Button button=new Button("yes");
button.setOnAction(e ->{
shibie();
});

textfiled1.setPrefSize(450, 100);
textfiled1.setEditable(false);

pane.setTop(getHBoxText("文本内容",s));
            pane.setCenter(button);
pane.setBottom(textfiled1);
Scene ascene= new Scene(pane);
Stage astage=new Stage();
astage.setTitle("NUMBER");
primaryStage.setScene(ascene);primaryStage.setTitle("Number");
primaryStage.show();
}
}
private VBox getHBoxText(String a,String b) {
VBox vbox=new VBox();
vbox.setPadding(new Insets(20,20,20,20));
vbox.setStyle("-fx-backgroud-color:gold");
TextField textfiled=new TextField();
textfiled.setText(b);
textfiled.setPrefSize(450, 200);
textfiled.setEditable(false);
vbox.getChildren().addAll(new Label(a),textfiled);
return vbox;
}   public static void main(String[] args) {
   launch(args);
   }
 
   /*public String IO() throws IOException {
   String s ="the empty text";
   try {
FileInputStream input=new FileInputStream(filesite);
byte[] buf = new byte[1024*20];
@SuppressWarnings("unused")
int k = input.read(buf, 0, buf.length);
s=new String(buf);
input.close();
System.out.println(s);
} catch (FileNotFoundException e) {
s="the empty text";
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
   } 
   */
public static String convertStreamToString() throws FileNotFoundException {  
FileInputStream input=new FileInputStream(filesite);
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));      
        StringBuilder sb = new StringBuilder();      
    
        String line = null;      
       try {      
           while ((line = reader.readLine()) != null) {      
                sb.append(line + "\n");      
            }      
        } catch (IOException e) {      
            e.printStackTrace();      
        } finally {      
           try {      
            input.close();      
            } catch (IOException e) {      
                e.printStackTrace();      
            }      
        }      
       System.out.println(sb);
       return sb.toString();      
    }
   // 识别 
   public void shibie() {
   r=" ";
   s.concat(" ");
   String[][] num= {{"if","0"},{"then","1"},{"else","2"},{"while","3"},{"begin","4"},{"do","5"},{"end","6"},{"and","39"},{"or","40"},{"not","41"}};
   String[] s1=s.split("\\ ");
   for(int a=0;a<s1.length;a++) {
   System.out.println(s1[a]);
   int log=1;
   if(isInteger(s1[a])==true) {
   r=r+"("+s1[a]+","+"57"+")"+" ";System.out.println(r);
   log=0;
   }
          if(log==1)
    {for(int b=0;b<num.length;b++) {
   if(s1[a].equals(num[b][0])) {
   r=r+"("+s1[a]+","+num[b][1]+")"+" ";
   System.out.println(r);
   log=0;
   break;
   }
   }
   }
          if(log==1)  {
   r=r+"("+s1[a]+","+"56"+")"+" ";System.out.println(r);
   }
   textfiled1.setText(r);
   }
   }
public static boolean isInteger(String str){  
   for(int i=str.length();--i>=0;){  
      int chr=str.charAt(i);  
      if(chr<48 || chr>57)  
         return false;  
   }  
   return true;  
}  
   }
程序运行结果