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

📄 testdlg.cpp

📁 词法分析器和语法分析的一小部分 希望对大家会有帮助
💻 CPP
字号:
// testDlg.cpp : implementation file
//

#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#include <fstream.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#define  MAXBUF 255
#define  MAX    40

#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int k=0;
int tem;

char  ch =' ';	     // 存放读入当前的输入字符

struct key_word		 //关键字
{
	char word[MAXBUF];
};

struct key_word table[MAX];
char * str[]={"program","scanf","printf","short","#include","break","continue","input","output","begin","end","var","integer","real","for","to","if","then","else","do","while","write","array","proceure","int","char","goto","const","long","sizeof","switch","void","main","case","double","return","default","define"};

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDlg)
	m_a = _T("");
	m_b = _T("");

	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
	for( int j=0; j<38; j++)
	{ 
		strcpy(table[j].word,str[j]);
	}
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDlg)
	DDX_Text(pDX, IDC_EDIT1, m_a);
	DDX_Text(pDX, IDC_EDIT2, m_b);

	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN1, OnBtn1)
	ON_BN_CLICKED(IDC_BTN2, OnBtn2)
	ON_BN_CLICKED(IDC_BTNCL, OnBtncl)
	ON_BN_CLICKED(IDC_BTN4, OnBtn4)
	ON_BN_CLICKED(IDC_STT, OnStt)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BTN3, OnBtn3)
	ON_BN_CLICKED(IDC_BTN5, OnBtn5)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_SETCURSOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers

BOOL CTestDlg::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);


/*	((CListBox*)GetDlgItem(IDC_LIST3))->AddString("词法分析");
    ((CListBox*)GetDlgItem(IDC_LIST3))->AddString("语法分析");
static int num01=1;
if(num01==1)
{
	   GetDlgItem(IDC_BTN1)->EnableWindow(true);
	   GetDlgItem(IDC_BTN2)->EnableWindow(true);
	   GetDlgItem(IDC_BTN3)->EnableWindow(true);
	   GetDlgItem(IDC_BTN4)->EnableWindow(false);
       GetDlgItem(IDC_STT)->EnableWindow(false);
	   num01++;
}*/
    

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



void CTestDlg::OnBtn1() 
{
	// TODO: Add your control notification handler code here
	CFileDialog fDlg(true);
	fDlg.m_ofn.lpstrTitle="打开对话框";
    fDlg.m_ofn.lpstrFilter="Text Files(*.txt;*.h;*.cpp)\0*.txt;*.h;*.cpp\0All Files(*.*)\0*.*\0\0";

    
	if(IDOK==fDlg.DoModal())
	{
		CFile file(fDlg.GetFileName(),CFile::modeRead);
		char *pBuf;
		DWORD dFilelen;
		dFilelen=file.GetLength();
		pBuf=new char[dFilelen+1];
		pBuf[dFilelen]=0;
		file.Read(pBuf,dFilelen);
		m_a=pBuf;
		UpdateData(false);
		//MessageBox(pBuf);
		file.Close();
	}
	
}

void CTestDlg::OnBtn2() 
{
	// TODO: Add your control notification handler code here
   	CFileDialog fDlg(false);
	fDlg.m_ofn.lpstrTitle="保存对话框";
    fDlg.m_ofn.lpstrFilter="Text Files(*.txt;*.h;*.cpp)\0*.txt;*.h;*.cpp\0All Files(*.*)\0*.*\0\0";
    fDlg.m_ofn.lpstrDefExt="txt";
     
	if(IDOK==fDlg.DoModal())
	{
	  CFile file(fDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);
	  CString str;
	  UpdateData();
	  str=m_b;
	  file.Write(str,strlen(str));
	  file.Close();
	
	}
	
}



int CTestDlg::search(char *temp)
{
	for(int i=0; i<sizeof(table)/sizeof(table[0]); i++)
	  if(!strcmp(table[i].word,temp)) return  1;
	return 0;
   
}

void CTestDlg::OnBtncl() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_a="";
	m_b="";
	UpdateData(false);
}

void CTestDlg::OnBtn4() 
{
	// TODO: Add your control notification handler code here
	CString strb2="语法分析结果如下:\r\n";

	UpdateData();
	if(m_a=="")
  {
	MessageBox("请输入代码!");
  }
  else
  {
	CString str01;
	str01=m_a;
	k=0;
	m_a=m_a+' ';

	EE();
	
   if (tem==1) m_b=str01+"       \r\n不是该文法的句子.\n";
   else  m_b=str01+"       \r\n是该文法的句子.\n";
  
  }
	UpdateData(false);
}

void CTestDlg::EE()
{
        
		ch=m_a[k++];

	    if (ch=='e')  
		{
			BB();
			if(ch==' ')
			{
			  k=0;
			}
            ch=m_a[k++];
			if (ch=='a')
			{
				AA();
				if (m_a[k]==' ')
				{
					tem=0;return;
				}
				else
				{
					tem=1;return;
				}

			}
			else
			{
				tem=1;return;;
			}
		}
		else 
		{
			tem=1;return;;
		}
}

