📄 parsingtree.h
字号:
#ifndef ParsingTree
#define ParsingTree
//function will be used,,,
char ignorspace(char c)
{
while(isspace(c)) {c=nextchar();}
return c;
}
char tempchar; //check the position of error
char match(char c1)
{
c=nextchar();
c=ignorspace(c);
if(c==c1){
printf("< TERMINAL,%c>\n",c);
// return 1;
}
else {
tempchar=retract(1);
while(isspace(tempchar)){tempchar=retract(1);}
printf("\nError after CHRACTER '%c' !\n\n",tempchar);
// tempchar=nextchar();
exit(0);
}
return c;
}
char checkchar[6]; //check match
int matchend=0;
int matchdo=0;
int matchthen=0;
int matchelse=0;
//construct the parsing tree here...
char list()
{
printf("list->stmt list'\n");
//---------
state=9;
c=nextchar();
c=ignorspace(c);
c=retract(1);
c=nexttoken();
stmt();
c=nextchar();
list1();
// printf("list ......finished!\n");
return c;
}
char list1()
{
c=ignorspace(c);
if(c=='\0')
{
printf("list'->ε\n");
}
else if(c==';')
{
c=retract(1);
printf("list->; stmt list'\n");
match(';');
//------------
state=9;
c=nextchar();
c=ignorspace(c);
if(c=='\0')
{
printf("\n\nYou have printed';' at the end of list,\npls construct another list OR erase ';'\n\n");
exit(0);
}
c=retract(1);
c=nexttoken();
stmt();
c=nextchar();
list1();
}
else if(isalpha(c)&&(matchend>0))
{
c=retract(1);
printf("list'->ε\n");
}
else
//(!isalpha(c)) //if it's not alphabet e.g the character like '!@#$%^'
{
printf("\n\nError CHARACTER '%c' !\n",c);
printf("To recover this mistake..\nYou can match ';' between two CHARACTERS to construct another statement,\nOr erase the space between them!\n");
exit(0);
}
return c;
}
void MatchBeginEnd()
{
matchend++;
// printf("stmt->BEGIN list END\n");
printf("< TERMINAL,%s>\n",alp);//keywords
list();
// printf("prepare matchend is ..%d\n",matchend);
if(matchend!=0) //match end
{
state=9;
c=nexttoken();
c=ignorspace(c);
if(strcmp(alp,"end")==0)
{
printf("< TERMINAL,%s>\n",alp);
matchend--; //match over
}
else //if the character is alphabet but there's space between them (except 'end')
{
printf("\n\nError on Spacekey!\nPls match ';' between two CHARACTERS to construct another statement,\nOr erase the space between them!\n");
exit(0);
}
}
}
void MatchIfThenElse()
{
printf("stmt->IF expr THEN stmt elsestmt\n");
matchthen=1;
printf("< TERMINAL,%s>\n",alp);
c=nextchar();
c=ignorspace(c);
expr(1);
state=9;
c=nextchar();
c=ignorspace(c);
c=retract(1);
c=nexttoken();
if(matchthen!=0)
{
//MatchThen();
if(strcmp(alp,"then")==0)
{
printf("< TERMINAL,%s>\n",alp);
matchthen=0;
matchelse++;
// printf("stmt->IF expr THEN stmt\n\;n")
//-------------
state=9;
c=nextchar();
c=ignorspace(c);
c=retract(1);
c=nexttoken();
stmt();
if(matchelse!=0)
{
c=nextchar();
c=ignorspace(c);
// if(strcmp(alp,"else")==0)
if(c=='e')
{
c=nextchar();
if(c=='l')
{
c=nextchar();
if(c=='s')
{
c=nextchar();
if(c=='e')
{
c=nextchar();
if(isspace(c))
{
//printf("stmt->IF expr THEN stmt ELSE stmt\n\n");
printf("elsestmt->else stmt\n");
printf("< TERMINAL,else>\n");
matchelse--;
//------------
state=9;
c=nextchar();
c=ignorspace(c);
c=retract(1);
c=nexttoken();
stmt();
}
else//not spacekey
{
c=retract(5);
printf("elsestmt->ε\n");
}
}
else// not e
{
c=retract(4);
printf("elsestmt->ε\n");
}
}
else//not s
{
c=retract(3);
printf("elsestmt->ε\n");
}
}
else//not l
{
c=retract(2);
printf("elsestmt->ε\n");
}
}//not e
else
{
c=retract(1);
printf("elsestmt->ε\n");
}
}
/**/
//else no 'else' to match
}
else
{
printf("\n\nError!\nPls match 'then' before '%s%s' to construct another statement,\nOr erase the space between them!\n",alp,digit);
exit(0);
}
}
}
char stmt()
{
if(strcmp(alp,"if")==0) // if expr then stmt (else stmt)
{
MatchIfThenElse();
}
else if(strcmp(alp,"while")==0) //stmt->WHILE expr DO stmt
{
matchdo=1;
printf("stmt->WHILE expr DO stmt\n");
printf("< TERMINAL,%s>\n",alp);
c=nextchar();
c=ignorspace(c);
expr(1);
//match do
state=9;
c=nextchar();
c=ignorspace(c);
c=retract(1);
c=nexttoken();
if(matchdo)
{
if(strcmp(alp,"do")==0)
{
printf("< TERMINAL,%s>\n",alp);
matchdo=0;// match over ...close..
state=9;
c=nextchar();
c=ignorspace(c);
c=retract(1);
c=nexttoken();
stmt();
}
else
{
printf("\n\nError!\nPls match 'do' before '%s%s' to construct another statement,\nOr erase the space between them!\n",alp,digit);
exit(0);
}
}
}
else if(strcmp(alp,"begin")==0) //stmt->BEGIN list END
{
MatchBeginEnd();
}
else if(isk==1) // stmt->ID:=expr
{
// c5=0;
printf("stmt->ID:=expr\n");
// printf("stmt->ID:=expr%d\n",etimes);
printf("< ID,%s>\n",alp);
match(':');
match('=');
c=nextchar();
expr(1);
}
return c;
}
char expr(int e)
{
if(e){
// printf("expr%d->term%d expr%d\n",etimes,ttimes,etimes+1);
printf("expr->term expr'\n");
term(1);
c=nextchar();
expr1();
}
return c;
}
char expr1()
{
c=ignorspace(c);
if(c=='+') {
printf("expr'->+ term expr'\n");
c=retract(1);
match('+');
c=nextchar();
term(1);
c=nextchar();
expr1();
}
else
{
printf("expr'->ε\n");
c=retract(1);
}
return c;
}
char term(int t)
{
if(t)
{
printf("term->factor term'\n");
// printf("term%d->factor term%d\n",ttimes,ttimes+1);
factor();
c=nextchar();
term1();
}
return c;
}
char term1()
{
c=ignorspace(c);
if(c=='*')
{
printf("term'->* factor term'\n");
c=retract(1);
match('*');
c=nextchar();
factor();
c=nextchar();
term1();
}
else //not '*' ,consider as ε,and just skip it
{
printf("term'->ε\n");
c=retract(1);
}
return c;
}
char factor()
{
c=ignorspace(c);
//while(isspace(c)) {c=nextchar();}
if(isdigit(c)){
printf("factor->NUM\n");
c=retract(1);
state=25; //12 20 25
c=nexttoken();
printf("< NUM,%s>\n",digit);
}
else if(isalpha(c)) //state 9 to 11
{
printf("factor->ID\n");
c=retract(1);
state=9;
c=nexttoken();
if(isk==1)
{
printf("< ID,%s>\n",alp); //check ...here............
}
else
{
printf("\n'%s' is keyword or illegal!\n",alp);
exit(0);
}
}
else if(c=='(')
{
printf("factor->( expr )\n");
c=retract(1);
match('(');
c=nextchar();
expr(1);
c=nextchar();
c=retract(1);
match(')');
}
else //not '(' ,alphabet , digit
{
printf("\nError on evaluation!\nPls use alphabet as ID ,digit as NUM,\nOr MATCHED PARENTHESIS to construct some expressions\n"); //evaluation wrong
exit(0);
}
return c;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -