C-Minus词法分析和语法分析设计编译器编译原理课程设计 下载本文

}

else if(currentToken.tokenType==VOID) { } else { }

if((p!=NULL)&&(currentToken.tokenType==ID)) {

q = newNode(IdK);

q->attr.name = currentToken.tokenString.c_str(); match(ID);

if(currentToken.tokenType==LPAREN)//'(':函数情况 { }

else if(currentToken.tokenType==LMBRACKET)//'[':数组声明 {

t = newNode(Var_DeclK);

TreeNode * m = newNode(Arry_DeclK);

match(LMBRACKET); match(NUM); t = newNode(FunK); t->child[0] = p; t->child[1] = q; match(LPAREN); t->child[2] = params(); match(RPAREN);

t->child[3] = compound_stmt(); syntaxError(\类型匹配错误\p = newNode(VoidK); match(VOID);

} else { } }

}

s = newNode(ConstK);

s->attr.val = atoi(lastToken.tokenString.c_str()); m->child[0] = q; m->child[1] = s; t->child[0] = p; t->child[1] = m; match(RMBRACKET); match(SEMI);

else if(currentToken.tokenType==SEMI)//';'结尾:普通变量声明 { } else { }

syntaxError(\t = newNode(Var_DeclK); t->child[0] = p; t->child[1] = q; match(SEMI);

syntaxError(\

return t;

TreeNode * Parser :: params(void) {

TreeNode * t = newNode(ParamsK); TreeNode * p = NULL;

if(currentToken.tokenType==VOID)//开头为void,参数列表可能是(void)和(void id,[……])两种情况

{ }

else if(currentToken.tokenType==INT)//参数列表为(int id,[……]) { } else { } }

TreeNode * Parser :: param_list(TreeNode * k)//k可能是已经被取出来的VoidK,但又不是(void)类型的参数列表,所以一直传到param中去,作为其一个子节点 {

TreeNode * t = param(k); TreeNode * p = t;

k = NULL;//没有要传给param的VoidK,所以将k设为NULL while(currentToken.tokenType==COMMA) {

TreeNode * q = NULL; match(COMMA); syntaxError(\

t->child[0] = param_list(p); p = newNode(VoidK); match(VOID);

if(currentToken.tokenType==RPAREN)//参数列表为(void) { }

else//参数列表为(void id,[……]) ->void类型的变量 { }

t->child[0] = param_list(p); if(t!=NULL)

t->child[0] = p;

return t;

}

q = param(k); if(q!=NULL) { }

if(t==NULL) { } else { }

p->sibling = q; p = q; t = p = q;

return t; }

TreeNode * Parser :: param(TreeNode * k) {

TreeNode * t = newNode(ParamK);

TreeNode * p = NULL;//ParamK的第一个子节点 TreeNode * q = NULL;//ParamK的第二个子节点

if(k==NULL&¤tToken.tokenType==INT) { }

else if(k!=NULL) { }

if(p!=NULL) {

p = k;

p = newNode(IntK); match(INT);