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

📄 luluview.cpp

📁 整个工作过程主要划分成词法分析阶段、语法分析阶段、中间代码生成和目标代码生成四个部分。 第一阶段是把输入的字符串形式的源程序分割成一个个单词符号。 第二阶段是识别由词法分析给出的单词符号序列在
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// luluView.cpp : implementation of the CLuluView class
//

#include "stdafx.h"
#include "lulu.h"
#include "MainDialog.h"
#include "MainFrm.h"
#include "Message.h"


#include "luluDoc.h"
#include "luluView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLuluView

IMPLEMENT_DYNCREATE(CLuluView, CView)

BEGIN_MESSAGE_MAP(CLuluView, CView)
	//{{AFX_MSG_MAP(CLuluView)
	ON_WM_CREATE()
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_WORD, OnWord)
	ON_COMMAND(ID_Grammer, OnGrammer)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLuluView construction/destruction

CLuluView::CLuluView()
{
	// TODO: add construction code here

}

CLuluView::~CLuluView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CLuluView drawing

void CLuluView::OnDraw(CDC* pDC)
{
	CLuluDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	//MainDialog MD;
	//if(MD.DoModal()==IDOK)
		//MD.DestroyWindow();
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CLuluView diagnostics

#ifdef _DEBUG
void CLuluView::AssertValid() const
{
	CView::AssertValid();
}

void CLuluView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CLuluView message handlers
void CLuluView::CreateEdit()
{
	m_edit.Create(ES_MULTILINE|WS_VSCROLL|WS_HSCROLL|ES_LEFT|WS_VISIBLE,
		CRect(10,0,756,429),this,IDC_EDIT);
}

int CLuluView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	CreateEdit();
	return 0;
}

void CLuluView::OnFileSave() 
{
	// TODO: Add your command handler code here
	CString tt;
	FILE* path;
	CFileDialog dlg(FALSE,"txt","lulu",OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY, "文本 文件(*.txt)|*.txt||", this); 
	dlg.m_ofn.lpstrTitle = _T("保存程序文件"); 
	if (dlg.DoModal() == IDOK)
	{
		CString fname=dlg.GetPathName();
		m_edit.GetWindowText(tt);
		path=fopen(fname,"w");
		fprintf(path,"%s",tt);
		fclose(path);
	}
}

void CLuluView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	char * temp=new char[128];
	CString tt;
	//int i;
	FILE* path;
	CFileDialog dlg(TRUE,"txt","",OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY, "文本 文件(*.txt)|*.txt||", this); 
	dlg.m_ofn.lpstrTitle = _T("打开程序文件"); 
	if (dlg.DoModal() == IDOK)
	{
		CString fname=dlg.GetPathName();
		path=fopen(fname,"r");
		while(fgets(temp,128,path))
			tt+=temp;
		//char * temp =new char[i];
		//strncpy(temp,tt,i);
		m_edit.SetWindowText(tt);
		fclose(path);
	}
	delete temp;
}

void CLuluView::OnFileNew() 
{
	// TODO: Add your command handler code here
	m_edit.SetWindowText("");
}

int Reserve(CString strToken)//查找是否为关键字
{
	FILE* path;
	CString aword;
	path=fopen("reserveword.rw","r");
	while(!feof(path))
	{
		fscanf(path,"%s",aword);
		if(!strToken.CompareNoCase(aword))
			return 9999;
	}
	fclose(path);
	return 0;
/*	if(!strToken.Compare("lulu"))
		return 9999;
    else if(!strToken.Compare("IF")) 
		return 100;
	else if(!strToken.Compare("THEN")) 
		return 200;
	else if(!strToken.Compare("WHILE")) 
		return 300;
	else if(!strToken.Compare("DO")) 
		return 400;
	else if(!strToken.Compare("ELSE")) 
		return 500;
	else if(!strToken.Compare("REPEAT")) 
		return 600;
	else if(!strToken.Compare("UNTIL")) 
		return 700;
	else if(!strToken.Compare("FOR")) 
		return 800;
	else if(!strToken.Compare("TO")) 
		return 900;
	else if(!strToken.Compare("BEGIN")) 
		return 10000;
	else if(!strToken.Compare("END")) 
		return 11000;
	else if(!strToken.Compare("READ")) 
		return 12000;
	else if(!strToken.Compare("WRITE")) 
		return 13000;
	else
		return 0;
		*/
}

