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

📄 scandlg.cpp

📁 simple 语言词法分析器
💻 CPP
字号:
// scanDlg.cpp : implementation file
//
#include"stdlib.h"
#include "stdafx.h"
#include "scan.h"
#include "scanDlg.h"
#include"is.h"
#include"Splash.h"
int s=0,en=0;
int l=0,r=0;
char * ch;
class Token
{
public:
  int zh;
  CString zi;
  CString name;
  CString type;
  CString kind;
};
class Error
{
public:
	int line;
	int row;
	set(int,int);
	CString s;
	Error();
};
Error::Error()
{
 s=" 未知错误!";
}
Error::set(int l,int r)
{
 line=l;
 row=r;
}
Error err[150];
Token token[1000];
CString str[100];
#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()

/////////////////////////////////////////////////////////////////////////////
// CScanDlg dialog

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

void CScanDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CScanDlg)
	DDX_Control(pDX, IDC_LIST4, m_list4);
	DDX_Control(pDX, IDC_LIST3, m_list3);
	DDX_Control(pDX, IDC_LIST2, m_list2);
	DDX_Control(pDX, IDC_LIST1, m_list1);
	DDX_Text(pDX, IDC_EDIT1, m_input);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CScanDlg, CDialog)
	//{{AFX_MSG_MAP(CScanDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScanDlg message handlers

