1、 编译原理课程设计编译原理课程设计 题题 目目 语法分析器语法分析器 学生姓名 学号 学 院 专 业 软件工程 指导教师 二一三二一三 年年 一月一月 十日十日 目 录 1 系统目标 1 1.1 要求介绍 3 1.2 功能要求 4 2 逻辑设计过程 4 3 系统展示 4 4 系统实现 6 4.1 词法分析的实现. 6 4.2 语法分析的实现. 13 1 系统目标 1.1 1.1 要求介绍要求介绍 课程设计所用的 Tiny文法: Program stmt-sequence stmt-sequence stmt-sequence ;statement | statement statement i
2、f-stmt | repeat-stmt | assign-stmt | read-stmt | write-stmt if-stmt if exp then stmt-sequence end | if exp then stmt-sequence else stmt-sequence end repeat-stmt repeat stmt-sequence until exp assign-stmt identifier := exp read-stmt read identifier write-stmt write exp exp simple-exp comparison-op si
3、mple-exp | simple-exp simple-exp simple-exp addop term | term comparison-op forward(); ch = buffercur; this-back(); i+; tempi = 0; for(int n = 0; n forward(); ch = buffercur; while(isdigit(ch) i+; tempi = ch; this-forward(); ch = buffercur; this-back(); i+; tempi = 0; makeWord(22,temp); else if (ch
4、= + | ch = - | ch = * | ch = / | ch = = | ch = id = length; ls-line = line; ls-type = 18; strcpy(ls-content, “); ls-next = NULL; if(head = NULL) le = ls; head = le; else le-next = ls; le = ls; length+; else if ( ch = ) if (isMatch() /已做错误处理 makeWord(17,“); else if ( ch = # ) makeWord(20,“#“); else i
5、f ( ch = ;) makeWord(15,“;“); else if ( ch = | ch = t | ch = r | ch = f) else if( ch = n ) this-line+; else Error(“非法符号“); void CiFa:shapeWord(int type, char *content ) /构造单词 ls = new struct word; ls-id = length; ls-line = line; ls-type = type; strcpy(ls-content, content); ls-next = NULL; if(head = NULL) le = ls; head = le; else le-next = ls; le = ls; length+; void CiFa:Error(char *content) /错误的定位 els = new struct error; els-id = elength; els-line = line; strcpy(els-reason, content); els-next = NULL; if(ehead = NULL) ele = els; ehead = ele; else ele-next = els; ele