void CLuluView::OnWord() 
{
	// TODO: Add your command handler code here
	CString tt;
	FILE* path;
		
	m_edit.GetWindowText(tt);
	path=fopen("word.wl","w+");

    int code;
	char ch;
	char* p=tt.GetBuffer(0);
	char* start=p;
	CString strToken ="";
	CString strtmp ="";
	ch=*p++;
	while(ch==' ')
		ch=*p++;
	int length=tt.GetLength();
	while(p-start<=length)
	{
	if(isalpha(ch))
	{
			  while (isalpha(ch) || isdigit(ch))
			  { 
				  strToken+=ch;
				  ch=*p++;
			  }
				  p--;
				   char* temp=p;
			if(ch =='[')
			{
				p++;
				strtmp+=ch;
				ch=*p++;
				if(isalpha(ch) || isdigit(ch))
				{
					while (isalpha(ch) || isdigit(ch))
					{
						strtmp+=ch;
						ch =*p++;
					}
					p--;
					if(ch ==']')
					{
						strtmp+=ch;
						ch=*p++;
						fprintf(path,"<%s,array>\n",strToken);
						fprintf(path,"<%s,array>\n",strtmp);
						
						strToken ="";
						strtmp="";
						continue;
					}
					else
						p=temp+1;
				}
				else
					p=temp+1;
			}
		code = Reserve(strToken);///////////////////////////////////////
		if(code==0)
		{
			fprintf(path,"<%s,id>\n",strToken);
			
		}
		   else
			fprintf(path,"<%s,->\n",strToken);
	} 
	else if (isdigit(ch))//查看是否为数字
	{
		   while(isdigit(ch))
		   {
			strToken+=ch;
			ch=*p++;
		   }
			p--;
		fprintf(path,"<%s,const>\n",strToken);
	}
	else if (ch =='=')//以下为一些符号的鉴定
		fprintf(path,"<=,->\n");
	else if (ch ==':')
		fprintf(path,"<:,->\n");
	else if (ch =='+') 
		{
		ch=*p++;
		if(ch=='+')
			fprintf(path,"<++,->\n");
		p--;
		fprintf(path,"<+,->\n");
	}
	else if (ch =='-') 
		{
		ch=*p++;
		if(ch=='-')
			fprintf(path,"<--,->\n");
		p--;
		fprintf(path,"<-,->\n");
	}	
	else if (ch =='*')
	{
		ch=*p++;
		if(ch=='*')
			fprintf(path,"<**,->\n");
		p--;
		fprintf(path,"<*,->\n");
	}
    else if (ch =='>')
	{
		ch=*p++;
		if(ch=='=')
			fprintf(path,"<>=,->\n");
		p--;
		fprintf(path,"<>,->\n");
	}
	else if (ch =='<')
	{
		ch=*p++;
		if(ch=='=')
			fprintf(path,"<<=,->\n");
		p--;
		fprintf(path,"<<,->\n");
	}
	else if(ch==';') 
		fprintf(path,"<;,->\n");
	else if(ch==',') 
		fprintf(path,"<;,->\n");
	else if(ch=='(') 
		fprintf(path,"<(,->\n");
	else if(ch==')') 
		fprintf(path,"<),->\n");
	else if(ch=='{') 
		fprintf(path,"<{,->\n");
	else if(ch=='}') 
		fprintf(path,"<},->\n");
	else if(ch=='AND') 
		fprintf(path,"<AND,logic>\n");
	else if(ch=='OR') 
		fprintf(path,"<OR,logic>\n");
	else if(ch=='NOT') 
		fprintf(path,"<NOT,logic>\n");
	else 
		CMessage::PrintStatusInfo("词法分析非正常结束,遇到非法字符!");//报错
	ch=*p++;
	strToken ="";
	}
	
	//fprintf(path,"%s",tt);
	fclose(path);
	CMessage::PrintStatusInfo("词法分析结束,结果已经保存至word.wl文件中");
}



/////////////////////////////////////////////////////////////////////////////语法分析部分
int ** Readlrtable()
{
	FILE *p;///定义指向文件的指针
	//int table[64][64];
	int **table=new int *[64];
	for(int i=0;i<64;i++)
		table[i]=new int[64];
	char * temp=new char[128];
	char ch,tch;
	char * pp;
	int j=0;
	p=fopen("lrtable.txt","r");
	fgets(temp,128,p);
	for(i=0;i<64;i++)
	{
		fgets(temp,128,p);
		pp=temp;
		ch=*pp;
		do{
			table[i][j++]=atoi(pp);
			while(isdigit(ch)||ch=='-')
				ch=*(++pp);
			tch=ch;
			ch=*(++pp);
		}
		while(tch==9);
		j=0;
		if(feof(p))
			break;
	}
	fclose(p);

	return table;
}

int stackc[128];///定义栈
int topc=-1;
CString stacks[128];
int tops=-1;
CString stackid[128];
int topid=-1;
long wordfile=0;
int passlength=0;
int rnum=0;

int popc()
{
	if (topc>-1)
		return stackc[topc--];
	else
		return -1;
}

void pushc(int num)
{
	if(topc<128)
		stackc[++topc]=num;
}

int gettopc()
{
	return stackc[topc];
}

void outputc(FILE * p)
{
	for(int i=0;i<=topc;i++)
		fprintf(p,"%d ",stackc[i]);
	fprintf(p,"\n");
}

CString pops()///如果还没到栈顶 就一直弹出
{
	if (tops>-1)
		return stacks[tops--];
	else
		return "";
}

void pushid(CString symbol)
{
	if(topid<128)
		stackid[++topid]=symbol;
}

CString popid()
{
	if (topid>-1)
		return stackid[topid--];
	else
		return "";
}

void pushs(CString symbol)///如果栈还没满 可一直压入
{
	if(tops<128)
		stacks[++tops]=symbol;
}

void outputs(FILE * p)//输出
{
	for(int i=0;i<=tops;i++)

⌨️ 快捷键说明

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