?? 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 + -