java 簡單的詞法分析

來源:互聯網
上載者:User

標籤:

package com.seakt.example;

import java.io.*;
import java.lang.String;


public class J_Scanner {

public String infile;
public String outfile;
public String []key = new String[33];
FileOutputStream out = null;


public J_Scanner(String infile,String outfile){

J_Scanner.this.infile = infile;
J_Scanner.this.outfile = outfile;
String[] key_temp = {"","auto","double","int","struct","break","else","long","switch",
"case", "enum","register","typedef","char","extern","return","union","const",
"float","short","unsigned","continue","for","signed","void","default","goto",
"sizeof","volatile","do","if","while","static"};
key = key_temp;

try {
out = new FileOutputStream(new File(outfile));
}
catch(IOException e){
e.printStackTrace();
}
}



//輸出關鍵字
public void print_key(){
for(int i=0;i<J_Scanner.this.key.length;i++){
System.out.printf("%s\r\n",J_Scanner.this.key[i]);
}
}

//讀檔案
public void readFile() {

File file = new File(J_Scanner.this.infile);
BufferedReader reader = null;
try {

reader = new BufferedReader(new FileReader(file));
String tempString = null;

while ((tempString = reader.readLine()) != null) {
System.out.println(tempString.length());
getToken(tempString);

}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}

//判斷是否字元
private boolean isLetter(char ch){
if((ch>=65&&ch<=90)||(ch>=97&&ch<=122)||ch==35||ch==46)
return true;
else
return false;
}

//判斷是否數字
private boolean isDigit(char ch){
if(ch>=48&&ch<=57)
return true;
else
return false;
}

//尋找關鍵字
private int reserve(String s){
for(int i=1;i<33;i++)
if(s==key[i])
return i;
return 0;

}


private void getToken(String s) throws IOException{


String str_write=null;

int i=0,code;
char ch=‘ ‘;
String temp="";
if(s.length()!=0){
ch=s.charAt(i);
}
while(i<s.length()){

//如果是空跳過
while(i<s.length()&&ch==‘ ‘){
i++;
ch=s.charAt(i);
}
//是字母
if(isLetter(ch)){
while((isLetter(ch)||isDigit(ch))&&i<s.length()){
temp+=ch;
i++;
if(i<s.length()-1){
ch=s.charAt(i);
}else{
ch=‘ ‘;
}
}
i--;
code=reserve(temp);
if(code==0){
str_write = temp+"\t"+"標識符"+"\r\n";
out.write(str_write.getBytes());
temp="";str_write="";
}else{
str_write = temp+"\t"+"關鍵字"+"\r\n";
out.write(str_write.getBytes());
temp="";str_write="";
}

}else if(isDigit(ch)){
while(isDigit(ch)){
temp+=ch;
i++;
if(i<s.length()-1){
ch=s.charAt(i);
}else{
ch=‘ ‘;
}
}
i--;

str_write = temp+"\t"+"常數"+"\r\n";
out.write(str_write.getBytes());
temp="";str_write="";
}else if(ch==‘=‘){
i++;
ch=s.charAt(i);
if(ch== ‘=‘ ){
str_write = "=="+"\t"+"判斷相等"+"\r\n";
out.write(str_write.getBytes());
temp="";str_write="";
}
else{
i--;
str_write = "="+"\t"+"賦值"+"\r\n";
out.write(str_write.getBytes());
temp="";str_write="";
}

}
else if(ch==‘+‘){
i++;
ch=s.charAt(i);
if(ch==‘+‘)
out.write(("++"+‘\t‘+"加1"+"\r\n").getBytes());
else{
i--;
out.write(("++"+‘\t‘+"加號"+"\r\n").getBytes());
}
}
else if(ch==‘&‘){
i++;
ch=s.charAt(i);
if(ch==‘&‘)
out.write(("&&"+‘\t‘+"與"+"\r\n").getBytes());
else{
i--;
out.write(("&"+‘\t‘+"按位與"+"\r\n").getBytes());
}
}
else if(ch==‘|‘){
i++;
ch=s.charAt(i);
if(ch==‘|‘)
out.write(("||"+‘\t‘+"或"+"\r\n").getBytes());
else{
i--;
out.write(("|"+‘\t‘+"按位或"+"\r\n").getBytes());
}
}

else if(ch==‘-‘)
out.write(( (char)ch+‘\t‘+"減號"+"\r\n").getBytes());
else if(ch==‘;‘)
out.write(( (char)ch+‘\t‘+"分號"+"\r\n").getBytes());
else if(ch==‘(‘)
out.write(( (char)ch+‘\t‘+"左括弧"+"\r\n").getBytes());
else if(ch==‘)‘)
out.write(( (char)ch+‘\t‘+"右括弧"+"\r\n").getBytes());
else if(ch==‘{‘)
out.write(( (char)ch+‘\t‘+"左花括弧"+"\r\n").getBytes());
else if(ch==‘}‘)
out.write(( (char)ch+‘\t‘+"右花括弧"+"\r\n").getBytes());
else if(ch==‘*‘){
i++;
ch=s.charAt(i);
if(ch==‘*‘)
out.write(("**"+‘\t‘+"運算子"+"\r\n").getBytes());
else{
i--;
out.write(("*"+‘\t‘+"乘號"+"\r\n").getBytes());
}


}
else if(ch==‘<‘){
i++;
ch=s.charAt(i);
if(ch==‘=‘)
out.write(("<="+‘\t‘+"小於等於"+"\r\n").getBytes());
else{
i--;
out.write(("<"+‘\t‘+"小於"+"\r\n").getBytes());
}

}
else if(ch==‘>‘){
i++;
if(i<s.length()-1){
ch=s.charAt(i);
}else{
ch=‘ ‘;
}
if(ch==‘=‘)
out.write((">="+‘\t‘+"大於等於"+"\r\n").getBytes());
else{
i--;
out.write(("<"+‘\t‘+"大於"+"\r\n").getBytes());
}
}
else
return;

i++;
if(i<s.length()){
ch=s.charAt(i);
}
}

}

//關閉輸出資料流
public void close_outStream() throws IOException{
J_Scanner.this.out.close();
}

}

 

java 簡單的詞法分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.