编译原理实现预测分析法(LL(1))

实现预测分析法的原理其实很简答,步骤如下:

1.判断文法有没有左递归,若有左递归,则对该文法消除左递归

2.对处理后的文法进行遍历,求出终结符号和非终结符号

3.求出first集合和follow集合

4.判断是不是LL(1)文法,若是,则构建预测分表

5.若是LL(1)文法,输入一个句子,模拟输出进栈出栈情况

截图如下:

编译原理实现预测分析法(LL(1))_第1张图片



代码如下:


package cn.mahui;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;


public class Function {
public static List oneFirstList=new ArrayList<>();
public static List oneFollowList=new ArrayList<>();
/**
* 从指定文件中读取文法
* @param path(文件的路径)
* @return
* @throws IOException 
*/
public static String readGrammarFile(String path) throws IOException{
StringBuilder grammar=new StringBuilder();
File file=new File(path);
@SuppressWarnings("resource")
BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
String readline;
while((readline=bufferedReader.readLine())!=null){
grammar.append(readline+"\n");
}
return grammar.toString();
}
/**
* 获得文法的产生式
* @param scanner(输入的文法)
* @return
*/
public static List getGrammar(String scanner){
List oldList=new ArrayList<>();
String[] grammarItems=scanner.split(" |\n");
for(int i=0;ioldList.add(grammarItems[i]);
}
return oldList;
}
/**
* 获得每一条产生式
* @param scanner(输入的文法)
* @return
*/
public static List getGrammarItems(String scanner){
List oldList=new ArrayList<>();
List oldListItems=new ArrayList<>();
String[] grammarItems=scanner.split(" |\n");
for(int i=0;ioldList.add(grammarItems[i]);
String[] items=grammarItems[i].split(">|\\|");
//添加每一条产生式
for(int k=1;koldListItems.add(grammarItems[i].charAt(0)+"-->"+items[k]);
}
return oldListItems;
}
/**
* 如果vn包含str,则返回真,不包含返回false
* @param vn
* @param ch(要判断的字符串)
* @return
*/
public static boolean isContain(List vn,String str){
for(int i=0;iif(vn.get(i).equals(str)){
return true;
}
}
return false;
}
/**
* 获得所有非终结符
* @param grammarItem(输入的文法)
* @return
*/
public static List getVN(String grammarItems){
List vn=new ArrayList<>();
String[] string=grammarItems.split("\n");
for(int i=0;iint index=string[i].indexOf("-");
String str=string[i].substring(0, index);
if(vn.size()==0){
vn.add(str);
}else{
if(!isContain(vn, str)){
vn.add(str);
}
}
}
return vn;
}
/**
* 消除文法的左递归
* @param oldListItems(未消除左递归的每一台产生式的集合)
* @return
*/
public static  List removeLeftRecursive(List oldListItems){
List newList=new ArrayList<>();
for(int i=0;iif(oldListItems.get(i).charAt(0)==oldListItems.get(i).charAt(4)){
for(int j=0;jif(oldListItems.get(j).startsWith(""+oldListItems.get(i).charAt(0))
&&oldListItems.get(j).length()==5){
newList.add(oldListItems.get(i).charAt(0)+"-->"+oldListItems.get(j).charAt(4)+oldListItems.get(i).charAt(0)+"’");
newList.add(oldListItems.get(i).charAt(0)+"’"+"-->"+oldListItems.get(i).substring(5, oldListItems.get(i).length())+oldListItems.get(i).charAt(0)+"’");
//~代表空
newList.add(oldListItems.get(i).charAt(0)+"’"+"-->"+"~");
}
}
}else{
int tag=0;
for(int k=0;kif(newList.get(k).contains(oldListItems.get(i).substring(1, oldListItems.get(i).length()))){
tag=1;
break;
}
}
if(tag==0){
newList.add(oldListItems.get(i));
}
}
}
return newList;
}
/**
* 获得非终结符号的first集合
* @param vn(非终结符号的集合)
* @param newList( 消除左递归后的文法)
* @return
*/
public static Map> getFirst(List vn,List newList){
Map> firstList=new HashMap<>();
for(int i=0;ioneFirstList.clear();
getOneFirst(vn.get(i),newList);
List temp=new ArrayList<>();
for(int k=0;ktemp.add(oneFirstList.get(k));
}
firstList.put(vn.get(i), temp);

}

return firstList;
}

/**
* 得到任意一个非终结符号的first集合
* @param vnItem(任意一个非终结符号)
* @param newList(消除左递归后的文法)
* @return
*/
public static void getOneFirst(String vnItem,List newList){
for(int i=0;iif(vnItem.length()==1){
if(newList.get(i).startsWith(vnItem)&&newList.get(i).charAt(1)=='-'){
int index=newList.get(i).indexOf('>')+1;
if(Character.isUpperCase(newList.get(i).charAt(index))){
getOneFirst(""+newList.get(i).charAt(index),newList);
}else{
if(!isContain(oneFirstList,""+newList.get(i).charAt(index))){
oneFirstList.add(""+newList.get(i).charAt(index));
}

}
}
}else{
if(newList.get(i).startsWith(vnItem)){
int index=newList.get(i).indexOf('>')+1;
if(Character.isUpperCase(newList.get(i).charAt(index))){
getOneFirst(""+newList.get(i).charAt(index),newList);
}else{
if(!isContain(oneFirstList,""+newList.get(i).charAt(index))){
oneFirstList.add(""+newList.get(i).charAt(index));
}

}
}
}

}
}
/**
* 获得任意一个非终结符号的follow集合
* @param vnItem(任意的一个非终结符号)
* @param vn(终结符号的集合)
* @param newList(消除左递归后的文法)
* @param firstList(所有非终结符号的first集合)
* @return
*/
public static void getOneFollow(String vnItem,List vn,List newList,Map> firstList){

//求follow集合的第一条
if(vnItem.equals(vn.get(0))){
if(!isContain(oneFollowList,"#")){
oneFollowList.add("#");
}
}
//求follow集合的第二条
for(int i=0;iint index1=newList.get(i).indexOf(">")+1;
String str=newList.get(i).substring(index1);
if(vnItem.length()==1){
//判断E类型的
if(str.contains(vnItem)&&str.charAt(str.indexOf(vnItem.charAt(0))+1)!='’'){
if(str.indexOf(vnItem.charAt(0))!=str.length()){
String temp=str.substring(str.indexOf(vnItem.charAt(0))+1);
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append(temp);
for(int k=stringBuilder.length();k>1;k--){
if(isVN(stringBuilder.toString(),vn)){
break;
}else{
int length=stringBuilder.length();
stringBuilder.deleteCharAt(length-1);
}
}
if(stringBuilder.length()>0){
if(Character.isUpperCase(stringBuilder.charAt(0))){
if(firstList.containsKey(stringBuilder.toString())){
List list=firstList.get(stringBuilder.toString());
for(int q=0;qif(!list.get(q).equals("~")){
if(!isContain(oneFollowList,list.get(q))){
oneFollowList.add(list.get(q));
}
}
}
}
}else{
if(!isContain(oneFollowList,stringBuilder.toString())){
oneFollowList.add(stringBuilder.toString());
}
}
}
}
}
}else{
//判断E’类型的
if(str.contains(vnItem)){
int index=str.indexOf(vnItem);
String left=str.substring(index+vnItem.length(), str.length());
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append(left);
for(int k=stringBuilder.length();k>1;k--){
if(isVN(stringBuilder.toString(),vn)){
break;
}else{
int length=stringBuilder.length();
stringBuilder.deleteCharAt(length);
}
}
if(stringBuilder.length()>0){
if(Character.isUpperCase(stringBuilder.charAt(0))){
if(firstList.containsKey(stringBuilder.toString())){
List list=firstList.get(stringBuilder.toString());
for(int q=0;qif(!list.get(q).equals("~")){
if(!isContain(oneFollowList,list.get(q))){
oneFollowList.add(list.get(q));
}
}
}
}
}else{
if(!isContain(oneFollowList,stringBuilder.toString())){
oneFollowList.add(stringBuilder.toString());
}
}
}
}
}

}
//求follow集合的第三条
for(int i=0;iint index1=newList.get(i).indexOf(">")+1;
String str=newList.get(i).substring(index1);
if(vnItem.length()==1){
if(str.contains(vnItem)){
if(str.indexOf(vnItem)==str.length()&&str.charAt(str.indexOf(vnItem.charAt(0))+1)!='’'){
String s=newList.get(i).substring(0,newList.get(i).indexOf("-"));
getOneFollow(s, vn, newList, firstList);
}else{
String left=str.substring(str.indexOf(vnItem)+1);
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append(left);
for(int k=stringBuilder.length();k>1;k--){
if(isVN(stringBuilder.toString(),vn)){
break;
}else{
int length=stringBuilder.length();
stringBuilder.deleteCharAt(length-1);
}
}
if(isVN(stringBuilder.toString(), vn)){
if(firstList.containsKey(stringBuilder.toString())){
List temp=firstList.get(stringBuilder.toString());
if(isContain(temp)){
String s=newList.get(i).substring(0,newList.get(i).indexOf("-"));
getOneFollow(s, vn, newList, firstList);
}
}
}
}
}

}else{
if(str.contains(vnItem)){
String st=newList.get(i);
if(st.endsWith(vnItem)){
if(!st.startsWith(vnItem)){
String s=newList.get(i).substring(0,newList.get(i).indexOf("-"));
getOneFollow(s, vn, newList, firstList);
}
}else{
String left=str.substring(str.indexOf(vnItem)+2);
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append(left);
for(int k=stringBuilder.length();k>1;k--){
if(isVN(stringBuilder.toString(),vn)){
break;
}else{
int length=stringBuilder.length();
stringBuilder.deleteCharAt(length-1);
}
}
if(isVN(stringBuilder.toString(), vn)){
if(firstList.containsKey(stringBuilder.toString())){
List temp=firstList.get(stringBuilder.toString());
if(isContain(temp)){
String s=newList.get(i).substring(0,newList.get(i).indexOf("-"));
getOneFollow(s, vn, newList, firstList);
}
}
}
}
}
}
}

}
/**
* 获得所有非终结符号的follow集合
* @param vn(非终结符号的集合)
* @param newList(消除左递归的文法)
* @param firstList(所有非终结符号的first集合)
* @return
*/
public static Map> getFollow(List vn,List newList,Map> firstList){
Map> followList=new HashMap<>();
for(int i=0;ioneFollowList.clear();
getOneFollow(vn.get(i),vn,newList,firstList);
List temp=new ArrayList<>();
for(int k=0;ktemp.add(oneFollowList.get(k));
}
followList.put(vn.get(i),temp );
}
return followList;
}
/**
* 判断是不是LL(1)文法
* @param firstList
* @param followList
* @return
*/
public static boolean isLL1(Map> firstList,Map> followList,List newList,List vn){
//因为消除了左递归,所以文法判断的第一条默认成立
//判断文法条件的第三条


for(int i=0;iif(firstList.get(vn.get(i)).contains("~")){
List first=new ArrayList<>();
first.addAll(firstList.get(vn.get(i)));
List follow=new ArrayList<>();
follow.addAll(followList.get(vn.get(i)));
first.retainAll(follow);
if(first.size()!=0){
return false;
}
}
}
//判断文法的第二条
for(int i=0;iStringBuilder stringBuilder=new StringBuilder();
for(int j=0;jif(newList.get(j).startsWith(vn.get(i)+"-->")){
String str=newList.get(j).substring(newList.get(j).indexOf(">")+1);
stringBuilder.append(str+" ");
}
}
String[] items=stringBuilder.toString().split(" ");
if(items.length>1){
for(int k=0;kList first=getItemFirst(items[k], firstList);
for(int n=k+1;nList second=getItemFirst(items[n], firstList);
first.retainAll(second);
if(first.size()!=0){
return false;
}
}
}
}
}
return true;
}
/**
* 判断是不是终结符号,是则返回true,不是则返回false
* @param ch(要判断的字符串)
* @return
*/
public static boolean isVN(String ch,List vn){
for(int i=0;iif(ch.equals(vn.get(i))){
return true;
}
}
return false;
}
/**
* 判断相应的first集合有没有空串,如果有返回true,反之为false
* @param list
* @return
*/
public static boolean isContain(List list){
for(int i=0;iif(list.get(i).equals("~")){
return true;
}
}
return false;
}
/**
* 获得每一个非终结符号的候选首符集
* @param str
* @param firstlist
* @return
*/
public static List getItemFirst(String str,Map> firstlist){
List list=new ArrayList<>();
if(Character.isUpperCase(str.charAt(0))){
if(str.length()>1){
if(str.charAt(1)!='’'){
list.addAll(firstlist.get(""+str.charAt(0)));
}else{
list.addAll(firstlist.get(""+str.charAt(0)+str.charAt(1)));
}
}
}else{
list.add(""+str.charAt(0));
}
return list;
}
/**
*  获得终结符号
* @param newList
* @return
*/
public static List getVT(List newList){
List list=new ArrayList<>();
for(int i=0;iint index1=newList.get(i).indexOf('>')+1;
String str=newList.get(i).substring(index1);
for(int k=0;kif(!Character.isUpperCase(str.charAt(k))){
if(str.charAt(k)!='’'&&str.charAt(k)!='~'){
if(!isContain(list, ""+str.charAt(k))){
list.add(""+str.charAt(k));
}
}
}
}
}
return list;
}
/**
* 生成LL(1)预测分析表
* @param vn
* @param vt
* @param firstList
* @param followList
* @param newList
* @throws IOException 
*/
public static void print(List vn,List vt,Map> firstList,Map> followList,List newList) throws IOException{
removeFile();
vt.add("#");
for(int i=0;iSystem.out.printf("\t%s",vt.get(i));
}
System.out.println();
for(int i=0;iSystem.out.printf("%s\t",vn.get(i));
for(int k=0;kString str=findItems(vn, newList, firstList, followList, ""+vt.get(k),""+vn.get(i));
if(str!=null){
System.out.printf(str+"  ");
writeToFile(vn.get(i),vt.get(k),str);
}else{
if(!firstList.get(vn.get(i)).contains(""+vt.get(k))&&firstList.get(vn.get(i)).contains("~")){
if(followList.get(vn.get(i)).contains(""+vt.get(k))){
System.out.printf(vn.get(i)+"-->~"+"  ");
writeToFile(vn.get(i),vt.get(k),vn.get(i)+"-->~");
}else{
System.out.printf(" \t");
}
}else{
System.out.printf(" \t");
}

}
}
System.out.println();
}
}
/**
* 填充预测分析表,找到非终结符号要推出终结符号时所用的产生式
* @param vn
* @param newList
* @param firstList
* @param followList
* @param vtItem
* @param vnItem
* @return
*/
public static String findItems(List vn,List newList,Map> firstList,Map> followList,String vtItem,String vnItem){
for(int i=0;iif(vnItem.length()==1){
if(newList.get(i).startsWith(vnItem)&&newList.get(i).charAt(1)!='’'){
int index=newList.get(i).indexOf(">")+1;
String str=newList.get(i).substring(index);
if(Character.isUpperCase(str.charAt(0))){
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append(str);
for(int k=stringBuilder.length();k>1;k--){
if(isVN(stringBuilder.toString(),vn)){
break;
}else{
int length=stringBuilder.length();
stringBuilder.deleteCharAt(length-1);
}
}
if(firstList.get(stringBuilder.toString()).contains(vtItem)){
return newList.get(i);
}else{

}

}else{
if(vtItem.equals(""+str.charAt(0))){
return newList.get(i);
}else{

}
}
}
}else{
if(newList.get(i).startsWith(vnItem)){
int index=newList.get(i).indexOf(">")+1;
String str=newList.get(i).substring(index);
if(Character.isUpperCase(str.charAt(0))){
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append(str);
for(int k=stringBuilder.length();k>1;k--){
if(isVN(stringBuilder.toString(),vn)){
break;
}else{
int length=stringBuilder.length();
stringBuilder.deleteCharAt(length-1);
}
}
if(firstList.get(stringBuilder.toString()).contains(vtItem)){
return newList.get(i);
}else{

}

}else{
if(vtItem.equals(""+str.charAt(0))){
return newList.get(i);
}else{

}
}
}
}
}
return null;
}
/**
* 将预测分析表以三元组的形式写入文件
* @param vn
* @param vt
* @param str
* @throws IOException
*/
public static void writeToFile(String vn,String vt,String str) throws IOException{
String path="/Users/mahui/Desktop/table.txt";
File file=new File(path);
if(!file.exists()){
file.createNewFile();
}
FileWriter fileWriter=new FileWriter(file,true);
fileWriter.write(vn+" "+vt+" "+str+"\n");
fileWriter.close();

}
/**
* 每次运行之前都删除文件,避免内容重复
* (因为我向文件写数据是以追加的形式,所以再将预测分析表写入文件时,先清除之前的数据)
*/
public static void removeFile(){
String path="/Users/mahui/Desktop/table.txt";
File file=new File(path);
if(file.exists()){
file.delete();
}
}
/**
* 将存储在文件中的预测分析表存储在List>
* Map中的键是非终结符号+空格+终结符号,值是所用到的产生式
* @return
*/
public static List> readAnalyse(){
List> analyse=new ArrayList<>();
String path="/Users/mahui/Desktop/table.txt";
File file=new File(path);
if(!file.exists()){
System.out.println("该文件不存在");
return null;
}else{
try {
@SuppressWarnings("resource")
BufferedReader reader=new BufferedReader(new FileReader(file));
String line;
while((line=reader.readLine())!=null){
String str[]=line.split(" ");
Map map=new HashMap<>();
map.put(str[0]+" "+str[1], str[2]);
analyse.add(map);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return analyse;
}
/**
* 对待分析串进行预测分析
* @param newList
*/
public static void analyse(List vn,List vt,String startVN){
List> analyse=readAnalyse();
boolean flag=true;
System.out.println("请输入待分析的串:");
@SuppressWarnings("resource")
Scanner scanner=new Scanner(System.in);
//输入待分析串
String str=scanner.nextLine();
List source=new ArrayList<>();
//将#号置入待分析串中
for(int i=0;isource.add(str.charAt(i)+"");
}
source.add("#");
//定义符号栈
List list=new ArrayList<>();
//将#和文法的开始符号入栈
list.add("#");
list.add(startVN);
String title1="符号栈";
String title2="输入串";
String title3="所用产生式";
String error="";
String accept="accept";
System.out.printf("%-20s%-20s%-20s\n", title1,title2,title3);
String a=source.get(0);
String item;
System.out.printf("%-20s%-20s\n",list,source);
while(flag){
//将stack栈顶的符号拖出去并放在x中
String x=list.get(list.size()-1);
list.remove(list.size()-1);
if(isVT(x,vt)){
if(x.equals(a)){
if(source.size()>1){
source.remove(0);
}
a=source.get(0);
System.out.printf("%-20s%-20s%-20s\n",list,source,error);
}else{
System.out.printf("%-20s%-20s%-20s\n",list,source,error);
}
}else if(x.equals("#")){
if(x.equals(a)){
flag=false;
System.out.printf("%-20s%-20s%-20s\n",list,source,accept);
}else{
System.out.printf("%-20s%-20s%-20s\n",list,source,error);


}
}else if((item=findGrammarItem(x,a,analyse))!=null){
StringBuilder s=new StringBuilder();
s.append(item);
StringBuilder string=new StringBuilder();
int index=s.indexOf(">")+1;
string.append(s.substring(index));
if(!string.toString().equals("~")){
List divide=divide(string, vn, vt);
list.addAll(divide);
System.out.printf("%-20s%-20s%-20s\n",list,source,item);
}else{
System.out.printf("%-20s%-20s%-20s\n",list,source,item);
}

}else{
System.out.printf("%-20s%-20s%-20s\n",list,source,error);


}
}
System.out.println("分析完毕");

}
/**
* 判断是不是终结符号,如果是返回true,反之返回false
* @param str
* @param newList
* @return
*/
public static boolean isVT(String str,List newList){
if(newList.contains(str)){
return true;
}
return false;
}
/**
* 通过预测分析表找寻对应的产生式
* @param vn
* @param vt
* @param analyse
* @return
*/
public static String findGrammarItem(String vn,String vt,List> analyse){

for(int i=0;iif(analyse.get(i).containsKey(vn+" "+vt)){
return analyse.get(i).get(vn+" "+vt);

}
}
return null;

}
/**
* 将找到的产生式分割成一个一个终结符号和非终结符号
* @param s
* @param vn
* @param vt
* @return
*/
public static List divide(StringBuilder s,List vn,List vt){
List divide=new ArrayList<>();
int j=s.length();
for(int i=s.length()-1;i>=0;){
String str=s.substring(i,j);
if(isVN(str, vn)||isVT(str,vt)){
divide.add(str);
j=i;
}
i--;
}
return divide;
}
}





package cn.mahui;


import java.io.IOException;
import java.util.List;
import java.util.Map;
public class MainFunction {


public static void main(String[] args) throws IOException {
String path="/Users/mahui/Desktop/grammar.txt";
String grammar=Function.readGrammarFile(path);
System.out.println("文法如下:");
List oldList=Function.getGrammar(grammar);
System.out.println(oldList);
System.out.println("文法的每一条产生式如下:");
List oldListItems=Function.getGrammarItems(grammar);
System.out.println(oldListItems);
System.out.println("所有非终结符号如下:");
List vn=Function.getVN(grammar);
System.out.println(vn);
System.out.println("文法的终结符号如下:");
List vt=Function.getVT(oldListItems);
System.out.println(vt);
System.out.println("消除左递归后的文法如下:");
List newList=Function.removeLeftRecursive(oldListItems);
System.out.println(newList);
StringBuilder stringBuilder=new StringBuilder();
for(int i=0;istringBuilder.append(newList.get(i)+"\n");
}
System.out.println("消除左递归后的文法的非终结符号如下:");
vn=Function.getVN(stringBuilder.toString());
System.out.println(vn);
System.out.println("消除左递归后的文法的终结符号如下:");
vt=Function.getVT(newList);
System.out.println(vt);
System.out.println("first集合如下:");
Map> firstList=Function.getFirst(vn, newList);
System.out.println(firstList);
System.out.println("follow集合如下:");
Map> followList=Function.getFollow(vn, newList, firstList);
System.out.println(followList);
System.out.println("该文法是LL(1)文法吗?"+Function.isLL1(firstList, followList, newList, vn));
if(Function.isLL1(firstList, followList, newList, vn)){
System.out.println("预测分析表如下:");
Function.print(vn, vt,firstList,followList,newList);
}
vt=Function.getVT(newList);
Function.analyse(vn,vt,vn.get(0));

}
}


本代码仅仅实现了此功能,代码中还有很多可以优化的地方

你可能感兴趣的:(编译原理)