编译原理词法分析 java简单实现
package com.csray; import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; public class LexicalAnalysis {
static String id;
public static void main(String[] args) throws IOException{
LexicalAnalysis la = new LexicalAnalysis();
String context = //la.BufferedReaderDemo
//("C:"+System.getProperty("file.separator")+"Users"+System.getProperty("file.separator")+"Administrator"+System.getProperty("file.separator")+"Desktop"+System.getProperty("file.separator")+"Lexical");
la.BufferedReaderDemo("C:\\Users\\Administrator\\Desktop\\Lexical\\lexical.c");
System.out.println(context); for(int i = 0; i < context.length();){
char nowc = context.charAt(i);
//System.out.println(nowc);
if(nowc == ' ' || nowc == '\n' || nowc == '\t'){
id = "illegalCharacter";
++i;
}else if(isAlpha(nowc)){
i = alphaProcess(context, nowc, i);
} else if(isDigit(nowc)){
i = digitProcess(context, nowc, i);
//i++;
} else {
i = otherProcess(context, nowc, i);
//i++;
}
}
} //read the lexical.c file to String
public String BufferedReaderDemo(String path) throws IOException{
File file = new File(path);
if(!file.exists() || file.isDirectory())
throw new FileNotFoundException(); BufferedReader br = new BufferedReader(new FileReader(file));
String tmp = null;
StringBuffer sbuff = new StringBuffer();
tmp = br.readLine();
while(tmp != null){
sbuff.append(tmp+" ");
tmp = br.readLine();
}
return sbuff.toString();
} //alphaProcess
public static int alphaProcess(String context, char c, int i){
StringBuffer word = new StringBuffer();
while((isAlpha(c)) || (isDigit(c)) || c == '_'){
word.append(c);
c = context.charAt(++i);
}
//System.out.println(word.toString()); //check this word if or not a keyword
if(checkKeyword(word.toString())){
id = "isKeyword";
System.out.println("( "+ word.toString() + ", " + id +" )");
} else {
id = "isCommonWord";
System.out.println("( "+ word.toString() + ", " + id +" )");
} return i;
} // checkKeyword
public static boolean checkKeyword(String word){
String keyword = "auto double int struct break else long switch case enum register typedef char "
+ "extern return union const float short unsigned continue for signed void default goto "
+ "sizeof volatile do if while static scnaf printf";
String[] keyWords = keyword.split(" ");
for(int i = 0; i < keyWords.length; ++i){
if(word.equals(keyWords[i])){
return true;
}
}
return false;
} // digitProcess
public static int digitProcess(String context, char c, int i){
StringBuffer digit = new StringBuffer();
while(isDigit(c)){
digit.append(c);
c = context.charAt(++i);
}
id = "isDigit";
System.out.println("( "+ digit.toString() + ", " + id + " )");
return i;
} // otherProcess
public static int otherProcess(String context, char c, int i){
//+ / ( ) { } += /= & " "
StringBuffer operator = new StringBuffer();
StringBuffer delimiter = new StringBuffer();
if(c == '+'){
operator.append(c);
id = "isOperator";
c = context.charAt(++i);
if(c == '='){
operator.append(c);
System.out.println("( "+ operator + ", " + id + " )");
return ++i;
}else if(c == '+'){
operator.append(c);
System.out.println("( "+ operator + ", " + id + " )");
return ++i;
}else {
System.out.println("( "+ operator + ", " + id + " )");
return --i;
}
} else if(c == '('){
delimiter.append(c);
id = "isDelimiter";
System.out.println("(" + delimiter + ", " + id + " )");
return ++i;
} else if(c == ')'){
delimiter.append(c);
id = "isDelimiter";
System.out.println("(" + delimiter + ", " + id + " )");
return ++i;
}else if(c == '{'){
delimiter.append(c);
id = "isDelimiter";
System.out.println("(" + delimiter + ", " + id + " )");
return ++i;
} else if(c == '}'){
delimiter.append(c);
id = "isDelimiter";
System.out.println("(" + delimiter + ", " + id + " )");
return ++i;
}else if(c == ';'){
delimiter.append(c);
id = "isDelimiter";
System.out.println("(" + delimiter + ", " + id + " )");
return ++i;
}else if(c == '='){
operator.append(c);
id = "isOperator";
c = context.charAt(++i);
if(c == '='){
operator.append(c);
System.out.println("( "+ operator + ", " + id + " )");
return ++i;
}else {
System.out.println("( "+ operator + ", " + id + " )");
return i;
}
} else if(c == '/'){
operator.append(c);
c = context.charAt(++i);
if(c == '='){
operator.append(c);
id = "isOperator";
System.out.println("( "+ operator + ", " + id + " )");
return ++i;
}else if(c == '/'){
operator.append(c);
id = "isAnnotation";
c = context.charAt(++i);
while(c != ' '){
operator.append(c);
c = context.charAt(++i);
}
System.out.println("( "+ operator + ", " + id + " )");
return i;
}
}else if(c == '&'){
operator.append(c);
id = "isOperator";
System.out.println("(" + operator + ", " + id + " )");
return ++i;
}else if(c == '"'){
delimiter.append(c);
id = "isDelimiter";
System.out.println("(" + delimiter + ", " + id + " )");
return ++i;
}else if(c == ','){
delimiter.append(c);
id = "isDelimiter";
System.out.println("(" + delimiter + ", " + id + " )");
return ++i;
}else if(c == '%'){
operator.append(c);
id = "isOperator";
c = context.charAt(++i);
if(c == 'd'){
operator.append(c);
System.out.println("(" + operator + ", " + id + " )");
return ++i;
} return --i;
}
return i;
} //isDigit
public static boolean isDigit(char c){
if(c >= '0' && c <= '9')
return true;
return false;
} //isAlpha
public static boolean isAlpha(char c){
if(c >= 'A' && c <= 'Z')
return true;
else if(c >= 'a' && c <= 'z')
return true;
return false;
}
}
实例:
//test
int main()
{ float nu0_m;
int num = 100;
num++;
scanf("%d", &num);
printf("%d", num);
return 0;
}
结果:
//test int main() { float nu0_m; int num = 100; num++; scanf("%d", &num); printf("%d", num); return 0; }
( //test, isAnnotation )
( int, isKeyword )
( main, isCommonWord )
((, isDelimiter )
(), isDelimiter )
({, isDelimiter )
( float, isKeyword )
( nu0_m, isCommonWord )
(;, isDelimiter )
( int, isKeyword )
( num, isCommonWord )
( =, isOperator )
( 100, isDigit )
(;, isDelimiter )
( num, isCommonWord )
( ++, isOperator )
(;, isDelimiter )
( scanf, isCommonWord )
((, isDelimiter )
(", isDelimiter )
(%d, isOperator )
(", isDelimiter )
(,, isDelimiter )
(&, isOperator )
( num, isCommonWord )
(), isDelimiter )
(;, isDelimiter )
( printf, isKeyword )
((, isDelimiter )
(", isDelimiter )
(%d, isOperator )
(", isDelimiter )
(,, isDelimiter )
( num, isCommonWord )
(), isDelimiter )
(;, isDelimiter )
( return, isKeyword )
( 0, isDigit )
(;, isDelimiter )
(}, isDelimiter )
编译原理词法分析 java简单实现的更多相关文章
- 编译原理-词法分析05-正则表达式到DFA-01
编译原理-词法分析05-正则表达式到DFA 要经历 正则表达式 --> NFA --> DFA 的过程. 0. 术语 Thompson构造Thompson Construction 利用ε ...
- 编译原理-词法分析04-NFA & 代码实现
编译原理-词法分析04-NFA & 代码实现 0.术语 NFA 非确定性有穷自动机nondeterministic finite automation. ε-转换ε-transition 是无 ...
- [JAVA] 一个可以编辑、编译、运行Java简单文件的记事本java实现
本来是Java课做一个仿windows记事本的实验,后来突然脑子一热,结果就给它加了一个编译运行Java文件的功能. 本工程总共大约3000行代码,基本上把所学的java界面.文件.控件的功能都包含在 ...
- 高性能页面加载技术--BigPipe设计原理及Java简单实现
1.技术背景 动态web网站的历史可以追溯到万维网初期,相比于静态网站,动态网站提供了强大的可交互功能.经过几十年的发展,动态网站在互动性和页面显示效果上有了很大的提升,但是对于网站动态网站的整体页面 ...
- 【Android】线程池原理及Java简单实现
线程池简介 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 假设一个服务器完成一项任务所需时间为: T1 创建线程时间 T2 在线程中 ...
- 编译原理-词法分析03-DFA
0.术语 DFA Deterministic finite automation,确定性有穷自动机.一般用于翻译正则表达式. 状态state DFA中的圆圈,表示模式在识别过程中的位置. 转换tran ...
- Atitit.编译原理与概论
Atitit.编译原理与概论 编译原理 词法分析 Ast构建,语法分析 语意分析 6 数据结构 1. ▪ 记号 2. ▪ 语法树 3. ▪ 符号表 4. ▪ 常数表 5. ▪ 中间代码 1. ▪ 临 ...
- Java 实现《编译原理》简单词法分析功能 - 程序解析
Java 实现<编译原理>简单词法分析功能 - 程序解析 简易词法分析功能 要求及功能 (1)读取一个 txt 程序文件(最后的 # 作为结束标志,不可省去) { int a, b; a ...
- Java 实现《编译原理》简单-语法分析功能-LL(1)文法 - 程序解析
Java 实现<编译原理>简单-语法分析功能-LL(1)文法 - 程序解析 编译原理学习,语法分析程序设计 (一)要求及功能 已知 LL(1) 文法为: G'[E]: E→TE' E'→+ ...
随机推荐
- Ubuntu安装SSH服务器故障分析及解决办法(错误1:E:软件包 openssh-server 还没有可供安装的候选者,错误2:E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系)
• 微博: 小样儿老师2015 Windows下做Linux开发需要SSH强大功能的支持.安装SSH的过程会出现了很多问题,看完这篇文章可以让你少走些弯路,PS:折腾一下午的成果. Ubuntu ...
- CNN车型分类总结
最近在做一个CNN车型分类的任务,首先先简要介绍一下这个任务. 总共30个类,训练集图片为车型图片,类似监控拍摄的车型图片,训练集测试集安6:4分,训练集有22302份数据,测试集有14893份数据. ...
- angular开发单页面应用--页面资源部分
关于angular是什么,能够干什么就不在这里解释了,自行搜索了,或者等稍晚一点再解释... angular适合开发单页面应用,这句话在介绍angular的网站和博客里都可以提到.因为angular是 ...
- HTML 接收本地文件
HTML代码请把文件拖到下面的框里触发drop事件读取拖放的文件常用情况:结合XMLHttpRequest和拖放文件实现上传查看和管理本地文件和图片 <!DOCTYPE HTML> < ...
- PPM图片格式及其C读写代码
PPM图像格式介绍 PPM图像格式是由Jef Poskanzer 大叔,在我出生那一年,也就是1991年所创造的,碰巧的是PPM也是天蝎座. PPM(Portable Pixmap Format)还有 ...
- grep-2.26 sed-4.2.2 awk-4.1.4 wget-1.18 pcregrep-8.39 pcre2grep-10.22 for windows 最新版本静态编译
-------------------------------------------------------------------------------------------- grep (G ...
- The note of Vue.js
In computed field, increment operator is not supported.
- Jquery获取select选中的文本与值
jquery获取select选择的文本与值获取select :获取select 选中的 text : $("#ddlregtype").find("option:s ...
- 修改git的远程仓库命令
1. 修改命令 git remte origin set-url URL 2.先删后加 git remote rm origin git remote add origin git@github.co ...
- AE+C# 版本更新问题 命名空间“ESRI”中不存在类型或命名空间名称“Arcgis”(是缺少程序集引用吗?)
解决办法: 1 引用 将下图中解决方案->引用中带感叹号的已用移除,然后添加新的.因为不同版本用的.dll不同,因此需要删除,然后重新加载. 如果是系统库文件, 直接在.NET下头添加,如果是自 ...