⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scannerdlg.cpp

📁 自己在在做该实验之前
💻 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_SAVE, OnSave)
	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() 
{
	if(this->strToken.IsEmpty())
	{
		MessageBox("请先选择一个源文件或者保存你编辑的代码后再进行词法分析","词法分析器",MB_ICONEXCLAMATION);
		return;
	}
	FILE* fpin=fopen(strToken.GetBuffer(0),"r");
	
	
	
	// TODO: Add your control notification handler code here
    if(ends=true)
	{
		ch=' ';
		code=0;
		ends=false;
		for(int i=0;i<50;i++)
		{
	      Identifier[i]="";
		}
    	for(i=0;i<50;i++)
		{
	       Const[i]="";
		}
	    for(i=0;i<50;i++)
		{
	       PrimaryKey[i]=0;
		}

		strToken="";
	    point=0;
		m_result="";
		m_source="";
		strToken="";
		for(i=0;i<50;i++)
		{
	       Symbol[i]="";
		}
	}
	CString cc;
	UpdateData(true);
	int length=m_source.GetLength();
	if(m_source=="")
	{
		return;
	}

	while(point<length)
	{   
		ch=GetChar();
		GetBC();
		if(ch=='/')
		{
            ch=GetChar();
			if(ch=='/')
			{
				 while(ch!='\n')
				   ch=GetChar();
				Retract();
			}
            else if(ch=='*')
			{
				a:  while(ch!='*')
					ch=GetChar();
						ch=GetChar();
				 if(ch!='/')
				 goto a;
				ch=GetChar();
			}
		}
		if(ch=='\\')
		{
    ch=GetChar();
    ch=GetChar();
		}
	/*if(ch=='+')
		{
          ch=GetChar();
		  if(ch=='+')  
                Retract(); 
		  else
           ch=GetChar(); 
		}*/
		if(IsLetter())
		{   
			while( IsLetter() || IsDigit() )
			{
				Concat();
                ch=GetChar();
			}
			Retract();
			code=Reserve();
			if(code==0)
			{
                value=InsertId();
				strToken="";
				//return ($ID ,value);
			}
			else
			{
                PrimaryKey[code]=1;
				strToken="";
				//return  (code,-);
			}
		}
		else
//		{
			if(IsDigit())
			{   
				while(IsDigit())
				{
					Concat();
					ch=GetChar();
					if(ch=='.')
					{
						strToken+='.';
						ch=GetChar();
					}
				}
				Retract();
				value=InsertConst();
				//return ( $ int ,value);
			}
			else 
			{
				value=InsertSymbol();
			}
//		}
    }
	m_result="";
	ShowOut();
	ends=true;
}


char CScannerDlg::GetChar()
{   
	
	char ch=m_source.GetAt(point);
//	if(point<length)
	{
    	point++;
	}
	//else
	{
	//	ends=true;
	}
   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()
{
	strToken+=ch;
}
void CScannerDlg::Retract()
{
	point--;
	ch=' ';
}
int CScannerDlg::Reserve()   /// 是否是关键字
{
	CVocabulary bu;
	int p=bu.GetCode(strToken);  ///// 是否是关键字
	return p;
}
int CScannerDlg::InsertId()  //插入标志符
{
	for(int i=0;i<50;i++)
	{
		if(strToken==Identifier[i])
		{  	
			strToken="";
			break ;
		}
		if(Identifier[i]=="")
		{
			Identifier[i]=strToken;
			strToken="";
			break ;
		}
	}

	return i;
}
int CScannerDlg::InsertConst()   //插入常数
{
	for(int i=0;i<50;i++)
	{
		if(Const[i]==strToken)
		{
			strToken="";
			return i;
		}
		if(Const[i]=="")
		{
			Const[i]=strToken;
			strToken="";
			return i;
		}
	}
	return i;
}
int CScannerDlg::InsertSymbol()
{
	CVocabulary bu;
	for(int i=51;i<87;i++)
	{
	   if(ch==bu.Vocabulary[i])
	   {
		   Symbol[i-50]=1;
		   break;
	   }
	}
	return i;
}

void CScannerDlg::ShowOut()
{
	m_result+="-------关键字-------";
	m_result+="\r\n";
	CVocabulary bu;
	for(int i=1;i<39;i++)
	{
		if(PrimaryKey[i]==1)
		{	
			m_result+=bu.Vocabulary[i];
            m_result+="\r\n";
		}
	}
	m_result+="-------符号--------";
	m_result+="\r\n";
	for(i=51;i<86;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";
	int l=0;
	for(i=0;i<50;i++)
	{
		l=0;
		if(Const[i]!="")
		{
          for(int j=0;j<Const[i].GetLength();j++)
		  {
              if(Const[i].GetAt(j)=='.')
				  l++;
		  }
		  if(l>1)
		  {
			  m_result+=  Const[i];
				m_result+= "不是数字\r\n";
		  }

		else	m_result+=Const[i];
			m_result+="\r\n";
		}
	}
	UpdateData(false);
	

}

void CScannerDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	if(this->strToken.IsEmpty())
	{
		CFileDialog fd(false);
		fd.m_ofn.lpstrTitle="请选择你要打开的文件";  //标题
		fd.m_ofn.lpstrInitialDir="d:\\";            //初始目录
		if(fd.DoModal())
		{
			strToken=fd.GetPathName();
		}
			
	}

	CFile sf;
	if(sf.Open(this->strToken,CFile::modeWrite|CFile::modeCreate))
	{
		UpdateData();
		char buf[200]={0};
		GetDlgItem(IDC_EDIT_SOURCE)->GetWindowText(buf,200);
		sf.Write(buf,strlen(buf));
		sf.Close();
		MessageBox("保存成功","词法分析器",MB_ICONINFORMATION);
	}	
}

void CScannerDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
	CFileDialog fd(true);
	fd.m_ofn.lpstrTitle="请选择你要打开的文件";  //标题
	fd.m_ofn.lpstrInitialDir="d:\\";            //初始目录
	if(fd.DoModal())
	{
		strToken=fd.GetPathName();
		
		CStdioFile sf;
		CString str;
		if(sf.Open(strToken.GetBuffer(0),CFile::modeRead))
		{
			CString  strTemp;
			while(sf.ReadString(strTemp))
			{
				str+=strTemp;
				str+="\r\n";
			}	
			SetDlgItemText(IDC_EDIT_SOURCE,str);
			
			sf.Close();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -