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

📄 shixunview.cpp

📁 笨程序实现了编译原理中的编译程序
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// shixunView.cpp : implementation of the CShixunView class
//

#include "stdafx.h"
#include "shixun.h"
#include <vector>
using namespace std;
#include "stdio.h"	//定义Kernighan和Ritchie在Unix System V 中定义的标准和扩展的类型和宏。
					//还定义标准I/O 预定义流:stdin,stdout和stderr,说明 I/O流子程序
//#include "math.h"	//说明数学运算函数,还定了 HUGE VAL 宏, 说明了matherr和matherr子程序用到的特殊结构
#include "string.h"	//说明一些串操作和内存操作函数
//#include "malloc.h"	//定义动态链表所用到的函数库
#include "stdlib.h"
#include "shixunDoc.h"
#include "shixunView.h"
char temp2[30];
char temp20[30];
char temp5[30];
char temp6[30];
char temp7[30];
char temp8[30];
char temp9[30];
char temp10[30];
char temp11[30];
char temp12[30];
/*char temp13[30];
char temp14[30];
char temp15[30];
char temp16[30];
char temp17[30];
char temp18[30];
char temp19[30];
char temp20[30];*/
int k2=0;

//using namespace std;
vector<char *> v;
#define NULL 0
#define EOP -1
char keyword[6][10]={{"real"},{"else"},{"main"},{"if"},{"char"},{"int"}};//标识符表 

CHARFORMAT yanse;

//unsigned int flog=0;	//全局变量,可随便调用

struct 		//保存结果二元式流的结构体
{
	char *class0;
	char value0[30];
}
result[100];

struct 		//保存坐标的结构体
{
	char value1[30];
	char value2[30];
}
result1[100];

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CShixunView

IMPLEMENT_DYNCREATE(CShixunView, CFormView)

BEGIN_MESSAGE_MAP(CShixunView, CFormView)
	//{{AFX_MSG_MAP(CShixunView)
	ON_BN_CLICKED(IDC_BUTTON_READ, OnButtonRead)
	ON_BN_CLICKED(IDC_BUTTON_BAOCUN, OnButtonBaocun)
	ON_BN_CLICKED(IDC_BUTTON_QINGLING, OnButtonQingling)
	ON_BN_CLICKED(IDC_BUTTON_CIFA, OnButtonCifa)
	ON_BN_CLICKED(IDC_BUTTON_TUICHU, OnButtonTuichu)
	ON_BN_CLICKED(IDC_BUTTON_YUFA, OnButtonYufa)
	ON_NOTIFY(NM_CLICK, IDC_RICHEDIT, OnClickRicheditResouce)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShixunView construction/destruction

CShixunView::CShixunView()
	: CFormView(CShixunView::IDD)
{
	//{{AFX_DATA_INIT(CShixunView)
	m_jieguo = _T("");
	m_chucuo = _T("");
	m_resouce = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	ZeroMemory(&yanse, sizeof(CHARFORMAT));			
	yanse.cbSize = sizeof(CHARFORMAT);
//	InitRichEdit();

}

CShixunView::~CShixunView()
{
}

void CShixunView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CShixunView)
	DDX_Control(pDX, IDC_RICHEDIT_RESOUCE, m_a);
	DDX_Control(pDX, IDC_BUTTON_YUYI, m_yuyi);
	DDX_Control(pDX, IDC_BUTTON_YUFA, m_yufa);
	DDX_Control(pDX, IDC_BUTTON_TUICHU, m_tuichu);
	DDX_Control(pDX, IDC_BUTTON_READ, m_read);
	DDX_Control(pDX, IDC_BUTTON_QINGLING, m_qingling);
	DDX_Control(pDX, IDC_BUTTON_CIFA, m_cifa);
	DDX_Control(pDX, IDC_BUTTON_BAOCUN, m_baocun);
	DDX_Text(pDX, IDC_EDIT_JIEGUO, m_jieguo);
	DDX_Text(pDX, IDC_EDIT_CHUCUO, m_chucuo);
	DDX_Text(pDX, IDC_RICHEDIT_RESOUCE, m_resouce);
	//}}AFX_DATA_MAP
}

