📄 syntax.cpp
字号:
Checkinput(ExpressionFollowSet,NULL,2,0);
}
//匹配标识符Params的子程序
//程序处理的函数声明时的,参数声明部分
//参阅状态转换图(自动机)
void Syntax::Params()
{
int flag;
char str[MAXLEN];
char tokenstr[MAXLEN];
//以下两个变量用在数组处理
char ch[2];
ch[0]='0';ch[1]=0;
int i=0;
flag=Checkinput(ParamsFirstSet,ParamsFollowSet,2,1);
if(flag!=MYOK)
{
if(temptoken.tokentype==RPARSY)
{
strcpy(str,"Params: void");
ptreeview->InsertItem(str);
ptreeview->GetParent();
}
else
MyError(ExceptToken,GetLineNO());
return ;
}
else
{
switch(temptoken.tokentype)
{
case VOIDSY:
strcpy(str,"Params: void");
ptreeview->InsertItem(str);
ptreeview->GetParent();
break;
case INTSY:
strcpy(str,"Params: ");
ptreeview->InsertItem(str);
while(1)
{
Match(INTSY);
strcpy(str,"Type: int");
ptreeview->InsertItem(str);
strcpy(tokenstr,temptoken.pchar);
Match(IDSY);
if(temptoken.tokentype==LMPARSY)
{
strcpy(str,"Array: ");
strcat(str,tokenstr);
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
}
else
{
strcpy(str,"Var: ");
strcat(str,tokenstr);
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
}
while(temptoken.tokentype==LMPARSY)
{
Match(LMPARSY);
i++;
ch[0]=i+'0';
strcpy(str,ch);
strcat(str," Dimension Location: ");
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
Value();
ptreeview->GetParent();
Match(RMPARSY);
}
ptreeview->GetParent(2);
if(temptoken.tokentype==COMMASY)
Match(COMMASY);
else
break;
}
ptreeview->GetParent();
break;
default:
break;
}
}
Checkinput(ParamsFollowSet,NULL,1,0);
}
//匹配标识符Declaration的子程序
//程序处理的全局变量声明,函数声明
//参阅状态转换图(自动机)
void Syntax::Declaration()
{
int flag;
char str[MAXLEN];
char tokenstr[MAXLEN];
//以下两个变量用在数组处理
char ch[2];
ch[0]='0';ch[1]=0;
int i=0;
flag=Checkinput(DeclartionFirstSet,DeclartionFollowSet,2,2);
if(flag!=MYOK)
{
MyError(ExceptToken,GetLineNO());
return ;
}
else
{
if(temptoken.tokentype==INTSY)
{
Match(INTSY);
strcpy(str,"Type: int");
ptreeview->InsertItem(str);
}
else
{
Match(VOIDSY);
strcpy(str,"Type: void");
ptreeview->InsertItem(str);
}
if(temptoken.tokentype==IDSY)
{
strcpy(tokenstr,temptoken.pchar);
Match(IDSY);
}
else if(temptoken.tokentype==MAINSY)
{
strcpy(tokenstr,temptoken.pchar);
Match(MAINSY);
}
if(temptoken.tokentype==LMPARSY)
{
strcpy(str,"ArrayDec: ");
strcat(str,tokenstr);
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
while(temptoken.tokentype==LMPARSY)
{
Match(LMPARSY);
i++;
ch[0]=i+'0';
strcpy(str,ch);
strcat(str," Dimension Size: ");
strcat(str,temptoken.pchar);
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
ptreeview->GetParent();
Match(DIGITSY);
Match(RMPARSY);
}
Match(SEMISY);
}
else if(temptoken.tokentype==LPARSY)
{
strcpy(str,"FunctionDec: ");
strcat(str,tokenstr);
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
Match(LPARSY);
Params();
Match(RPARSY);
if(temptoken.tokentype==SEMISY)
Match(SEMISY);
else
Compound_stmt();
}
else
{
strcpy(str,"VarnDec: ");
strcat(str,tokenstr);
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
}
ptreeview->GetParent(2);
}
Checkinput(DeclartionFollowSet,NULL,2,0);
}
//匹配标识符Statement的子程序
//程序处理的是复合语句体中的基本语句,
//如:IF 语句,WHILE语句,FOR语句,RETURN语句,赋值语句,函数调用语句
//参阅状态转换图(自动机)
void Syntax::Statement()
{
int flag;
char str[MAXLEN];
/*//以下两个变量用在数组处理
char ch[2];
ch[0]='0';ch[1]=0;
int i=0;*/
flag=Checkinput(StatementFirstSet,StatementFollowSet,7,9);
if(flag!=MYOK)
{
MyError(ExceptToken,GetLineNO());
return ;
}
else
{
switch(temptoken.tokentype)
{
case IDSY:
case SEMISY:
Forexpression();
Match(SEMISY);
break;
case IFSY:
Match(IFSY);
strcpy(str,"IF");
ptreeview->InsertItem(str);
Match(LPARSY);
Expression();
Match(RPARSY);
Statement();
if(temptoken.tokentype==ELSESY)
{
strcpy(str,"ELSE");
ptreeview->InsertItem(str);
Match(ELSESY);
Statement();
ptreeview->GetParent();
}
ptreeview->GetParent();
break;
case WHILESY:
Match(WHILESY);
strcpy(str,"WHILE");
ptreeview->InsertItem(str);
Match(LPARSY);
Expression();
Match(RPARSY);
Statement();
ptreeview->GetParent();
break;
case FORSY:
Match(FORSY);
strcpy(str,"FOR");
ptreeview->InsertItem(str);
Match(LPARSY);
Forexpression();
Match(SEMISY);
Expression();
Match(SEMISY);
Forexpression();
Match(RPARSY);
Statement();
ptreeview->GetParent();
break;
case RETURNSY:
Match(RETURNSY);
strcpy(str,"RETURN: ");
ptreeview->InsertItem(str);
if(temptoken.tokentype!=SEMISY)
Expression();
Match(SEMISY);
ptreeview->GetParent();
default:
Compound_stmt();
break;
}
}
Checkinput(StatementFollowSet,NULL,9,0);
}
//匹配标识符Compound_stmt的子程序
//程序处理的是复合语句体,包括局部变量声明,和基本语句处理
//参阅状态转换图(自动机)
void Syntax::Compound_stmt()
{
int flag;
char str[MAXLEN];
char tokenstr[MAXLEN];
//以下两个变量用在数组处理
char ch[2];
ch[0]='0';ch[1]=0;
int i=0;
flag=Checkinput(Compound_stmtFirstSet,Compound_stmtFollowSet,1,11);
if(flag!=MYOK)
{
MyError(ExceptToken,GetLineNO());
return ;
}
else
{
strcpy(str,"Compound stmt:");
ptreeview->InsertItem(str);
Match(LBPARSY);
while(temptoken.tokentype==INTSY)
{
Match(INTSY);
strcpy(str,"Type: int");
ptreeview->InsertItem(str);
strcpy(tokenstr,temptoken.pchar);
Match(IDSY);
if(temptoken.tokentype==LMPARSY)
{
strcpy(str,"ArrayDec: ");
strcat(str,tokenstr);
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
}
else
{
strcpy(str,"VarDec: ");
strcat(str,tokenstr);
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
}
while(temptoken.tokentype==LMPARSY)
{
Match(LMPARSY);
i++;
ch[0]=i+'0';
strcpy(str,ch);
strcat(str," Dimension Size: ");
strcat(str,temptoken.pchar);
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
ptreeview->GetParent();
Match(DIGITSY);
Match(RMPARSY);
}
Match(SEMISY);
ptreeview->GetParent(2);
}
while(temptoken.tokentype==IDSY||temptoken.tokentype==IFSY||
temptoken.tokentype==WHILESY||temptoken.tokentype==FORSY||
temptoken.tokentype==RETURNSY||temptoken.tokentype==LBPARSY||
temptoken.tokentype==SEMISY)
Statement();
Match(RBPARSY);
ptreeview->GetParent();
}
Checkinput(Compound_stmtFollowSet,NULL,11,0);
}
//整个程序对外接口,调用这个子程序分析源代码
void Syntax::Programe()
{
int flag;
GetNextToken(&temptoken);
flag=Checkinput(ProgrameFirstSet,NULL,2,0);
if(flag!=MYOK)
{
MyError(ExceptToken,GetLineNO());
return ;
}
else
{
while(temptoken.tokentype==INTSY||temptoken.tokentype==VOIDSY)
{
Declaration();
}
}
}
//匹配标识符Forexpression的子程序
//程序处理的是基本语句中的赋值语句和函数调用
//因为这个子程序是为处理for语句中的初始化和。故取名Forexpression
//参阅状态转换图(自动机)
void Syntax::Forexpression()
{
int flag;
char str[MAXLEN];
char tokenstr[MAXLEN];
//以下两个变量用在数组处理
char ch[2];
ch[0]='0';ch[1]=0;
int i=0;
flag=Checkinput(ForexpressionFirstSet,ForexpressionFollowSet,2,2);
if(flag!=MYOK)
{
MyError(ExceptToken,GetLineNO());
return ;
}
else
{
switch(temptoken.tokentype)
{
case IDSY:
strcpy(tokenstr,temptoken.pchar);
Match(IDSY);
if(temptoken.tokentype==LPARSY)
{
strcpy(str,"FuctionCall: ");
strcat(str,tokenstr);
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
Match(LPARSY);
if(temptoken.tokentype==LPARSY||temptoken.tokentype==IDSY||
temptoken.tokentype==DIGITSY||temptoken.tokentype==NOTSY)
{
strcpy(str,"Params: ");
ptreeview->InsertItem(str);
}
else
{
Match(RPARSY);
//Match(SEMISY);
ptreeview->GetParent();
return;
}
while(temptoken.tokentype==LPARSY||temptoken.tokentype==IDSY||
temptoken.tokentype==DIGITSY||temptoken.tokentype==NOTSY)
{
Expression();
if(temptoken.tokentype==COMMASY)
Match(COMMASY);
else
break;
}
Match(RPARSY);
// Match(SEMISY);
ptreeview->GetParent();
}
else
{
strcpy(str,"Assign to: ");
ptreeview->InsertItem(str);
if(temptoken.tokentype==LMPARSY)
{
strcpy(str,"Array: ");
strcat(str,tokenstr);
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
while(temptoken.tokentype==LMPARSY)
{
Match(LMPARSY);
i++;
ch[0]=i+'0';
strcpy(str,ch);
strcat(str," Dimension Location: ");
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
Value();
ptreeview->GetParent();
Match(RMPARSY);
}
ptreeview->GetParent();
}
else
{
strcpy(str,"Var: ");
strcat(str,tokenstr);
str[MAXLEN-1]=0;
ptreeview->InsertItem(str);
ptreeview->GetParent();
}
Match(ASSIGNSY);
Expression();
ptreeview->GetParent();
}
break;
case SEMISY:
case RPARSY:
strcpy(str,"Blank line.");
ptreeview->InsertItem(str);
ptreeview->GetParent();
break;
default:
break;
}
}
Checkinput(ForexpressionFollowSet,NULL,2,0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -