📄 firstpass.cs
字号:
//跳过整个方法部分
while (!this.matchToken("}"))
{
this.getNextToken();
}
//跳过 }
this.getNextToken();
//抛出
throw new ComException(theError.getRow(), theError.getColumn(), 6);
}
}
catch (ComException ce)
{ //假如错误队列
this.errorList.Add(ce.getMessage());
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
//方法体部分 }下
TreeNode function_in_part()
{
try
{
//一个方法体节点
TreeNode the_function_in = new TreeNode(13);
//跳过 {
this.getNextToken();
do
{
//一个方法体内容节点
TreeNode the_funct_in_cont = this.funct_in_cont_part();
//作为方法体的孩子
the_function_in.addChildren(the_funct_in_cont);
} while (this.matchToken("int")||matchToken("real")||getToken().isIdentifier()||matchToken("if")||matchToken("while")||matchToken("read")||matchToken("write")||matchToken(";")||matchToken("return"));
// }
if (this.matchToken("}"))
{
this.getNextToken();
return the_function_in;
}
else
{ //记录错误
Token theError = this.getToken();
//跳过错误单词
this.getNextToken();
while (!this.matchToken("}")) { this.getNextToken(); }
//跳过 }
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 9);
}
}
catch (ComException ce)
{ //假如错误队列
this.errorList.Add(ce.getMessage());
return null;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
}
//方法调用式部分 参数 方法名 下
TreeNode funct_call_part(Token the_identifier)
{
try
{
//生成一个方法调用式节点
TreeNode the_funct_call = new TreeNode(15);
//标识符为孩子
the_funct_call.addChildren(the_identifier);
this.getNextToken();
//无参数,方法调用语句结束
if (this.matchToken(")"))
{
this.getNextToken();
return the_funct_call;
}
//有参数
else
{
//一个传参数节点
TreeNode the_send_para = new TreeNode(16);
do
{ //如果现在指向 "," 则指针前移
if (this.matchToken(",")) { this.getNextToken(); }
//一个表达式节点
TreeNode the_expression = this.expression_part();
//表达式作为传参数节点的孩子
the_send_para.addChildren(the_expression);
} while (this.matchToken(","));
//传递多个参数
//方法调用语句结束
if (this.matchToken(")"))
{
this.getNextToken();
the_funct_call.addChildren(the_send_para);
return the_funct_call;
}
else
{
//记录错误
Token theError = this.getToken();
this.getNextToken();
//跳到分号
while (!this.matchToken(";"))
{
this.getNextToken();
}
//抛出
throw new ComException(theError.getRow(), theError.getColumn(), 7);
}
}
}
catch (ComException ce)
{ //假如错误队列
this.errorList.Add(ce.getMessage());
return null;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
}
//数组元素部分 下
TreeNode array_elem_part(Token the_identifier)
{
try
{
//生成一个数组元素节点
TreeNode the_array_elem=new TreeNode(21);
//标识符作为数组元素节点的孩子
the_array_elem.addChildren(the_identifier);
this.getNextToken();
//数字为索引
if (this.getToken().isPositiveInt())
{
//索引作为数组元素的孩子
the_array_elem.addChildren(this.getToken());
this.getNextToken();
if (this.matchToken("]"))
{
this.getNextToken();
//到了下一个
return the_array_elem;
}
//报错
else
{
//记录错误
Token theError = this.getToken();
//跳过错误单词
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 3);
}
}
//标识符为索引
else if (this.getToken().isIdentifier())
{
//索引作为数组元素的孩子
the_array_elem.addChildren(this.getToken());
this.getNextToken();
if (this.matchToken("]"))
{
this.getNextToken();
//到了下一个
return the_array_elem;
}
//报错
else
{
//记录错误
Token theError = this.getToken();
//跳过错误单词
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 3);
}
}
//报错
else
{
//记录错误
Token theError = this.getToken();
//跳过错误单词
this.getNextToken();
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 3);
}
}
catch (ComException ce)
{ //假如错误队列
this.errorList.Add(ce.getMessage());
return null;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
}
//因子部分 下
TreeNode atom_part()
{
try
{
//生成一个因子节点
TreeNode the_atom = new TreeNode(20);
//标识符 或 方法调用式 或 数组元素
if (this.getToken().isIdentifier())
{
//记录标识符
Token the_identifier = this.getToken();
this.getNextToken();
//方法调用式
if (this.matchToken("("))
{
//一个方法调用节点
TreeNode the_funct_call = this.funct_call_part(the_identifier);
//作为因子的孩子
the_atom.addChildren(the_funct_call);
//到了下一个
return the_atom;
}
//数组元素
else if (this.matchToken("["))
{
//一个数组元素节点
TreeNode the_array_elem = this.array_elem_part(the_identifier);
//作为因子的孩子
the_atom.addChildren(the_array_elem);
//到了下一个
return the_atom;
}
//单个标识符
else
{ //标识符作为因子的孩子
the_atom.addChildren(the_identifier);
//到了下一个
return the_atom; }
}
//数字
else if (this.getToken().getNumType() > 0)
{
//数字 作为 因子 的孩子
the_atom.addChildren(this.getToken());
//到了下一个
this.getNextToken();
return the_atom;
}
//又遇表达式
else if (this.matchToken("("))
{
this.getNextToken();
//一个表达式节点
TreeNode the_expression2 = this.expression_part();
//匹配右括号 )
if (this.matchToken(")"))
{
//作为 因子 的孩子
the_atom.addChildren(the_expression2);
//到了下一个
this.getNextToken();
return the_atom;
}
else
{
//错误
//记录错误
Token theError = this.getToken();
//跳过错误单词
this.getNextToken();
while (!this.matchToken(";"))
{
this.getNextToken();
}
throw new ComException(theError.getRow(), theError.getColumn(), 8);
}
}
//错误
else
{
//记录错误
Token theError = this.getToken();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -