对于 编译原理实验二
实验二主要分析实验一中的得到的多元式是进行计算器的语义分析
需要从实验1中获取分析成功的多元(种别码,字符串原貌,Value属性,Type属性)式序列
根据多元式中的Type属性获取类型(int、double、运算符等)然后逐步分析所有的多元式,是否符合语义
如果符合则分析下一个,如果不符合,则跳出
如:2+4.2*(5+6)
编写代码控制台将依次输出一下信息
获取实验一分析结果
待分析的第4个token串为:2+4.2*(5+6)分析如下:
待分析的字符串不包含词法错误
Token{(5,2,2,int)}
Token{(1,+,null,null)}
Token{(8,4.2,4.2,double)}
Token{(3,*,null,null)}
Token{(6,(,null,null)}
Token{(5,5,5,int)}
Token{(1,+,null,null)}
Token{(5,6,6,int)}
Token{(7,),null,null)}
该表达式不存在语法错误。
获取实验二分析结果
已分析的结果构成的四元式如下:
(1)FAC{+,5,6,t1}
(2)FAC{*,4.2,t1,t2}
(3)FAC{+,2,t2,t3}
已分析的结果的四元式中对应的t?的值如下:
Connect{t1=11, t2=46.2, t3=48.2}
3、根据DFA分析得到
4、对代码进行编写
主要代码如下所示
/**
* Created by haol on 2016/11/18.
* 主要方法
*/
public class GuideTranslate {
public static HashMap expression = new HashMap();
public static HashMap option = new HashMap();
public static HashMap wordEnding = new HashMap<>();
public static int i;
boolean end = false;
public static List stateStack = new ArrayList();/*状态栈*/
public static List semanticStack = new ArrayList();//语义栈
public static List signStack = new ArrayList();//符号栈
public static List fourAddressCode = new ArrayList();
QuerySLR querySLR = new QuerySLR();
// @OperateFAC operateFAC = new OperateFAC();
SemanticStake anySemanticStack = new SemanticStake();
public List initializeSemantic(List tokens) {
Calculate calculate = new Calculate();
calculate.initializeCalculate();
MathExpression definition = new MathExpression();
List list = definition.definitionMemanticsRule();
option = list.get(0);// =
expression = list.get(1);// = <表达式右部,表达式左部>
wordEnding = definition.wordEnding();/*表达式定义*/
tokens.add(new Token("#", "#", "#", "#"));/*将token串回追加#结束符*/
/*初始化开始*/
i = 0;
fourAddressCode.clear();
/*初始化所有栈为空*/
stateStack.clear();
signStack.clear();
semanticStack.clear();
/*设置所有栈的初始值*/
stateStack.add(0);
semanticStack.add("-");
signStack.add("#");
return tokens;
}
//主要方法是这个,实现手动的填表过程,并将操作语义栈,符号盏,获取剩余要进栈的字符串
public List analyseSemantic(List tokens) {
//根据对应的字符获取goto和action的纵坐标
GetColumn getCol = new GetColumn();
tokens = this.initializeSemantic(tokens);
String actionState;/*SRL表中action状态*/
int row;/*行下标*/
int column;/*列下表*/
while (!end) {
/*获取现在状态盏最后一个字符*//*获取现在等待符号盏的字符*/
row = stateStack.get(stateStack.size() - 1);
column = getCol.reActionCol(tokens.get(i).getStrCodes());
actionState = querySLR.actionState(row, column);
if (actionState != null) {
if (actionState.substring(0, 1).equals("S")) {
this.shiftIn(tokens, row, column, actionState);
} else if (actionState.substring(0, 1).equals("R")) {
this.redex(actionState);
} else if (actionState.equals("acc")) {
end = true;
System.out.println("该表达式不存在语法错误。");
}
} else {
System.out.println("ERROR:第" + (signStack.size() + 1) + "个字符" + tokens.get(i).getStrApp() + "存在语义错误!");
break;
}
}
return fourAddressCode;
}
public void shiftIn(List tokens, int row, int column, String actionState) {
stateStack.add(Integer.valueOf(actionState.substring(1)));/*将S?状态的?放入状态盏*/
if (column == CreateSLRTable.C) {/*如果为C则为常数将值放入语意盏*/
semanticStack.add(tokens.get(i).getStrValue());
} else {/*将-放入*/
semanticStack.add("-");
}
signStack.add(tokens.get(i).getStrApp());/*在符号盏存入待进盏的字符*/
i++;
}
public void redex(String actionState) {
GetColumn getRow = new GetColumn();
OperateList operateList = new OperateList();
String gotoState;/*SRL表中goto状态*/
int rightNum;/*归约时右部长度*/
String rightStr;/*规约时表达式右部*/
String leftStr;/*归约时表达式左部*/
rightStr = option.get(actionState);
leftStr = expression.get(rightStr);
rightNum = rightStr.length();
/*将符号栈需要归约的弹出*//*将归约后的入栈*/
signStack = operateList.deleteState(signStack, rightNum);
signStack.add(leftStr);
/*将状态盏对应的弹出*//*获取现在状态盏最后一个字符*/
/*获取符号盏的刚刚归约的字符,也就是最后面的*/
stateStack = operateList.deleteState(stateStack, rightNum);
int row = stateStack.get(stateStack.size() - 1);
int column = getRow.reGotoCol(signStack.get(signStack.size() - 1));
gotoState = querySLR.gotoState(row, column);
stateStack.add(Integer.valueOf(gotoState));
if (rightNum > 1) {
List semantic = operateList.getSemantic(semanticStack, stateStack.size() - 1, rightNum);
semanticStack = anySemanticStack.analyseSemanticStack(rightStr, semantic, actionState, semanticStack);
//@this.analyseSemanticStack(rightStr, semantic, actionState);
}
}
public boolean isCorrent() {
return end;
}
源代码请点击下载