C语言词法分析程序java版

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

附录:核心代码清单

C语言词法分析代码算法实习--------------------------------------------------------By NLLWRQ

功能类:

package conversion;

import java.io.*;

public class Work{

public String filename;

public int flage=1;

public int error[]={0,0,0,0,0};

public int error1[]={0,0,0,0,0,0};

StringBuffer buffer=new StringBuffer();

private String

Keychar[]={"int","char","long","float","double","short","unsigned","struct","union","enum","auto","ext ern",

"static","register","typedef","void","if","else","switch","case","default","do","while","for","break", "continue","return","goto","define","include","undef","ifdef","endif","line","sizeof","asm","fortran","ad a","pascal"};

private String Keyfile[]={"stdio","windows","stdlib","ctype","math","time","string"};

public Work(String filename){

this.filename=filename;

}

boolean isKeyfile(String ch){

for(int i=0;i

if(Keyfile[i].equals(ch))

return true;

return false;

}

boolean isKeychar(String ch){

for(int i=0;i

if(Keychar[i].equals(ch))

return true;

return false;

}

boolean isDigit(char ch){

if(ch>='0'&&ch<='9')

return true;

else

return false;

}

boolean isLeter(char ch){

if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')||ch=='_') return true;

else

return false;

}

boolean isendline(char ch)

{

if(ch=='@')

return true;

else

return false;

}

public void readFile()throws FileNotFoundException{ try{

FileReader fr=new FileReader(this.filename);

BufferedReader br=new BufferedReader(fr);

String temp=null;

char end='@';

while((temp=br.readLine())!=null){

buffer.append(temp);

buffer.append(end);

}

}catch(Exception e){

System.out.println("文件操作错误:"+e.toString());

}

}

public String Analysis(){

int i=0;

char ch;

String str1="";

ViewFile a=null;

while(i

ch=buffer.charAt(i);

if(isLeter(ch)){

StringBuffer temp=new StringBuffer();

temp.append(ch);

ch=buffer.charAt(++i);

while(isLeter(ch)||isDigit(ch)){

temp.append(ch);

ch=buffer.charAt(++i);

}

if(isKeyfile(temp.toString()))

{

ch=buffer.charAt(i++);

if(ch!='.')

error[2]++;

else

{

temp.append(ch);

ch=buffer.charAt(i++);

if(ch!='h')

{

error[3]++;

}

else

temp.append(ch);

}

str1+="("+temp+","+"“"+"line="+flage+"”"+")"+"\t是头文件"+"\n";

}

else if(isKeychar(temp.toString()))

str1+="("+temp+","+"“"+"line="+flage+"”"+")"+"\t是关键字"+"\n";

else

str1+="("+temp+","+"“"+"line="+flage+"”"+")"+"\t是标识符"+"\n";

}

else if((ch=='[')||(ch==']'))

{

str1+="("+ch+","+"”"+"line="+flage+"“"+")"+"\t是运算符"+"\n";

i++;

if(ch=='[')

error[0]++;

else

error[1]++;

}

相关文档
最新文档