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);
}
}
0917 词法分析程序(java版)的更多相关文章
- 应用程序初次运行数据库配置小程序(Java版)
应用程序初始化数据库配置小程序 之前写过一个Java版的信息管理系统,但部署系统的时候还需要手动的去配置数据库和导入一些初始化的数据才能让系统运行起来,所以我在想是不是可以写一个小程序在系统初次运行的 ...
- 遗传算法 | Java版GA_TSP(我的第一个Java程序)
嗯哼,第一次写博客,准确说是第一次通过文字的方式记录自己的工作,闲话少叙,技术汪的博客就该直奔技术主题(关于排版问题,会在不断写博客的过程中慢慢学习,先将就着用吧,重在技术嘛~~~). 遗传算法(Ge ...
- JAVA版CORBA程序
1.题目分析题目1.Java版CORBA程序1——HelloWorld编写实现显示“Hello,World!+班级+中文姓名”字符串.题目2.JAVA版CORBA程序2——Counter编写实现连加. ...
- java程序猿如何练习java版的易筋经?
故事背景 电视剧<天龙八部>中,阿朱易容后进入少林寺偷走了<易筋经>,她一直想把这本书送给乔峰.耿直的乔峰觉得此书来历不正,不肯接受.几番波折,这本书最后落到聚贤庄庄主游坦之手 ...
- 第六篇 :微信公众平台开发实战Java版之如何自定义微信公众号菜单
我们来了解一下 自定义菜单创建接口: http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_to ...
- 第五篇 :微信公众平台开发实战Java版之如何获取公众号的access_token以及缓存access_token
一.access_token简介 为了使第三方开发者能够为用户提供更多更有价值的个性化服务,微信公众平台 开放了许多接口,包括自定义菜单接口.客服接口.获取用户信息接口.用户分组接口.群发接口等, 开 ...
- 第二篇 :微信公众平台开发实战Java版之开启开发者模式,接入微信公众平台开发
第一部分:微信公众号对接的基本介绍 一.填写服务器配置信息的介绍 登录微信公众平台官网后,进入到公众平台后台管理页面. 选择 公众号基本设置->基本配置 ,点击“修改配置”按钮,填写服务器地址( ...
- 复利计算--4.0 单元测试之JAVA版-软件工程
复利计算--4.0 单元测试-软件工程 前言:由于本人之前做的是C语言版的复利计算,所以为了更好地学习单元测试,于是将C语言版的复利计算修改为JAVA版的. 一.主要的功能需求细分: 1.本金为100 ...
- 网页爬虫的设计与实现(Java版)
网页爬虫的设计与实现(Java版) 最近为了练手而且对网页爬虫也挺感兴趣,决定自己写一个网页爬虫程序. 首先看看爬虫都应该有哪些功能. 内容来自(http://www.ibm.com/deve ...
随机推荐
- 网站jcms流程分析
本实例大致流程:基于jsp页面,通过servlet传递数据调用方法,利用service更改数据库.本文重点分析的是其中的两个小方法add()和delete(),来反映出反射机制的一个具体作用:减少Se ...
- eclipse 打开一个新工程的基本设置
1.代码自动提示 Window -> Preferences -> Java -> Editor -> Content Assist -> Auto Activation ...
- MySql多表关联,根据某列取前N条记录问题
近来遇到一个问题:“MySql多表关联,根据某列取前N条记录”. 刚开始一直在想,SQL语句是否可以做到直接查询出来,但几经折磨,还是没能写出SQL语句,-------如果有大牛的话,望指点迷津.我把 ...
- 控制台打印九九乘法表(for循环的使用)
控制台输出九九乘法表 for(int i=1;i<=9;i++){ for(int j = 1; j <= i; j ++) { System.out.print(j+"*&qu ...
- jQuery属性操作之.attr()
目录 .attr() 调用形式:$("xxx").attr(name) 调用形式:$("xxx").attr(name,value); 调用形式:$(" ...
- php设置文件类型content-type
在PHP中可以通过header函数来发送头信息,还可以设置文件的content-type,下面整理了一些常见文件类型对于的content-type值. //date 2015-06-22//定义编码h ...
- 编译Libuv
Libuv https://github.com/libuv/libuv LibSourcey是基于libuv,集合了第三方用于视频流的开源库,使用C++11. 下载最新 https://dist.l ...
- WPF ControllTemplate Triggers小记 - 简书
原文:WPF ControllTemplate Triggers小记 - 简书 WPF中,样式模板中如果定义EventTrigger事件方式实现动画.那么需要注意两点: 1.对于绑定的属性的Event ...
- ELKStack入门篇(三)之logstash收集日志写入redis
1.部署Redis 1.1.下载redis [root@linux-node2 ~]# wget http://download.redis.io/releases/redis-4.0.6.tar.g ...
- Struts 2(七):国际化
基于Struts 2的Web应用国际化开发非常简单,其中Struts 2的国际化包括如下几部分:校验提示信息国际化.类型转换提示信息国际化.Action信息国际化以及JSP页面国际化. 第一节 JSP ...