📄 firstpass.cs
字号:
//一个条件节点
TreeNode the_condition = this.condition_condi_part();
//作为 循环语句 的孩子
the_loop.addChildren(the_condition);
if(this.matchToken(")"))
{
this.getNextToken();
if(this.matchToken("{"))
{
//一个方法体节点
TreeNode the_function_in_while = this.function_in_part();
//作为循环语句的孩子
the_loop.addChildren(the_function_in_while);
//循环语句 作为 方法体内容 的孩子
the_funct_in_cont.addChildren(the_loop);
return the_funct_in_cont;
}
else
{// { 错误
//记录错误
Token theError = this.getToken();
//跳到 } 以后
this.getNextToken();
while (!this.matchToken("}"))
{
this.getNextToken();
}
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 7);
}
}
else
{ // ) 错误
//记录错误
Token theError = this.getToken();
//跳到 } 以后
this.getNextToken();
while (!this.matchToken("}"))
{
this.getNextToken();
}
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 9);
}
}
else
{// ( 错误
//记录错误
Token theError = this.getToken();
//跳到分号以后
this.getNextToken();
while (!this.matchToken("}"))
{
this.getNextToken();
}
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 9);
}
}
//读语句
else if (this.matchToken("read"))
{
//一个读语句节点
TreeNode the_read = new TreeNode(26);
this.getNextToken();
if(this.matchToken("("))
{
this.getNextToken();
if( this.getToken().isIdentifier())
{
//标识符 作为 read 语句 的孩子
the_read.addChildren(this.getToken());
this.getNextToken();
if(this.matchToken(")"))
{
this.getNextToken();
if(this.matchToken(";"))
{
this.getNextToken();
// 读语句 作为 方法体内容 的孩子
the_funct_in_cont.addChildren(the_read);
return the_funct_in_cont;
}
else
{ // ; 错误
//记录错误
Token theError = this.getToken();
//跳到分号以后
this.getNextToken();
while (!this.matchToken(";"))
{
this.getNextToken();
}
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 7);
}
}
else
{// ) 错误
//记录错误
Token theError = this.getToken();
//跳到分号以后
this.getNextToken();
while (!this.matchToken(";"))
{
this.getNextToken();
}
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 7);
}
}
else
{// 非标识符错误
//记录错误
Token theError = this.getToken();
//跳到分号以后
this.getNextToken();
while (!this.matchToken(";"))
{
this.getNextToken();
}
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 7);
}
}
else
{ // ( 错误
//记录错误
Token theError = this.getToken();
//跳到分号以后
this.getNextToken();
while (!this.matchToken(";"))
{
this.getNextToken();
}
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 7);
}
}
//写语句
else if (this.matchToken("write"))
{
//一个写语句节点
TreeNode the_write = new TreeNode(27);
this.getNextToken();
if (this.matchToken("("))
{
this.getNextToken();
//一个表达式节点
TreeNode the_expression = this.expression_part();
//表达式作为写语句的孩子
the_write.addChildren(the_expression);
if(this.matchToken(")"))
{
this.getNextToken();
if(this.matchToken(";"))
{
this.getNextToken();
//写语句 作为 方法体内容 的孩子
the_funct_in_cont.addChildren(the_write);
return the_funct_in_cont;
}
else
{// ; 错误
//记录错误
Token theError = this.getToken();
//跳到分号以后
this.getNextToken();
while (!this.matchToken(";"))
{
this.getNextToken();
}
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 7);
}
}
else
{// ) 错误
//记录错误
Token theError = this.getToken();
//跳到分号以后
this.getNextToken();
while (!this.matchToken(";"))
{
this.getNextToken();
}
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 7);
}
}
else
{ // ( 错误
//记录错误
Token theError = this.getToken();
//跳到分号以后
this.getNextToken();
while (!this.matchToken(";"))
{
this.getNextToken();
}
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 7);
}
}
//空语句
else if (this.matchToken(";"))
{
the_funct_in_cont.addChildren(this.getToken());
this.getNextToken();
return the_funct_in_cont;
}
//返回语句
else if (this.matchToken("return"))
{
//一个返回语句节点
TreeNode the_return = new TreeNode(28);
the_return.addChildren(this.getToken());
this.getNextToken();
if (this.matchToken(";"))
{
this.getNextToken();
the_funct_in_cont.addChildren(the_return);
return the_funct_in_cont;
}
else
{
//一个表达式节点
TreeNode the_expression = this.expression_part();
//作为返回语句的孩子
the_return.addChildren(the_expression);
if (this.matchToken(";"))
{
this.getNextToken();
the_funct_in_cont.addChildren(the_return);
return the_funct_in_cont;
}
else
{// ; 错误
//记录错误
Token theError = this.getToken();
//跳到分号以后
this.getNextToken();
while (!this.matchToken(";"))
{
this.getNextToken();
}
this.getNextToken();
throw new ComException(theError.getRow(), theError.getColumn(), 7);
}
}
}
//其他
else
{
//记录错误
Token theError = this.getToken();
this.getNextToken();
//跳过 } ;
while (!(this.matchToken("}") || this.matchToken(";")))
{
this.getNextToken();
}
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 return_type_part()
{
try
{
//生成一个返回类型节点
TreeNode the_return_type = new TreeNode(12);
//int或int[]
if (this.matchToken("int"))
{
this.getNextToken();
//int[]返回类型
if (this.matchToken("["))
{
this.getNextToken();
//匹配右边 ]
if (this.matchToken("]"))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -