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

📄 编译原理_词法分析dlg.cpp

📁 这是很好的编译原理的词法分析程序。供大家参考!
💻 CPP
字号:
// 编译原理_词法分析Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "编译原理_词法分析.h"
#include "编译原理_词法分析Dlg.h"
#include  "fstream.h"
#include "iostream.h" 
#include <cstring>
#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()

/////////////////////////////////////////////////////////////////////////////
// C_Dlg dialog

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

void C_Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(C_Dlg)
	DDX_Control(pDX, ID_input, m_input);
	DDX_Text(pDX, IDC_EDIT1, m_key);
	DDX_Text(pDX, ID_output, m_editedit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(C_Dlg, CDialog)
	//{{AFX_MSG_MAP(C_Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// C_Dlg message handlers

BOOL C_Dlg::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);
	UpdateData(true);// Set small icon
	m_editedit.Empty();
	m_key.Empty();
	UpdateData(false);
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

#define  buffer_max_length  100000     //定义缓冲区的最大长度
#define  flag_max_length  11        //用来定义取出的标识符的最大长度,如果长度大于这个值,则接下去的字忽略不计
#define  key_number   8        //定义要比较的关键字的个数
#define  sign_number 12      //定义要比较的符号的个数
#define  limit_flag_number  6       //定义要比较的界符的个数
#define  word_taken_word_typepe_number  '5'       //定义单词的总类型共有5种
#define  word_long_taken_word_typepe '8'       //定义单词不合法且过长时的类型
#define  word_error_taken_word_typepe '7'       //定义单词不合法但没超过长度的类型

int taking_a_flag();//用来从缓冲区中取一个字符
 
char get_a_word_from_buffer();//从缓冲区中取一个单词

//using namespace std;
char Str[buffer_max_length];//建立缓冲区
char Filename[100];//定义存放路径名的字符串
int  point_str=-1;     //用来表示从缓冲区取字母时的指针,定义为-1,为使以下的程序++point_str可以先指到缓冲区的第一个字母
char ch;        //用来表示从缓冲区中取出的一个字母
char *KEY[key_number]={{"if"},{"int"},{"for"},{"while"},{"do"},{"return"},{"break"},{"continue"}};
char *Sign[sign_number]={{"+"},{"-"},{"*"},{"/"},{"%"},{"="},{"<"},{">="},{"<="},{"!="},{"=="},{">"}};
char *Limit[limit_flag_number]={{","},{";"},{"{"},{"}"},{"("},{")"}};
int  flag_buffer;//标志缓冲区中是否还有字母,如果没有的话,则flag_buffer的值为-1;如果还有的话,则其值为1
char Word[flag_max_length];//用来标识标识取出的单词,且其长度不超过
char word_taken_word_typepe;//用来标识得到的单词的类型
char first_last_taken_word_typepe;//用来标识是其前面的单词的类型,如果是
char taken_word_type;//定义在调用取单词后取出的单词的类型

void C_Dlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(true);
    //	UpdateData(true);
		char string[1000];

    m_editedit.Empty();
	m_key.Empty();

//	m_editedit+="";
//	m_key+="";
	int length,i,j,lengthA,sum;
	if(m_input.GetLineCount()>=2)
	{
	   sum=0;
	   for(i=0;i<m_input.GetLineCount();i++)
	   {
	      length=m_input.LineLength(m_input.LineIndex(i));
	      m_input.GetLine(i,string, length);
		  string[length]='\0';		  
		  for(j=0;j<length;j++)
		  {  
			  Str[sum+j]=string[j]; 		  
		  } 
		   sum+=length;   
	     //  AfxMessageBox(string);		
	   }
	}
	else
	{
	      length=m_input.LineLength(m_input.LineIndex(0));
	      m_input.GetLine(0,Str, length);
		  Str[length]='\0';
	}
	   lengthA=strlen(Str);
	   Str[lengthA]='\0';
	 	flag_buffer= taking_a_flag(); //从文件中取出第一个字符中,并存在ch变量中
    	flag_buffer=0;//初始化定义,认为缓冲区中还有字母

//	CString ab;
	while(flag_buffer!=-1)
	{ 
		
	//	flag_buffer= taking_a_flag();
		taken_word_type=get_a_word_from_buffer();//调用get_a_word_from_buffer()函数,得到一个单词,并返回此单词的类型
		if(taken_word_type==word_error_taken_word_typepe)//单词不合法,长度没超过flag_max_length,显示其具体内容
		{	m_editedit+="iderror    ";
		    m_editedit+="\"";
			 m_editedit+=Word;
			  m_editedit+="\"";
			   m_editedit+="\r\n";
			   	 UpdateData(false); 
		}
	 
		else if (taken_word_type==word_long_taken_word_typepe)//单词不合法,给出模糊描述
		{
			m_editedit+="id is too long    ";
		    m_editedit+="\"";
			 m_editedit+=Word;
			 m_editedit+="\"";
			  m_editedit+="......";
			   m_editedit+="\r\n";
			   	 UpdateData(false); 
		
		}
		
	 
		else if (taken_word_type<=word_taken_word_typepe_number)//单词合法,输出其类型,以及内容
		{
			 	m_editedit+="(";
					m_editedit+=taken_word_type;
						m_editedit+=",";
			       m_editedit+="\"";
				    m_editedit+=Word;
			    m_editedit+="\"";
				 m_editedit+=")";	
				 	 UpdateData(false); 
			if(taken_word_type=='1')
				   m_key+=Word;
			m_key+="\r\n";
				 UpdateData(false); 
		}
			m_editedit+="\r\n";
		
		//	m_key+="\r\n";
	}
    // m_editedit.Empty();
	//flag_buffer=1;
    lengthA=0;
 	Str[lengthA]='\0';
	 
	 UpdateData(false); 
 
	//CDialog::OnOK();
	 
}

