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
0917 词法分析程序(java版)相关推荐
- java订单号 github_GitHub - github2zhang/J12306: 12306抢票程序JAVA版
J12306抢票助手 12306抢票程序JAVA版,自动登录-验证-查票-购票/自动候补.只需简单的配置即可运行进行快捷抢票. 使用说明 引入jar依赖 手动添加项目lib文件夹中的依赖包 配置文件c ...
- 微信小程序java版Spring Cloud+Spring Boot+mybatis+security+uniapp+Redis+MQ+VR全景
@源码地址来源: https://minglisoft.cn/honghu2/business.html 微信小程序登录代码: /*** Copyright © 2012-2017 <a hre ...
- 上班太难熬?想知道还有多久时间下班?分享一段下班倒计时程序(Java版)(#^.^#)
先看效果 上代码(中文命名纯属好玩) import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; impo ...
- 最终版宝宝取名程序,java版,我家宝宝名字已经确定。 HttpURLConnection
运行的时候,需要Log4j.jar 最好使用命令运行 java -cp log4j.jar,. XingMing > xingming.txt 仅供娱乐. import java.io.Buf ...
- 最终版宝宝取名程序,java版,我宝名字已经确定。 HttpURLConnection
运行的时候,需要Log4j.jar 最好使用命令运行 java -cp log4j.jar,. XingMing > xingming.txt 仅供娱乐. import java.io.Buff ...
- 京东抢购飞天茅台秒杀程序JAVA版
这两天在Github 上看到了一个开源的抢茅台程序源码(点我查看),分享下. 亲测程序可编译可用,试过1次没抢到哈哈哈,你也可以跑起来试试或者改下源代码优化下试试! 使用: 1. 配置文件initDa ...
- 实验一基于 的词法分析实验_[源码和文档分享]基于JAVA实现的基于DFA的词法分析程序...
1 实验目的 根据自己确定的正规表达式,编写.调试一个词法分析程序,对语句进行词法分析,从而更好理解词法分析原理. 2 内容描述 此程序用java编写.程序读取一个文本文件,并对其中的内容进行词法分析 ...
- 应用程序初次运行数据库配置小程序(Java版)
应用程序初始化数据库配置小程序 之前写过一个Java版的信息管理系统,但部署系统的时候还需要手动的去配置数据库和导入一些初始化的数据才能让系统运行起来,所以我在想是不是可以写一个小程序在系统初次运行的 ...
- php 3des 兼容java,java版3des加密程序,可与php兼容
java版3des加密程序,可与php兼容 时间:2009-03-29 22:35来源:未知 作者:admin 点击:次 代码: import java.io.UnsupportedEncodingE ...
- CRMEB全开源Java版微信小程序商城,附源码
CRMEB-JAVA版简介 CRMEB商城JAVA版,SpringBoot + Maven + Swagger + Mybatis Plus + Redis + Uniapp +Vue 包含移动端.小 ...
最新文章
- Ambari安装之部署3个节点的HA分布式集群
- 5、JUC--实现 Callable 接口
- 人才短缺、成本高昂,制造企业智能化转型路径如何破局?
- 上拉加载 php,php+jquery 上拉加载
- java exception 二次抛出_java – 如何在scheduleWithFixedDelay抛出异常时重新启动计划?...
- Java 14:instanceof的模式匹配
- python中字符串格式化的形式_Python中format函数字符串格式化入门
- Android 启动过程简析(一)之 init 进程
- 【BZOJ2440】完全平方数,莫比乌斯反演+二分答案+容斥思想
- Opera R3 将使用新的用户界面
- 教妹学Java(十七):do-while 循环
- SNF快速开发平台MVC-集成了百度开源项目echars
- 【FPGA的小娱乐】tft显示屏生成信号辅助测试阵列
- InDesign 教程如何制作明信片?
- 去掉RecyclerView的默认item动画
- oracle永久解scott,Oracle 11g中解决被锁定的scott用户的方法
- Cris 的 Spark Streaming 笔记
- JVM日历:Java 2018大事回顾
- 速递!MongoDB最新书籍出版啦:MongoDB进阶与实战-微服务整合、性能优化、架构管理
- source 命令解析
热门文章
- 使用Java 自身Timer API实现定时器的方法
- 运行Android项目时指定特定的AVD进行测试
- Parcelable接口的使用
- C++ 随机数函数rand()与srand()的思考
- MySQL 索引的查询、创建与删除
- 15. 注意string实现的多样性
- 12. Copy all parts of an object
- 二叉树的前中后序遍历的三种实现(java)以及一些小习题
- python链表逆序实例_python 单链表翻转的简单示例
- python字符串长度排序_python-对混合类型和不同长度的字符串进行排序