当前位置:文档之家› 中小企业示范基地申请报告.

中小企业示范基地申请报告.

5 实验结果与分析测试文本test.cint a[10];int min(int a[],int low,void a {int k;int x;int i;k=low;while(i<low{a[0]=1;if(k>0{x=1;}return x;}测试结果成功实现语法分析6 小结通过这次实验,我加深了对语法分析的认识,掌握了递归向下分析方法,实现了对词法分析程序所提供的Token序列的语法检查和结构分析。

语法分析程序编写相对于词法分析要困难得多,首先要将BNF化为EBNF,运用递归向下的方法进行编写,构造出语法树,判别语法分析过程中是否出错以及出错位置和错误类型。

虽然EBNF 转换成代码的过程原理比较简单,但是操作起来比较繁琐。

一开始我对TreeNode数据结构也不是很理解,通过阅读书后的tiny语言语法分析源代码,我弄懂了语法树的输出。

附录(源代码)Main.c#include "global..= TRUE;int Error = FALSE;int main(void{TreeNode * syntaxTree;char pgm[120];*用于存储文件名*printf("输入文件名:";scanf("%s",pgm;if(strchr (pgm, '.' ==NULLstrcat(pgm,".c";}source = fopen(pgm,"r";if(source==NULL{fprintf(stderr,"File %s not found\n",pgm; exit(1;}listing = stdout;* listing在屏幕上输出 * fprintf(listing,"\nC COMPILATION: %s\n",pgm; while (getToken(!=ENDFILE{;}syntaxTree = parse(;printTree(syntaxTree;fclose(source;return 0;}Parse..;* *TreeNode * parse(void;TreeNode * declaration_list(void;TreeNode * declaration(void;TreeNode * params(void;TreeNode * param_list(TreeNode * k;TreeNode * param(TreeNode * k;TreeNode * compound_stmt(void;TreeNode * local_declaration(void;TreeNode * statement_list(void;TreeNode * statement(void;TreeNode * expression_stmt(void;TreeNode * selection_stmt(void;TreeNode * iteration_stmt(void;TreeNode * return_stmt(void;TreeNode * expression(void;TreeNode * var(void;TreeNode * simple_expression(TreeNode * k;TreeNode * additive_expression(TreeNode * k;TreeNode * term(TreeNode * k;TreeNode * factor(TreeNode * k;TreeNode * call(TreeNode * k;TreeNode * args(void;static void syntaxError(char* message{fprintf(listing,"\n>>> ";fprintf(listing,"Syntax error at line %d: %s",lineno,message; Error = TRUE;static void match(TokenType expected {if(token == expectedtoken = getToken(;else{syntaxError("unexpected token -> "; printToken(token,tokenString;fprintf(listing," ";}}TreeNode * parse(void{TreeNode * t;token = getToken(;t = declaration_list(;if(token!=ENDFILE{syntaxError("endfile_error";}return t;}TreeNode * declaration_list(voidTreeNode * t = declaration(;TreeNode * p =t;程序以变量声明开始while((token!=INT&&(token!=VOID&&(token!=ENDFILE {syntaxError("开始不是类型声明";token = getToken(;if(token==ENDFILEbreak;}while(token==INT||token==VOID{TreeNode * q;q = declaration(;if(q!=NULL{if(t==NULL{t=p=q;}else{p->sibling=q;}}}match(ENDFILE;return t;}TreeNode * declaration(void {TreeNode * t =NULL; TreeNode * p =NULL; TreeNode * q =NULL; TreeNode * s =NULL; TreeNode * a =NULL;if(token==INT{p=newNode(IntK;match(INT;}else if(token==VOID{p=newNode(VoidK;match(VOID;}{syntaxError("类型错误";}if(p!=NULL&&token==ID{q = newNode(IdK;q-> = copyString(tokenString; match(ID;if(token==LPAREN{t = newNode(FunK;t->child[0]= p;p是t子节点t->child[1]= q;match(LPAREN;t->child[2]= params(;match(RPAREN;t->child[3]= compound_stmt(;}else if(token==LBRACKET{t = newNode(Var_DeclK;a = newNode(Arry_DeclK;t->child[0]= p;p是t子节点t->child[1]= a;match(LBRACKET;s = newNode(ConstK;s->attr.val = atoi(tokenString; match(NUM;a->child[0]=q;a->child[1]=s;match(RBRACKET;match(SEMI;}else if(token==SEMI{t = newNode(Var_DeclK;t->child[0]= p;t->child[1]= q;match(SEMI;}else{syntaxError("";}}else{syntaxError("";}return t;}TreeNode * params(void{TreeNode * t = newNode(ParamsK; TreeNode * p =NULL;if(token==VOID{p = newNode(VoidK;match(VOID;if(token==RPAREN{if(t!=NULLt->child[0]= p;}else参数列表为(void id,[……]{t->child[0] = param_list(p;申}{{TreeNode * tmatch(COMMA;增强企业活力和市场竞争能力的优质项目。

我公司积极响应市委、市府提出的“三名城一基地”的建设,坚持走“绿色崛起”之路,在县委、县府积极有力的支持下(贵公司所在地政府的一些口号),于X 年月成立公司,按“公司+基地+农户司合作高校、研究所等名称)p=q;Xp->sibling = q5p = q;}公司}。

相关主题