资源简介
语法分析器java实现,包含词法分析器。程序代码作为词法分析器的输入,词法分析器的输出作为语法分析器的输入,由语法分析器输出语法分析的结果。

代码片段和文件信息
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 词法分析器
*/
public class LexicalAnalyzer {
// 单词种别码 1-17为关键字种别码
public static final int CHAR = 1;
public static final int SHORT = 2;
public static final int INT = 3;
public static final int LONG = 4;
public static final int FLOAT = 5;
public static final int DOUBLE = 6;
public static final int FINAL = 7;
public static final int STATIC = 8;
public static final int IF = 9;
public static final int ELSE = 10;
public static final int WHILE = 11;
public static final int DO = 12;
public static final int FOR = 13;
public static final int BREAK = 14;
public static final int CONTINUE = 15;
public static final int VOID = 16;
public static final int RETURN = 17;
// 20为标识符种别码
public static final int ID = 20;
// 30为常量种别码
public static final int NUM = 30;
// 31-40为运算符种别码
public static final int AS = 31; // =
public static final int EQ = 32; // ==
public static final int GT = 33; // >
public static final int LT = 34; // <
public static final int GE = 35; // >=
public static final int LE = 36; // <=
public static final int ADD = 37; // +
public static final int SUB = 38; // -
public static final int MUL = 39; // *
public static final int DIV = 40; // /
// 41-49为界限符种别码
public static final int LP = 41; // (
public static final int RP = 42; // )
public static final int LBT = 43; // [
public static final int RBT = 44; // ]
public static final int LBS = 45; // {
public static final int RBS = 46; // }
public static final int COM = 47; //
public static final int COL = 48; // :
public static final int SEM = 49; // ;
// -1为无法识别的字符标志码
public static final int ERROR = -1;
public static int errorNum = 0; // 记录词法分析错误的个数
// 判断是否为字母
public static boolean isLetter(char c) {
if (((c >= ‘a‘) && (c <= ‘z‘)) || ((c >= ‘A‘) && (c <= ‘Z‘))) {
return true;
}
return false;
}
// 判断是否为关键字,若是则返回关键字种别码
public static int isKeyID(String str) {
String keystr[] = {“char“ “short“ “int“ “long“ “float“ “double“ “final“ “static“ “if“ “else“ “while“
“do“ “for“ “break“ “continue“ “void“ “return“};
for (int i = 0; i < keystr.length; i++) {
if (str.equals(keystr[i])) {
return i + 1;
}
}
return 0;
}
// 判断是否为常量(整数、小数、浮点数)
public static boolean isNum(String str) {
int dot = 0; // .的个数
int notNum = 0; // 不是数字的个数
for (int i = 0; i < str.length(); i++) {
if (!(str.charAt(i) >= ‘0‘ && str.charAt(i) <= ‘9‘)) {
notNum++;
if (notNum > dot + 1) {
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-12-06 18:08 SyntaxAnalyzer\
目录 0 2019-01-18 10:22 SyntaxAnalyzer\.idea\
文件 278 2018-12-05 15:49 SyntaxAnalyzer\.idea\misc.xm
文件 275 2018-12-05 15:49 SyntaxAnalyzer\.idea\modules.xm
文件 17040 2019-01-18 10:22 SyntaxAnalyzer\.idea\workspace.xm
目录 0 2018-12-05 20:10 SyntaxAnalyzer\out\
目录 0 2018-12-05 20:10 SyntaxAnalyzer\out\production\
目录 0 2018-12-06 19:31 SyntaxAnalyzer\out\production\SyntaxAnalyzer\
目录 0 2018-12-06 20:02 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\
文件 70 2018-12-06 19:42 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\input1.txt
文件 56 2018-12-06 19:46 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\input2.txt
文件 53 2018-12-06 19:49 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\input3.txt
文件 112 2018-12-06 19:52 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\input4.txt
文件 77 2018-12-06 19:56 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\input5.txt
文件 375 2018-12-06 20:02 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\input6.txt
文件 3275 2018-12-06 19:44 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\output1.txt
文件 3838 2018-12-06 19:47 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\output2.txt
文件 1818 2018-12-06 19:51 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\output3.txt
文件 6756 2018-12-06 17:03 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\output4.txt
文件 5089 2018-12-06 20:01 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\output5.txt
文件 29494 2018-12-06 19:39 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\output6.txt
文件 38 2018-12-06 19:39 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\test1.txt
文件 20 2018-12-06 19:44 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\test2.txt
文件 17 2018-12-06 19:47 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\test3.txt
文件 53 2018-12-06 19:51 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\test4.txt
文件 36 2018-12-06 17:03 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\test5.txt
文件 179 2018-12-06 20:01 SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\test6.txt
文件 517 2018-12-05 22:00 SyntaxAnalyzer\out\production\SyntaxAnalyzer\LexicalAnalyzer$Pair.class
文件 9945 2018-12-05 22:00 SyntaxAnalyzer\out\production\SyntaxAnalyzer\LexicalAnalyzer.class
文件 595 2018-12-06 19:31 SyntaxAnalyzer\out\production\SyntaxAnalyzer\SyntaxAnalyzer$Production.class
文件 14143 2018-12-06 19:31 SyntaxAnalyzer\out\production\SyntaxAnalyzer\SyntaxAnalyzer.class
............此处省略23个文件信息
- 上一篇:UUID生成工具
- 下一篇:含界面的java的词法分析器
相关资源
- 微博系统(Java源码,servlet+jsp),适
- java串口通信全套完整代码-导入eclip
- JNA所需要的jar包
- jsonarray所必需的6个jar包.rar
- 三角网构TIN生成算法,Java语言实现
- utgard用到的jar包
- java代码编写将excel数据导入到mysql数据
- Java写的cmm词法分析器源代码及javacc学
- JAVA JSP公司财务管理系统 源代码 论文
- JSP+MYSQL旅行社管理信息系统
- commons-beanutils-1.8.3.jar
- 推荐算法的JAVA实现
- 基于Java的酒店管理系统源码(毕业设
- java-图片识别 图片比较
- android毕业设计
- ehcache-core-2.5.1.jar
- android-support-v4.jar已打包进去源代码
- java23种设计模式+23个实例demo
- java Socket发送/接受报文
- JAVA828436
- java界面美化 提供多套皮肤直接使用
- 在线聊天系统(java代码)
- 基于Java的图书管理系统807185
- java中实现将页面数据导入Excel中
- java 企业销售管理系统
- java做的聊天系统(包括正规课程设计
- Java编写的qq聊天室
- 商店商品管理系统 JAVA写的 有界面
- JAVA开发聊天室程序
- 在linux系统下用java执行系统命令实例
评论
共有 条评论