收藏 分享(赏)

四川大学 C minus语法分析器 纯代码.doc

上传人:精品资料 文档编号:8228316 上传时间:2019-06-15 格式:DOC 页数:26 大小:37.45KB
下载 相关 举报
四川大学 C minus语法分析器 纯代码.doc_第1页
第1页 / 共26页
四川大学 C minus语法分析器 纯代码.doc_第2页
第2页 / 共26页
四川大学 C minus语法分析器 纯代码.doc_第3页
第3页 / 共26页
四川大学 C minus语法分析器 纯代码.doc_第4页
第4页 / 共26页
四川大学 C minus语法分析器 纯代码.doc_第5页
第5页 / 共26页
点击查看更多>>
资源描述

1、#include#include#include#includeusing namespace std;#define BUFLEN 256#define MAXLEN 256#define MAXTOKENLEN 40#define MAXCHILDREN 4static int lineno;static int linepos = 0;/读取的字符在 lineBuf 的位置static int EOF_FLAG = false;static int bufsize = 0;/lineBuf 的长度static char lineBufBUFLEN;FILE * source;char t

2、okenStringMAXTOKENLEN+1;string output;/输出文件enum TokenTypeENDFILE,ERROR,IF,ELSE,INT,RETURN,VOID,WHILE,ID,NUM,ASSIGN,EQ,LT,PLUS,MINUS,TIMES,OVER,LPAREN,RPAREN,SEMI,LBRACKET,RBRACKET,LBRACE,RBRACE,COMMA,GT,GEQ,NEQ,LEQ;enum StateTypeSTART,INASSIGN,INCOMMENT,INNUM,INID,DONE,PRECOMMENT,AFTERCOMMENT;struct

