编译原理词法分析报告C++.docx
- 文档编号:9313461
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:26
- 大小:47.44KB
编译原理词法分析报告C++.docx
《编译原理词法分析报告C++.docx》由会员分享,可在线阅读,更多相关《编译原理词法分析报告C++.docx(26页珍藏版)》请在冰豆网上搜索。
编译原理词法分析报告C++
词法分析实验报告
一、实验目的
1.掌握词法分析的原理。
2.熟悉保留字表等相关的数据结构与单词的分类方法。
3.掌握词法分析器的设计与调试。
二、实验内容
根据编译中的分词原理,用C++语言编写一个C语言的词法分析程序:
.
三、实验要求
1.输入:
任意一个C语言程序的源代码。
2.处理:
对输入进行分析,分离出保留字、标识符、常量、算符和界符。
3.输出:
对应的二元式
四、实验环境
WindowsXPProfessionalSP3,VisualStudio2010
五、关键代码
//Scanner.cpp
#include"stdafx.h"
#include"CuteC.h"
#include"Scanner.h"
#include"CuteCView.h"
intCScanner:
:
m_constListIndex=0;
intCScanner:
:
g_place=0;
//CScanner
IMPLEMENT_DYNAMIC(CScanner,CWnd)
CScanner:
:
CScanner()
{
}
CScanner:
:
CScanner(CStringstr)
{
//初始化保留字
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:
:
tokentypeCScanner:
:
reservedlookup(CStringstr)
{
for(inti=0;i<13;i++)
{
if(str==reservedWords[i].str)
returnreservedWords[i].tok;
}
returntokentype:
:
ID;
}
//取下一个字符
wchar_tCScanner:
:
getnextchar(void)
{
if(m_nCount>=m_nLength)
{
m_nCount++;
return'';
}
returnm_cChar[m_nCount++];
}
//获取标识符
CStringCScanner:
:
getidentifier(void)
{
CStringstrTemp;//保存单词变量
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;
}
}
returnstrTemp;
}
//获取字符串
CStringCScanner:
:
getChar(void)
{
CStringstrTemp;//保存字符串变量
m_ch=m_cChar[m_nCount-1];//获取当前字符
if(m_nCount==m_nLength)
{
returnstrTemp;
}
m_ch=getnextchar();
m_col++;
while(m_ch!
='\'')
{
strTemp+=m_ch;
if(m_ch=='\n')
{
returnstrTemp;
}
if(m_nCount==m_nLength)
{
returnstrTemp;
}
m_ch=getnextchar();
m_col++;
}
returnstrTemp;
}
//获取数字串
CStringCScanner:
:
getNumber(void)
{
CStringstrTemp;//保存数字串
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++;
}
returnstrTemp;
}
////词法分析主程序
voidCScanner:
:
morphologyAnalyse()
{
m_nCount=0;//记录当前扫描数字的下标
m_ch=getnextchar();
CStringstrTemp;//当前分析的标识符
tokentypecurrenttoken;//当前分析单词的类型
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();
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_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);
}
elseif(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++;
}
elseif(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_ch=getnextchar();
m_col++;
}
elseif(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);
}
elseif(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);
}
elseif(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);
}
elseif(m_ch=='/')
{
m_ch=getnextchar();
m_col++;
if(m_ch=='/')//行注释
{
m_ch=getnextchar();
m_col++;
while(m_ch!
='\n')
{
m_ch=getnextchar();
}
m_row++;
m_col=0;
m_ch=getnextchar();
m_col++;
}
elseif(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);
}
}
elseif(m_ch=='=')
{
m_ch=getnextchar();
m_col++;
if(m_ch=='=')
{
m_words.str="==";
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);
}
}
elseif(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);
}
}
elseif(m_ch=='<')
{
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);
}
}
elseif(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);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 词法 分析 报告 C+
![提示](https://static.bdocx.com/images/bang_tan.gif)