int C_Dlg::taking_a_flag()
{
    ch=Str[++point_str];
	if (ch=='\0') return(-1);//当取出的字符是'\0',则表明缓冲区中已无字符,返回值为-1
	else return(1);
}

char C_Dlg::get_a_word_from_buffer()
{
     	bool flag=false;//用来表示取出的单词是否为关键词,如果是则flag的值为true,否则为false
	int k=-1;       //表示取出的字符放在单词数组的指针
	word_taken_word_typepe='0';      //预先定义的取出的单词的类型
	first_last_taken_word_typepe='0';   //初始化先前的类型,此变量为判断正负数用
	while (ch<=32 && ch>0)//去掉不能显示的字符
		flag_buffer=taking_a_flag();
	if (ch>='a'&& ch<='z'|| ch>='A'&& ch<='Z')//取出标识符或者是关键词
	{
		k=-1;
		while(true)//取出的单词长度不超过flag_max_length,如果超过,则其后的字符无效
		{  
			if((++k)<flag_max_length)//(++k)为了使取出的单词最长为flag_max_length
			{
				Word[k]=ch;
			}
			flag_buffer=taking_a_flag();
			  
			//如果取出的当前的字符不是字母或是数字,则此次取单词结束
			if  (!(ch>='a'&& ch<='z'|| ch>='A'&& ch<='Z'||ch>='0' && ch<='9'))
				break; 
		}
		Word[++k]='\0';//以'\0'标识取出单词的结束,以方便后面的判断此单词是标识符还是关键词
		for (int i=0;i<key_number;i++)//用来判断取出的当前单词是不是关键词
			if (strcmp(Word,KEY[i])==0)
			{ 
				flag=true;
				break;
			}
			if (flag)  word_taken_word_typepe='1';//如果是关键词,则把此单词的类型定义为1型
			else word_taken_word_typepe='2';      //否则是标识符,其类型为2
	
	}       
	else if (ch>='0' && ch<='9')//判断是不是为整数
	{  
		Word[++k]=ch;
		flag_buffer=taking_a_flag();
		while(true)//如果是数字,则一直接受,且定义它的类型为3
			if(ch>='0' && ch<='9')
			{
				Word[++k]=ch;
				flag_buffer=taking_a_flag();
			}
			else  {word_taken_word_typepe='3';break;}//不是数字则跳出循环
			//如果以字母开头,且长度不超过flag_max_length,且下面跟有字母,则此整数非法,输出类型为8,在主程序中输出出错信息
			if (k<flag_max_length)
				if (ch>='a'&& ch<='z'|| ch>='A'&& ch<='Z')
				{
					Word[++k]=ch;
					flag_buffer=taking_a_flag();
					//取这个非法整数的单词,其中可包含英文字母和数字
					while (ch>='a'&& ch<='z'|| ch>='A'&& ch<='Z'|| ch>='0' && ch<='9'||flag_buffer==-1)
						if (k<flag_max_length)
						{
							Word[++k]=ch;
							flag_buffer=taking_a_flag();
						}
						else 
						{
							flag_buffer=taking_a_flag();
							k++;
						}
						if (k>flag_max_length)//如果长度大于取出单词定义的最大长度,则返回类型为8,在主程序中打出出错信息
							word_taken_word_typepe=word_long_taken_word_typepe;
						else word_taken_word_typepe=word_error_taken_word_typepe;//否则返回类型7,在主程序出错,并明确写出此单词的具体内容
		}
			Word[++k]='\0';	
	}
	else if (ch=='+'||ch=='-'||ch=='*'||ch=='/')//取出是运算符号的单词
	{   
		Word[++k]=ch;
		if (ch=='+' || ch=='-')//如果是+号或者是-号,则还要判断是不是正负数
			if (first_last_taken_word_typepe=='6')//判断其取出的当前字母的前一个单词是6号类型的,即是<、>、<=、>=、==、=时则可判断现在取出的是整数
			{	
			
				flag_buffer=taking_a_flag();
				while(ch>='0' && ch<='9')//取出整数
				{
					Word[++k]=ch;
					flag_buffer=taking_a_flag();
				}
				word_taken_word_typepe='3';//如果正负整数,则直接返回类型为3,则表示此单词为整数
				return(word_taken_word_typepe);
         
			}
			else flag_buffer=taking_a_flag();//如果不是整数,则定义当前取出的单词是运算符,定义此类型为4
			Word[++k]=ch;
			word_taken_word_typepe='4';
	}
	else if (ch=='>'||ch=='<'||ch=='='||ch=='!')//取出运算符的另几类,即<、<=、>、>=、==、=、!=
	{    
		Word[++k]=ch;flag_buffer=taking_a_flag();
		if( ch=='=')//判断是不是<=、>=、==、!=
		{ 
			Word[++k]=ch;
			flag_buffer=taking_a_flag();
		}
		Word[++k]='\0';
		for (int i=0;i<sign_number;i++)//判断取出的单词是不是运算符
			if (strcmp(Sign[i],Word)==0) 
			{ 
				flag=true;
				break;
			}
			if (flag)  word_taken_word_typepe='4';
			
			first_last_taken_word_typepe='6';
		
	} 
	else if (ch==','||ch==';'||ch=='{'||ch=='}'||ch=='('||ch==')')//判断当前取出的单词是不是界符,如果是界符,则定义其类型为5
	{     
		word_taken_word_typepe='5';
		Word[++k]=ch;
		Word[++k]='\0';
		flag_buffer=taking_a_flag();
	}

	return(word_taken_word_typepe);
}

⌨️ 快捷键说明

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