用JAVA实现的Pascal语言的词法分析器

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

import java.io.*;

public class WordAnalyse {

static char[] strbuf = new char[150];//定义一个数组,用以存放从文件读取来的字符串

int keywordIndex; //取关键字的类号

String[] keyWord = {"and","begin","const","div","do","else","end",

"function","if","integer","not","or","procedure","program",

"read","real","then","type","var","while","write"};

public static void main(String[] args) throws IOException {

WordAnalyse wa = new WordAnalyse();

wa.readFile("d:/pascal.txt");

System.out.println("******用JAVA实现的Pascal语言的词法分析器*********");

System.out.println("******The Result:******"+"\n");

wa.run();

System.out.println("\n"+"******Complete !******");

}

//从文件中把字符串读取到一个字符数组中

private void readFile(String url) throws IOException{

int ch,i=0;

FileReader fr = new FileReader(url);

while( (ch=fr.read())!=-1){

strbuf[i++]=(char)ch;

}

}

private boolean isLetter(char ch) {

if('a'<=ch&ch<='z'||'A'<=ch&ch<='Z')

return true;

else return false;

}

private boolean isDigit(char ch) {

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

return true;

else return false;

}

private void run(){ //分析整个strbuf里的字符串

StringBuffer buf = new StringBuffer(); //定义一个缓冲区

for(int i=0; i

//当读头读到space\enter\line的时候,忽略!

if(strbuf[i]==' '||strbuf[i]=='\t'||strbuf[i]=='\n')

i++;

if(isLetter(strbuf[i])) {

int k;

buf.delete(0, buf.length());

while(isLetter(strbuf[i])||isDigit(strbuf[i])) {

buf.append(strbuf[i]);

i++;

}

i--;

//查找buf里面的字符串是否为关键字

for(k =0; k

if(new String(buf).equals(keyWord[k])){

keywordIndex = k;

System.out.println(buf + "\t\t" +keywordIndex);

break;

}

}

if(k>20)

System.out.println(buf + "\t\t" +21);

}

if(isDigit(strbuf[i])) { 1

buf.delete(0, buf.length());

while(isDigit(strbuf[i])){

buf.append(strbuf[i]);

i++;

}

i--;

System.out.println(buf + "\t\t" +22);

}

switch((char)strbuf[i]){

case',':System.out.println(strbuf[i] + "\t\t" + 23);break;

case';':System.out.println(strbuf[i] + "\t\t" + 24);break;

case'.':System.out.println(strbuf[i] + "\t\t" + 26);break;

case'(':System.out.println(strbuf[i] + "\t\t" + 27);break;

case')':System.out.println(strbuf[i] + "\t\t" + 28);break;

case'[':System.out.println(strbuf[i] + "\t\t" + 29);break;

case']':System.out.println(strbuf[i] + "\t\t" + 30);break;

case'+':System.out.println(strbuf[i] + "\t\t" + 34);break;

case'-':System.out.println(strbuf[i] + "\t\t" + 35);break;

case'=':System.out.println(strbuf[i] + "\t\t" + 38);break;

case':':{

buf.delete(0, buf.length());

buf.append(strbuf[i]);

i++;

if(strbuf[i]=='='){

buf.append(strbuf[i]);

System.out.println(buf + "\t\t" + 44);

} else{

System.out.println(buf + "\t\t" + 25); i++;

}

};break;

case'>':{

buf.delete(0, buf.length());

buf.append(strbuf[i]);

i++;

if(strbuf[i]=='='){

buf.append(strbuf[i]);

System.out.println(buf + "\t\t" + 43);

} else{

System.out.println(buf + "\t\t" + 40); i--;

}

};break;

case'<':{

buf.delete(0, buf.length());

buf.append(strbuf[i]);

i++;

if(strbuf[i]=='='){

buf.append(strbuf[i]);

System.out.println(buf + "\t\t" + 42); i++;

} else if(strbuf[i]=='>'){

buf.append(strbuf[i]);

System.out.println(buf + "\t\t" + 41); i++;

} else{

System.out.println(buf + "\t\t" + 39); i++;

}

};break;

}//switch结束

}

} 2 }

相关文档
最新文档