3、char* str;TokenType tok;ReserverWords6= “if“,IF,“else“,ELSE,“int“,INT,“return“,RETURN,“void“,VOID,“while“,WHILE ;void UnGetNextChar()if (!EOF_FLAG)linepos-;int GetNextChar()if(!(linepos)|(c=)|(c=!)state = INASSIGN;assign+=char(c);else if (c = ) | (c = t) | (c = n)save = false;else if (c = /)save = f

4、alse;state = PRECOMMENT;elsestate = DONE;switch (c)case EOF:save = false;CurrentToken = ENDFILE;break;case +:CurrentToken = PLUS;break;case -:CurrentToken = MINUS;break;case *:CurrentToken = TIMES;break;case (:CurrentToken = LPAREN;break;case ):CurrentToken = RPAREN;break;case ;:CurrentToken = SEMI;

5、break;case :CurrentToken = LBRACKET;break;case :CurrentToken = RBRACKET;break;case :CurrentToken = LBRACE;break;case :CurrentToken = RBRACE;break;case ,:CurrentToken = COMMA;break;default:CurrentToken = ERROR;break;break;case INCOMMENT:save = false;if (c = EOF)state = DONE;CurrentToken = ENDFILE;els

6、e if (c = *)state = AFTERCOMMENT;elsestate=INCOMMENT;break;case INASSIGN:if (c = =)CurrentToken = EQ;state=DONE;else if(assign=“!“)UnGetNextChar();save = false;CurrentToken = ERROR;state=DONE;else if(assign=“=“)UnGetNextChar();save = false;CurrentToken =ASSIGN ;state=DONE;else if(assign=“,GT,“=“,GEQ

7、,“!=“,NEQ,“childi = NULL;t-sibling = NULL;t-nodekind = kind;t-lineno = lineno;if (kind=OpK|kind=IntK|kind=IdK)if(kind=IdK)t-attr.name = “;if(kind=ConstK)t-attr.val = 0;return t;TreeNode * declaration_list(void)/declaration_list-declaration_list declaration | declarationTreeNode * t = declaration();T

8、reeNode * p =t;while(token!=INT)if(token=ENDFILE)break;while(token=INT|token=VOID)TreeNode * q;q = declaration();if (q!=NULL)if (t=NULL)t=p=q;elsep-sibling=q;p=q;match(ENDFILE);return t;TreeNode * declaration(void)TreeNode * t = NULL;TreeNode * p = NULL;TreeNode * q = NULL;TreeNode * s = NULL;TreeNo

9、de * a = NULL;if (token=INT)p=newNode(IntK);match(INT);else/(token=VOID)p=newNode(VoidK);match(VOID);if(p!=NULLq-attr.name = copyString(tokenString); match(ID);if (token=LPAREN)t = newNode(FuncK);t-child0 = p; /p 是 t 子节点t-child1 = q;match(LPAREN);t-child2 = params();match(RPAREN);t-child3 = compound

10、_stmt();else if (token=LBRACKET)t = newNode(Var_DeclK);a = newNode(Arry_DeclK);t-child0 = p; /p 是 t 子节点t-child1 = a;match(LBRACKET);s = newNode(ConstK);s-attr.val = atoi(tokenString);match(NUM);a-child0=q;a-child1=s;match(RBRACKET);match(SEMI);else if (token=SEMI)t = newNode(Var_DeclK);t-child0 = p;

11、t-child1 = q;match(SEMI);else;else;return t;TreeNode * params(void)/params_list | voidTreeNode * t = newNode(ParamsK);TreeNode * p = NULL;if (token=VOID)p = newNode(VoidK);match(VOID);if (token=RPAREN)if(t!=NULL)t-child0 = p;else/参数列表为(void id,) t-child0 = param_list(p);else/(token=INT)t-child0 = pa

12、ram_list(p);return t;TreeNode * param_list(TreeNode * k)/params_list-params_list,param | paramTreeNode * t = param(k);TreeNode * p = t; k = NULL;/没有要传给 param 的 VoidK,所以将 k 设为 NULLwhile (token=COMMA)TreeNode * q = NULL;match(COMMA);q = param(k);if (q!=NULL)if (t=NULL)t=p=q;elsep-sibling = q;p = q;ret

13、urn t;TreeNode * param(TreeNode * k)TreeNode * t = newNode(ParamK);TreeNode * p = NULL;TreeNode * q = NULL;if(k=NULLmatch(VOID);else if(k=NULLmatch(INT);else if (k!=NULL)p = k;if(p!=NULL)t-child0 = p;if (token=ID)q = newNode(IdK);q-attr.name = copyString(tokenString);t-child1 = q;match(ID);if (token

14、=LBRACKETt-child2 = newNode(IdK);match(RBRACKET);else return t; else ;return t;TreeNode * compound_stmt(void) TreeNode * t = newNode(CompK);match(LBRACE);t-child0 = local_declaration();t-child1 = statement_list(); match(RBRACE);return t;TreeNode * local_declaration(void) TreeNode * t = NULL;TreeNode

15、 * q = NULL;TreeNode * p = NULL;while(token=INT | token=VOID) p = newNode(Var_DeclK);if(token=INT)TreeNode * q1 = newNode(IntK);p-child0 = q1;match(INT);else if(token=VOID)TreeNode * q1 = newNode(VoidK);p-child0 = q1;match(INT);if(p!=NULL) q2-attr.name = copyString(tokenString);p-child1 = q2;match(I

16、D);if(token=LBRACKET) TreeNode * q3 = newNode(Var_DeclK);p-child3 = q3;match(LBRACKET);match(RBRACKET);match(SEMI);else if(token=SEMI)match(SEMI);elsematch(SEMI); if(p!=NULL)if(t=NULL)t = q = p;elseq-sibling = p;q = p;return t;TreeNode * statement_list(void) TreeNode * t = statement(); TreeNode * p

17、= t;while (IF=token | LBRACKET=token | ID=token | WHILE=token |RETURN =token | SEMI=token | LPAREN=token | NUM=token) TreeNode * q;q = statement();if(q!=NULL)if(t=NULL) t = p = q;else p-sibling = q;p = q;return t;TreeNode * statement(void) TreeNode * t = NULL;switch(token)case IF:t = selection_stmt(

18、); break;case WHILE:t = iteration_stmt(); break;case RETURN:t = return_stmt(); break;case LBRACE:t = compound_stmt(); break;case ID: case SEMI: case LPAREN: case NUM: t = expression_stmt(); break;default:token = GetToken();break;return t;TreeNode * selection_stmt(void) TreeNode * t = newNode(Selecti

19、on_StmtK);match(IF);match(LPAREN);if(t!=NULL) t-child0 = expression();match(RPAREN);t-child1 = statement();if(token=ELSE) match(ELSE);if(t!=NULL) t-child2 = statement();return t;TreeNode * iteration_stmt(void) TreeNode * t = newNode(Iteration_StmtK);match(WHILE);match(LPAREN);if(t!=NULL) t-child0 =

20、expression();match(RPAREN);if(t!=NULL) t-child1 = statement();return t;TreeNode * return_stmt(void) TreeNode * t = newNode(Return_StmtK);match(RETURN);if (token=SEMI) match(SEMI);return t;else if(t!=NULL) t-child0 = expression(); match(SEMI);return t;TreeNode * expression_stmt(void) TreeNode * t = N

21、ULL;if(token=SEMI) match(SEMI);return t;else t = expression();match(SEMI);return t;TreeNode * expression(void) TreeNode * t = var();if(t=NULL)/不是以 ID 开头,只能是 simple_expression 情况 t = simple_expression(t); else/以 ID 开头,可能是赋值语句,或 simple_expression 中的 var 和 call 类型的情况 TreeNode * p = NULL;if(token=ASSIGN

22、)/赋值语句 p = newNode(AssignK);match(ASSIGN);p-child0 = t;p-child1 = expression();return p;else /simple_expression 中的 var 和 call 类型的情况 t = simple_expression(t); return t;TreeNode * var(void) TreeNode * t = NULL;TreeNode * p = NULL;TreeNode * q = NULL;if(token=ID)p = newNode(IdK);p-attr.name = copyStrin

23、g(tokenString); match(ID);if(token=LBRACKET) match(LBRACKET);q = expression();match(RBRACKET);t = newNode(Arry_ElemK);t-child0 = p;t-child1 = q;elset = p;return t;TreeNode * simple_expression(TreeNode * k) TreeNode * t = additive_expression(k);k = NULL;if(EQ=token | GT=token | GEQ=token | LT=token |

24、 LEQ=token | NEQ=token) TreeNode * q = newNode(OpK);q-attr.op = token; q-child0 = t;t = q;match(token);t-child1 = additive_expression(k); return t;return t;TreeNode * additive_expression(TreeNode * k) TreeNode * t = term(k);k = NULL;while(token=PLUS)|(token=MINUS) TreeNode * q = newNode(OpK);q-attr.

25、op = token; q-child0 = t; match(token);q-child1 = term(k);t = q;return t;TreeNode * term(TreeNode * k) TreeNode * t = factor(k);k = NULL;while(token=TIMES)|(token=OVER) TreeNode * q = newNode(OpK);q-attr.op = token; q-child0 = t; t = q;match(token);q-child1 = factor(k);return t;TreeNode * factor(Tre

26、eNode * k) TreeNode * t = NULL;if(k!=NULL)/k 为上面传下来的已经解析出来的以 ID 开头的 var,可能为 call 或varif(token=LPAREN else t = k; else/没有从上面传下来的 var switch(token)case LPAREN:match(LPAREN);t = expression();match(RPAREN);break;case ID:k = var();if(LPAREN=token else/如果是连续计算,进入这一步t=k;break;case NUM:t = newNode(ConstK);i

27、f(t!=NULL) match(NUM);break;default:token = GetToken();break; return t;TreeNode * call(TreeNode * k) TreeNode * t = newNode(CallK);if(k!=NULL)t-child0 = k;match(LPAREN);if(token=RPAREN) match(RPAREN);return t;else if(k!=NULL) t-child1 = args();match(RPAREN);return t;TreeNode * args(void) TreeNode *

28、t = newNode(ArgsK);TreeNode * s = NULL;TreeNode * p = NULL;if(token!=RPAREN)s = expression();p = s;while(token=COMMA) TreeNode * q;match(COMMA);q = expression();if(q!=NULL)if(s=NULL) s = p = q;else p-sibling = q;p = q;if(s!=NULL)t-child0 = s;return t;int blank_number=0;void PreOrder(TreeNode* t)stri

29、ng blank=“ “;int i;for(i=0;inodekind=OpK)coutattr.op)attr.op)+“n“;else if(t-nodekind=IdK)coutnodekind)attr.namenodekind)+“: “+t-attr.name+“n“;else if(t-nodekind=ConstK)coutnodekind)attr.valattr.val;strstream ss;string s;ss s;output+=blank+Change(t-nodekind)+“: “+s+“n“;else if(t-nodekind=AssignK)cout

30、nodekind=Selection_StmtK)coutnodekind=Iteration_StmtK)coutnodekind=Return_StmtK)coutnodekind)nodekind)+“n“;for(i=0;ichildi!=NULL)blank_number+=2;PreOrder(t-childi);blank_number-=2;if(t-sibling!=NULL)PreOrder(t-sibling);void parse(void)TreeNode * t;cout“Syntax tree:“endl;token = GetToken();t = declar

31、ation_list();PreOrder(t);void main()char* file=(char *)malloc(100);/打开的文件名string result;/输出结果文件名cout“请输入文件名 .例如:gcd.c-“ endl;scanf(“%s“,file);while(source=fopen(file,“r“)=NULL)cout“文件名无效,请重新输入“endl;scanf(“%s“,file);ofstream write;/输出文件result=string(file)+“-Result.txt“;write.open(result);write“Syntax tree:“endl;parse();writeoutput;write.close();system(“PAUSE“);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报