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

📄 compiledlg.cpp

📁 用于用C++编写的词法分析分析器
💻 CPP
字号:
// CompileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Compile.h"
#include "CompileDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CCompileDlg dialog

CCompileDlg::CCompileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCompileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCompileDlg)
	m_fileName = _T("");
	m_edit = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCompileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCompileDlg)
	DDX_Control(pDX, IDC_LIST_RESAULT, m_listResault);
	DDX_Control(pDX, IDC_LIST_FILE, m_listFile);
	DDX_Text(pDX, IDC_EDIT_FILENAME, m_fileName);
	DDX_Text(pDX, IDC_EDIT1, m_edit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCompileDlg, CDialog)
	//{{AFX_MSG_MAP(CCompileDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_LOADFILE, OnButtonLoadfile)
	ON_LBN_SELCHANGE(IDC_LIST_FILE, OnSelchangeListFile)
	ON_BN_CLICKED(IDC_BUTTON_ANALYSE, OnButtonAnalyse)
	ON_BN_CLICKED(IDC_BUTTON_ANALYSEALL, OnButtonAnalyseall)
	ON_BN_CLICKED(IDC_BUTTON_ANALYSEEDIT, OnButtonAnalyseedit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCompileDlg message handlers

BOOL CCompileDlg::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
//状态表
	for(int j=0;j<16;j++)
	{
		for(int i=0;i<23;i++)
		{
			State[j][i]=0;

		}
	}//初试化

	
	//构造第一个状态的转换
//	State=new int[23][16];
	State[0][0]=1;
	State[0][1]=2;
	State[0][2]=3;
	State[0][3]=4;
	State[0][4]=8;
	State[0][5]=9;
	State[0][6]=11;
	State[0][7]=13;
	State[0][8]=15;
	State[0][9]=17;
//	State[0][10]=2;
	State[0][11]=19;
//	State[0][12]=2;
	State[0][13]=21;
//	State[0][14]=2;
	State[0][15]=0;    //状态为非终结的

	//构造第二个状态转换
	State[1][0]=1;
	State[1][15]=1;//状态为终结的

	//构造第三个状态
	State[2][1]=2;
	State[2][15]=1;

		//构造第五个状态
	State[4][4]=5;
	State[4][15]=1;

	//构造第五个状态
	State[4][4]=5;
	State[4][15]=1;

	//构造第六个状态
	//构造第七个状态
	for(int p=0;p<16;p++)
	{
		State[5][p]=5;
		State[6][p]=5;

	}
	State[5][4]=6;
	State[6][3]=7;
	State[6][4]=6;
	State[5][15]=State[6][15]=0;

	//构造其它状态
	State[9][7]=10;
	State[11][7]=12;
	State[13][7]=14;
	State[15][7]=16;
	State[17][10]=18;
	State[19][12]=20;
	State[20][14]=22;

//定义是否是结束状态
	State[1][15]=1;
	State[2][15]=1;
	State[3][15]=1;
	State[4][15]=1;
	State[7][15]=1;
	State[8][15]=1;
	State[9][15]=1;
	State[10][15]=1;
	State[11][15]=1;
	State[12][15]=1;
	State[13][15]=1;
	State[14][15]=1;
	State[15][15]=1;
	State[16][15]=1;
	State[18][15]=1;
	State[20][15]=1;
	State[22][15]=1;

	//CString headString=_T("");

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCompileDlg::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 CCompileDlg::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 CCompileDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCompileDlg::OnButtonLoadfile() 
{
    CStdioFile textFile;
	CString strItem;
	UpdateData(TRUE);
	if(!m_fileName.IsEmpty())
	{
		if(textFile.Open(m_fileName,CFile::modeRead | CFile::typeText))
		{
			    textFile.SeekToBegin();
				while(textFile.ReadString(strItem))
				{
					this->m_listFile.AddString(strItem);
					
				}
	        	UpdateData(TRUE);
			

		}
		else
		{
			AfxMessageBox("OPEN FILE FAIL!");
		}

		
	}
	else
	{
		AfxMessageBox("PLEASE INPUT A FILE NAME!");
	}
		
}

void CCompileDlg::OnSelchangeListFile() 
{
	this->m_listResault.ResetContent();
	CString selectString;
	int pos=this->m_listFile.GetCurSel( );
	m_listFile.GetText(pos,selectString);
	//获得选择的字符串
	Analyse(selectString);
	
}


void CCompileDlg::Analyse(CString& analyseString)
{
	//分析过程
	
	int begin=0;
	int front=0;
	int size=analyseString.GetLength();
	int preSta=0;
	int Sta=0;
	char preChar;

	for(int i=0;i<size;i++)//匹配,并且跳转状态
	{
		preSta=Sta;
		preChar=analyseString.GetAt(i);

   		if(isalpha(preChar))
		{

			Sta=State[Sta][0];
		}
		else if(isdigit(preChar))
		{
			Sta=State[Sta][1];
			
		}
		else if(preChar=='+'||preChar=='-'||preChar==';'||preChar==',')
		{
			Sta=State[Sta][2];
		}
		else if(preChar=='/')
		{
            Sta=State[Sta][3];
		}
		else if(preChar=='*')
		{
            Sta=State[Sta][4];
		}
		else if(preChar=='<')
		{
            Sta=State[Sta][5];
		}
		else if(preChar=='>')
		{
            Sta=State[Sta][6];
		}
		else if(preChar=='=')
		{
            Sta=State[Sta][7];
		}
		else if(preChar=='!')
		{
            Sta=State[Sta][8];
		}
		else if(preChar=='(')
		{
            Sta=State[Sta][9];
		}
		else if(preChar==')')
		{
            Sta=State[Sta][10];
		}
		else if(preChar=='[')
		{
            Sta=State[Sta][11];
		}
		else if(preChar==']')
		{
            Sta=State[Sta][12];
		}
		else if(preChar=='{')
		{
            Sta=State[Sta][13];
		}
		else if(preChar=='}')
		{
            Sta=State[Sta][14];
		}
		else if(isspace(preChar))
		{
			Sta=0;
		}
		else
		{
			Sta=-1;
		}
		//如果有错误
		if(Sta==-1)
		{
			CString errorchar=analyseString.Mid(i,1);
			this->m_listResault.AddString("THERE ARE BAD CHARAVTOR:"+errorchar);
			break;
		}
		//如果,跳转到一个错误的状态(回到初试化状态)
		if(Sta==0)
		{
			CString token;
			token=analyseString.Mid(begin,i-begin);
			token.Remove(' ');
			if(State[preSta][15]==1)
			{
				
				//判断跳转之前的状态是否为结束
				if(preSta==1)
				{
					if(token=="else"||token=="if"||token=="int"||token=="return"||token=="void"||token=="while")
					{
						this->m_listResault.AddString("KEY WORD:"+token);
					}
					else
					{
						this->m_listResault.AddString("ID:"+token);
					}
				}
				else if(preSta==2)
				{
					this->m_listResault.AddString("NUMBER:"+token);
				}
				else 
				{
					this->m_listResault.AddString("OPERATION:"+token);
				}
			}
			else
			{
				this->m_listResault.AddString("ERRORTOKEN:"+token);

			}
			begin=i;

		}
		//假如字符为最后一个字符了

		if(i==size-1)
		{
//			AfxMessageBox();
			CString token;
			token=analyseString.Mid(begin);
			token.Remove(' ');
			if(State[Sta][15]==1)
			{
				
				
				if(Sta==1)
				{
					if(token=="else"||token=="if"||token=="int"||token=="return"||token=="void"||token=="while")
					{
						this->m_listResault.AddString("KEY WORD:"+token);
					}
					else
					{
						this->m_listResault.AddString("ID:"+token);
					}
				}
				else if(preSta==2)
				{
					this->m_listResault.AddString("NUMBER:"+token);
				}
				else 
				{
					this->m_listResault.AddString("OPERATION:"+token);
				}


			}
			else
			{
				
				this->m_listResault.AddString("ERRORTOKEN:"+token);

			}
		}


	}

}

void CCompileDlg::OnButtonAnalyse() 
{
	if(this->m_listFile.GetSel(0)!=LB_ERR)
	{
	    OnSelchangeListFile();
	}
	
}

void CCompileDlg::OnButtonAnalyseall() 
{
	this->m_listResault.ResetContent();
	CString selectString;
	for(int i=0;i<m_listFile.GetCount();i++)
	{
	     m_listFile.GetText(i,selectString);
	     Analyse(selectString);
		 this->m_listResault.AddString("\n");
	}
	
}

void CCompileDlg::OnButtonAnalyseedit() 
{
    this->m_listResault.ResetContent();
	CString selectString;
	UpdateData(TRUE);
	selectString=this->m_edit;
	Analyse(selectString);
	
	
}



⌨️ 快捷键说明

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