编译原理词法分析 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'→+ ...
随机推荐
- Python绘图
1.二维绘图 a. 一维数据集 用 Numpy ndarray 作为数据传入 ply 1. import numpy as np import matplotlib as mpl import mat ...
- linux虚拟系统determining IP information for eth0...failed
这几天学习老男孩Linux运维 其中有一节视频进行连接网络时出现: 上网搜了很多方法,包括:向如下三个文件中添加如下代码 check_link_down() { return 1; } (1)/etc ...
- pycharm快捷键及一些常用设置
pycharm快捷键及一些常用设置,有需要的朋友可以参考下. Alt+Enter 自动添加包 Ctrl+t SVN更新 Ctrl+k SVN提交 Ctrl + / 注释(取消注释)选择的行 Ctrl+ ...
- SAP模板
用的是kuangbin的模板:http://www.cnblogs.com/kuangbin/archive/2012/09/29/2707955.html ;//点数的最大值 ;//边数的最大值 c ...
- SQL分页获取数据
SQL Server分页 select * from (') t Oracle分页 SELECT * FROM (' ORDER BY MaterialNM) t
- 耿丹CS16-2班第六次作业汇总
Deadline: 2016-11-13 11:59 作业内容 第六次作业总结 00.本次题目分值最高为**6分/题 × 7题 + 5分/篇 × 1篇 = 47分**,其中有新解法者每题加原创分**2 ...
- 收集免费可用稳定的vpn
收集免费可用稳定的vpn,经常用到,所以记录一下,方便自己不备之需. 1,https://www.lvbeivpn.cc/share.shtml?id=a3bd9527225d4746bb3a5761 ...
- 父类div高度适应子类div
父类div高度适应子类div 通常有许多div的高度由子类的高度决定父类的高度,所以需要父类div要适应子类div的高度,一般情况父类的高度可以直接设置成“auto”即可. 在有的情况下,子类div会 ...
- monkeyrunner脚本录制
1.在窗口输入 monkeyrunner monkey_recorder.py 调用录制脚本工具 2.在窗口输入 monkeyrunner monkey_playback.py d:\game ...
- Maven之安装与简单入门一
Maven 是一个项目管理和构建自动化工具,我们最关心的是它的项目构建功能.Maven 使用惯例优于配置的原则. 1,下载安装包,并根据文档说明安装: http://maven.apache.org/ ...