实编译原理验一词法分析程序文档格式.docx
- 文档编号:19007054
- 上传时间:2023-01-03
- 格式:DOCX
- 页数:17
- 大小:90.59KB
实编译原理验一词法分析程序文档格式.docx
《实编译原理验一词法分析程序文档格式.docx》由会员分享,可在线阅读,更多相关《实编译原理验一词法分析程序文档格式.docx(17页珍藏版)》请在冰豆网上搜索。
int
char
float
void
const
if
else
do
while
scanf
11
12
13
14
15
16
17
18
19
20
printf
return
main
+
–
*
/
%
=
21
22
23
24
25
26
27
28
29
30
==
>
<
!
&
||
!
31
32
33
34
35
36
37
38
39
40
(
)
{
}
;
“
‘
++
--
将词法分析程序设计成独(入口)立一遍扫描源程序的结构。
其主流程图如下:
四、程序源代码
#include"
stdafx.h"
词法分析.h"
词法分析Dlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
#defineBUFFERSIZE1200
#defineletter1
#definedigit0
#definenotld-1
#defineksize21
#definessize25
/////////////////////////////////////////////////////////////////////////////
char*k[ksize]={"
main"
"
int"
float"
double"
for"
while"
"
struct"
do"
char"
if"
else"
return"
"
void"
scanf"
print"
cout"
cin"
endl"
include"
iostream"
define"
};
char*s[ssize]={"
("
)"
{"
}"
;
"
="
+"
*"
//1234567891011
?
++"
+="
--"
-="
=="
//12131415161718192021
#"
["
]"
."
//22
charbuffer[BUFFERSIZE]={"
intplace[BUFFERSIZE][2];
charch;
charstrToken[100]={"
intpp=0;
char*biaoshifu[100];
intbiao=0;
char*constt[100];
intcon=0;
BOOLCMyDlg:
:
OnInitDialog()
CDialog:
OnInitDialog();
//Add"
About..."
menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&
0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<
0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->
AppendMenu(MF_SEPARATOR);
AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'
smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);
//Setbigicon
SetIcon(m_hIcon,FALSE);
//Setsmallicon
UINTarray[3];
for(inti=0;
i<
3;
i++)
array[i]=100+i;
m_StatusBar.Create(this);
//创建状态栏窗口
m_StatusBar.SetIndicators(array,sizeof(array)/sizeof(UINT));
//添加面板
for(intn=0;
n<
n++)
m_StatusBar.SetPaneInfo(n,array[n],0,290);
//设置面板宽度
//设置面板文本
m_StatusBar.SetPaneText(0,"
狂风制作"
);
m_StatusBar.SetPaneText(1,"
欢迎大家使用"
m_StatusBar.SetPaneText(2,"
有理想就有梦想"
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
SetTimer(1,300,NULL);
//设置定时器
returnTRUE;
//returnTRUEunlessyousetthefocustoacontrol
voidCMyDlg:
OnSysCommand(UINTnID,LPARAMlParam)
if((nID&
0xFFF0)==IDM_ABOUTBOX)
CAboutDlgdlgAbout;
dlgAbout.DoModal();
else
CDialog:
OnSysCommand(nID,lParam);
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
OnPaint()
if(IsIconic())
CPaintDCdc(this);
//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&
rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
OnPaint();
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCMyDlg:
OnQueryDragIcon()
return(HCURSOR)m_hIcon;
OnClickRichedit2(NMHDR*pNMHDR,LRESULT*pResult)
//TODO:
Addyourcontrolnotificationhandlercodehere
*pResult=0;
char*CMyDlg:
print(inti)
returnk[i];
Onfenxi()
//CFilefile(NULL,CFile:
modeCreate|CFile:
modeWrite);
CStringstrText1="
CStringstrText2="
strText1=dealwith();
m_richedit2.SetWindowText(strText1);
GetChar()
ch=buffer[pp];
pp++;
getbc()
while(ch=='
'
||int(ch)==10)
GetChar();
concat()
strcat(strToken,&
ch);
intCMyDlg:
IsLetterorDigit()
if(('
a'
=ch&
ch<
='
z'
)||('
A'
Z'
))returnletter;
elseif('
0'
9'
)returndigit;
elsereturnnotld;
reserve()
for(inti=0;
ksize;
if(strcmp(strToken,k[i])==0)returni+1;
return0;
reserve1()
ssize;
if(strcmp(strToken,s[i])==0)returni+1;
if(int(strToken[0])==13)return-1;
retract()
ch='
pp--;
insertid(char*a)
biaoshifu[biao++]=a;
returnbiao;
insertConst(char*a)
constt[con++]=a;
returncon;
CStringCMyDlg:
dealwith()
CStringstrText="
str="
pp=0;
intcode,valuee;
//code为单词种别标号valuee为单词符号的属性值
intplace1=0;
str+="
单词符号\t单词类型\t类型编码\t行数,列数\n"
strText+=str;
while(pp<
BUFFERSIZE&
buffer[pp]!
=NULL)
for(intj=0;
j<
100;
j++)
strToken[j]=NULL;
getbc();
place1=pp-1;
if(1==IsLetterorDigit())//ch里存放的是是字母
while(1==IsLetterorDigit()||0==IsLetterorDigit())
{
concat();
GetChar();
}
retract();
code=reserve();
if(code==0)
valuee=insertid(strToken);
str.Format("
%s\t\t标识符\t\t%d\t\t(%d,%d)\n"
strToken,0,place[place1][0],place[place1][1]);
else
%s\t\t关键字\t\t%d\t\t(%d,%d)\n"
strToken,40+code,place[place1][0],place[place1][1]);
elseif(digit==IsLetterorDigit())//返回是是数字
intisletter=0;
//判断数字后面是否是字母,如果是isletter=1,如果不是的话istetter=0
while((digit==IsLetterorDigit())||(letter==IsLetterorDigit())||ch=='
.'
if(letter==IsLetterorDigit())isletter=1;
if(ch=='
)isletter=2;
if(isletter==1)
%s\t\tERROR\t\t%d\t\t(%d,%d)\n"
strToken,100,place[place1][0],place[place1][1]);
elseif(isletter==2)
valuee=insertConst(strToken);
%s\t\t非整形常量\t\t%d\t\t(%d,%d)\n"
strToken,30,place[place1][0],place[place1][1]);
%s\t\t整形常量\t\t%d\t\t(%d,%d)\n"
strToken,31,place[place1][0],place[place1][1]);
elseif(-1==IsLetterorDigit())//既非数字,又非字母,是界符,运算符等等,
inti=0;
switch(ch)
case'
'
while(-1==IsLetterorDigit()&
ch!
=NULL&
int(ch)!
=13)
{concat();
GetChar();
}if(int(ch)!
=13)retract();
break;
+'
-'
default:
concat();
i=reserve1();
if(i==0)
elseif(i!
=-1)
%s\t\t算符\t\t%d\t\t(%d,%d)\n"
strToken,i,place[place1][0],place[place1][1]);
strText+=str;
str="
}
returnstrText;
OnButtonopen()
//TODO:
CFileDialogdlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"
AllFiles(*.*)|*.*||"
AfxGetMainWnd());
//CMyAppaa;
CStringstrPath,strText="
if(dlg.DoModal()==IDOK)
strPath=dlg.GetPathName();
CFilefile(strPath,CFile:
modeRead);
charread[1000];
charc;
//定义变量c记录当前从文件中读入的一个字符
introw=1;
//记录文件的行数
intline=1;
//记录文件的列数
intj=0;
file.Read(read,1000);
file.GetLength();
strText+=read[i];
c=read[i];
if(c=='
\n'
)//回车换行列数变为1,行数加1
line=1;
row+=1;
if(int(c)!
=13||char(c)!
=0)
buffer[i]=c;
place[j][0]=row;
place[j][1]=line;
j++;
=0)line+=1;
file.Close();
m_richedit1.SetWindowText(strText);
//char*temp;
//temp=print
(1);
//strText2+="
lll"
//strText2+=temp;
/*for(i=0;
21;
temp=print(i);
strText2+=temp;
strText2+='
\t'
if((i+1)%5==0)strText2+='
}*/
//MessageBox(temp,"
sadf"
MB_OK);
/*m_richedit1.SetWindowText(strText);
i=0;
charch;
ch=strText[0];
while(i<
100)
ch=strText[i++];
strText2+=ch;
strText2+="
\n###"
m_richedit2.SetWindowText(strText2);
*/
UpdateData(FALSE);
OnButtonsave()
CFileDialog
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 词法 分析 程序