c语言ll1Follow集用代码实现,编译原理LL1文法Follow集算法实现

该博客详细介绍了如何计算文法的 Follow 集合,并给出了 Java 实现。通过遍历文法规则,确定非终结符的 Follow 集合,用于解析和语法分析。文章提供了具体的右线性文法示例,并展示了计算过程及结果。
摘要由CSDN通过智能技术生成

importhjzgg.first.First;importjava.util.LinkedHashMap;importjava.util.Map;importjava.util.Set;importjava.util.TreeMap;importjava.util.TreeSet;public classFollow {private Map> first = null;private Map> follow = new TreeMap>();private Map mp = null;public Follow(Map mp, Map>first) {super();this.first =first;this.mp =mp;

}public Map>getFollowSet(){returnfollow;

}private void getFirstSet(Set st, String node, intk){if(k >= node.length()) return;if(node.charAt(k)==‘\‘‘) --k;

String nextNode= "" +node.charAt(k);if(k+1

nextNode+= ‘\‘‘;++k;

}if(!mp.containsKey(nextNode)){//终结点

st.add(nextNode.charAt(0));

}else{

st.addAll(first.get(nextNode));if(first.get(nextNode).contains(‘$‘))

getFirstSet(st, node, k+1);

}

}private voidfindFollow(String curNode){

Set st = null;for(String leftNode : mp.keySet()){

String rightNodes[]=mp.get(leftNode);for(int i=0; i

index=rightNodes[i].indexOf(curNode, nextIndex);continue;

}

index= index+curNode.length();if(index == rightNodes[i].length()){//末尾的非终结点, A->@B

if(follow.get(leftNode) == null)

findFollow(leftNode);if(follow.get(curNode) == null){

st= new TreeSet();

st.addAll(follow.get(leftNode));

follow.put(curNode, st);

}elsefollow.get(curNode).addAll(follow.get(leftNode));

}else{

String nextNode= ""+rightNodes[i].charAt(index);if(index+1 < rightNodes[i].length() && rightNodes[i].charAt(index+1)==‘\‘‘){

nextNode+= ‘\‘‘;++index;

}if(mp.containsKey(nextNode)){//非终结符

if(first.get(nextNode).contains(new Character(‘$‘))){//A->@B&, 而 &->$

if(follow.get(leftNode) == null)

findFollow(leftNode);if(follow.get(curNode) == null){

st= new TreeSet();

st.addAll(follow.get(leftNode));

follow.put(curNode, st);

}elsefollow.get(curNode).addAll(follow.get(leftNode));

}//好特殊的情况啊....

{//A->@B&, First(&)^$ 放入follow(B)

Set tmpSt = new TreeSet();

getFirstSet(tmpSt, rightNodes[i], index);

tmpSt.remove(‘$‘);if(follow.get(curNode) == null){

st= new TreeSet();

st.addAll(tmpSt);

follow.put(curNode, st);

}elsefollow.get(curNode).addAll(tmpSt);

}

}else {//终结符

if(follow.get(curNode) == null){

st= new TreeSet();

st.add(nextNode.charAt(0));

follow.put(curNode, st);

}else follow.get(curNode).add(nextNode.charAt(0));

}

}

index=rightNodes[i].indexOf(curNode, nextIndex);

}

}

}

}publicString followKernealCode(){

String content= "";boolean flag = true;for(String leftNode : mp.keySet()){if(flag){

Set st = new TreeSet();

st.add(‘#‘);

follow.put(leftNode, st);

flag= false;

}

findFollow(leftNode);

}//打印first集合

System.out.println("Follow 集合如下:");for(Map.Entry>entry : follow.entrySet()){

content+= entry.getKey() + " : " + entry.getValue() + "\n";

System.out.println(entry.getKey()+ " : " +entry.getValue());

}returncontent;

}public static voidmain(String[] args) {

String[] rightLinearGrammar={"E->TE\‘","E\‘->+TE\‘|$","T->FT\‘","T\‘->*FT\‘|$","F->(E)|i"};//String[] rightLinearGrammar = {//"S->ABc",//"A->a|$",//"B->b|$"//};

Map mp = new LinkedHashMap();try{for(int i=0; i

String split1[]= rightLinearGrammar[i].split("->");

String split2[]= split1[1].split("\\|");

mp.put(split1[0], split2);

}

}catch(Exception e){

e.printStackTrace();

System.out.println("右线性文法错误!");

}

First first= newFirst(mp);

first.firstKernealCode();newFollow(mp, first.getFirstSet()).followKernealCode();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值