码迷,mamicode.com
首页 > 编程语言 > 详细

0917 词法分析程序(java版)

时间:2015-10-06 12:52:32      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:

1.程序说明:

          c语言和java两个版本的都做了一次,略作比较后觉得虽然用的语言不一样,但是中心思想都是一样的。本程序(java版)思路是先把源代码程序存入一个大字符串中,

   然后分析该字符串,按照不同的词法分拆成一个个小字符串存到一个字符串数组中去,接着识别一些关键词,并对应相应的种别码,利用循环体一一列出。而对于c语言的

  版本,笔者的思路是:由于C语言没有字符串类型,所以先把源代码输入到一个字符数组中去,然后按照词法截取单词或符号,逐一存到一个结构体数组中去,由于需要一

  个个字符操作,要用到较多的循环体,所以速度相对较慢,相信有更好的思路,笔者还想琢磨一下,所以就不发表C语言版了。

 

2.发现问题和不足:

        一开始觉得这个程序很小,可能会太简单,但实际操作起来发现也会出现很多问题,甚至包括一些很低级的错误。还有就是程序本身具有一定的局限性,能识别的关键词

   有限,能输入的格式有限,还有其他一些细节上的问题,要想做得更好就仍需慢慢修改。打代码就像种花,有时真的急不来,在保证效率的同时也需要耐心。

 

3.运行结果:

技术分享

4.源代码:

 

 

package 词法分析;
import java.util.Scanner;
public class fenxi {public static void main(String[] args) {   //主函数

Scanner scanner=new Scanner(System.in);
int k=0,i=0;
String[] word=new String[20];
word[0]="";
System.out.println("请输入一个程序(以‘#‘结束):");
String sent=scanner.nextLine();
// System.out.println(sent);
judgeType(k, i, word, sent);//判断分析组成类型
justOne(word);//只有一个英文字母的特殊情况统一为"l(l|d)*"
outPut(word);
}

 

private static void justOne(String[] word) {//判断是否有单个字母的特殊情况
int i = 0,k = 0;
while(word[i]!= null){
if(word[i]!=""){
if(Character.isLetter(word[i].charAt(0))&&word[i].length()==1){
word[i]="l(l|d)*";
k++;
if(k>1)
word[i]="NULL";//不要重复的
}
}
i++;
}
}

private static void outPut(String[] word) { //对应种别码,并输出列表
int i = 0;
System.out.println("单词符号\t\t\t"+"种别码");
while(word[i]!=null){
switch(word[i]){
case "begin":
System.out.println(word[i]+"\t\t\t"+"1");
break;
case "if":
System.out.println(word[i]+"\t\t\t"+"2");
break;
case "then":
System.out.println(word[i]+"\t\t\t"+"3");
break;
case "while":
System.out.println(word[i]+"\t\t\t"+"4");
break;
case "do":
System.out.println(word[i]+"\t\t\t"+"5");
break;
case "end":
System.out.println(word[i]+"\t\t\t"+"6");
break;
case "l(l|d)*":
System.out.println(word[i]+"\t\t\t"+"7");
break;
case "dd*":
System.out.println(word[i]+"\t\t\t"+"8");
break;
case "+":
System.out.println(word[i]+"\t\t\t"+"9");
break;
case "-":
System.out.println(word[i]+"\t\t\t"+"10");
break;
case "*":
System.out.println(word[i]+"\t\t\t"+"11");
break;
case "/":
System.out.println(word[i]+"\t\t\t"+"12");
break;
case ":":
System.out.println(word[i]+"\t\t\t"+"13");
break;
case ":=":
System.out.println(word[i]+"\t\t\t"+"14");
break;
case "<":
System.out.println(word[i]+"\t\t\t"+"15");
break;
case "<=":
System.out.println(word[i]+"\t\t\t"+"16");
case "<>":
System.out.println(word[i]+"\t\t\t"+"17");
break;
case ">":
System.out.println(word[i]+"\t\t\t"+"18");
break;
case ">=":
System.out.println(word[i]+"\t\t\t"+"19");
break;
case "=":
System.out.println(word[i]+"\t\t\t"+"20");
break;
case ";":
System.out.println(word[i]+"\t\t\t"+"21");
break;
case "(":
System.out.println(word[i]+"\t\t\t"+"22");
break;
case ")":
System.out.println(word[i]+"\t\t\t"+"23");
break;
case "#":
System.out.println(word[i]+"\t\t\t"+"24");
break;
case "for":
System.out.println(word[i]+"\t\t\t"+"25");
break;
}
i++;
}
}

private static void judgeType(int k, int i, String[] word, String sent) {
while(sent.charAt(k)!=‘#‘){
word[i]="";
while(Character.isLetter(sent.charAt(k))){//是否为字母
word[i]=word[i]+sent.charAt(k);
k++;

}
i++;
word[i]="";

//是否为连续的运算符
while((sent.charAt(k)==‘<‘||sent.charAt(k)==‘>‘||sent.charAt(k)==‘=‘||sent.charAt(k)==‘:‘)&&(sent.charAt(k+1)==‘<‘||sent.charAt(k+1)==‘>‘||sent.charAt(k+1)==‘=‘||sent.charAt(k+1)==‘:‘)){
word[i]=""+sent.charAt(k)+sent.charAt(k+1);
i++;
k=k+2;
word[i]="";
}

//是否为单个符号或运算符
while(sent.charAt(k)==‘(‘||sent.charAt(k)==‘)‘||sent.charAt(k)==‘;‘||sent.charAt(k)==‘:‘||sent.charAt(k)==‘+‘||sent.charAt(k)==‘-‘||sent.charAt(k)==‘*‘||sent.charAt(k)==‘/‘||sent.charAt(k)==‘<‘||sent.charAt(k)==‘>‘||sent.charAt(k)==‘=‘){
word[i]=""+sent.charAt(k);
k++;
i++;
word[i]="";
}


while(sent.charAt(k)==‘ ‘){//是否为空格
// System.out.println("space");
word[i]="NULL";
k++;
i++;
word[i]="";
}

while(Character.isDigit(sent.charAt(k))){//是否为数字
word[i]="dd*";
k++;
i++;
word[i]="";
}

}

word[i]=""+‘#‘;
// System.out.println("跳出循环"+word[i]+i);
}
}

 

0917 词法分析程序(java版)

标签:

原文地址:http://www.cnblogs.com/liangzhilin/p/4856939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!