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

更新时间:2023-08-27 18:10:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

我做编译原理写的,希望对你有帮助。

词法分析实验报告

一、实验目的

1. 掌握词法分析的原理。

2. 熟悉保留字表等相关的数据结构与单词的分类方法。

3. 掌握词法分析器的设计与调试。

二、实验内容

根据编译中的分词原理,用C++语言编写一个C语言的词法分析程序:.

三、实验要求

1. 输入:任意一个C语言程序的源代码。

2. 处理:对输入进行分析,分离出保留字、标识符、常量、算符和界符。

3. 输出:对应的二元式

四、实验环境

Windows XP Professional SP3,Visual Studio 2010

五、关键代码

// Scanner.cpp

#include "stdafx.h"

#include "CuteC.h"

#include "Scanner.h"

#include "CuteCView.h"

int CScanner::m_constListIndex=0;

int CScanner::g_place=0;

// CScanner

IMPLEMENT_DYNAMIC(CScanner, CWnd)

CScanner::CScanner()

{

}

CScanner::CScanner(CString str)

{

//初始化保留字

reservedWords[0].str=L"void";

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()

{

}

BEGIN_MESSAGE_MAP(CScanner, CWnd)

END_MESSAGE_MAP()

// 检查标识符是不是保留字,是就返回保留字类型

CScanner::tokentype CScanner::reservedlookup(CString str)

{

for(int i=0;i<13;i++)

{

if(str==reservedWords[i].str)

return reservedWords[i].tok;

}

return tokentype::ID;

}

// 取下一个字符

我做编译原理写的,希望对你有帮助。

wchar_t CScanner::getnextchar(void)

{

if(m_nCount>=m_nLength)

{

m_nCount++;

return ' ';

}

return m_cChar[m_nCount++];

}

// 获取标识符

CString CScanner::getidentifier(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==':'

|| m_ch=='>'|| m_ch=='<'|| m_ch=='\r'))

{

if(m_ch=='\n')

{

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;

}

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="#"; 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();

我做编译原理写的,希望对你有帮助。

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_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_words.GrammarWords='s'; m_words.type=tokentype::CHARACTOR; 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="char"; g_place+=4; m_constListIndex++; }

我做编译原理写的,希望对你有帮助。

m_col++; } else if(m_ch=='+') { m_ch=getnextchar(); m_col++; m_words.str="+"; m_words.GrammarWords='+'; m_words.type=tokentype::ADD; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } else if(m_ch=='-') { m_ch=getnextchar(); m_col++; m_words.str="-"; m_words.GrammarWords='-'; m_words.type=tokentype::SUB; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } else if(m_ch=='*') { m_ch=getnextchar(); m_col++; m_words.str="*"; m_words.GrammarWords='*'; m_words.type=tokentype::MUL; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } else if(m_ch=='/') { m_ch=getnextchar(); m_col++; if(m_ch=='/')//行注释 { m_ch=getnextchar(); m_col++;

我做编译原理写的,希望对你有帮助。

{ m_ch=getnextchar(); } m_row++; m_col=0; m_ch=getnextchar(); m_col++; } else if(m_ch=='*')//块注释 { m_ch=getnextchar(); m_col++; while(1) { if(m_ch=='*') { m_ch=getnextchar(); m_col++; if(m_ch=='/') { m_ch=getnextchar(); break; } } m_ch=getnextchar(); } } else { m_words.str="/"; m_words.GrammarWords='/'; m_words.type=tokentype::DIV; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } } else if(m_ch=='=') { m_ch=getnextchar(); m_col++; if(m_ch=='=') {

我做编译原理写的,希望对你有帮助。

m_words.GrammarWords = 'o'; m_words.type=tokentype::EQ; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); m_ch=getnextchar(); m_col++; } else { m_words.str="="; m_words.GrammarWords = '='; m_words.type=tokentype::ASSIGN; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } } else if(m_ch=='!') { m_ch=getnextchar(); m_col++; if(m_ch=='=') { m_words.str="!="; m_words.GrammarWords = 'p'; m_words.type=tokentype::NE; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); m_ch=getnextchar(); m_col++; } else { m_words.str="!"; m_words.GrammarWords = '!'; m_words.type=tokentype::NOT; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } }

我做编译原理写的,希望对你有帮助。

{ m_ch=getnextchar(); m_col++; if(m_ch=='=') { m_words.str="<="; m_words.GrammarWords = 'q'; m_words.type=tokentype::LE; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); m_ch=getnextchar(); m_col++; } else { m_words.str="<"; m_words.GrammarWords = '<'; m_words.type=tokentype::LT; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } } else if(m_ch=='>') { m_ch=getnextchar(); m_col++; if(m_ch=='=') { m_words.str=">="; m_words.GrammarWords = 'r'; m_words.type=tokentype::GE; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); m_ch=getnextchar(); m_col++; } else { m_words.str=">";

我做编译原理写的,希望对你有帮助。

m_words.type=tokentype::GT; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } } else if(m_ch=='&') { m_ch=getnextchar(); m_col++; m_words.str="&&"; m_words.GrammarWords = 'n'; m_words.type=tokentype::ADD; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); m_ch=getnextchar(); m_col++; } else if(m_ch=='|') { m_ch=getnextchar(); m_col++; m_words.str="||"; m_words.GrammarWords = 'm'; m_words.type=tokentype::OR; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); m_ch=getnextchar(); m_col++; } else if(m_ch=='(') { m_ch=getnextchar(); m_col++; m_words.str="("; m_words.GrammarWords = '('; m_words.type=tokentype::LPAREN;//左圆括号 m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); }

我做编译原理写的,希望对你有帮助。

{ m_ch = getnextchar(); m_col++; m_words.str = ")"; m_words.GrammarWords = ')'; m_words.type = tokentype::RPAREN;//右圆括号 m_words.row = m_row; m_words.col = m_col - 1; m_wordList.AddTail(m_words); } else if(m_ch=='[') { m_ch=getnextchar(); m_col++; m_words.str="["; m_words.GrammarWords = '['; m_words.type=tokentype::LBRACKET;/*左方括号*/ m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } else if(m_ch==']') { m_ch=getnextchar(); m_col++; m_words.str="]"; m_words.GrammarWords = ']'; m_words.type=tokentype:: RBRACKET;/*右方括号*/ m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } else if(m_ch=='{') { m_ch=getnextchar(); m_col++; m_words.str="{"; m_words.GrammarWords = '{'; m_words.type=tokentype::LBRACE;//左花括号 m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); }

我做编译原理写的,希望对你有帮助。

} } { m_ch=getnextchar(); m_col++; m_words.str="}"; m_words.GrammarWords = '}'; m_words.type=tokentype::RBRACE;//左花括号 m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } else if(m_ch==',') { m_ch=getnextchar(); m_col++; m_words.str=","; m_words.GrammarWords = ','; m_words.type=tokentype::COMMA; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } else if(m_ch==';') { m_ch=getnextchar(); m_col++; m_words.str=";"; m_words.GrammarWords = ';'; m_words.type=tokentype::SEMI; m_words.row=m_row; m_words.col=m_col-1; m_wordList.AddTail(m_words); } else { currenttoken=tokentype::_ERROR; m_error=TRUE; m_ch=getnextchar(); m_col++; }

六、实验结果

我做编译原理写的,希望对你有帮助。

(1)测试代码

void main()

{

int i = 10;

int j = 0;

int max;

scanf(i);

scanf(j);

if(i>j)

{

max=i;

}

else

{

max=j;

}

printf(max);

}

2. 处理结果

我做编译原理写的,希望对你有帮助。

七、实验总结

通过这次实验,我对词法分析的理解更深刻了。

实验过程比较顺利,但仍遇到一些问题,比如为什么整型数据在token表里输出不直观。这些问题我会记在本子上,课后多花时间去弄懂。

本文来源:https://www.bwwdw.com/article/58ji.html

Top