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

0917 词法分析程序(java版)的更多相关文章

  1. 应用程序初次运行数据库配置小程序(Java版)

    应用程序初始化数据库配置小程序 之前写过一个Java版的信息管理系统,但部署系统的时候还需要手动的去配置数据库和导入一些初始化的数据才能让系统运行起来,所以我在想是不是可以写一个小程序在系统初次运行的 ...

  2. 遗传算法 | Java版GA_TSP(我的第一个Java程序)

    嗯哼,第一次写博客,准确说是第一次通过文字的方式记录自己的工作,闲话少叙,技术汪的博客就该直奔技术主题(关于排版问题,会在不断写博客的过程中慢慢学习,先将就着用吧,重在技术嘛~~~). 遗传算法(Ge ...

  3. JAVA版CORBA程序

    1.题目分析题目1.Java版CORBA程序1——HelloWorld编写实现显示“Hello,World!+班级+中文姓名”字符串.题目2.JAVA版CORBA程序2——Counter编写实现连加. ...

  4. java程序猿如何练习java版的易筋经?

    故事背景 电视剧<天龙八部>中,阿朱易容后进入少林寺偷走了<易筋经>,她一直想把这本书送给乔峰.耿直的乔峰觉得此书来历不正,不肯接受.几番波折,这本书最后落到聚贤庄庄主游坦之手 ...

  5. 第六篇 :微信公众平台开发实战Java版之如何自定义微信公众号菜单

    我们来了解一下 自定义菜单创建接口: http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_to ...

  6. 第五篇 :微信公众平台开发实战Java版之如何获取公众号的access_token以及缓存access_token

    一.access_token简介 为了使第三方开发者能够为用户提供更多更有价值的个性化服务,微信公众平台 开放了许多接口,包括自定义菜单接口.客服接口.获取用户信息接口.用户分组接口.群发接口等, 开 ...

  7. 第二篇 :微信公众平台开发实战Java版之开启开发者模式,接入微信公众平台开发

    第一部分:微信公众号对接的基本介绍 一.填写服务器配置信息的介绍 登录微信公众平台官网后,进入到公众平台后台管理页面. 选择 公众号基本设置->基本配置 ,点击“修改配置”按钮,填写服务器地址( ...

  8. 复利计算--4.0 单元测试之JAVA版-软件工程

    复利计算--4.0 单元测试-软件工程 前言:由于本人之前做的是C语言版的复利计算,所以为了更好地学习单元测试,于是将C语言版的复利计算修改为JAVA版的. 一.主要的功能需求细分: 1.本金为100 ...

  9. 网页爬虫的设计与实现(Java版)

    网页爬虫的设计与实现(Java版)     最近为了练手而且对网页爬虫也挺感兴趣,决定自己写一个网页爬虫程序. 首先看看爬虫都应该有哪些功能. 内容来自(http://www.ibm.com/deve ...

随机推荐

  1. [jQuery]常用正则表达式

    验证网址:^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$电子 ...

  2. scrapy管道MySQL简记

    import pymysqlfrom scrapy.exceptions import DropItemimport time class ErshouchePipeline(object): def ...

  3. Centos 下 mysql 安装过程

    1. 检查系统自带的Mysql,并卸载自带的版本 [root@iZ2366ycl7sZ config]# yum list installed |grep mysql mysql.x86_64 5.1 ...

  4. 需求:promise执行买菜做饭过程

    需求:promise执行买菜做饭过程 1.买菜 2.洗菜 3.做饭 4.吃饭 <!DOCTYPE html> <html lang="en"> <he ...

  5. JavaScript入门学习(1)

    <html> <script type ="text/javascript"> var i,j; for (i=1;i<10;i++){ for (j ...

  6. Delphi获取Android下GPS的NMEA 0183数据

    下面的程序,可以实现Android下获取GNSS的NMEA0183数据: unit utAndroidNmea; interface uses Androidapi.JNIBridge, Androi ...

  7. 05.kafka提前准备工作:搭建zookeeper集群环境

    总体参考:http://www.cnblogs.com/zhangs1986/p/6564839.html 搭建之间同步下spark01.02.03的环境 复制/opt/flume这个文件夹到 spa ...

  8. Shellinabox on centos6.9

    介绍 一款实用的web linux终端, 并且保证操作安全性(屏蔽root用户) 下面以centos6.9为例 安装 首先安装epel仓库,再安装shellinabox yum -y install ...

  9. C语言顺序队列

    顺序队列是一种只能在一头进和另一头出的数据结构,所以结构体里设2个指针分别指向头部和尾部,用数组来存储数据. #define MAXSIZE 1024 typedef int elemtype; ty ...

  10. (数据科学学习手札51)用pymysql来操控MySQL数据库

    一.简介 pymysql是Python中专门用来操控MySQL数据库的模块,通过pymysql,可以编写简短的脚本来方便快捷地操控MySQL数据库,本文就将针对pymysql的基本功能进行介绍: 二. ...