第一范文网 - 专业文章范例文档资料分享平台

毕业设计编译原理设计报告c语言词法与语法分析器的实现

来源:用户分享 时间:2025/8/17 13:21:51 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

t = expression();

match(SEMI); } return t; }

TreeNode * if_stmt(void) { TreeNode * t = newStmtNode(IfK); if(t!=NULL) {

match(IF);

match(LPAREN);

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

t->child[1] = statement(); if (token==ELSE) { match(ELSE);

if (t!=NULL) t->child[2] = newStmtNode(ElseK);

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

TreeNode * while_stmt(void) { TreeNode * t = newStmtNode(WhileK); match(WHILE);

match(LPAREN);

if (t!=NULL) t->child[0] = expression(); match(RPAREN);

if (t!=NULL) t->child[1] = statement(); return t;

}

TreeNode * return_stmt(void)

{ }

TreeNode * t = newStmtNode(RetK); if(token == RETURN) match(RETURN); if(token == SEMI) match(SEMI); else {

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

TreeNode * expression(void) { TreeNode * t = simple_exp(); return t; }

TreeNode* var(void) { TreeNode* t = newExpNode(IdK); if ((t!=NULL) && (token==ID)) t->attr.name = copyString(tokenString); match(ID); if(token == LZKH) { match(token); t->type = ArrayUnit; }

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

return t;

TreeNode * simple_exp(void) {

TreeNode * t = additive_expression();

if(t!=NULL){

if (token == LT || token == LE|| token == MT || token == ME||token ==EQ||token ==NEQ) { TreeNode * p = newExpNode(OpK); if(p!=NULL) { p->attr.op = token; p->child[0] = t; match(token); p->child[1] = additive_expression();

}

}

t=p;

} return t; }

TreeNode* additive_expression(void) {

TreeNode * t = term();

while(token == PLUS || token == MINUS) {

TreeNode * p = newExpNode(OpK); p->attr.op = token; p->child[0] = t; match(token);

p->child[1] = term(); t = p; }

return t; }

TreeNode * 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;

match(token); p->child[1] = factor(); t = p; } }

return t;

}

TreeNode * 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 = var();

if (token == ASSIGN) { TreeNode* p = newStmtNode(AssignK); p->attr.name = t->attr.name;

match(token); p->child[0] = expression(); t = p; }

if (token == LPAREN ) {

TreeNode * p = newStmtNode(CallK); p->attr.name = t->attr.name;

毕业设计编译原理设计报告c语言词法与语法分析器的实现.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c58yt92166g565jb3urou8mpoj7ocb000zp9_4.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top