📄 lexical_analyzerdlg.cpp
字号:
// lexical_analyzerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "lexical_analyzer.h"
#include "lexical_analyzerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
char ch =' '; // 存放读入当前的输入字符
int lineno;
struct reserve //关键字
{
char lexptr[MAXBUF];
int token;
};
struct reserve symtable[MAX];
char * str[]={"program","input","output","begin","end", //关键字表
"var","integer","real","for","to","if","then","else",
"do","while","write","array","proceure" };
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLexical_analyzerDlg dialog
CLexical_analyzerDlg::CLexical_analyzerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLexical_analyzerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLexical_analyzerDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CLexical_analyzerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLexical_analyzerDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLexical_analyzerDlg, CDialog)
//{{AFX_MSG_MAP(CLexical_analyzerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_open, Onopen)
ON_BN_CLICKED(IDC_analyz, Onanalyz)
ON_BN_CLICKED(IDC_save, Onsave)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLexical_analyzerDlg message handlers
BOOL CLexical_analyzerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CLexical_analyzerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLexical_analyzerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLexical_analyzerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CLexical_analyzerDlg::init()
{
for( int j=0; j<18; j++)
{
strcpy(symtable[j].lexptr,str[j]); //把结构体symtable用18个关键字初始化
symtable[j].token=j+3; //而标号从3开始,1为普通标志符2为数字
}
}
int CLexical_analyzerDlg::search(char *temp) //找关键字
{
for(int i=0; i<sizeof(symtable)/sizeof(symtable[0]); i++)//求关键字个数
{
if(!strcmp(symtable[i].lexptr ,temp)) //如果当前字符是关键字
{
return symtable[i].token; //返回关键字标号
}
}
return 0;
}
void CLexical_analyzerDlg::analyse(FILE *fpin, FILE *fpout)
{
char arr[MAXBUF];
int i=0;
int j=0;
while((ch=fgetc(fpin))!=EOF) //读入字符判断,空格、字母、数字、界符
{
if(ch==' '||ch=='\t') //fgetc()从流中读入字符的函数
{
}
else if(ch=='\n') //如果是换行符,则行号加1
{
lineno++;
}
else if(isdigit(ch)) //如果是数字,isdigit()返回非0值,满足if条件
{
while(isdigit(ch)) //判断和读取数字
{
arr[j]=ch;
j++;
ch=fgetc(fpin);
}
arr[j]='\0';
j=0;
fseek(fpin,-1L,SEEK_CUR);
fprintf(fpout,"%s\t%d\n",arr,2) ;
}
else if (isalpha(ch)) //如果是字母
{
while(isalpha(ch)||isdigit(ch))
{
arr[j]=ch;
j++;
ch=fgetc(fpin);
}
fseek(fpin,-1L,SEEK_CUR);
arr[j]='\0';
j=0;
if (search(arr)) //如果是关键字
{
fprintf(fpout,"%s\t%d\n",arr,search(arr));
}
else
fprintf(fpout,"%s\t%d\n",arr,1); //普通标志符
}
else if(ch==':')
{
ch=fgetc(fpin);
if(ch=='=')
{
fprintf(fpout,"%s\t%d\n",":=",29); //如果是 :=
}
else
{
fprintf(fpout,"%s\t%d\n",":",30); //如果是 :
fseek(fpin,-1L,SEEK_CUR);
}
}
else if (ch=='>')
{
ch=fgetc(fpin);
if(ch=='=') //如果是 >=
{
fprintf(fpout,"%s\t%d\n",">=",32);
}
else
{
fprintf(fpout,"%s\t%d\n",">",31); //如果是 >
fseek(fpin,-1L,SEEK_CUR);
}
}
else if(ch=='<')
{
ch=fgetc(fpin);
if(ch=='>')
{
fprintf(fpout,"%s\t%d\n","<>",35); // 如果是 <>
}
else if(ch=='=')
{
fprintf(fpout,"%s\t%d\n","<=",34); //如果是 <=
}
else
{
fprintf(fpout,"%s\t%d\n","<",33); //如果是 <
fseek(fpin,-1L,SEEK_CUR);
}
}
else if(ch=='/')
{
ch=fgetc(fpin);
if(ch=='*')
{
ch=fgetc(fpin);
s:
while(ch!='*')
{
ch=fgetc(fpin);
}
while(ch=='*')
{
ch=fgetc(fpin);
while(ch!='/')
{
goto s; //如果是注释 /* */
}
}
}
else if(ch=='/')
{
ch=fgetc(fpin);
while(ch!='\n')
{
ch=fgetc(fpin); //如果是注释 //
}
}
else
{
fprintf(fpout,"%s\t%d\n","/",24);
fseek(fpin,-1L,SEEK_CUR);
}
}
else if(ch=='+') //为以下字符定义数字标号
{
fprintf(fpout,"%s\t%d\n","+",21);
}
else if(ch=='-')
{
fprintf(fpout,"%s\t%d\n","-",22);
}
else if(ch=='*')
{
fprintf(fpout,"%s\t%d\n","*",23);
}
else if(ch=='(')
{
fprintf(fpout,"%s\t%d\n","(",25);
}
else if(ch==')')
{
fprintf(fpout,"%s\t%d\n",")",26);
}
else if(ch=='[')
{
fprintf(fpout,"%s\t%d\n","[",27);
}
else if(ch==']')
{
fprintf(fpout,"%s\t%d\n","]",28);
}
else if(ch=='.')
{
fprintf(fpout,"%s\t%d\n",".",39);
}
else if(ch==';')
{
fprintf(fpout,"%s\t%d\n",";",36);
}
else if(ch=='=')
{
fprintf(fpout,"%s\t%d\n","=",38);
}
else if(ch==',')
{
fprintf(fpout,"%s\t%d\n",",",40);
}
else fprintf(fpout,"无法识别的字符 %c\n",ch) ;
}
}
void CLexical_analyzerDlg::Onopen()
{
// TODO: Add your control notification handler code here
CFileDialog openFile(true); //定义对话框对象
openFile.m_ofn.lpstrTitle="请选择你要词法分析的文件"; //对话框显示的标题
openFile.m_ofn.lpstrInitialDir="e:\\"; //对话框初始目录
if(openFile.DoModal()) //顾名思义:DO Modal 弹出对话框成功
{
m_strFileName=openFile.GetPathName();//对话框对象获取路径名
CStdioFile fstream; //定义能打开流式文件的对象
CString str; //存放文件字符串
if(fstream.Open(m_strFileName.GetBuffer(0),CFile::modeRead))//成功打开文件
{
//UpdateData();
CString strTemp; //定义临时保存字符串变量
while(fstream.ReadString(strTemp))//对象读流式文件
{
str+=strTemp; //每读一个字符串都换行
str+="\r\n";
}
SetDlgItemText(IDC_input,str); //在源文件框输入框显示
fstream.Close(); //关闭流文件
}
}
}
void CLexical_analyzerDlg::Onanalyz()
{
// TODO: Add your control notification handler code here
if(this->m_strFileName.IsEmpty()) //如果当前没有打开文件
{
MessageBox("请先选择文件","词法分析器",MB_ICONEXCLAMATION);
return;
}
FILE* fpin=fopen(m_strFileName.GetBuffer(0),"r");//定义打开读文件指针对象
FILE* fpout=fopen("output.txt","w"); //定义打开写文件指针对象
init();
analyse(fpin,fpout);
fclose(fpin);
fclose(fpout);
CStdioFile fstream;
CString str;
if(fstream.Open("output.txt",CFile::modeRead)) //输出到结果显示框
{
CString strTemp;
while(fstream.ReadString(strTemp))
{
str+=strTemp;
str+="\r\n";
}
SetDlgItemText(IDC_result,str);
fstream.Close();
}
}
void CLexical_analyzerDlg::Onsave()
{
// TODO: Add your control notification handler code here
if(this->m_strFileName.IsEmpty()) //如果没有打开文件报错
{
MessageBox("请先选择文件","词法分析器",MB_ICONEXCLAMATION);
return;
}
CFile fstream;
if(fstream.Open(this->m_strFileName,CFile::modeWrite|CFile::modeCreate))
{
UpdateData();
char buf[200]={0};
GetDlgItemText(IDC_result,buf,200); //把结果输出框的字节读到buf[]中
fstream.Write(buf,strlen(buf)); //把strlen(buf)长度的字节流写到buf[]中
fstream.Close();
MessageBox("保存成功,在词法分析的文件目录下","词法分析器",MB_ICONINFORMATION);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -