欢迎来到天天文库
浏览记录
ID:44714862
大小:18.52 KB
页数:16页
时间:2019-10-25
《LL(1)文法判断程序java代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、LL(1)文法判断程序java代码分为两个类importjava.util.ArrayList;importjava.util.HashMap;importjava.util.HashSet;importjava.util.Stack;publicclassLL_1{publicHashMap>firstSet=newHashMap>();publicHashMap>firstSetX
2、=newHashMap>();publicCharacterS='E';publicHashMap>followSet=newHashMap>();publicHashSetVnSet=newHashSet();publicHashSetVtSet=newHashSet();publicHashMa
3、p>experssionSet=newHashMap>();publicString[]inputExperssion={"E->TK","K->+TK","K->ε","T->FM","M->*FM","M->ε","F->i","F->(E)"};publicString[][]table;publicStackanalyzeStatck=newStack();publicStringstrI
4、nput="i+i*i#";publicStringaction="";intindex=0;publicvoidInit(){for(Stringe:inputExperssion){String[]str=e.split("->");charc=str[0].charAt(0);ArrayListlist=experssionSet.containsKey(c)?experssionSet.get(c):newArrayList();list.add(str[1]);experssionSet.put(c,list
5、);}for(Characterc:VnSet)getFirst(c);for(Characterc:VnSet){ArrayListl=experssionSet.get(c);for(Strings:l)getFirst(s);}getFollow(S);for(Characterc:VnSet){getFollow(c);}}publicvoidgetNvNt(){for(Stringe:inputExperssion){String[]str=e.split("->");VnSet.add(str[0].charAt(0));
6、}for(Stringe:inputExperssion){String[]str=e.split("->");Stringright=str[1];for(inti=0;ilist=experssionSet.get(c);HashSetset=firstSet.c
7、ontainsKey(c)?firstSet.get(c):newHashSet();//c为终结符直接添加if(VtSet.contains(c)){set.add(c);firstSet.put(c,set);return;}//c为非终结符处理其每条产生式for(Strings:list){//c推出空串直接添加if(s==Character.toString('ε')){set.add('ε');}//X->Y1Y2Y3…情况else{//从左往右扫描生成式右部inti=0;while(i8、rtn=s.charAt(i);//先处理防止未初始化getFirst(tn);HashSet
8、rtn=s.charAt(i);//先处理防止未初始化getFirst(tn);HashSet
此文档下载收益归作者所有