博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编译原理LL1文法分析表算法实现
阅读量:7088 次
发布时间:2019-06-28

本文共 5256 字,大约阅读时间需要 17 分钟。

import hjzgg.first.First;import hjzgg.follow.Follow;import hjzgg.tablenode.TableNode;import hjzgg.treenode.TreeNode;import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.Map;import java.util.Set;import java.util.Stack;import java.util.TreeMap;import java.util.TreeSet;public class AnalysisTable {    private final int noFinalCharacterCount = 100;    private Map
stringToInt = new TreeMap
();//分析表行 列 字母映射到数字 private Map
tableRow = new TreeMap
();//分析表 行列 数字 映射到字母 private Map
tableCol = new TreeMap
(); private String[][] table = new String[noFinalCharacterCount][];//分析表 private Set
terminalCharacters = new TreeSet
();//终结符集合 private Map
> first = null; private Map
> follow = null; private Map
mp = null; private int nodeCntRow = 0, nodeCntCol=0; public static final int treeNodeCnt = 200;//树最多节点的个数 private int cntTreeNode = 0; private ArrayList
usedProduction = new ArrayList
();//预测分析中所用到的产生式 private int fatherNode;//treeGraphic搜素是的开始节点 private TreeNode[] treeGraphic = new TreeNode[treeNodeCnt]; private String[] analysisStack = null; public String[] getAnalysisStack(){ return analysisStack; } public Map
getStringToInt(){ return stringToInt; } public Set
getTerminalCharacters(){ return terminalCharacters; } public int getFatherNode(){ return fatherNode; } public TreeNode[] getTreeGraphic(){ return treeGraphic; } public AnalysisTable(Map
> first, Map
> follow, Map
mp) { super(); this.first = first; this.follow = follow; this.mp = mp; init(); } private void init(){ for(String leftNode : mp.keySet()){ stringToInt.put(leftNode, ++nodeCntRow); tableRow.put(nodeCntRow, leftNode); String[] rightNodes = mp.get(leftNode); for(int i=0; i
analysisTableKernealCode(){ for(String leftNode : mp.keySet()){ String[] rightNodes = mp.get(leftNode); for(int i=0; i
productionFirstSet = new TreeSet
();//产生式对应first集 for(int k=0; k
@ 对于每一个终结符 a属于FIRST(@),加入M[A, a] int col = stringToInt.get(""+finalCharacter); int row = stringToInt.get(leftNode); if(table[row]==null) table[row] = new String[nodeCntCol+1]; table[row][col] = leftNode + "->" + rightNodes[i]; } /***********************************************************************/ else if(productionFirstSet.contains('$')){ //A->@ 对于$属于First(@),对任何b属于Follow(A)则把A->@加入 if(follow.get(leftNode).contains(finalCharacter)){ int col = stringToInt.get(""+finalCharacter); int row = stringToInt.get(leftNode); if(table[row]==null) table[row] = new String[nodeCntCol+1]; table[row][col] = leftNode + "->" + rightNodes[i]; } } } } } return printTalbe(); } public ArrayList
printTalbe(){ ArrayList
tableNodeAl = new ArrayList
(); System.out.println("分析表如下:"); for(int i=1; i<=nodeCntRow; ++i){ for(int j=1; j<=nodeCntCol; ++j) if(table[i]!=null && table[i][j]!=null){ tableNodeAl.add(new TableNode(i, j, table[i][j])); System.out.println(tableRow.get(i) + ":" + tableCol.get(j) + " " + table[i][j]); } } return tableNodeAl; } public boolean predictiveAnalysis(String formula){ ArrayList
stringStack = new ArrayList
(); System.out.println("开始进行预测分析,分析栈如下:"); Stack
stack = new Stack
(); stack.push("#"); stack.push(tableRow.get(1)); int formulaIndex = 0; char a = formula.charAt(formulaIndex++); boolean flag = false; while(true){ if(stack.size() == 0){ //error break; } stringStack.add(stack.toString()); System.out.println(stack); String x = stack.pop(); if(!mp.containsKey(x)){ //终结符 if(x.charAt(0)==a){ if(a=='#'){ flag = true; break; } a = formula.charAt(formulaIndex++); } else { //error } } else { //非终结符 if(table[stringToInt.get(x)] != null){ String production = table[stringToInt.get(x)][stringToInt.get(""+a)]; if(production != null){ usedProduction.add(production); if(!production.contains("$")){ //X->X1X2X3....Xk 中 Xk....X3X2X1压入栈中 for(int i=production.length()-1; i>=0; --i){ if(production.charAt(i)=='>') break; if(production.charAt(i)=='\''){ stack.push(""+production.charAt(i-1)+production.charAt(i)); --i; } else{ stack.push(""+production.charAt(i)); } } } } else { //error } } else { //error } } } analysisStack = stringStack.toArray(new String[stringStack.size()]); return flag; } private int produceAnalysisTree(int curRow, String preNode){ String curProduction = usedProduction.get(curRow); String splits[] = curProduction.split("->"); if(preNode != null && !preNode.equals(splits[0])) return produceAnalysisTree(curRow+1, preNode); TreeNode treeNode = new TreeNode(); treeNode.content = splits[0]; for(int i=0; i
iCtSA|a",// "A->$|eS", // "C->b"// }; 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 split2[] = split1[1].split("\\|"); mp.put(split1[0], split2); } } catch(Exception e){ e.printStackTrace(); System.out.println("右线性文法错误!"); } First first = new First(mp); first.firstKernealCode(); Follow follow = new Follow(mp, first.getFirstSet()); follow.followKernealCode(); AnalysisTable analysisTable = new AnalysisTable(first.getFirstSet(), follow.getFollowSet(), mp); analysisTable.analysisTableKernealCode(); analysisTable.predictiveAnalysis("i+i#"); analysisTable.AnalysisTree(); }}

 

转载地址:http://qcfql.baihongyu.com/

你可能感兴趣的文章
Docopt命令行库
查看>>
阿里云数据管理DMS企业版发布年度重大更新 多项功能全面升级
查看>>
BCH(比特币现金):比特币最成功补丁,价值第一的中国区块链项目
查看>>
laravel 多数据库操作
查看>>
小网客博客
查看>>
为python添加tab自动补全功能
查看>>
用Node.js 写web框架(三)
查看>>
强行重置Mysql的账号密码
查看>>
centos7 搭建svn服务器
查看>>
利用netca创建数据库时监听器没有启动引发的错误
查看>>
Windows 8(developer preview)安装体验
查看>>
CentOS 7安装SQL Server功能组件
查看>>
第一章 Linux系统安装及基本操作
查看>>
我的友情链接
查看>>
JSP - SpringMVC 传递复杂集合参数
查看>>
linux shell脚步使用讲解
查看>>
Dwr3.0纯注解(纯Java Code配置)配置与应用浅析二之前端调用后端
查看>>
Dubbo源码分析(11):服务发布
查看>>
CentOS静默安装Oracle数据库
查看>>
第3大的数 Third Maximum Number
查看>>