package com.oxford.linkidea;
import java.io.File;
import java.io.FileReader;
import java.util.Stack;
public class SyntaxThink2 {
private static String path = "/home/liujin/workspace2/Demo/expression_input.txt";
private static String path2="/home/liujin/workspace2/Demo/oxford.txt";
static Stack stack = new Stack();
static String[] data;
static int count=-1;
static int flag = 0;
//LL(1)分析表,数组中E`由A代替,T`由B代替
static String[][] list ={{"","i","+","*","(",")","#"},
{"E","TA",null,null,"TA",null,null},
{"A",null,"+TA",null,null,"@","@"},
{"T","FB",null,null,"FB",null,null},
{"B",null,"@","*FB",null,"@","@"},
{"F","i",null,null,"(E)",null,null}
};
/*
* 读取文件
*/
public static String getFileContent(String paths) throws Exception{
File file= new File(paths);
String expression_input = null;
if(!file.exists()){
System.out.println("文件不存在,请输入正确的文件路径");
}
FileReader fr = new FileReader(file);
int rs = 0;
char []buf = new char[1024];
while((rs = fr.read(buf)) > 0){
expression_input = new String(buf,0,rs).trim();
}
fr.close();
return expression_input;
}
/**
* 判断是否是终结符
* @param args
* @throws Exception
*/
public static boolean isVn(String str){
if(str.equals("E")||str.equals("A")||str.equals("T")||str.equals("B")||str.equals("F"))
return true;
return false;
}
@SuppressWarnings("unchecked")
public static int Vn_menthod_i(String str,int i){
if(str.equals("E")){
stack.pop();
stack.push("A");
stack.push("T");
}else if(str.equals("A")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("T")){
stack.pop();
stack.push("B");
stack.push("F");
}else if(str.equals("B")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("F")){
stack.pop();
stack.push("i");
}
return i ;
}
@SuppressWarnings("unchecked")
public static int Vn_menthod_add(String str,int i){
if(str.equals("E")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("A")){
stack.pop();
stack.push("A");
stack.push("T");
stack.push("+");
}else if(str.equals("T")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("B")){
stack.pop();
}else if(str.equals("F")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}
return i;
}
@SuppressWarnings("unchecked")
public static int Vn_menthod_chen(String str,int i){
if(str.equals("E")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("A")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("T")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("B")){
stack.pop();
stack.push("B");
stack.push("F");
stack.push("*");
}else if(str.equals("F")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}
return i;
}
@SuppressWarnings("unchecked")
public static int Vn_menthod_left(String str,int i){
if(str.equals("E")){
stack.pop();
stack.push("A");
stack.push("T");
}else if(str.equals("A")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("T")){
stack.pop();
stack.push("B");
stack.push("F");
}else if(str.equals("B")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("F")){
stack.pop();
stack.push(")");
stack.push("E");
stack.push("(");
}
return i ;
}
public static int Vn_menthod_right(String str,int i){
if(str.equals("E")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("A")){
stack.pop();
}else if(str.equals("T")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("B")){
stack.pop();
}else if(str.equals("F")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}
return i ;
}
public static int Vn_menthod_last(String str,int i){
if(str.equals("E")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("A")){
stack.pop();
}else if(str.equals("T")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}else if(str.equals("B")){
stack.pop();
}else if(str.equals("F")){
count++;
if(count<data.length){
System.out.println(data[count]);
System.out.println("输出:错误");
while(!stack.empty()){
stack.pop();// 将数据弹出堆栈
}
i = data[count].length()+flag;
stack.push("error");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java实现语法分析器及词法分析器.zip (2个子文件)
SyntaxThink2.java 11KB
WordThink3.java 4KB
共 2 条
- 1
资源评论
- qq_410135952018-12-31我很想知道这2个.java怎么打开运行的
- Jerry5452022-05-27这是什么鬼,是这么写的?
- ErrorCode10052016-09-07感觉一般吧,比较适合初学者。
牛水聿
- 粉丝: 4
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功