【问题描述】
设计一个给定文法和对应的FIRSTVT和LASTVT集,能依据依据文法和FIRSTVT和LASTVT生成算符优先分析表。
import java.util.ArrayList;
import java.util.Iterator;
public class OP {
public ArrayList<Production> production = new ArrayList<Production>();// 存储产生式
String[] vn;
String[] vt;
int[][] FstTable; // FirstVT表
int[][] LstTable; // LastVT表
int[][] PriorTable; // 优先级表
public void input() {
production.add(new Production("B", "BoT"));
production.add(new Production("B", "T"));
production.add(new Production("T", "TaF"));
production.add(new Production("T", "F"));
production.add(new Production("F", "nF"));
production.add(new Production("F", "(B)"));
production.add(new Production("F", "t"));
production.add(new Production("F", "f"));
/* 初始化非终结字符集 */
vn = new String[3];
vn[0] = "B";
vn[1] = "T";
vn[2] = "F";
/* 初始化终结字符集 */
vt = new String[7];
vt[0] = "o";
vt[1] = "a";
vt[2] = "n";
vt[3] = "(";
vt[4] = ")";
vt[5] = "t";
vt[6] = "f";
FstTable = new int[vn.length][vt.length];
LstTable = new int[vn.length][vt.length];
PriorTable = new int[vt.length][vt.length];
/* B的Firstvt */
FstTable[0][0] = 1;
FstTable[0][1] = 1;
FstTable[0][2] = 1;
FstTable[0][3] = 1;
FstTable[0][4] = 0;
FstTable[0][5] = 1;
FstTable[0][6] = 1;
/* T的Firstvt */
FstTable[1][0] = 0;
FstTable[1][1] = 1;
FstTable[1][2] = 1;
FstTable[1][3] = 1;
FstTable[1][4] = 0;
FstTable[1][5] = 1;
FstTable[1][6] = 1;
/* F的Firstvt */
FstTable[2][0] = 0;
FstTable[2][1] = 0;
FstTable[2][2] = 1;
FstTable[2][3] = 1;
FstTable[2][4] = 0;
FstTable[2][5] = 1;
FstTable[2][6] = 1;
/* B的Lastvt */
LstTable[0][0] = 1;
LstTable[0][1] = 1;
LstTable[0][2] = 1;
LstTable[0][3] = 0;
LstTable[0][4] = 1;
LstTable[0][5] = 1;
LstTable[0][6] = 1;
/* T的Lastvt */
LstTable[1][0] = 0;
LstTable[1][1] = 1;
LstTable[1][2] = 1;
LstTable[1][3] = 0;
LstTable[1][4] = 1;
LstTable[1][5] = 1;
LstTable[1][6] = 1;
/* F的Lastvt */
LstTable[2][0] = 0;
LstTable[2][1] = 0;
LstTable[2][2] = 1;
LstTable[2][3] = 0;
LstTable[2][4] = 1;
LstTable[2][5] = 1;
LstTable[2][6] = 1;
}
/* 判断是否为终结符 */
public boolean isVT(String str) {
boolean bool = false;
for (int i = 0; i < vt.length; i++) {
if (str.equals(vt[i])) {
bool = true;
}
}
return bool;
}
/* 判断是否为非终结符 */
public boolean isVN(String str) {
boolean bool = false;
for (int i = 0; i < vn.length; i++) {
if (str.equals(vn[i])) {
bool = true;
}
}
return bool;
}
/* 获取某字符在终结字符数组的索引 */
public int getVTIndex(String str) {
int index = -1;
for (int i = 0; i < vt.length; i++) {
if (str.equals(vt[i])) {
index = i;
}
}
return index;
}
/* 获取某字符在终结字符数组的索引 */
public int getVNIndex(String str) {
int index = -1;
for (int i = 0; i < vn.length; i++) {
if (str.equals(vn[i])) {
index = i;
}
}
return index;
}
public void p(String str) {
System.out.print(str);
}
public void showProduction(){
p("*****以下显示的是您输入的产生式*****/n");
Iterator iterator = production.iterator();
while (iterator.hasNext()) {
Production pro = (Production) iterator.next();
System.out.println(pro.left+"->"+pro.right);
}
}
/*显示输入的FIRSTVT集*/
public void showFst(){
p("*****以下显示的是您输入的FIRSTVT集*****/n");
p(" ");
for (int i = 0; i < vt.length; i++) {
p(" " + vt[i]);
}
for(int i=0;i<FstTable.length;i++){
p("/n" + vn[i]);
for(int j=0;j<FstTable[i].length;j++){
if (FstTable[i][j] == 0)
p(" ");
if (FstTable[i][j] == 1)
p(" 1");
}
}
}
/*显示输入的LASTVT集*/
public void showLst(){
p("/n*****以下显示的是您输入的LASTVT集*****/n");
p(" ");
for (int i = 0; i < vt.length; i++) {
p(" " + vt[i]);
}
for(int i=0;i<LstTable.length;i++){
p("/n" + vn[i]);
for(int j=0;j<LstTable[i].length;j++){
if (LstTable[i][j] == 0)
p(" ");
if (LstTable[i][j] == 1)
p(" 1");
}
}
}
/* 构造文法的优先关系表 */
public void ShowPriorTalbe() {
Iterator iterator = production.iterator();
while (iterator.hasNext()) {
Production pro = (Production) iterator.next();
/* 对产生式的右部进行遍历 */
for (int i = 0; i < pro.right.length() - 1; i++) {
/* 获取产生式右部的第i个和第i+1个字符 */
String ch = String.valueOf(pro.right.charAt(i));
String ch1 = String.valueOf(pro.right.charAt(i + 1));
/* 获得该终结符在终结符集的索引 */
int VTindex = getVTIndex(ch);
int VTindex1 = getVTIndex(ch1);
/* 获得该终结符在非终结符集的索引 */
int VNindex = getVNIndex(ch);
int VNindex1 = getVNIndex(ch1);
/*IF X(i)和X(i+1)均为终结符*/
if (isVT(ch) && isVT(ch1)) {
// x(i)=x(i+1)
if (VTindex == -1 || VTindex1 == -1) {
System.out.println("该字符不在终结字符集内");
} else {
PriorTable[VTindex][VTindex1] = 2;// 标记 "="
}
}
/*IF i<=n-2 且X(i)和X(i+2)都为终结符,但X(i+1)为非终结符*/
if (i < pro.right.length() - 2 && isVT(ch)
&& isVT(String.valueOf(pro.right.charAt(i + 2)))
&& isVN(ch1)) {
// xi = x(i+2)
int prior2 = getVTIndex(String.valueOf(pro.right
.charAt(i + 2)));
if (VTindex == -1 || prior2 == -1) {
System.out.println("该字符不在终结字符集内");
} else {
PriorTable[VTindex][prior2] = 2;// 标记 "="
}
}
/*IF X(i)为终结符而X(i+1)为非终结符
* THEN FOR FIRSTVT(X(i+1)) 中的每个 b DO 置 X(i)<b;
* */
if (isVT(ch) && isVN(ch1)) {
// x(i)<b
for (int j = 0; j < FstTable[VNindex1].length; j++) {
if (FstTable[VNindex1][j] == 1) {
PriorTable[VTindex][j] = 1;// 标记 "<"
}
}
}
/*IF X(i)为非终结符 而X(i+1)为终结符
* THEN FOR LASTVT(X(i)中的每个a DO 置a>X(i+1);
* */
if (isVN(ch) && isVT(ch1)) {
// a>x(i+1)
for (int j = 0; j < LstTable[VNindex].length; j++) {
if (LstTable[VNindex][j] == 1) {
PriorTable[j][VTindex1] = 3; // 标记 ">"
}
}
}
}
}
}
/* 根据二维数组显示输出优先关系分析表 */
public void output() {
p("/n*****以下显示的是构造出来的优先关系表*****/n");
p(" ");
for (int i = 0; i < vt.length; i++) {
p(" " + vt[i]);
}
for (int i = 0; i < PriorTable.length; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
p("/n" + vt[i]);
for (int j = 0; j < PriorTable[i].length; j++) {
if (PriorTable[i][j] == 0)
p(" ");
if (PriorTable[i][j] == 1)
p(" <");
if (PriorTable[i][j] == 2)
p(" =");
if (PriorTable[i][j] == 3)
p(" >");
}
}
}
public static void main(String[] args) {
OP op = new OP();
op.input();
op.showProduction();
op.showFst();
op.showLst();
op.ShowPriorTalbe();
op.output();
}
}
/* 产生式结构 */
class Production {
public String left;
public String right;
public Production(String left, String right) {
this.left = left;
this.right = right;
}
}