0917 词法分析程序(java版)

本文介绍了一个简单的词法分析程序实现案例,该程序能够读取用户输入的字符串并进行词法分析,输出对应的种别码。通过此示例可以了解如何识别关键字、运算符、标识符等基本元素。

 

1.运行结果:

2.源代码:

 

 

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

 

转载于:https://www.cnblogs.com/liangzhilin/p/4856939.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值