BOOL CScanDlg::OnInitDialog()
{   
    CREATESTRUCT cs;
    cs.dwExStyle|=WS_EX_TOOLWINDOW|WS_EX_TOPMOST;
    if(!PreCreateWindow(cs))
		MessageBox("PreCreateWindow Fails!");

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

void CScanDlg::OnButton1() 
{
 UpdateData(TRUE);
 m_list1.AddString(m_input);
 m_input="";
 UpdateData(FALSE);
}

void CScanDlg::OnButton2() 
{
  CString path;
  
  char filter[]="txt文件(*.txt)|*.txt|All Files(*.*)|*.*||";
  CFileDialog dlg(TRUE,"txt",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,filter,this);
  dlg.m_ofn.lpstrInitialDir="C:\\Documents and Settings\\stud\\桌面";
  if(dlg.DoModal()==IDOK)
  {
   path=dlg.GetPathName();
    
  

  CFile   file;   
  file.Open(path,CFile::modeRead,NULL);   
  CArchive   arIN(&file,CArchive::load);   
  CString   buf;   
  while (arIN.ReadString(buf)) //读一行   
  {   //static jishu=0;
      m_list1.AddString(buf);  
      //str[jishu]=buf;
	  //jishu++;
  } 
 }	
}

void CScanDlg::OnButton4() 
{
  int n1=m_list1.GetCurSel();
  m_list1.DeleteString(n1);
   int n2=m_list2.GetCurSel();
  m_list2.DeleteString(n2);
   int n3=m_list3.GetCurSel();
  m_list3.DeleteString(n3);
   int n4=m_list4.GetCurSel();
  m_list4.DeleteString(n4);
 
}

//DEL BOOL CScanDlg::PreCreateWindow(CREATESTRUCT &cs)
//DEL {
//DEL  if( !CDialog::PreCreateWindow(cs) )
//DEL 		return FALSE;
//DEL }

void CScanDlg::OnButton3()
{
 m_list4.ResetContent();
 m_list3.ResetContent();
 en=0;
 l=0;
 r=0;
 store(); 
 char *t2; 
 int  s1=0,s2=0;
 s1=s;
 for(int i=0;i<m_list1.GetCount();i++)
 {
 t2=(char*)(LPCSTR)str[i]; 
 readline(t2);
 } 
 s2=s;
 for(i=s1;i<s2;i++)
 {
 CString output(token[i].zi+")");
 char tk[10];
 sprintf(tk,"%d",token[i].zh);  
 output="("+CString(tk)+CString(',')+output;
 m_list2.AddString(output);
 }
 CString Errstr("错误数为:");
 char errn[10];
 sprintf(errn,"%d",en);
 Errstr+=CString(errn);
 m_list4.AddString(Errstr);
 for(i=0;i<en;i++)
 {   char ln[10],rw[10];
     sprintf(ln,"%d",err[i].line);
	 sprintf(rw,"%d",err[i].row+1);
	 m_list4.AddString("("+CString(ln)+","+CString(rw)+")"+err[i].s);
 }	
 
 for(i=s1;i<s2;i++)
 { 
   m_list3.AddString("("+token[i].name+","+token[i].type+","+token[i].kind+")"); 
	 
 }
}

void CScanDlg::readline(char *cha)
{ r=0;
  l++;
  ch=cha;
  while(*ch!='\0')
  {
   if(ischar(*ch))
	   readch();
   else if(isnum(*ch))
	   readnum();
   else if (isjie(*ch))
       readjie();
   else if(isnull(*ch)||*ch=='.')
   {ch++;r++;}
   else 
   {
	 err[en].set(l,r);
	 err[en].s=" 非法字符";
     en++;ch++;r++;
   }
  }
}

void CScanDlg::readch()
{ char result[25];
  for(int i=0;(ischar(*ch)||isnum(*ch))&&(*ch!='\0');i++,ch++,r++)
	 result[i]=*ch;
     result[i]='\0';
	 int t=getzh(result);
	 token[s].zh=t;
	 token[s].name=result;
	 
	 if(t==34) 
	 {
	   token[s].zi=result;
	   token[s].kind="标识符";
	   token[s].type="字符";
	 }
	 else 
	 {
	   token[s].zi="_";
	   token[s].kind="关键字";
	   token[s].type="字符";
	 }
	 s++;

}

int CScanDlg::getzh(CString s)
{ 
  if     (!strcmp(s,"and"))       return 1;
  else if(!strcmp(s,"array"))     return 2;
  else if(!strcmp(s,"begin"))	  return 3;
  else if(!strcmp(s,"bool"))      return 4;
  else if(!strcmp(s,"call"))      return 5;
  else if(!strcmp(s,"case"))      return 6;
  else if(!strcmp(s,"char"))      return 7;
  else if(!strcmp(s,"constant"))  return 8;
  else if(!strcmp(s,"do"))        return 9;
  else if(!strcmp(s,"else"))      return 10;
  else if(!strcmp(s,"end"))       return 11;
  else if(!strcmp(s,"false"))     return 12;
  else if(!strcmp(s,"for"))       return 13;
  else if(!strcmp(s,"if"))        return 14;
  else if(!strcmp(s,"input"))     return 15;
  else if(!strcmp(s,"integer"))   return 16;
  else if(!strcmp(s,"not"))       return 17;
  else if(!strcmp(s,"of"))        return 18;
  else if(!strcmp(s,"or"))        return 19;
  else if(!strcmp(s,"output"))    return 20;
  else if(!strcmp(s,"procedure")) return 21;
  else if(!strcmp(s,"program"))   return 22;
  else if(!strcmp(s,"read"))      return 23;
  else if(!strcmp(s,"real"))      return 24;
  else if(!strcmp(s,"repeat"))    return 25;
  else if(!strcmp(s,"set"))       return 26;
  else if(!strcmp(s,"then"))      return 27;
  else if(!strcmp(s,"to"))        return 28; 
  else if(!strcmp(s,"true"))      return 29;
  else if(!strcmp(s,"until"))     return 30;
  else if(!strcmp(s,"var"))       return 31;
  else if(!strcmp(s,"while"))     return 32;
  else if(!strcmp(s,"write"))     return 33;
  else if(!strcmp(s,"'"))         return 38;
  else if(!strcmp(s,"("))         return 39;
  else if(!strcmp(s,")"))         return 40;
  else if(!strcmp(s,"*"))         return 41;
  else if(!strcmp(s,"+"))         return 43;
  else if(!strcmp(s,","))         return 44;
  else if(!strcmp(s,"/"))         return 48;
  else if(!strcmp(s,":"))         return 50;
  else if(!strcmp(s,"<"))         return 53;
  else if(!strcmp(s,">"))         return 57;
  else if(!strcmp(s,"="))         return 56;
  else if(!strcmp(s,"["))         return 59;
  else if(!strcmp(s,"]"))         return 60;
  else if(!strcmp(s,"*/"))         return 42;
  else if(!strcmp(s,"**"))         return 47;
  else if(!strcmp(s,"/*"))         return 49;
  else if(!strcmp(s,":="))         return 51;
  else if(!strcmp(s,"<="))         return 54;
  else if(!strcmp(s,"<>"))         return 55;
  else if(!strcmp(s,">="))         return 58;
  else if(!strcmp(s,";"))          return 52;
  else if(!strcmp(s,"()"))         return 100;
  
  else if(!strcmp(s,"();"))        return 1000;
  else return 34;
}

void CScanDlg::readnum()
{
 int dotnum=0;
 r--;
 char result[15];
 for(int i=0;(*ch=='.'||isnum(*ch))&&(*ch!='\0');i++,ch++,r++)
	 result[i]=*ch;
     result[i]='\0';
 for(i=0;result[i]!='\0';i++)
 {
   if(result[i]=='.')
	dotnum++;
 }
   if(dotnum==0)
   {
	   token[s].zh=35;
	   token[s].name=result;
	   token[s].kind="常数";
	   token[s].type="整型";
	   token[s].zi=result;
       s++;
   }
   else if(dotnum==1)
   {
	   token[s].zh=36;
	   token[s].name=result;
	   token[s].kind="实常数";
	   token[s].type="实型";
	   token[s].zi=result;
       s++;
   }
   else
   {
     err[en].set(l,r);
	 err[en].s=" 小数点多于一个!";
	 en++;
   }
   
}

void CScanDlg::readjie()
{
 char result[15];
 r--;
 for(int i=0;(isjie(*ch)&&(*ch!='\0'));i++,ch++,r++)
	 result[i]=*ch;
     result[i]='\0';
	 int yz;
	 yz=getzh(result);
	 if(yz==34)
	 { 
		 err[en].set(l,r);
		 err[en].s="界符错误";
		 en++;
	 }
	 else if(yz==100)
	 {
	  ch--,r--;
	  token[s].zh=39;
	  token[s].name="(";
	  token[s].kind="界符";
	  token[s].type="字符";
	  token[s].zi="_";
	 s++;
	 }
	 else if(yz==1000)
	 {        
		token[s].zh=39;
	    token[s].name="(";
	    token[s].kind="界符";
	    token[s].type="字符";
	    token[s].zi="_";
	    s++;
		token[s].zh=40;
	    token[s].name=")";
	    token[s].kind="界符";
	    token[s].type="字符";
	    token[s].zi="_";
	    s++;
		token[s].zh=52;
	    token[s].name=";";
	    token[s].kind="界符";
	    token[s].type="字符";
	    token[s].zi="_";
	    s++;
	 }
	 else
	 {
	 token[s].zh=yz;
	 token[s].name=result;
	 token[s].kind="界符";
	 token[s].type="字符";
	 token[s].zi="_";
	 s++;
	 }
}

void CScanDlg::store()
{
 int cou=m_list1.GetCount();
 for(int i=0;i<cou;i++)
	 m_list1.GetText(i,str[i]);
 m_list2.ResetContent();
}





void CScanDlg::OnButton5() 
{
 m_list1.ResetContent();	
}



void CScanDlg::OnButton6() 
{   
  CString path;
  char filter[]="txt文件(*.txt)|*.txt|All Files(*.*)|*.*||";
  CFileDialog dlg(FALSE,"txt",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,filter,this);
  dlg.m_ofn.lpstrInitialDir="C:\\Documents and Settings\\stud\\桌面";
  if(dlg.DoModal()==IDOK)
  {
   path=dlg.GetPathName();    
  }
  CFile   file;   
  file.Open(path,CFile::modeCreate|CFile::modeWrite,NULL);   
  CArchive   arIN(&file,CArchive::store);   
  CString sv;
	for(int i=0;i<m_list2.GetCount();i++)
	{
	  m_list2.GetText(i,sv);
	  arIN.WriteString(sv);	
	  
	}
	
}

int CScanDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	 CSplashWnd::ShowSplashScreen(this);
	return 0;
}

⌨️ 快捷键说明

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