编译原理词法分析 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'→+ ...
随机推荐
- Go收藏
Go项目收藏 电子书 1.Go Web 编程 2.Go入门指南(the-way-to-go_ZH_CN) 3.Go语言圣经(中文版) Go by Example 中文 一些Go英文电子书 High P ...
- windows安装rabbitmq
官网下载windows安装版本:http://www.rabbitmq.com/install-windows.html ,安装文件rabbitmq-server-3.6.5.exe 前提:安装erl ...
- 无意之间发现的Servlet3.0新特性@WebServlet
今天无意之间看到了一个注解,这个注解就是@WebServlet,@WebServlet 用于将一个类声明为 Servlet,该注解将会在部署时被容器处理,容器将根据具体的属性配置将相应的类部署为 Se ...
- vim 添加php自动补全 并格式化代码
自动补全,修改/etc/vimrc的配置 vim /etc/vimrc 添加: filetype plugin on autocmd FileType php set omnifunc=phpcomp ...
- C和指针 第十七章 习题
17.8 为数组形式的树编写模块,用于从树中删除一个值,如果没有找到,程序节点 ArrayBinaryTree.c // // Created by mao on 16-9-18. // #inclu ...
- angularjs向后台传递数据,与后端进行交互
angularjs之数据交互 function loadLeftFirstNodes (){ $http.get(sourceUrl,{ params:{ mpId: mpId, visits: ce ...
- Redis Cluster 介绍与使用
Redis Cluster 功能特性 Redis 集群是分布式的redis 实现,具有以下特性: 1. 高可用性与可线性扩张到1000个节点 2. 数据自动路由到多个节点 3. 节点间数据共享 4. ...
- PHP window下安装Spl_Types模块
1. Window下,Spl_Types的模块的下载地址:http://pecl.php.net/package/SPL_Types/0.4.0/windows 2. php的可执行文件已经加到系统的 ...
- redis lua
需求是在缓存最近一周内用户所有消息列表,考虑用Redis 存储:为每个存储一个独立Sorted Set,value 为消息体,Score 为MessageId,用以实现增量消息同步. 问题就来了:So ...
- 利用chrome插件批量读取浏览器页面内容并写入数据库
试想一下,如果每天要收集100页网页数据甚至更多.如果采用人工收集会吐血,用程序去收集也就成为一个不二的选择.首先肯定会想到说用java.php.C#等高级语言,但这偏偏又有个登陆和验证码,搞到无所适 ...