📄 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 + -