grammerLR.java
资源名称:SLR.rar [点击查看]
上传用户:jzksjx
上传日期:2022-07-12
资源大小:14k
文件大小:19k
源码类别:
词法分析
开发平台:
Java
- import java.util.*;
- import java.io.*;
- class pair{
- public int x, y;
- pair(int a, int b){
- x = a;
- y = b;
- }
- }
- public class grammerLR {
- String[][] syntax;//保存文法
- String[][] syntax2;//消除左递归的文法
- LinkedList<String> term;//终结符
- LinkedList<String> nonterm;//非终结符
- LinkedList<String> term2;//副本,求first, follow用
- LinkedList<String> nonterm2;//副本,求first, follow用
- LinkedList<HashSet<String>> nontermFirst;//记录first
- LinkedList<HashSet<String>> nontermFollow;//记录follow
- boolean[] hasFirst;//记录first 是否求完
- boolean[] hasFollow;//记录follow 是否求完
- LinkedList<HashSet<pair>> state;//项集族
- int[][][] table1;// ACTION表 // table1 第一层操作类型:1=s, 2=r,3=accept,0=error=默认;
- int[][] table2;//GOTO表
- String path;
- Scanner in;
- String[] inExpr;//保存输入的需要判断的表达式
- int pindex;
- int exprIndex;
- String readLine(){
- return in.nextLine();
- }
- void readExpr(){//读入表达式
- inExpr = readLine().trim().split("[ ]+");
- pindex = 0;
- }
- String readNext(){//每次读入一个词
- exprIndex++;
- if(pindex < inExpr.length){
- return inExpr[pindex++];
- }else if(pindex == inExpr.length){
- pindex++;
- return "$";
- }else{
- return null;
- }
- }
- void scanS() throws IOException{//读入文法 ,并处理文法
- File source = new File(path);
- BufferedReader in = new BufferedReader(new FileReader(source));
- String s;
- while((s = in.readLine()) != null){
- if(s.equals("%%1")){
- int i = 0;
- LinkedList<String> list = new LinkedList<String>();
- while((s = in.readLine()) != null && !s.equals("%%")){
- String[] st = s.trim().split("[ ]+");
- for(String one : st){
- list.add(one);
- i++;
- }
- }
- term = list;
- }else if(s.equals("%%2")){
- int i = 0;
- LinkedList<String> list = new LinkedList<String>();
- while((s = in.readLine()) != null && !s.equals("%%")){
- String[] st = s.trim().split("[ ]+");
- for(String one : st){
- list.add(one);
- i++;
- }
- }
- nonterm = list;
- }else if(s.equals("%%3")){
- LinkedList<String[]> list = new LinkedList<String[]>();
- while((s = in.readLine()) != null && !s.equals("%%")){
- String[] st = s.trim().split("[ ]+");
- list.add(st);
- }
- syntax = list.toArray(new String[0][0]);
- }
- }
- in.close();
- }
- grammerLR(){
- in = new Scanner(System.in);
- System.out.println("Enter the path of syntax : ");
- path = readLine();
- try{//读取文法文件
- scanS();
- }catch(IOException e){
- System.out.println("IOExcpetion error");
- }
- exprIndex = 0;
- //removeLeftRecursion();//消除左递归等
- removeLeftRecursion2();//只是简单的复制,term2, nonterm2, syntax2
- extendSyntax();//扩展文法
- table1 = new int[50][term.size()+1][2];//-----------------------分析表-第一维 行的大小无法确定,只能尽量合适--------------------------
- table2 = new int[50][nonterm.size()];
- //table1初始化
- for(int i = 0; i < table1.length; i++){
- for(int j = 0; j < table1[i].length; j++){
- table1[i][j][0] = 0;
- table1[i][j][1] = -1;
- }
- }
- //table2初始化
- for(int i = 0; i < table2.length; i++){
- for(int j = 0 ; j < table2[i].length; j++){
- table2[i][j] = -1;
- }
- }
- term.add("$");
- items();
- firstAll();
- followAll();
- makeTable();
- }
- void extendSyntax(){//扩展文法
- LinkedList<String[]> syntax0 = new LinkedList<String[]>();
- String s = "E_` "+"::= " + syntax[0][0];
- String[] st = s.trim().split("[ ]+");
- syntax0.add(st);
- for(String[] one : syntax){//用syntax填充syntax0
- syntax0.add(one);
- }
- syntax = new String[syntax0.size()][];
- for(int i = 0; i < syntax0.size(); i++){
- syntax[i] = syntax0.get(i).clone();
- }
- }
- void removeLeftRecursion2(){//只是简单的复制
- term2 = new LinkedList<String>(term);
- nonterm2 = new LinkedList<String>(nonterm);
- LinkedList<LinkedList<String>> syntax1 = new LinkedList<LinkedList<String>>();
- for(String[] one : syntax){//用syntax填充syntax1
- LinkedList<String> temp1 = new LinkedList<String>();
- for(String two : one){
- temp1.add(two);
- }
- syntax1.add(temp1);
- }
- syntax2 = new String[syntax1.size()][];
- for(int i = 0; i < syntax1.size(); i++){
- syntax2[i] = syntax1.get(i).toArray(new String[0]);
- }
- }
- void removeLeftRecursion(){//消除左递归
- term2 = new LinkedList<String>(term);
- nonterm2 = new LinkedList<String>(nonterm);
- LinkedList<LinkedList<String>> syntax1 = new LinkedList<LinkedList<String>>();
- for(String[] one : syntax){//用syntax填充syntax1
- LinkedList<String> temp1 = new LinkedList<String>();
- for(String two : one){
- temp1.add(two);
- }
- syntax1.add(temp1);
- }
- for(int i = 0; i < nonterm.size(); i++){
- for(int j = 0; j < i; j++){
- for(int k = 0; k < syntax1.size(); k++){
- if(nonterm.get(i).equals(syntax1.get(k).get(0)) && nonterm.get(j).equals(syntax1.get(k).get(2)) ){
- for(int m = 0; m < syntax1.size(); m++){
- if(nonterm.get(j).equals(syntax1.get(m).get(0))){//找到所有非终结符j的产生式
- syntax1.get(k).addAll(2+1, syntax1.get(m));
- syntax1.get(k).remove(2);
- }
- }
- }
- }
- }
- //消除立即左递归
- String newMark = nonterm.get(i) + "`";//创造新符号
- for(int j = 0; j < syntax1.size(); j++){
- if(syntax1.get(j).get(0).equals(nonterm.get(i))){
- if(syntax1.get(j).get(0).equals(syntax1.get(j).get(2))){//有左递归
- nonterm2.add(newMark);
- syntax1.get(j).add(0+1, newMark);
- syntax1.get(j).remove(0);
- syntax1.get(j).remove(2);
- syntax1.get(j).add(newMark);
- for(int k = 0; k < syntax1.size(); k++){
- if(syntax1.get(k).get(0).equals(nonterm.get(i))){
- if(!syntax1.get(k).get(0).equals(syntax1.get(k).get(2))){
- syntax1.get(k).add((newMark));
- //移动到前面去
- LinkedList<String> temp3 = new LinkedList<String>(syntax1.get(k));
- syntax1.remove(k);
- syntax1.add(k-1, temp3);
- }
- }
- }
- LinkedList<String> temp2 = new LinkedList<String>();
- temp2.add(newMark);
- temp2.add("::=");
- temp2.add("ε");
- syntax1.add(j+2, temp2);
- if(!term2.contains("ε")){
- term2.add("ε");
- }
- }
- }
- }
- }
- syntax2 = new String[syntax1.size()][];
- for(int i = 0; i < syntax1.size(); i++){
- syntax2[i] = syntax1.get(i).toArray(new String[0]);
- }
- }
- HashSet<pair> closure(HashSet<pair> one){
- LinkedList<pair> list = new LinkedList<pair>(one);
- for(int i = 0; i < list.size(); i++){
- pair two = list.get(i);
- if(two.y < syntax[two.x].length && nonterm.contains((syntax[two.x][two.y])) && !syntax[two.x][0].equals(syntax[two.x][two.y])){//点后紧跟非终结符,不在末尾,需要被扩展 ,
- String signal = new String(syntax[two.x][two.y]);
- int index = 0;
- for(String three[] : syntax){
- if(three[0].equals(signal) && !list.contains(new pair(index, 2))){//找到其产生式,并且未包含
- list.add(new pair(index, 2));
- }
- index++;
- }
- }
- }
- one = new HashSet<pair>(list);
- return one;
- }
- void items(){
- state = new LinkedList<HashSet<pair>>();
- HashSet<pair> i0 = new HashSet<pair>();
- i0.add(new pair(0,2));
- i0 = closure(i0);
- state.add(i0);
- int index = 0;
- while(true){
- LinkedList<HashSet<pair>> other = new LinkedList<HashSet<pair>>(state);
- int num = 0;
- for(ListIterator<HashSet<pair>> it = other.listIterator(index); it.hasNext(); ){
- HashSet<pair> one = it.next();
- HashSet<String> signal = new HashSet<String>();//保存 只能接受的那些输入
- for(pair pattern : one){
- if(syntax[pattern.x].length > pattern.y){
- signal.add(syntax[pattern.x][pattern.y]);
- }
- }
- for(String mark : signal){
- HashSet<pair> temp = new HashSet<pair>();
- for(pair pattern : one){//对于项集的每个项
- if(syntax[pattern.x].length > pattern.y && syntax[pattern.x][pattern.y].equals(mark)){
- temp.add(new pair(pattern.x, pattern.y+1));
- }
- }
- temp = closure(temp);
- boolean isContain = false;
- int stateIndex = -1;
- for(HashSet<pair> four : state){//判断是否已经包含此项集
- boolean isThere = true;
- for(pair five : four){
- boolean isFind = false;
- for(pair six : temp){
- if(five.x == six.x && five.y == six.y){
- isFind = true;
- break;
- }
- }
- if(!isFind){
- isThere = false;
- break;
- }
- }
- if(isThere){
- isContain = true;
- stateIndex = state.indexOf(four);
- break;
- }
- }
- if(!isContain){//判断是否已经包含此项集
- state.add(temp);
- if(term.contains(mark)){//生成分析表的 规则 2-1
- int row = state.indexOf(one);
- int cell = term.indexOf(mark);
- int value = state.indexOf(temp);
- table1[row][cell][0] = 1;//操作为 s,即移入
- table1[row][cell][1] = value;
- }
- if(nonterm.contains(mark)){//生成分析表的 规则 3
- int row = state.indexOf(one);
- int cell = nonterm.indexOf(mark);
- int value = state.indexOf(temp);
- table2[row][cell] = value;
- }
- }else{
- if(term.contains(mark)){//生成分析表的 规则 2-1
- int row = state.indexOf(one);
- int cell = term.indexOf(mark);
- int value = stateIndex;;
- table1[row][cell][0] = 1;//操作为 s,即移入
- table1[row][cell][1] = value;
- }
- if(nonterm.contains(mark)){//生成分析表的 规则 3
- int row = state.indexOf(one);
- int cell = nonterm.indexOf(mark);
- int value = stateIndex;
- table2[row][cell] = value;
- }
- }
- }
- num++;
- }
- index++;
- if(state.size() <= other.size()){
- break;
- }
- }
- }
- void appendFirst(HashSet<String> a, HashSet<String> b){
- HashSet<String> temp = new HashSet<String>(b);
- temp.remove("ε");
- a.addAll(temp);
- }
- HashSet<String> makeFirst(String token){//求token的first 集合 ,递归
- if(term2.contains(token)){//是终结符
- HashSet<String> temp = new HashSet<String>();
- temp.add(token);
- return temp;
- }else if(nonterm2.contains(token)){//是非终结符
- int index = nonterm2.indexOf(token);
- if(!hasFirst[index]){//还没生成first,现在生成
- for(String[] one : syntax2){
- if(one[0].equals(token)){
- if(one[0].equals(one[2])){//对于会左递归的文法
- continue;
- }
- boolean isZero = false;
- for(int i = 2; i < one.length; i++){
- appendFirst(nontermFirst.get(index), makeFirst(one[i]));
- if(term2.contains(one[i]) && !one[i].equals("ε") ){
- isZero = true;
- break;
- }else if(nonterm2.contains(one[i]) && !nontermFirst.get(nonterm2.indexOf(one[i])).contains("ε")){
- isZero = true;
- break;
- }
- }
- if(!isZero){//每个都有 ε ,则最后加入 ε 到 集合里
- nontermFirst.get(index).add("ε");
- }
- }
- }
- hasFirst[index] = true;//标记 此 符号 的 first 已经找完了
- }
- return nontermFirst.get(index);
- }else{//基本不会到这里面
- System.out.println(token + " : " + "Error! 没有这个符号");
- System.exit(0);
- return null;
- }
- }
- void firstAll(){//first集合生成 主程序
- hasFirst = new boolean[nonterm2.size()];
- nontermFirst = new LinkedList<HashSet<String>>();
- for(int i = 0; i < nonterm2.size(); i++){
- hasFirst[i] = false;
- HashSet<String> temp0 = new HashSet<String>();
- nontermFirst.add(temp0);
- }
- for(String one : nonterm2){//生成 非终结符 的 first
- makeFirst(one);// makeFirst里已经计算完了 吧
- }
- }
- void appendFollow(HashSet<String> a, HashSet<String> b){
- HashSet<String> temp = new HashSet<String>(b);
- temp.remove("ε");
- a.addAll(temp);
- }
- Map<String, Integer> cicle;//防止 文法里出现循环递归
- HashSet<String> makeFollow(String token){//生成token 的 follow集合
- if(cicle.get(token) != null){//已经存在这个符号, 说明 出现递归 环 了
- return nontermFollow.get(nonterm2.indexOf(token));
- }
- cicle.put(token, 1);
- if(hasFollow[nonterm2.indexOf(token)]){
- return nontermFollow.get(nonterm2.indexOf(token));
- }
- for(String[] one : syntax2){//计算 follow
- for(int i = 2; i < one.length; i++){
- if(one[i].equals(token)){
- boolean allZero = true;
- for(int j = i+1; j < one.length; j++){//规则2
- if(nonterm2.contains(one[j])){//非终结符
- appendFollow(nontermFollow.get(nonterm2.indexOf(token)), nontermFirst.get(nonterm2.indexOf(one[j])));
- if(!nontermFirst.get(nonterm2.indexOf(one[j])).contains("ε")){
- allZero = false;
- break;
- }
- }else if(term2.contains(one[j])){//终结符
- HashSet<String> temp = new HashSet<String>();
- temp.add(one[j]);
- appendFollow(nontermFollow.get(nonterm2.indexOf(token)), temp);
- if(!one[j].equals("ε")){
- allZero = false;
- break;
- }
- }
- }
- if(allZero){//规则3
- appendFollow(nontermFollow.get(nonterm2.indexOf(one[i])), makeFollow(one[0]));
- }
- }
- }
- }
- hasFollow[nonterm2.indexOf(token)] = true;
- return nontermFollow.get(nonterm2.indexOf(token));
- }
- void followAll(){//follow集合生成 主程序
- hasFollow = new boolean[nonterm2.size()];
- nontermFollow = new LinkedList<HashSet<String>>();
- for(int i = 0; i < nonterm2.size(); i++){
- hasFollow[i] = false;
- HashSet<String> temp0 = new HashSet<String>();
- nontermFollow.add(temp0);
- }
- //把 $ 加入到 开始符号 里
- HashSet<String> temps = new HashSet<String>();
- temps.add("$");
- appendFollow(nontermFollow.get(0), temps);// 开始符号
- for(String one : nonterm2){
- cicle = new HashMap<String, Integer>();
- makeFollow(one);
- }
- }
- void makeTable(){
- // table1 第一层操作类型:1=s, 2=r,3=accept,0=error=默认;
- int index = 0;
- for(HashSet<pair> one : state){
- for(pair two : one){
- if(two.y == syntax[two.x].length){//
- if(two.x == 0){//规则 2-3
- table1[index][term.indexOf("$")][0] = 3;
- table1[index][term.indexOf("$")][1] = -3;
- }else{//规则 2-2
- for(String token : nontermFollow.get(nonterm.indexOf(syntax[two.x][0]))){
- table1[index][term.indexOf(token)][0] = 2;
- table1[index][term.indexOf(token)][1] = two.x;
- }
- }
- }
- }
- index++;
- }
- }
- void analysisDriver(){
- Stack<Integer> stateStack = new Stack<Integer>();
- Stack<String> signalStack = new Stack<String>();
- exprIndex = 0;
- stateStack.push(0);
- String mark = readNext();
- int now;
- // table1 第一层操作类型:1=s, 2=r,3=accept,0=error=默认;
- System.out.println("n**************************");
- while(true){
- now = stateStack.peek();
- //System.out.println("now = " + now + "," + term.indexOf(mark));
- //打印分析栈
- for(int i : stateStack){
- System.out.print(i + ",");
- }
- if(stateStack.size() > 3){
- System.out.print("t");
- }else{
- System.out.print("tt");
- }
- for(String one : signalStack){
- System.out.print(one + ",");
- }
- if(signalStack.size() > 3){
- System.out.print("t");
- }else{
- System.out.print("tt");
- }
- System.out.print(mark + " ");
- for(int i = exprIndex; i < inExpr.length; i++){
- System.out.print(inExpr[i] + " ");
- }
- System.out.println();
- if(table1[now][term.indexOf(mark)][0] == 1){
- stateStack.push(table1[now][term.indexOf(mark)][1]);
- signalStack.push(mark);
- mark = readNext();
- }else if(table1[now][term.indexOf(mark)][0] == 2){
- int syntaxIndex = table1[now][term.indexOf(mark)][1];
- for(int i = 2; i < syntax[syntaxIndex].length; i++){
- signalStack.pop();
- stateStack.pop();
- }
- signalStack.push(syntax[syntaxIndex][0]);
- now = stateStack.peek();
- stateStack.push(table2[now][nonterm.indexOf(signalStack.peek())]);
- }else if(table1[now][term.indexOf(mark)][0] == 3){
- System.out.println("*** Accept ***");
- break;
- }else{
- System.out.println("*** UnAccept ***");
- break;
- }
- }
- }
- void printAll(){//输入中间产生的各种信息
- int index = 0;
- System.out.println("n******************** syntax ********************");
- for(String[] one : syntax){
- System.out.print("" + index++ + ":t");
- for(String two : one){
- System.out.print(two + "t");
- }
- System.out.println();
- }
- index = 0;
- System.out.println("n******************** syntax2 ********************");
- for(String[] one : syntax2){
- System.out.print("" + index++ + ":t");
- for(String two : one){
- System.out.print(two + "t");
- }
- System.out.println();
- }
- System.out.println("n******************** term ********************");
- for(String one : term){
- System.out.print(one + "t");
- }
- System.out.println();
- System.out.println("n******************** nonterm ********************");
- for(String one : nonterm){
- System.out.print(one + "t");
- }
- System.out.println();
- System.out.println("n******************** term2 ********************");
- for(String one : term2){
- System.out.print(one + "t");
- }
- System.out.println();
- System.out.println("n******************** nonterm2 ********************");
- for(String one : nonterm2){
- System.out.print(one + "t");
- }
- System.out.println();
- index = 0;
- System.out.println("n********************nontermFirst ********************");
- for(HashSet<String> one : nontermFirst){
- System.out.print("" + index++ + ":t");
- for(String two : one){
- System.out.print(two + "t");
- }
- System.out.println();
- }
- index = 0;
- System.out.println("n********************nontermFollow********************");
- for(HashSet<String> one : nontermFollow){
- System.out.print("" + index++ + ":t");
- for(String two : one){
- System.out.print(two + "t");
- }
- System.out.println();
- }
- index = 0;
- System.out.println("n******************** state ********************");
- for(HashSet<pair> one : state){
- System.out.print("" + index++ + ":t");
- for(pair two : one){
- System.out.print("(" + two.x + "," + two.y + ")t");
- }
- System.out.println();
- }
- index = 0;
- System.out.println("n******************** table ********************");
- System.out.print("t");
- for(String one : term){
- System.out.print(one + "t");
- }
- System.out.println();
- for(int i = 0; i < state.size(); i++){
- System.out.print("" + index++ + ":t");
- for(int j = 0; j < table1[i].length; j++){
- if(table1[i][j][0] != 0){
- System.out.print(table1[i][j][0] + ",");
- System.out.print(table1[i][j][1] + "t");
- }else{
- System.out.print("---t");
- }
- }
- System.out.println();
- }
- index = 0;
- System.out.println("--------------------");
- System.out.print("t");
- for(String one : nonterm){
- System.out.print(one + "t");
- }
- System.out.println();
- for(int i = 0; i < state.size(); i++){
- System.out.print("" + index++ + ":t");
- for(int j = 0; j < table2[i].length; j++){
- if(table2[i][j] != -1){
- System.out.print(table2[i][j] + "t");
- }else{
- System.out.print("-t");
- }
- }
- System.out.println();
- }
- }
- public static void main(String[] args){
- grammerLR myLR = new grammerLR();
- myLR.printAll();
- while(true){
- System.out.println("n*****************************************************");
- System.out.print("Enter expr:");
- myLR.readExpr();
- myLR.analysisDriver();
- }
- }
- }