java 简单的词法分析

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();
}

}

 

posted @ 2015-05-05 02:18  ouminghai  阅读(1583)  评论(0编辑  收藏  举报