📄 scannerdlg.cpp
字号:
// ScannerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Scanner.h"
#include "ScannerDlg.h"
#include "Vocabulary.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CScannerDlg dialog
CScannerDlg::CScannerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CScannerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CScannerDlg)
m_result = _T("");
m_source = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CScannerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CScannerDlg)
DDX_Control(pDX, IDC_EDIT_SOURCE, m_edit_source);
DDX_Control(pDX, IDC_EDIT_RESULT, m_edit_result);
DDX_Text(pDX, IDC_EDIT_RESULT, m_result);
DDX_Text(pDX, IDC_EDIT_SOURCE, m_source);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CScannerDlg, CDialog)
//{{AFX_MSG_MAP(CScannerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SCAN, OnScan)
ON_BN_CLICKED(IDC_CLOSE, OnClose)
ON_BN_CLICKED(IDC_OPEN, OnOpen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CScannerDlg message handlers
BOOL CScannerDlg::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 CScannerDlg::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 CScannerDlg::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 CScannerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CScannerDlg::OnScan() //“词法分析”按钮的函数
{
// TODO: Add your control notification handler code here
if(ends=true)
{
ch=' ';
code=-1;
ends=false;
for(int i=0;i<50;i++)
{
Identifier[i]="";
Intt[i]="";
Floatt[i]="";
PrimaryKey[i]=0;
Symbol[i]="";
}
strToken="";
point=0;
m_result="";
m_source="";
}
CString cc;
UpdateData(true);
int length=m_source.GetLength(); //源文件长度
if(m_source=="") //如果源文件为空
{
return;
}
while(point<length)
{
ch=GetChar();
GetBC();
if(IsLetter()) //首个是字母
{
while( IsLetter() || IsDigit() )
{
Concat();
ch=GetChar();
}
//非字母和数字,到了终结态,回调一位
Retract();
code=Reserve(); //GetCode(strToken)的值,在关键字表Vocabulary中的位置或返回0
if(code==-1) //不是关键字,是自定义标识符
{
value=InsertId();
strToken="";
//////return ($ID ,value);
}
else
{
PrimaryKey[code]=1; //是关键字,关键字表相应位置置1
strToken="";
////////return (code,-);
}
}
else
if(IsDigit()) //首个是数字
{
int count=0; //小数点个数
while(IsDigit())
{
Concat();
ch=GetChar();
if(ch=='.') //浮点数
{
count++;
strToken+='.';
ch=GetChar();
}
}
Retract();
if(count==0)
{
InsertInt();
}
else if(count==1)
{
InsertFloat();
}
else //两个以上小数点则不是小数
strToken="";
///////return ( $ int ,value);
}
else //不是以字母和数字开头的情况,视为符号
{
//解决两位连字符
CString s;
if(ch=='+')
{
if(m_source.GetAt(point)=='+')
{
s="++";
point++;
}
else if(m_source.GetAt(point)=='=')
{
s="+=";
point++;
}
}
else if(ch=='-')
{
if(m_source.GetAt(point)=='-')
{
s="--";
point++;
}
else if(m_source.GetAt(point)=='=')
{
s="-=";
point++;
}
}
else if(ch=='*' && m_source.GetAt(point)=='=')
{
s="*=";
point++;
}
else if(ch=='=' && m_source.GetAt(point)=='=')
{
s="==";
point++;
}
else if(ch=='<' && m_source.GetAt(point)=='<')
{
s="<<";
point++;
}
else if(ch=='>' && m_source.GetAt(point)=='>')
{
s=">>";
point++;
}
else if(ch=='/' && m_source.GetAt(point)=='/') //忽略单行注释
{
while(!(ch=='\r' && m_source.GetAt(point)=='\n'))
ch=GetChar();
point++;
}
else if(ch=='/' && m_source.GetAt(point)=='*') //忽略多行注释
{
while(!(ch=='*' && m_source.GetAt(point)=='/'))
ch=GetChar();
point++;
}
else //单个字符的情况
{
s=ch;
}
value=InsertSymbol(s);
}
}
m_result="";
ShowOut();
ends=true;
}
char CScannerDlg::GetChar() //将下一输入字符读到ch中
{
char ch=m_source.GetAt(point); //CWnd类成员函数,返回给定索引处的值
point++; //每读出一个字符,指针增1
return ch;
}
void CScannerDlg::GetBC() //去空格
{
while(ch==' ')
{
ch=GetChar();
}
}
BOOL CScannerDlg::IsLetter()
{
if((ch>='A'&& ch<='Z') || (ch>='a' && ch<='z'))
{
return TRUE;
}
return FALSE;
}
BOOL CScannerDlg::IsDigit()
{
if(ch>='0' && ch<='9')
{
return TRUE;
}
return FALSE;
}
void CScannerDlg::Concat() //将ch中的字符连接到strToken
{
strToken+=ch;
}
void CScannerDlg::Retract() //回调一个字符
{
point--;
ch=' ';
}
int CScannerDlg::Reserve() /// 判断是关键字还是用户自定义的标识符
{
CVocabulary bu;
int p=bu.GetCode(strToken); ///// 是否是关键字
return p; //返回字符串在关键字表Vocabulary中的位置或返回0
}
int CScannerDlg::InsertId() //插入标志符
{
for(int i=0;i<50;i++)
{
if(strToken==Identifier[i]) //如果该标识符已经在标识符中
{
strToken="";
break ;
}
if(Identifier[i]=="") //Identifier[50]
{
Identifier[i]=strToken;
strToken="";
break ;
}
}
return i;
}
/*
int CScannerDlg::InsertConst() //插入常数
{
for(int i=0;i<50;i++) //Const[50]
{
if(Const[i]==strToken)
{
strToken="";
return i;
}
if(Const[i]=="")
{
Const[i]=strToken;
strToken="";
return i;
}
}
return i;
}
*/
int CScannerDlg::InsertInt() //插入整数
{
for(int i=0;i<50;i++) //Int[50]
{
if(Intt[i]==strToken)
{
strToken="";
break;
}
if(Intt[i]=="")
{
Intt[i]=strToken;
strToken="";
break;
}
}
return i;
}
int CScannerDlg::InsertFloat() //插入浮点型
{
for(int i=0;i<50;i++) //Float[50]
{
if(Floatt[i]==strToken)
{
strToken="";
return i;
}
if(Floatt[i]=="")
{
Floatt[i]=strToken;
strToken="";
return i;
}
}
return i;
}
int CScannerDlg::InsertSymbol(CString s) //插入符号
{
CVocabulary ca;
int p=ca.FindSymbol(s);
Symbol[p-50]=1;
return p;
}
void CScannerDlg::ShowOut()
{
m_result+="---------关键字---------";
m_result+="\r\n";
CVocabulary bu;
for(int i=0;i<46;i++)
{
if(PrimaryKey[i]==1)
{
m_result+=bu.Vocabulary[i];
m_result+="\r\n";
}
}
m_result+="---------符 号----------";
m_result+="\r\n";
for(i=51;i<96;i++)
{
if(Symbol[i-50]==1)
{
m_result+=bu.Vocabulary[i];
m_result+="\r\n";
}
}
m_result+="---------标识符---------";
m_result+="\r\n";
for(i=0;i<50;i++)
{
if(Identifier[i]!="")
{
m_result+=Identifier[i];
m_result+="\r\n";
}
}
m_result+="---------整 数----------";
m_result+="\r\n";
for(i=0;i<50;i++)
{
if(Intt[i]!="") //
{
m_result+=Intt[i];
m_result+="\r\n";
}
}
m_result+="---------小 数----------";
m_result+="\r\n";
for(i=0;i<50;i++)
{
if(Floatt[i]!="") //
{
m_result+=Floatt[i];
m_result+="\r\n";
}
}
UpdateData(false);
}
void CScannerDlg::OnClose()
{
// TODO: Add your control notification handler code here
OnOK();
}
void CScannerDlg::OnOpen()
{
// TODO: Add your control notification handler code here
CWnd *pParentWnd; //当前窗口句柄
pParentWnd = GetDlgItem( IDD_SCANNER_DIALOG );
CFileDialog openfile( 1, NULL, NULL, OFN_HIDEREADONLY, NULL, pParentWnd ); //“打开”对话框
openfile.m_ofn.lpstrTitle = "导入进行词法分析的源程序" ; //对话框的标题
if( openfile.DoModal() == IDOK ) //如果点击了“打开”按钮
{
CString path = openfile.GetPathName(); //所打开的文件的完整路径
CFile file( path, CFile::modeRead); //文件类对象
char* pBuf;
DWORD dwFileLen; //文件的长度
dwFileLen = file.GetLength();
pBuf = new char[dwFileLen+1]; //申请数组的长度为文件长度加1
pBuf[dwFileLen] = 0; //数组最后一个字符为/0
file.Read( pBuf, dwFileLen); //读取文件
file.Close();
m_source += pBuf; //将数组中内容输出到“源程序”编辑框
UpdateData(false); //显示输出
delete []pBuf;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -