📄 ganalysis.cs
字号:
//声明语句:int|real[算术表达式] 标识符;
else if (stWords.Count >= 6
&& stWords[1].Equals("[")
&& stWords[stWords.Count - 3].Equals("]")
&& wordAnalysis.GetWordType((string)stWords[stWords.Count - 2]) == ID)
{
//获得下标内的表达式
string exp = "";
for (int i = 2; i < (stWords.Count - 3); i++)
exp += stWords[i] + " ";
if (ArthmExp.IsArthmExp(exp))
{
st.Els[0] = (string)stWords[stWords.Count - 2];
st.Els[1] = (string)stWords[0];
st.Els[2] = exp.Trim();
st.Els[3] = NOTASS;
return;
}
else
{
st.Type = ERROR;
dealError(st.Con, "数组下标表达式 " + exp + " 不是合法的算术表达式");
return;
}
}
//声明语句:int|real 标识符[算术表达式];
else if (stWords.Count >= 6
&& wordAnalysis.GetWordType((string)stWords[1]) == ID
&& stWords[2].Equals("[")
&& stWords[stWords.Count - 2].Equals("]"))
{
string exp = "";
for (int i = 3; i < (stWords.Count - 2); i++)
exp += stWords[i] + " ";
if (ArthmExp.IsArthmExp(exp))
{
st.Els[0] = (string)stWords[1];
st.Els[1] = (string)stWords[0];
st.Els[2] = exp.Trim();
st.Els[3] = NOTASS;
return;
}
else
{
st.Type = ERROR;
dealError(st.Con, "数组下标表达式 " + exp + " 不是合法的算术表达式");
return;
}
}
//声明赋值语句:int|real 标识符 = 算术表达式;
else if (stWords.Count >= 5
&& wordAnalysis.GetWordType((string)stWords[1]) == ID
&& stWords[2].Equals("="))
{
//提取算术表达式
string exp = "";
for (int i = 3; i < (stWords.Count - 1); i++)
exp += stWords[i] + " ";
st.Els[0] = (string)stWords[1];
st.Els[1] = (string)stWords[0];
st.Els[2] = NOTARR;
st.Els[3] = exp.Trim();
return;
}
//如果不符合上述情况即为不合法语句
else
{
st.Type = ERROR;
dealError(st.Con, "不合法的声明语句");
return;
}
}
//分析输入语句
private void AnalyzeInput(ref CMMStatement st)
{
ArrayList stWords = wordAnalysis.GetWordLst(st.Con);
//输入语句:read 标识符;
if (stWords.Count == 3
&& wordAnalysis.GetWordType((string)stWords[1]) == ID)
{
st.Els[0] = (string)stWords[1];
st.Els[1] = NOTARR;
return;
}
//输入语句:read 标识符[算术表达式];
else if (stWords.Count >= 6
&& wordAnalysis.GetWordType((string)stWords[1]) == ID
&& stWords[2].Equals("[")
&& stWords[stWords.Count - 2].Equals("]"))
{
string exp = "";
for (int i = 3; i < stWords.Count - 2; i++)
exp += stWords[i] + " ";
if (ArthmExp.IsArthmExp(exp))
{
st.Els[0] = (string)stWords[1];
st.Els[1] = exp.Trim();
return;
}
else
{
st.Type = ERROR;
dealError(st.Con, "数组下标表达式 " + exp + " 不是合法的算术表达式");
return;
}
}
//如果不符合上述情况即为不合法语句
else
{
st.Type = ERROR;
dealError(st.Con, "不合法的输入语句");
return;
}
}
//分析输出语句
private void AnalyzeOutput(ref CMMStatement st)
{
ArrayList stWords = wordAnalysis.GetWordLst(st.Con);
//输出语句:write 算术表达式;
if (stWords.Count >= 3)
{
//提取算术表达式
string exp = "";
for (int i = 1; i < (stWords.Count - 1); i++)
exp += stWords[i] + " ";
st.Els[0] = exp;
return;
}
//如果不符合上述情况即为不合法语句
else
{
st.Type = ERROR;
dealError(st.Con, "不合法的输出语句");
return;
}
}
//分析赋值语句
private void AnalyzeAssign(ref CMMStatement st)
{
ArrayList stWords = wordAnalysis.GetWordLst(st.Con);
//普通赋值语句:已声明普通标识符 = 算术表达式;
if (stWords.Count >= 4
&& wordAnalysis.GetWordType((string)stWords[0]) == ID
&& stWords[1].Equals("="))
{
//提取算术表达式
string exp = "";
for (int i = 2; i < stWords.Count - 1; i++)
exp += stWords[i] + " ";
st.Els[0] = (string)stWords[0];
st.Els[1] = NOTARR;
st.Els[2] = exp.Trim();
return;
}
//数组赋值语句:已声明数组标识符[算术表达式] = 算术表达式;
else if (stWords.Count >= 7
&& wordAnalysis.GetWordType((string)stWords[0]) == ID
&& stWords[1].Equals("["))
{
int eqIndex = stWords.IndexOf("=");
if (eqIndex < 4 || eqIndex > stWords.Count - 3
|| !stWords[eqIndex - 1].Equals("]"))
{
st.Type = ERROR;
dealError(st.Con, "不合法的赋值语句");
return;
}
//提取数组下标算术表达式
string arrexp = "";
for (int i = 2; i < eqIndex - 1; i++)
arrexp += stWords[i] + " ";
//提取右侧算术表达式
string assexp = "";
for (int i = eqIndex + 1; i < (stWords.Count - 1); i++)
assexp += stWords[i] + " ";
st.Els[0] = (string)stWords[0];
st.Els[1] = arrexp.Trim();
st.Els[2] = assexp.Trim();
return;
}
//如果不符合上述情况即为不合法语句
else
{
st.Type = ERROR;
dealError(st.Con, "不合法的赋值语句");
return;
}
}
//分析循环语句
private void AnalyzeLoop(ref CMMStatement st)
{
//检查 while (
string loop_st = st.Con;
if (!loop_st.Remove(0, 5).TrimStart().StartsWith("("))
{
st.Type = ERROR;
dealError(st.Con, "代码中有语法错误:while语句的格式应为 'while (关系表达式) 语句'");
return;
}
loop_st = loop_st.Remove(0, loop_st.IndexOf("("));
//获取关系表达式
string relat_exp = "";
if (!GetBracketContent(ref loop_st, ref relat_exp))
{
st.Type = ERROR;
dealError(st.Con, "代码中有语法错误:关系式括号不匹配 '");
return;
}
//loop_st剩余的部分即为while的循环体
st.Els[0] = relat_exp;
st.Els[1] = loop_st;
}
//分析选择语句
private void AnalyzeSelect(ref CMMStatement st)
{
//检查 if (
string if_st = st.Con;
if (!if_st.StartsWith("if")
|| !if_st.Remove(0, 2).TrimStart().StartsWith("("))
{
st.Type = ERROR;
dealError(st.Con, "代码中有语法错误:if语句格式应为 'if (关系表达式) 语句 else 语句'\r\n");
return;
}
if_st = if_st.Remove(0, if_st.IndexOf("("));
//获取关系表达式
string relat_exp = "";
if (!GetBracketContent(ref if_st, ref relat_exp))
{
st.Type = ERROR;
dealError(st.Con, "代码中有语法错误:关系式括号不匹配 '");
return;
}
//获取then分支
CMMStatement then_st = SeparateFirstStatement(ref if_st);
st.Els[0] = relat_exp;
st.Els[1] = then_st.Con;
//检查是否含else分支
string else_st = if_st.Trim();
if (else_st.Length == 0)
st.Els[2] = "";
else
{
if (!else_st.StartsWith("else"))
{
st.Type = ERROR;
dealError(st.Con, "代码中有语法错误:if语句else分支应以关键字'else'开始,后接else语句\r\n");
}
else
{
else_st = else_st.Remove(0, 4).TrimStart();
st.Els[2] = else_st;
}
}
}
//分析关系表达式
public void AnalyzeRelatExp(ref CMMStatement exp)
{
//关系表达式: 算术表达式 <|<>|== 算术表达式
string expstr = exp.Con.Trim();
bool isRelatExp = false;
foreach (string compar in Compars)
{
int comparIndex = expstr.IndexOf(compar);
//关系运算符不处于首尾
if (comparIndex > 0 && comparIndex < expstr.Length - compar.Length)
{
//左侧的算术表达式
string exp_left = expstr.Substring(0, comparIndex);
//右侧的算术表达式
string exp_right = expstr.Substring(comparIndex + compar.Length);
if (ArthmExp.IsArthmExp(exp_left) && ArthmExp.IsArthmExp(exp_right))
{
isRelatExp = true;
exp.Type = RELAT_EXP;
exp.Els[0] = compar;
exp.Els[1] = exp_left;
exp.Els[2] = exp_right;
}
else
{
exp.Type = ERROR;
dealError(expstr, "关系运算符两侧的算式表达式不合法");
}
break;
}
}
if (!isRelatExp)
{
exp.Type = ERROR;
dealError(expstr, "不合法的关系表达式");
}
}
//获得语句树节点
private TreeNode GetNode(CMMStatement st)
{
AnalyzeStatement(ref st);
switch (st.Type)
{
case NULLST: //无语句
return null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -