云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 毕业设计编译原理设计报告c语言词法与语法分析器的实现

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

  • 62 次阅读
  • 3 次下载
  • 2025/6/4 7:15:40

语法分析子流程图:

3.程序代码实现

整个词法以及语法的程序设计在一个工程里面,一共包含了8个文件,分别为main.cpp、parse.cpp、scan.cpp、util.cpp、scan.h、util.h、 globals.h、parse.h,其中scan.cpp和scan.h为词法分析程序。

以下仅列出各个文件中的核心代码:

Main.cpp

#include \

#define NO_PARSE FALSE #include \#if NO_PARSE #include \#else

#include \#endif

int lineno=0; FILE * source; FILE * listing; FILE * code;

int EchoSource = TRUE;

int TraceScan=TRUE; int TraceParse=TRUE; int Error = FALSE;

int main(int argc,char * argv[]) {

TreeNode * syntaxTree; char pgm[120]; scanf(\ source=fopen(pgm,\ if(source==NULL)

{

fprintf(stderr,\exit(1);

} listing = stdout; fprintf(listing,\TION: %s\\n\#if NO_PARSE while(getToken()!=ENDFILE); #else

syntaxTree = parse(); if(TraceParse){ fprintf(listing,\ printTree(syntaxTree); } #endif fclose(source); return 0; }

Parse.cpp

#include \#include \#include \#include \

static TokenType token; /* holds current token */

/* function prototypes for recursive calls */ static TreeNode * declaration_list(void); static TreeNode * declaration(void); static TreeNode * params(void); static TreeNode * param_list(void); static TreeNode * param(void);

static TreeNode * compound_stmt(void); static TreeNode * local_declarations(void); static TreeNode * statement_list(void); static TreeNode * statement(void);

static TreeNode * expression_stmt(void); static TreeNode * if_stmt(void); static TreeNode * while_stmt(void); static TreeNode * return_stmt(void); static TreeNode * expression(void); static TreeNode * var(void);

static TreeNode * simple_exp(void);

static TreeNode * additive_expression(void); static TreeNode * term(void); static TreeNode * factor(void);

static TreeNode * args(void); static TreeNode * arg_list(void);

static void syntaxError(char * message) { fprintf(listing,\

fprintf(listing,\Error = TRUE; }

/*判断读取的字符*/

static void match(TokenType expected) {

if(token==expected) {

token=getToken( ); }

else {

syntaxError(\

printToken(token,tokenString);

fprintf(listing,\ \ } }

/*进行语法分析,构建语法树*/ TreeNode * declaration_list(void) { TreeNode * t= declaration(); TreeNode * p= t; while ((token==INT) || (token==VOID) ) {

TreeNode *q = declaration(); if (q!=NULL) { if (t==NULL) t = p = q; }

else /* now p cannot be NULL either */ { p->sibling = q; p = q; }

} return t; }

TreeNode * declaration(void) { TreeNode * t = NULL; switch (token) {

case VOID : case INT :

t = newStmtNode(DecK); if(token == INT) t->type =Integer; else

t->type = Void; match(token); switch (token) {

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

语法分析子流程图: 3.程序代码实现 整个词法以及语法的程序设计在一个工程里面,一共包含了8个文件,分别为main.cpp、parse.cpp、scan.cpp、util.cpp、scan.h、util.h、 globals.h、parse.h,其中scan.cpp和scan.h为词法分析程序。 以下仅列出各个文件中的核心代码: Main.cpp #include \#define NO_PARSE FALSE #include \#if NO_PARSE #include \#else #include \#endif int lineno=0; FILE * source; FILE * listing; FILE *

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com