好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

编译原理实验.docx

11页
  • 卖家[上传人]:鲁**
  • 文档编号:382258564
  • 上传时间:2022-09-08
  • 文档格式:DOCX
  • 文档大小:283.63KB
  • / 11 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • TINY+语言的语法分析软件软件设计说明文档院系 计算机学院 专业 计算机科学与技术 年级 2008级 班级 2班 姓名 张俊发 学号 20082101032 实验名称 综合利用多媒体制作网站或者应用程序 实验时间 5月1日至25日 指导老师及职称 黄煜廉 1■总体描述 1.1总体功能概述TINY+语言的语法分析软件提供Window界面,用户可以点击【打 开】按钮打开或者在编辑框中输入一个扩展Tiny+语言源程序;通过 【打印语法树】复选按钮可以选择在分析结果中打印语法树,【语法 分析】按钮提供Tiny语言词法分析功能,对源程序进行语法分析后 在内存中生成语法树,并将分析结果显示在编辑框中;【文件另存为】 按钮可以保存文件1.2设计目标设计目标:(1)扩充的语法规则有:While-stmt —〉 while exp do stmt-sequence endwhile Dowhile-stmt--〉do stmt-sequence while expfor-stmt--〉for identifier:二simple-exp to simple-exp do stmt-sequence enddo 步 长递增1for-stmt--〉for identifier:二simple-exp downto simple-expdo stmt-sequence enddo 步 长递减1 软件能对Tiny+语言源程序进行语法分析并生成语法树。

      1) 要提供一个源程序编辑界面,以让用户输入源程序(可保存、打开源程序)(2) 可由用户选择是否生成语法树,并可查看所生成的语法树1.3软件总体结构本软件包括下面3个功能模块:词法分析功能模块;语法分析功能模 块;文本编辑模块;■ "| Scan |• getNextCharQ• getTokenQ• printToken(MyTokenType token]■ reservedLookup(char *s].ScanQ• ~ScanQ• ungetNextCharQ 识 bufsize& EOF_flag谀 lineBuf@ lineno◎ linepos> listing◎ source> tokenString■ ■ Parse.assign_stmt(].copyStringfchar *str]• doParseQ• dowhile_stmt(]• expO.factorQ• for_stmt(].if stmtO.match(MyTokenType expected]• newExpNode(ExpKind kind]• newStmtNode(StmtKind kind]• ParseQ• ~Parse(].printSpacesQ.printTreejTreeNode *tree].read stmtQ.repeat_stmtQ• simple_expO.statementQ• stmt_sequence(]• syntaxErrorfchar *message].termQ• while_stmt(].write stmtQ 谀 indentno 识 m_Error谀sc识 token■・ CTinyDIg• CTinyDlg(CWnd *pParent = NULL]9* DoDataExchange(CDataExchange *pDX] OnCheckIOOnFileOpenQOnFileSaveAsf] OnlnitDialogQ 询 OnPaintQOnQueryDraglconQ询 OnSyntaxParseQ询 OnSysCommand(UINT nID, LPARAM IParam) 谀 m flag9 识 m_hlcon识 m_listing谀 m parse识 m_printTree识 m_source谀 m_strText识 m_syntaxTree谀 m textEdit2•软件功能设计 2.1软件功能界面:2.2词法分析模块功能设计2.2.1功能描述对输入源程序进行扫描,识别Tiny+记号。

      222功能设计Tiny+语言扫描程序DFA图如下:2.2.3代码实现Scan:这部分的功能的核心函数是getToken ()函数MyTokenType Scan::getToken(void){int tokenStringlndex = 0;MyTokenType currentToken;StateType state = START;int save;while(state!=DONE){int c = getNextChar();save =switch(state){caseif (isdigit(c) ) state = INNUM; else 讦(isalpha(c) ) state = INID; else if ( c ==':') state = INASSIGN;else f ((c == ' ') II (c == '\t') II (c == '\n')) save = FALSE;else f (c =='{'){save = FALSE;state = INCOMMENT;}else{state=DONE;switch(c){case EOF :save = FALSE;currentToken = MYENDFILE;break; case '=':currentToken = EQ;break;case 'v':currentToken = LT;break;case '+':currentToken = PLUS;break;case '-':currentToken = MINUS;break;case '*':currentToken = TIMES;break;case 7':currentToken = OVER;break;case '%':currentToken = MOD;break;case '(':currentToken = LPAREN;break;case ')':currentToken = RPAREN;break;case ';':currentToken = SEMI;break; default:currentToken = MYERROR;break;}}break;case INCOMMENT:save = FALSE;f (c == EOF){ state = DONE; currentToken = MYENDFILE;}else f (c =='}') state = START;break;case INASSIGN:state = DONE;f (c =='=') currentToken = ASSIGN;else{ /* backup in the input */ ungetNextChar(); save = FALSE;currentToken = MYERROR;}break;case INNUM:if (!isdigit(c)){ /* backup in the input */ ungetNextChar();save = FALSE;state = DONE;currentToken = NUM;}break;case INID:if (!isalpha(c)){ /* backup in the input */ ungetNextChar();save = FALSE;state = DONE;currentToken = ID;}break;case DONE: default: /* should never happen */ fprintf(listing,"Scanner Bug: state= %d\n",state); state = DONE;currentToken = MYERROR;break;} 讦((save) && (tokenStringlndex <= MAXTOKENLEN)) tokenString[tokenStringIndex++] = (char) c;讦(state == DONE){ tokenString[tokenStringIndex] = '\0';if (currentToken == ID) currentToken = reservedLookup(tokenString);}} fprintf(listing,"\t%d: ",lineno); printToken(currentToken); return currentToken;}2.3词法分析模块功能设计2.3.1功能描述TINY+语言文法: program —> stmt—sequence stmt-sequence —> statement {;statement} statement --> if-stmt|repeat-stmt|assign-stmt|read-stmt|write-stmt|While-stmt|Dowhile-stmt|for-stmtif-stmt --> if exp then stmt-sequence [else stmt-sequence] end repeat-stmt --> repeat stmt-sequence until exp assign-stmt --> identifier :二 exp read-stmt --> read identifierwrite-stmt --> write exp exp --> simple-exp [ comparison-op simple-exp ] comparison-op --> < | = simple-exp --> term { addop term } addop --> + | - term --> factor { mulop factor } mulop --> * | / | % factor --> (exp) | number | identifier While-stmt --> while exp do stmt-sequence endwhile Dowhile-stmt --> do stmt-sequence while exp for-stmt --> for identifier := simple-exp for-choose' for-choose' --> to simple-exp do stmt-sequence enddo for-choose' --> downto simple-exp do stmt-sequence enddo根据Tiny+文法对输入的Tiny+源程序进行语法分析,并生成语法树。

      2.3.2功能设计Tiny+语言语法图如下:2.2.3代码实现扩充文法实现:TreeNode * Parse::while_stmt(void){TreeNode * t=newStmtNode(WhileK); match(WHILE);i。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.