编译原理词法分析 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'→+ ...
随机推荐
- excel to datatable (c#用NPOI将excel文件内容读取到datatable数据表中)
将excel文件内容读取到datatable数据表中,支持97-2003和2007两种版本的excel 1.第一种是根据excel文件路径读取excel并返回datatable /// <sum ...
- TKT中文编程语言简介
TKT中文编程语言简介 TKT语言是新型的类似自然语言的汉语编程语言. 它是基于新的语言设计思想创造的语言,和现存的易语言.习语言.O语言.汉编等中文编程语言没有关系. TKT语言特点一: 中文编程 ...
- PYTHON lambda表达式
lambda相当于def定义函数 一一对应
- c/c++中关于sizeof、strlen的使用说明
sizeof: 一般指类型.变量等占用的内存大小(由于在编译时计算,因此sizeof不能用来返回动态分配的内存空间的大小) strlen: c字符串的长度(参数必须是字符型指针 char*,当数组名作 ...
- 33个超级有用必须要收藏的PHP代码样例
作为一个正常的程序员,会好几种语言是十分正常的,相信大部分程序员也都会编写几句PHP程序,如果是WEB程序员,PHP一定是必备的,即使你没用开发过大型软件项目,也一定多少了解它的语法. 在PHP的流行 ...
- Nodejs 饭店
一个Node.js饭店的发展历程 前面的一堆理论似乎不太好明白,最后讲一个关于饭店发展历程的故事作为结尾吧. 第一年 饭店开张,只有一个厨师(同时还兼任老板.服务员.打荷.收银员),当一个客 ...
- css3-columns多列布局
/*css3中的布局*/ .wrapper{ margin:auto; width:300px; height:200px; border:2px dotted blue; -webkit-colum ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- SQL中循环和条件语句
.if语句使用示例: declare @a int begin print @a end else begin print 'no' end .while语句使用示例: declare @i int ...
- 【Python】if __name__ == "__main__"做了什么?
在Python的很多源文件中,会有这样的语句: if __name__ == '__main__': # script code here 比如有两个源文件A.py和B.py,都有上面的代码:在B.p ...