当前位置:文档之家› 编译原理词法分析报告(C++)

编译原理词法分析报告(C++)

词法分析实验报告
一、实验目的
1. 掌握词法分析的原理。 2. 熟悉保留字表等相关的数据结构与单词的分类方法。 3. 掌握词法分析器的设计与调试。
二、实验内容
根据编译中的分词原理,用C++语言编写一个C语言的词法分析程 序:.
三、实验要求
1. 输入:任意一个C语言程序的源代码。 2. 处理:对输入进行分析,分离出保留字、标识符、常量、算符和界 符。 3. 输出:对应的二元式
{ m_words.GrammarWords='t'; } if(L"printf"==strTemp) { m_words.GrammarWords='u'; } if(L"while"==strTemp) { m_words.GrammarWords='k'; } } m_words.type=currenttoken; m_words.str=strTemp; m_words.row=m_row; m_words.col=m_col-m_words.str.GetLength(); m_wordList.AddTail(m_words); } else if(m_ch>='0' && m_ch<='9') { m_words.str=getNumber(); m_words.type=tokentype::NUM; m_words.GrammarWords='h'; m_words.row=m_row; m_words.col=m_col-m_words.str.GetLength(); m_wordList.AddTail(m_words); m_constList[m_constListIndex].val=m_words.str; m_constList[m_constListIndex].place=g_place; m_constList[m_constListIndex].type="int"; g_place+=4; m_constListIndex++; } else if(m_ch=='\'') { m_words.str=getChar(); if(m_words.str!="') { m_row++; } strTemp+=m_ch; m_ch=getnextchar(); m_col++; if(m_ch=='\t') { m_col+=3; } } return strTemp; } // 获取字符串 CString CScanner::getChar(void) { CString strTemp;//保存字符串变量 m_ch=m_cChar[m_nCount-1];//获取当前字符 if(m_nCount==m_nLength) { return strTemp; } m_ch=getnextchar(); m_col++; while(m_ch!='\'') { strTemp+=m_ch; if(m_ch=='\n') { return strTemp; } if(m_nCount==m_nLength) { return strTemp; }
reservedWords[0].tok=tokentype::_VOID; reservedWords[1].str=L"char"; reservedWords[1].tok=tokentype::CHAR; reservedWords[2].str=L"const"; reservedWords[2].tok=tokentype::_CONST; reservedWords[3].str=L"double"; reservedWords[3].tok=tokentype::DOUBLE; reservedWords[4].str=L"else"; reservedWords[4].tok=tokentype::ELSE; reservedWords[5].str=L"float"; reservedWords[5].tok=tokentype::FLOAT; reservedWords[6].str=L"for"; reservedWords[6].tok=tokentype::FOR; reservedWords[7].str=L"if"; reservedWords[7].tok=tokentype::IF; reservedWords[8].str=L"int"; reservedWords[8].tok=tokentype::INT; reservedWords[9].str=L"main"; reservedWords[9].tok=tokentype::MAIN; reservedWords[10].str=L"scanf"; reservedWords[10].tok=tokentype::SCANF; reservedWords[11].str=L"printf"; reservedWords[11].tok=tokentype::PRINTF; reservedWords[12].str=L"while"; reservedWords[12].tok=tokentype::WHILE; m_error=FALSE; m_strText=str; m_nLength=m_strText.GetLength(); m_cChar=m_strText.GetBuffer(m_nLength);//数组化字符串 m_nCount=0; m_row=1; m_col=0; } CScanner::~CScanner() { }
m_words.GrammarWords = '#'; m_words.type=tokentype::OVER; m_words.row=m_row; m_words.col=m_col+1; m_wordList.AddTail(m_words); return; } while(m_ch==32 || m_ch=='\t' || m_ch=='\n') { if(m_ch=='\n') { m_row++; m_col=0; } if('\t'==m_ch) { m_col+=4; } if(m_nCount>m_nLength) { m_words.str="#"; m_words.GrammarWords = '#'; m_words.type=tokentype::OVER; m_words.row=m_row; m_words.col=m_col+1; m_wordList.AddTail(m_words); return ; } m_ch=getnextchar(); m_col++; } if((m_ch>='A' && m_ch<='Z') || m_ch>='a' && m_ch<='z') { strTemp=getidentifier(); currenttoken=reservedlookup(strTemp); if(currenttoken ==tokentype::ID) {
m_words.GrammarWords='g'; } else { if(L"void"==strTemp) { m_words.GrammarWords='a'; } if(L"main"==strTemp) { m_words.GrammarWords='b'; } if(L"char"==strTemp) { m_words.GrammarWords='c'; } if(L"int"==strTemp) { m_words.GrammarWords='d'; } if(L"if"==strTemp) { m_words.GrammarWords='i'; } if(L"else"==strTemp) { m_words.GrammarWords='j'; } if(L"const"==strTemp) { m_words.GrammarWords='v'; } if(L"for"==strTemp) { m_words.GrammarWords='l'; } if(L"scanf"==strTemp)
m_ch=getnextchar(); m_col++; } return strTemp; } // 获取数字串 CString CScanner::getNumber(void) { CString strTemp;//保存数字串 m_ch=m_cChar[m_nCount-1];//获取当前字符 strTemp+=m_ch; m_ch=getnextchar(); m_col++; while(!(m_ch=='\n' || m_ch==32/*空格*/ || m_ch=='\t' || m_ch=='+' || m_ch=='-'|| m_ch=='*'|| m_ch=='/'|| m_ch=='%'|| m_ch=='(' || m_ch==')'|| m_ch=='='|| m_ch==','|| m_ch==';'|| m_ch=='&' || m_ch=='|'|| m_ch=='!'|| m_ch=='^'|| m_ch=='~'|| m_ch=='[' || m_ch==']'|| m_ch=='{'|| m_ch=='}'|| m_ch=='?'|| m_ch==':')) { strTemp+=m_ch; m_ch=getnextchar(); m_col++; } return strTemp; } // //词法分析主程序 void CScanner::morphologyAnalyse() { m_nCount=0;//记录当前扫描数字的下标 m_ch=getnextchar(); CString strTemp;//当前分析的标识符 tokentype currenttoken;//当前分析单词的类型 while(1) { if(m_strText=="" || m_nCount>m_nLength) { m_words.str="#";
相关主题