BOOL CShixunView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CShixunView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CShixunView printing

BOOL CShixunView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CShixunView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CShixunView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CShixunView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CShixunView diagnostics

#ifdef _DEBUG
void CShixunView::AssertValid() const
{
	CFormView::AssertValid();
}

void CShixunView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CShixunDoc* CShixunView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CShixunDoc)));
	return (CShixunDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CShixunView message handlers

void CShixunView::OnButtonRead() 
{
	// TODO: Add your control notification handler code here
	char ch;
	FILE *fp;
	m_resouce=_T("");
	//m_chucuo.Format("gg%d",12);
	m_jieguo=_T("");
	UpdateData(FALSE);
	CString strFilter = _T("TXT File (*.txt)|*.txt|BY File (*.by)|*.by|DOC File (*.doc)|*.doc|");
	CFileDialog dlgOpen(TRUE, NULL, NULL, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY, 
		strFilter, this);
	if (IDOK == dlgOpen.DoModal()) 
	{
		filePath = dlgOpen.GetPathName();
		fp=fopen(filePath,"r");
	}
	else
	{
		fp=NULL;
	}
	if(fp==NULL)
		AfxMessageBox("无法读取文件,请检查路径是否正确再重试");
	else
	{
		m_resouce=_T("");
		ch=fgetc(fp);
		while(ch!=EOF)
		{
		
			if(ch=='\n')
			m_resouce+=_T("\r");
			m_resouce+=_T(ch);
			//row++;
			ch=fgetc(fp);
		}
		m_resouce+=_T("\r");
		UpdateData(FALSE);
	}
	//filePath.Close();
	if(m_resouce!=_T(""))
	{
		m_baocun.EnableWindow(TRUE);
		m_cifa.EnableWindow(TRUE);
	}
}

void CShixunView::OnButtonBaocun() 
{
	// TODO: Add your control notification handler code here
	FILE *fp;
	int l;
	UpdateData(TRUE);
	if(m_resouce==_T(""))
		AfxMessageBox("文件为空,无法保存!");
	else
	{
		m_resouce.Replace("\r","");
		l=m_resouce.GetLength();	
		if((fp=fopen("F:/text1.by","w+t"))!=NULL)
		fwrite(m_resouce,sizeof(char),l,fp);
		fclose(fp);
		MessageBox("源文件保存成功!");
		
		//this->OnButtonRead();
		
	}
}

void CShixunView::OnButtonQingling() 
{
	// TODO: Add your control notification handler code here
	m_resouce=_T("");
	//m_shezhi=_T("");
	m_jieguo=_T("");
	m_chucuo=_T("");
	UpdateData(FALSE);
	m_baocun.EnableWindow(FALSE);
	m_cifa.EnableWindow(FALSE);
	m_yufa.EnableWindow(FALSE);
	m_yuyi.EnableWindow(FALSE);
}

void CShixunView::OnButtonCifa() 
{
	// TODO: Add your control notification handler code here
	int i=0,j=0,j1=0,add=0;
	int k=0;
	int c=0,d=0;
	unsigned int row=1,line=1;	//记录行数,列数
	char a[1000];        //保存源文件
	char temp[30];      //临时存储数据数组
	char A[1000];
	//char temp1[100];
	
	strcpy(a,"\0");
	strcpy(temp,"\0");
	UpdateData(TRUE);
	m_chucuo=_T("");
	m_jieguo=_T("");
	UpdateData(FALSE);
	c=m_resouce.GetLength();
	while(c--)
	{
		a[d]=m_resouce.GetAt(d); //将程序存放道a[]数组中
		d++;
	}
	a[d]='\0';					
	int b=0;
	char R[300];
	char L[300];
	R[b]=row;
	L[b]=line;

	yanse.crTextColor = RGB(0,0,0);//设置颜色
	//m_a.Setsel(0,-1);
	//m_a.SetSelectionCharFormat(yanse);
	while(a[i]!='\0')
	{
		if(isalpha(a[i]))
		{
			int m=0,n=0,q=0;
			strcpy(temp,"\0");
			while(isalpha(a[i])||(isdigit(a[i]))) //isalpha()判断某字符是否是英文字母,是则返回非零值,否则返回零值。isdigit()判断一个字符是否为数字(0-9),是数字返回1,不是返回0;
			{
				temp[m]=a[i];
				m++;
				i++;
			}
			temp[m]='\0';
			if(m>=8)            //判断字符长度是否大于八,是则出错
			{
				itoa(row,R,10);
				itoa(line,L,10);
				add++;
				itoa(add,A,10);
				m_chucuo+=_T(A);
				m_chucuo+=_T(".位置:");
				m_chucuo+=_T("非法标识符");
				m_chucuo+=_T(temp);
				m_chucuo+=_T("出现在第");
				m_chucuo+=_T(R);
				m_chucuo+=_T("行,第");
				m_chucuo+=_T(L);
				m_chucuo+=_T("列");
				m_chucuo+=_T(";\t");
				m_chucuo+=_T("//标识符字符长度不大于8!\r\n");
			}
			while(strcmp(temp,keyword[n])!=0) //跟关键字匹配
				{
					n++;
					if(n>=6)
						break;
				}
			if(n<6)
			{
				result[j].class0="1";
				strcpy(result[j].value0,temp);
				itoa(row,R,10);
				itoa(line,L,10);
				strcpy(result1[j1].value1,R);
				strcpy(result1[j1].value2,L);
				line+=m;
			}
			
			else
			{
				result[j].class0="2";
				strcpy(result[j].value0,temp);
				itoa(row,R,10);
				itoa(line,L,10);
				strcpy(result1[j1].value1,R);
				strcpy(result1[j1].value2,L);
				line+=m;
			}
			j++;
			j1++;
		}
		else
		{
			if(isdigit(a[i]))
			{
				if('1'<=a[i]&&a[i]<='9')
				{
					i++;	
					if(isdigit(a[i])||(a[i]=='.'))
					{
						int h=0,z=0;
						strcpy(temp,"\0");

						i=i-1;
						while(isdigit(a[i])||(a[i]=='.'))
						{
							temp[h]=a[i];
							h++;
							i++;
						}
						temp[h]='\0';
						z=atoi(temp);
						if(z>65535)
						{
							itoa(row,R,10);
							itoa(line,L,10);
							add++;
							itoa(add,A,10);
							m_chucuo+=_T(A);
							m_chucuo+=_T(".位置:");
							m_chucuo+=_T("非法常量");
							m_chucuo+=_T(temp);
							m_chucuo+=_T("出现在第");
							m_chucuo+=_T(R);
							m_chucuo+=_T("行,第");
							m_chucuo+=_T(L);
							m_chucuo+=_T("列");
							m_chucuo+=_T(";\t");
							m_chucuo+=_T("//整数不能超过65535!\r\n");
						}
						result[j].class0="3";
						strcpy(result[j].value0,temp);
						j++;
						itoa(row,R,10);
						itoa(line,L,10);
						strcpy(result1[j1].value1,R);
						strcpy(result1[j1].value2,L);
						line+=h;
						j1++;
					}
					else if((a[i]==',')||(a[i]==';')||(a[i]=='+')||(a[i]=='-')||(a[i]=='*')||(a[i]=='/'))
					{
						int s=0;
						strcpy(temp,"\0");
						i=i-1;
						temp[s]=a[i];
						i++;
						s++;
						temp[s]='\0';
						result[j].class0="3";
						strcpy(result[j].value0,temp);
						j++;
						itoa(row,R,10);
						itoa(line,L,10);
						strcpy(result1[j1].value1,R);
						strcpy(result1[j1].value2,L);
						line+=s;
						j1++;
					}
					else
					{
						int h=0,z=0;
						strcpy(temp,"\0");

						i=i-1;
						temp[h]=a[i];
						h++;
						i++;
						temp[h]='\0';
						result[j].class0="2";
						strcpy(result[j].value0,temp);
						j++;
						itoa(row,R,10);
						itoa(line,L,10);
						strcpy(result1[j1].value1,R);
						strcpy(result1[j1].value2,L);
						line+=h;
						j1++;
					}
				
				}
				else if(a[i]=='0')
				{
					i++;
					
					if(isdigit(a[i]))
					{
						int h=0,z=0;
						strcpy(temp,"\0");
						i=i-1;
						temp[h]=a[i];
						h=1;
						i=i+1;
						while(isdigit(a[i]))
						{
							temp[h]=a[i];
							h++;
							i++;
						}
						temp[h]='\0';
						//temp[s]='\0';
						result[j].class0="8";
						strcpy(result[j].value0,temp);
						j++;
						itoa(row,R,10);
						itoa(line,L,10);
						strcpy(result1[j1].value1,R);
						strcpy(result1[j1].value2,L);
						line+=h;
						j1++;
						add++;
						itoa(add,A,10);
						m_chucuo+=_T(A);
						m_chucuo+=_T(".位置:");
						m_chucuo+=_T("非法标识符");
						m_chucuo+=_T(temp);
						m_chucuo+=_T("出现在第");
						m_chucuo+=_T(R);
						m_chucuo+=_T("行,第");
						m_chucuo+=_T(L);
						m_chucuo+=_T("列");
						m_chucuo+=_T(";\t");
						m_chucuo+=_T("//常量无法识别!\r\n");
					}
					else if(a[i]=='.')
					{
						int q=0,z=0;
						strcpy(temp,"\0");
						i=i-1;
						temp[q]=a[i];
						q=1;
						i=i+1;
						temp[q]=a[i];
						q=2;
						i=i+1;
						while(isdigit(a[i]))
						{
							temp[q]=a[i];
							i++;
							q++;
						}
						temp[q]='\0';
						result[j].class0="3";
						strcpy(result[j].value0,temp);
						j++;
						itoa(row,R,10);
						itoa(line,L,10);
						strcpy(result1[j1].value1,R);
						strcpy(result1[j1].value2,L);
						line+=q;
						j1++;
					}
					else
					{
						int h=0,z=0;
						strcpy(temp,"\0");

						i=i-1;
						temp[h]=a[i];
						h++;
						i++;
						temp[h]='\0';
						result[j].class0="3";
						strcpy(result[j].value0,temp);
						j++;
						itoa(row,R,10);
						itoa(line,L,10);
						strcpy(result1[j1].value1,R);
						strcpy(result1[j1].value2,L);
						line+=h;
						j1++;
					}
					

					
				}

			
			}
			else
			{
				strcpy(temp,"\0");
				if(a[i]=='+')
				{
					result[j].class0="4";
					strcpy(result[j].value0,"+");					
					j++;
					i++;
					itoa(row,R,10);
					itoa(line,L,10);
					strcpy(result1[j1].value1,R);
					strcpy(result1[j1].value2,L);
					line+=1;
					j1++;
				}
				else if(a[i]=='-')
				{
					result[j].class0="4";
					strcpy(result[j].value0,"-");
					j++;
					i++;
					itoa(row,R,10);
					itoa(line,L,10);
					strcpy(result1[j1].value1,R);
					strcpy(result1[j1].value2,L);
					line+=1;
					j1++;
				}
				else if(a[i]=='*')
				{
					result[j].class0="4";
					strcpy(result[j].value0,"*");
					j++;
					i++;
					itoa(row,R,10);
					itoa(line,L,10);
					strcpy(result1[j1].value1,R);
					strcpy(result1[j1].value2,L);
					line+=1;
					j1++;
				}
				else if(a[i]=='/')
				{
					i++;
					if(a[i]=='/')
					{
						i++;
						line+=2;
						while(a[i]!=10&&a[i+1]!=NULL)i++;
						if(a[i+1]!=NULL)
						{
							row++;
							line=1;
						}
					}
					else if(a[i]=='*')
					{
						i++;
						line+=2;
						 while(a[i]!=NULL)
						 {
							 if((a[i]=='*')&&(a[i+1]=='/'))
							 {
								 i+=2;
								 line+=2;
								 break;
							 }
							else if(a[i]==10)
							 {
								 row++;
								 line=1;
							 }
							else if(a[i]==9)
							{
								line+=8-(line%8)+1;
								//i++;
							}
							else
								line++;
								i++;
						 }
					}
					else
					{
						i--;
						result[j].class0="4";
						strcpy(result[j].value0,"/");
						j++;

⌨️ 快捷键说明

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