void CTestDlg::AA()
{
     ch=m_a[k++];
		if (ch=='a')  
		{

		}

		else if (ch=='b')
		{
		    AA();
		  if(ch==' ')
		  {
			 k=0;
		  }
          else
          {
            ch=m_a[k++];
			if (ch=='c')
			{
				BB();
			}
			else
			{
				tem=1;return;;
			}
          }
		}

	    else 
		{
			tem=1;return;;
		}
}

void CTestDlg::BB()
{
      ch=m_a[k++];
    	if (ch=='d')  
		{
		    EE();
			if(ch==' ')
			{
			  k=0;
			}
			else 
			{
            ch=m_a[k++];
			if (ch=='d')
			{
			
			}
			else
			{
				tem=1;return;;
			}
			}
		}

		else if (ch=='a')
		{
			CC();
		}

	    else 
		{
			tem=1;return;;
		}
}

void CTestDlg::CC()
{
      if(ch==' ')
	  {
	   k=0;
	  }
	  else
      { 
	    ch=m_a[k++];
		if (ch=='e')  
		{
		    
		}

		else if (ch=='d')
		{
		    CC();
		}

	    else 
		{
			tem=1;return;;
		}
	  }
}



void CTestDlg::OnStt() 
{
	// TODO: Add your control notification handler code here
    CString bl("\n\n     03计本8班 林金森 17号");
    static bool num01=true;
	if (num01==true)
	{
	  GetDlgItem(IDC_STT)->SetWindowText("   文法G(E):\n     E->eBaA\n     A->a|bAcB\n     B->dEd|aC\n     C->e|dc");
	  num01=false;
	}
    else 
	{
	  GetDlgItem(IDC_STT)->SetWindowText(bl);
      num01=true; 
	}
	
}

void CTestDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	CString bl("\n\n     03计本8班 林金森 17号");
	
	GetDlgItem(IDC_STT)->SetWindowText(bl);



}

