📄 parse.h
字号:
class Parse:public Scan, public TreeNode
{
private:
TokenType token;
TreeNode * stmt_sequence(void);
TreeNode * statement(void);
TreeNode * if_stmt(void);
TreeNode * repeat_stmt(void);
TreeNode * assign_stmt(void);
TreeNode * read_stmt(void);
TreeNode * write_stmt(void);
TreeNode * exp(void);
TreeNode * simple_exp(void);
TreeNode * term(void);
TreeNode * factor(void);
void SyntaxError(char * message);
void match(TokenType expected);
public:
Parse(char *);
TreeNode * parse();
};
Parse::Parse(char* name):Scan(name){};
void Parse::SyntaxError(char * message)
{
cout<<"\nSyntax error at line "<<lineno<<" "<<message<<endl;
Error = 1;
}
void Parse::match(TokenType expected)
{
if (token == expected) token = getToken();
else
{
SyntaxError("unexpected token -> ");
printToken(token,tokenString);
cout<<" ";
}
}
TreeNode * Parse::stmt_sequence(void)
{
TreeNode * t = statement();
TreeNode * p = t;
while ((token!=ENDFILE) && (token!=END) && (token!=ELSE) && (token!=UNTIL))
{
TreeNode * q;
match(SEMI);
q = statement();
if (q!=NULL)
{
if (t==NULL) t = p = q;
else
{
p->sibling = q;
p = q;
}
}
}
return t;
}
TreeNode * Parse::statement(void)
{
TreeNode * t = NULL;
switch (token)
{
case IF : t = if_stmt(); break;
case REPEAT : t = repeat_stmt(); break;
case ID : t = assign_stmt(); break;
case READ : t = read_stmt(); break;
case WRITE : t = write_stmt(); break;
default :
SyntaxError("unexpected token -> ");
printToken(token,tokenString);
token = getToken();
break;
}
return t;
}
TreeNode * Parse::if_stmt(void)
{
TreeNode * t = NewStmtNode(IfK);
match(IF);
if (t!=NULL) t->child[0] = exp();
match(THEN);
if (t!=NULL) t->child[1] = stmt_sequence();
if (token==ELSE)
{
match(ELSE);
if (t!=NULL) t->child[2] = stmt_sequence();
}
match(END);
return t;
}
TreeNode * Parse::repeat_stmt(void)
{
TreeNode * t = NewStmtNode(RepeatK);
match(REPEAT);
if (t!=NULL) t->child[0] = stmt_sequence();
match(UNTIL);
if (t!=NULL) t->child[1] = exp();
return t;
}
TreeNode * Parse::assign_stmt(void)
{
TreeNode * t = NewStmtNode(AssignK);
if((t != NULL) && (token == ID) )
t->attr.name = CopyString(tokenString);
match(ID);
match(ASSIGN);
if(t != NULL)
t->child[0] = exp();
return t;
}
TreeNode * Parse::read_stmt(void)
{
TreeNode * t = NewStmtNode(ReadK);
match(READ);
if ((t!=NULL) && (token==ID))
t->attr.name = CopyString(tokenString);
match(ID);
return t;
}
TreeNode * Parse::write_stmt(void)
{
TreeNode * t = NewStmtNode(WriteK);
match(WRITE);
if (t!=NULL) t->child[0] = exp();
return t;
}
TreeNode* Parse::exp(void)
{
TreeNode * t = simple_exp();
if ((token==LT)||(token==EQ))
{
TreeNode * p = NewExpNode(OpK);
if (p!=NULL)
{
p->child[0] = t;
p->attr.op = token;
t = p;
}
match(token);
if (t!=NULL)
t->child[1] = simple_exp();
}
return t;
}
TreeNode * Parse::simple_exp(void)
{
TreeNode * t = term();
while ((token==PLUS)||(token==MINUS))
{
TreeNode * p = NewExpNode(OpK);
if (p!=NULL)
{
p->child[0] = t;
p->attr.op = token;
t = p;
match(token);
t->child[1] = term();
}
}
return t;
}
TreeNode * Parse::term(void)
{
TreeNode * t = factor();
while ((token==TIMES)||(token==OVER))
{
TreeNode * p = NewExpNode(OpK);
if (p!=NULL)
{
p->child[0] = t;
p->attr.op = token;
t = p;
match(token);
p->child[1] = factor();
}
}
return t;
}
TreeNode * Parse::factor(void)
{
TreeNode * t = NULL;
switch (token)
{
case NUM :
t = NewExpNode(ConstK);
if ((t!=NULL) && (token==NUM))
t->attr.val = atoi(tokenString);
match(NUM);
break;
case ID :
t = NewExpNode(IdK);
if ((t!=NULL) && (token==ID))
t->attr.name = CopyString(tokenString);
match(ID);
break;
case LPAREN :
match(LPAREN);
t = exp();
match(RPAREN);
break;
default:
SyntaxError("unexpected token -> ");
printToken(token,tokenString);
token = getToken();
break;
}
return t;
}
TreeNode * Parse::parse(void)
{
TreeNode * t;
token = getToken();
t = stmt_sequence();
printTree(t);
if (token!=ENDFILE)
SyntaxError("Code ends before file\n");
cout<<"\n\n";
return t;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -