C-Minus词法分析和语法分析设计编译器编译原理课程设计 联系客服

发布时间 : 星期二 文章C-Minus词法分析和语法分析设计编译器编译原理课程设计更新完毕开始阅读4fc4cd488bd63186bcebbc8a

}

t = selection_stmt(); break;

t = iteration_stmt(); break;

t = return_stmt(); break;

t = compound_stmt(); break;

t = expression_stmt(); break; syntaxError(\

currentToken = getToken(); break;

case WHILE:

case RETURN:

case LBBRACKET:

case ID: case SEMI: case LPAREN: case NUM:

default:

return t; }

TreeNode * Parser :: selection_stmt(void) {

TreeNode * t = newNode(Selection_StmtK); match(IF); match(LPAREN); if(t!=NULL) { }

match(RPAREN); t->child[1] = statement();

if(currentToken.tokenType==ELSE)

t->child[0] = expression();

{ } return t; }

TreeNode * Parser :: expression_stmt(void) {

TreeNode * t = NULL;

if(currentToken.tokenType==SEMI) { } else { } return t; }

TreeNode * Parser :: iteration_stmt(void) {

TreeNode * t = newNode(Iteration_StmtK); match(WHILE); match(LPAREN); if(t!=NULL) {

t->child[0] = expression(); t = expression(); match(SEMI); match(SEMI); return t; match(ELSE); if(t!=NULL) { }

t->child[2] = statement();

}

match(RPAREN); if(t!=NULL) { } return t; }

TreeNode * Parser :: return_stmt(void) {

TreeNode * t = newNode(Return_StmtK); match(RETURN);

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

match(SEMI); return t; }

TreeNode * Parser :: expression(void) {

TreeNode * t = var();

if(t==NULL)//不是以ID开头,只能是simple_expression情况

if(t!=NULL) { }

t->child[0] = expression(); match(SEMI); return t;

t->child[1] = statement();

{ t = simple_expression(t);

}

else//以ID开头,可能是赋值语句,或simple_expression中的var和call类型的情况 { TreeNode * p = NULL;

if(currentToken.tokenType==ASSIGN)//赋值语句 { p = newNode(AssignK);

p->attr.name = lastToken.tokenString.c_str(); match(ASSIGN); p->child[0] = t;

p->child[1] = expression(); return p;

}

else //simple_expression中的var和call类型的情况 { t = simple_expression(t); } }

return t;

}

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

TreeNode * t = additive_expression(k); k = NULL;

if(EQ==currentToken.tokenType || GT==currentToken.tokenType GEQ==currentToken.tokenType ||

LT==currentToken.tokenType

LEQ==currentToken.tokenType || NEQ==currentToken.tokenType)

{ TreeNode * q = newNode(OpK); q->attr.op = currentToken.tokenType;

q->child[0] = t;

|| ||