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

📄 expset.cpp

📁 一个简单实现了词法分析
💻 CPP
字号:
#include "stdafx.h"
#include "stdio.h"
#include "ExpSet.h"
#include "string.h"
#if! defined _ExpSet_h_
#define _ExpSet_h_

CExpMan::CExpMan()
{}
CExpMan::~CExpMan()
{}
int CExpMan::Open(char* FileName)
{
	EFHandle=fopen(FileName,"r");
	if(EFHandle==0)
		return 0;
	return 1;
}
int CExpMan::ReadOneExpWord(char* buffer)
{	 
	if(EFHandle==0)
	{
		return 0;
	}	
	int i=0;
	char ch;
	ch=fgetc(EFHandle);
	while(ch==' '||ch=='\n')
	{
		ch=fgetc(EFHandle);
	}
	while((int)ch!=-1)
	{
		if(ch!=' '||ch!='\n')
		{
			buffer[i]=ch;
			ch=fgetc(EFHandle);
			i++;
		}
		if(ch=='\n')
		{
			buffer[i]='\0';
			return 2;
		}
		if(ch==' ')
		{
			buffer[i]='\0';
			return 1;
		}
	}
	buffer[i]='\0';
	
	return 3;
}
int CExpMan::ReadOneExp(CExp* TmpExp)
{
	char buffer[20];
	int RightS=0;
	int i=0;
	int j=0;
	i=ReadOneExpWord(buffer);	
	strcpy(TmpExp->ExpLeft,buffer);
	
	i=ReadOneExpWord(buffer);	 
	while(i==1)//buffer's problem
	{
		if(strlen(buffer)!=0)
		{
			strcpy(TmpExp->ExpRight[j],buffer);
			j++;
			i=ReadOneExpWord(buffer);
		}		 
	}
	if(i==2)
	{
		if(strlen(buffer)!=0)
		{
			strcpy(TmpExp->ExpRight[j],buffer);
			j++;
			RightS=j;
			TmpExp->ExpRightSize=RightS;
			return 1;
		}
		return 3;
	}
	if(i==3)
	{
		if(strlen(buffer)!=0)
		{
			strcpy(TmpExp->ExpRight[j],buffer);
			j++;
			RightS=j;
			TmpExp->ExpRightSize=RightS;
			return 2;
		}
	}
	return 3;
	
}
int CExpMan::ReadExp()
{
	CExp TExp;
	int Number_Id=0;
	/*if(ReadOneExp(&TExp)!=1)
	{
		return Number_Id;
	}*/
	while(ReadOneExp(&TExp)==1)
	{
		Number_Id++;
		TExp.IdNumber=Number_Id;
		ExpV.push_back(TExp);
	}
	if(ReadOneExp(&TExp)==2)
	{
		Number_Id++;
		TExp.IdNumber=Number_Id;
		ExpV.push_back(TExp);
	}
	return Number_Id;
}
void CExpMan::PrintExp()
{
	int len=ExpV.size();
	for(int i=0;i<len;i++)
	{
		cout<<ExpV[i].IdNumber<<"  ";
		cout<<ExpV[i].ExpLeft<<" ---";
		cout<<ExpV[i].ExpRightSize<<" ";
		for(int j=0;j<ExpV[i].ExpRightSize;j++)
		{
			cout<<ExpV[i].ExpRight[j]<<" ";
		}
		cout<<endl;
		
	}
}
#endif

⌨️ 快捷键说明

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