void CTestDlg::OnBtn3() 
{
	// TODO: Add your control notification handler code here

	// TODO: Add your control notification handler code here

     char arr[MAXBUF];
 	 int k=0;
 	 int j=0;
     int nLength;
   	 int nLine=1; 

   if(m_a==" ")
   {
 	MessageBox("请输入代码!");
   }
   else
   {     
     UpdateData();
     CString str1("\r词法分析如果如下:\r\n");
 	m_b=m_b+str1;
 	nLength=m_a.GetLength()+1;
 	m_a=m_a+" ";
    ch=m_a[k++];
 
 		while (nLength!=k) 		//读入字符判断,空格、字母、数字、界符
	{
		
		if(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')
		{
		  ch=m_a[k++];
		}

		else if(isdigit(ch))
		{
			while(isdigit(ch))			//判断和读取数字
			{ 
				arr[j++]=ch;
				ch=m_a[k++];
			} 
			arr[j]='\0';
			j=0;
            m_b=m_b+"3  数  字"+"     "+arr+"\r\n";
		}

		else if(isalpha(ch))
		{
			while(isalpha(ch)||isdigit(ch))			//判断和读取标识符
			{ 
				arr[j++]=ch;
				ch=m_a[k++];
			} 
			arr[j]='\0';
			j=0;
            if (search(arr))	//如果是关键字
			{
				m_b=m_b+"1  保留字"+"     "+arr+"\r\n";
			}
			else  
				m_b=m_b+"2  标识符"+"     "+arr+"\r\n";	//普通标志符
		}

		else if(ch==':')  
		{
			ch=m_a[k++];
			if(ch=='=') 
			{
				m_b=m_b+"4  运算符"+"     "+":="+"\r\n";     //如果是 :=
				ch=m_a[k];
			}
			else
			{
				m_b=m_b+"4  运算符"+"     "+":"+"\r\n";      //如果是 :
				k--;
				ch=m_a[k];
			}
		}

		else if(ch=='!')  
		{
			ch=m_a[k++];
			if(ch=='=') 
			{
				m_b=m_b+"4  运算符"+"     "+"!="+"\r\n";     //如果是 !=
				ch=m_a[k];
			}
			else
			{
				m_b=m_b+"非法字符"+"\r\n";      //如果是!
				k--;
				ch=m_a[k];
			}
		}

		else if(ch=='&')  
		{
			ch=m_a[k++];
			if(ch=='&') 
			{
				m_b=m_b+"4  运算符"+"     "+"&&"+"\r\n";     //如果是 &&
				ch=m_a[k];
			}
			else
			{
				m_b=m_b+"4  运算符"+"     "+"&"+"\r\n";      //如果是 &
				k--;
				ch=m_a[k];
			}
		}

		else if(ch=='|')  
		{
			ch=m_a[k++];
			if(ch=='|') 
			{
				m_b=m_b+"4  运算符"+"     "+"||"+"\r\n";     //如果是 ||
				ch=m_a[k];
			}
			else
			{
				m_b=m_b+"4  运算符"+"     "+"|"+"\r\n";      //如果是 |
				k--;
				ch=m_a[k];
			}
		}



		else if (ch=='>')
		{
			ch=m_a[k++];
			if(ch=='=')        
			{
				m_b=m_b+"4  运算符"+"     "+">="+"\r\n";     //如果是 >=
				ch=m_a[k];
			}
			else
			{
				m_b=m_b+"4  运算符"+"     "+">"+"\r\n";      //如果是 >
				k--;
				ch=m_a[k];
			}
		}

		else if(ch=='<')
		{ 
			ch=m_a[k++];
			if(ch=='>')
			{
				m_b=m_b+"4  运算符"+"     "+"<>"+"\r\n";  // 如果是 <>
				ch=m_a[k];
			}
			else if(ch=='=')
			{
				m_b=m_b+"4  运算符"+"     "+"<="+"\r\n";   //如果是 <=
				ch=m_a[k];
			}
			else 
			{
				m_b=m_b+"4  运算符"+"     "+"<"+"\r\n";   //如果是 <
				k--;
				ch=m_a[k];
			}
		}

		else if(ch=='/')
		{ 
			ch=m_a[k++];
			if(ch=='*')
			{
				ch=m_a[k++];
s:
				while(ch!='*')
				{
					ch=m_a[k++];
				}
				while(ch=='*')
				{
					ch=m_a[k++];
					while(ch!='/')
					{
						goto s;			//如果是注释
					}
					m_b=m_b+"注 释"+"     "+"//"+"\r\n";
				}
			}
			else if(ch=='/')
			{
				ch=m_a[k++];
				while(ch!='\n')
				{
					ch=m_a[k++];	  //如果是注释 //
				}
				m_b=m_b+"注 释"+"     "+"/* */"+"\r\n";
			}
			else 
			{  
				k--;
				ch=m_a[k];
			}
		}

		else if(ch=='+')
		{
			m_b=m_b+"4  运算符"+"     "+"+"+"\r\n";ch=m_a[k++];
		}
		else if(ch=='-')
		{
			m_b=m_b+"4  运算符"+"     "+"-"+"\r\n";ch=m_a[k++];
		}
	    else if(ch=='#')
		{
			m_b=m_b+"4  运算符"+"     "+"#"+"\r\n";ch=m_a[k++];
		}
		else if(ch=='*')
		{
			m_b=m_b+"4  运算符"+"     "+"*"+"\r\n";ch=m_a[k++];
		}
		else if(ch=='^')
		{
			m_b=m_b+"4  运算符"+"     "+"^"+"\r\n";ch=m_a[k++];
		}
		else if(ch=='(')
		{
			m_b=m_b+"5  界符  "+"     "+"("+"\r\n";ch=m_a[k++];
		}
		else if(ch==')')
		{
			m_b=m_b+"5  界符  "+"     "+")"+"\r\n";ch=m_a[k++];
		}
		else if(ch=='{')
		{
			m_b=m_b+"5  界符  "+"     "+"{"+"\r\n";ch=m_a[k++];
		}
		else if(ch=='}')
		{
			m_b=m_b+"5  界符  "+"     "+"}"+"\r\n";ch=m_a[k++];
		}
		else if(ch=='[')
		{
			m_b=m_b+"5  界符  "+"     "+"["+"\r\n";ch=m_a[k++];
		}
		else if(ch==']')
		{
			m_b=m_b+"5  界符  "+"     "+"]"+"\r\n";ch=m_a[k++];
		}
		else if(ch=='.')
		{
			m_b=m_b+"4  运算符"+"     "+"."+"\r\n";ch=m_a[k++];
		}
		else if(ch==';')
		{
			m_b=m_b+"5  界符  "+"     "+";"+"\r\n";ch=m_a[k++];
		}
		else if(ch=='=')
		{
			m_b=m_b+"4  运算符"+"     "+"="+"\r\n";ch=m_a[k++];
		}
		else if(ch==',')
		{
			m_b=m_b+"5  界符  "+"     "+","+"\r\n";ch=m_a[k++];
		}


		else 
		{
			m_b=m_b+"6  非法字符"+"\r\n";
			ch=m_a[k++];
		}
			
	}
		UpdateData(FALSE);

	
}
}

void CTestDlg::OnBtn5() 
{
	// TODO: Add your control notification handler code here
	MessageBox("还没完成 -_-!");
	 
}

void CTestDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    
    
    
	CDialog::OnLButtonDown(nFlags, point);
}

void CTestDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
   


	CDialog::OnLButtonUp(nFlags, point);
}

void CTestDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR1));
	CDialog::OnMouseMove(nFlags, point);
}

BOOL CTestDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default

	
	return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

⌨️ 快捷键说明

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