?? syntaxanalyze.java
字號:
/*
* 呂淵 200532580144
* 使用工具:eclipse
* Java SE 6
*
* 完成日期 2007-10-21
* *** 生日紀念
*/
import java.util.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.regex.Pattern;
public class SyntaxAnalyze extends LexicalAnalyze{
//用于存儲節點
public DefaultMutableTreeNode syntaxTree= new DefaultMutableTreeNode("Syntax Tree");
//正確及出現錯誤時候的輸出
private ArrayList<String> syntaxOutput = new ArrayList<String>();
private ArrayList<String> syntaxWrongOutput = new ArrayList<String>();
public ArrayList<Integer> tokenRightStart = new ArrayList<Integer>();
public ArrayList<Integer> tokenRightEnd = new ArrayList<Integer>();
public ArrayList<Integer> tokenErrorStart = new ArrayList<Integer>();
public ArrayList<Integer> tokenErrorEnd = new ArrayList<Integer>();
//用于計數輸出時預留的空格數
private int countSpace = -1;
//用于存儲嵌套的層次及行號
public LinkedList<Integer> deep = new LinkedList<Integer>();
//是否出現語法錯誤
public boolean syntaxError = false;
//進行一次語法分析操作后臨時存儲剩余內容
//條件語句、循環語句中僅有單行執行語句且無大括號時使用
private LinkedList<Tokens> listTT = new LinkedList<Tokens>();
//用語存儲待分析內容的字符串值
private String arithmetic = "";
//構造方法
public SyntaxAnalyze(String inputString) {
super(inputString);
//詞法分析未發現錯誤
if (lexicalError == false)
syntaxAnalyze(syntaxTree, list, true);
else syntaxError = true;
}
//語法分析
/*以下函數參數含義:
* DefaultMutableTreeNode 父節點
* (條件語句中從if到else if及到else對其分析過程只計為一次分析,并非單獨進入語法分析的循環執行,父節點相同);
* LinkedList<Tokens> listT 待分析內容;
* boolean executeTimes 所需分析的次數
* (true表示需進入循環將待分析內容分析完畢,false則只分析一次語句并將剩余結果存入listTT中).
*/
private void syntaxAnalyze(DefaultMutableTreeNode tree,
LinkedList<Tokens> listT, boolean executeTimes){
countSpace++;
matchingAnalyze(listT, tree, executeTimes);
countSpace--;
}
//匹配分析
private void matchingAnalyze(LinkedList<Tokens> listT,
DefaultMutableTreeNode matchingTree, boolean executeTimes) {
//空的剩余內容無操作
if (listT.size()==0){}
//剩余內容以保留字開始
else if (listT.get(0).type == CMMK) {
keywordMatch(listT, matchingTree, executeTimes);
}
//以標志符開始
else if (listT.get(0).type == CMMI) {
evaluate(listT, matchingTree, executeTimes);
}
//其他開始方式
else {
//以大括號開始
if (listT.getFirst().token.equals("{")) {
//計算大括號結束位置
int end = analyzeBracket(listT, "{", "}", 0);
if (end == listT.size()-1) {
listT.removeLast();
listT.removeFirst();
matchingAnalyze(listT, matchingTree, executeTimes);
}
else
matchingAnalyze(statementWrong(listT, true), matchingTree, executeTimes);
}
else if (listT.getFirst().token.equals(";")) {
listT.removeFirst();
//進入后續分析或分析結束并備份剩余待分析內容
if (executeTimes)
matchingAnalyze(listT, matchingTree, executeTimes);
else
listTT = listT;
}
//其他各種錯誤開始方式
else
matchingAnalyze(statementWrong(listT, true), matchingTree, executeTimes);
}
}
//以保留字開始的匹配分析
private void keywordMatch(LinkedList<Tokens> listT,
DefaultMutableTreeNode keywordMatchTree, boolean executeTimes) {
//以if開始
if (listT.getFirst().token.equals("if")) {
//添加子節點及輸出
DefaultMutableTreeNode ifTreeT = new DefaultMutableTreeNode("條件語句");
keywordMatchTree.add(ifTreeT);
syntaxOutput.add(showSpace() + "條件語句:");
tokenRightStart.add(listT.getFirst().start);
tokenRightEnd.add(listT.getFirst().end);
deep.add(countSpace);
countSpace++;
//進入條件語句的分析
ifClause(listT, ifTreeT, false);
countSpace--;
//是否進入后續分析
if (executeTimes)
matchingAnalyze(listTT, keywordMatchTree, true);
}
//以real開始
else if (listT.getFirst().token.equals("real")) {
matchingAnalyze(statementWrong(listT, true), keywordMatchTree, executeTimes);
}
//以else開始
else if (listT.getFirst().token.equals("else")) {
elseBlockWrong(listT, executeTimes);
}
//以while開始
else if (listT.getFirst().token.equals("while")) {
loop(listT, keywordMatchTree, executeTimes);
}
//以read開始
else if (listT.getFirst().token.equals("read")) {
read(listT, keywordMatchTree, executeTimes);
}
//以write開始
else if (listT.getFirst().token.equals("write")) {
write(listT, keywordMatchTree, executeTimes);
}
//以int開始
else {
//長度限制
if (listT.size()<3) {
keywordUseWrong(listT.getFirst());
syntaxWrongOutput.add(" 錯誤原因 :語句長度過短");
tokenErrorStart.add(listT.getFirst().start);
tokenErrorEnd.add(listT.getFirst().end);
listT.clear();
listTT = listT;
}
//格式限制
else if (listT.get(1).type!=CMMI) {
syntaxWrong();
syntaxWrongOutput.add(" 錯誤的變量聲明 #" + listT.getFirst().lineNo);
tokenErrorStart.add(listT.getFirst().start);
tokenErrorEnd.add(listT.getFirst().end);
listT.removeFirst();
listT = statementWrong(listT, true);
//進入后續分析或分析結束并備份剩余待分析內容
if (executeTimes)
matchingAnalyze(listT, keywordMatchTree, executeTimes);
else
listTT = listT;
}
else {
//聲明int型變量不進行初始化
if (listT.get(2).token.equals(";")) {
//添加子節點及輸出
DefaultMutableTreeNode declarationTree = new DefaultMutableTreeNode("int型變量聲明");
keywordMatchTree.add(declarationTree);
declarationTree.add(new DefaultMutableTreeNode("變量: " + listT.get(1).token));
syntaxOutput.add(showSpace() + "變量聲明:");
deep.add(countSpace);
tokenRightStart.add(listT.getFirst().start);
tokenRightEnd.add(listT.getFirst().end);
syntaxOutput.add(showSpace() + " 變量: " + listT.get(1).token +
" #" + listT.get(1).lineNo);
deep.add(countSpace + 1);
tokenRightStart.add(listT.get(1).start);
tokenRightEnd.add(listT.get(1).end);
//剩余待分析內容
if (listT.size()==3) listT.clear();
else
listT = new LinkedList<Tokens>(listT.subList(3, listT.size()));
//進入后續分析或分析結束并備份剩余待分析內容
if (executeTimes)
matchingAnalyze(listT, keywordMatchTree, executeTimes);
else
listTT = listT;
}
//聲明int型變量并進行初始化
else if (listT.get(2).token.equals("="))
declare(listT, keywordMatchTree, executeTimes);
//聲明數組變量
else if (listT.get(2).token.equals("["))
arrayDeclare(listT, keywordMatchTree, executeTimes);
//錯誤的變量聲明
else {
keywordUseWrong(listT.getFirst());
listT.removeFirst();
listT = statementWrong(listT, true);
//進入后續分析或分析結束并備份剩余待分析內容
if (executeTimes)
matchingAnalyze(listT, keywordMatchTree, executeTimes);
else
listTT = listT;
}
}
}
}
//條件語句
//if及if else處理
//block表示進入的是if或else if語句內容(false表示if,true為else if)
private void ifClause(LinkedList<Tokens> listT, DefaultMutableTreeNode ifTree, boolean block) {
//添加子節點及輸出
String str = "";
str += block ? "else if" : "if";
DefaultMutableTreeNode ifTreeT = new DefaultMutableTreeNode(str + " 語句");
ifTree.add(ifTreeT);
syntaxOutput.add(showSpace() + str + " 語句");
tokenRightStart.add(listT.getFirst().start);
if (block) listT.removeFirst();
tokenRightEnd.add(listT.getFirst().end);
deep.add(countSpace);
//錯誤:語句長度過短
if (listT.size() < 3) {
keywordUseWrong(listT.getFirst());
syntaxWrongOutput.add(" 錯誤原因 :語句長度過短");
tokenErrorStart.add(listT.getFirst().start);
tokenErrorEnd.add(listT.getFirst().end);
listT.clear();
listTT = listT;
}
//錯誤:if之后非小括號
else if (!(listT.get(1).token.equals("("))) {
keywordUseWrong(listT.getFirst());
syntaxWrongOutput.add(" 錯誤原因 : if 之后非小括號\n");
tokenErrorStart.add(listT.get(1).start);
tokenErrorEnd.add(listT.get(1).end);
listT.removeFirst();
listTT = listT;
}
else {
//計算對應括號位置
int end1 = analyzeBracket(listT, "(", ")", 1);
//錯誤:沒有對應括號結束
if (end1 == -1) {
syntaxWrong();
syntaxWrongOutput.add(" 括號無法匹配 #" + listT.get(1).lineNo);
tokenErrorStart.add(listT.get(1).start);
tokenErrorEnd.add(listT.get(1).end);
listT.clear();
}
else {
//錯誤:沒有布爾表達式
if (end1 == 2) {
syntaxWrong();
syntaxWrongOutput.add(" 沒有布爾表達式 #" + listT.get(end1).lineNo);
tokenErrorStart.add(listT.get(1).start);
tokenErrorEnd.add(listT.get(2).end);
}
//含有布爾表達式
else {
boolean bracketWrong = false;
for (int i = 2; i < end1; i++) {
//錯誤:布爾表達式中含有大括號
if (listT.get(i).token.equals("{") || listT.get(i).token.equals("}")) {
bracketWrong = true;
syntaxWrong();
syntaxWrongOutput.add(" 括號使用有誤 #" + listT.get(1).lineNo);
tokenErrorStart.add(listT.get(i).start);
tokenErrorEnd.add(listT.get(i).end);
break;
}
}
if (bracketWrong) {}
//括號中為real
else if (end1==3 && listT.get(2).token.equals("real")){
//添加子節點及輸出
ifTreeT.add(new DefaultMutableTreeNode("布爾值: real"));
syntaxOutput.add(showSpace() + " 布爾值: real #" + listT.get(2).lineNo);
tokenRightStart.add(listT.get(2).start);
tokenRightEnd.add(listT.get(2).end);
deep.add(countSpace + 1);
}
//括號中為布爾表達式
else {
countSpace++;
relationOperation(new LinkedList<Tokens>(listT.subList(2, end1)),ifTreeT);
countSpace--;
}
//含有執行語句
if (end1!=listT.size()-1) {
boolean noBlock = true;
//含有block
if (listT.get(end1+1).token.equals("{")) {
noBlock = false;
//block結束位置
int end2 = analyzeBracket(listT, "{", "}",end1+1);
//block未結束
if (end2 == -1) {
syntaxWrong();
syntaxWrongOutput.add(" 括號無法匹配 #" + listT.get(1).lineNo);
tokenErrorStart.add(listT.get(end1+1).start);
tokenErrorEnd.add(listT.get(end1+1).end);
listT.clear();
}
//block正常結束
else {
//block中含有執行語句 對其進行分析
if (end2!=end1+2)
syntaxAnalyze(ifTreeT,
new LinkedList<Tokens>(listT.subList(end1+2, end2)), true);
//剩余待分析內容
if (end2!=listT.size()-1)
listT = new LinkedList<Tokens>(listT.subList(end2+1, listT.size()));
else listT.clear();
}
//存入臨時內容中
listTT = listT;
}
//if及else if之后有執行語句且不使用大括號
//僅進行一次分析
else {
countSpace++;
matchingAnalyze(
new LinkedList<Tokens>(listT.subList(end1+1, listT.size())),
ifTreeT, false);
countSpace--;
}
//將臨時內容加載
if (noBlock)
listT = listTT;
//含有待分析內容,以else開始
if (!listT.isEmpty() && listT.getFirst().token.equals("else")) {
//錯誤:剩余內容僅為單獨else
if (listT.size()==1){
keywordUseWrong(listT.getFirst());
syntaxWrongOutput.add(" 錯誤原因 :單獨的else");
tokenErrorStart.add(listT.getFirst().start);
tokenErrorEnd.add(listT.getFirst().end);
listT.clear();
}
else {
//待分析內容為else if語句
if (listT.get(1).token.equals("if"))
ifClause(listT, ifTree, true);
//待分析內容為else語句
else
elseClause(listT, ifTree);
}
}
}
//錯誤:不含有執行語句
else {
keywordUseWrong(listT.getFirst());
syntaxWrongOutput.add(" 錯誤原因 :沒有執行語句");
tokenErrorStart.add(listT.getFirst().start);
tokenErrorEnd.add(listT.getFirst().end);
listT.clear();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -