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

📄 getchar.h

📁 一个进行整数计算的C语言词法分析器
💻 H
字号:
#include "string.h"
#include "iostream.h"
#include "fstream.h"
const MAX=20;             //变量名允许的最长长度20
struct word               //单词结构体
{
	char *ch;         //存储单词
	char chname[10];    //存放单词名
	int line;             //指明单词所在行数
	int type;             //指明单词的类型,保留字=0,变量=1,常数=2,还是特殊符号=3,错误符号=4
} w={NULL,"\0",1,-1};

int Ischar(char c)
{
	return ((c>=0x41&&c<=0x5A||c>=0x61&&c<=0x7A));
}
int Isdigit(char c)
{
	return((c>=0x30&&c<=0x39));
}

void getword(word& w,ifstream myfile)
{
	w.ch=new char[MAX];
	for(int i=0;i<MAX;i++)     w.ch[i]='\0';
	char temp;                       //存放临时扫描字符
	myfile.get(temp);

		 
   while((temp==0x20||temp==0x0d||temp==0xA||temp==0x9)&&(!myfile.eof())) 
   {
	   
	   if(temp==0xA) w.line++;
	   myfile.get(temp);
   }
   
 
   int j=0,len=MAX; 
	   w.ch[j++]=temp; 
	   
	   if(Ischar(temp)||Isdigit(temp))    //判定是不是数字或字母
	   {
		   myfile.get(temp);
		   while(Ischar(temp)||Isdigit(temp))
		   {
			   if(j<MAX)                    //防止字符串过长
			   {
				   w.ch[j++]=temp;
			   }
			   myfile.get(temp);
		   }
		   myfile.seekg(-1,ios::cur);
		   return;
	   }
	   if(temp=='+')        //判定是不是++,+=
	   {
		   myfile.get(temp);
		   if(temp=='+'||temp=='=')    
		   {
			   w.ch[j++]=temp;
			   return;
		   }
		   myfile.seekg(-1,ios::cur);
		   return;
	   }
	   if(temp=='-')        //判定是不是--,-=
	   {
		   myfile.get(temp);
		   if(temp=='-'||temp=='=')    
		   {
			   w.ch[j++]=temp;
			   return;
		   }
		   myfile.seekg(-1,ios::cur);
		   return;
	   }
	   if(temp=='%')        //判定是不是%d
	   {
		   myfile.get(temp);
		   if(temp==0x64)    
		   {
			   w.ch[j++]=temp;
			   return;
		   }
		   myfile.seekg(-1,ios::cur);
		   return;
	   }
	   if(temp=='/')        //判定是不是//,/=,/*
	   {
		   myfile.get(temp);
		   if(temp=='/'||temp=='='||temp=='*')    
		   {
			   w.ch[j++]=temp;
			   return;
		   }
		   myfile.seekg(-1,ios::cur);
		   return;
	   }
	   if(temp=='*')        //判定是不是*=,*/
	   {
		   myfile.get(temp);
		   if(temp=='='||temp=='/')    
		   {
			   w.ch[j++]=temp;
			   return;
		   }
		   myfile.seekg(-1,ios::cur);
		   return;
	   }
	   if(temp=='&')        //判定是不是&&
	   {
		   myfile.get(temp);
		   if(temp=='&')    
		   {
			   w.ch[j++]=temp;
			   return;
		   }
		   myfile.seekg(-1,ios::cur);

		   return;
	   }
	   if(temp=='|')        //判定是不是||
	   {
		   myfile.get(temp);
		   if(temp=='|')    
		   {
			   w.ch[j++]=temp;
			   return;
		   }
		   myfile.seekg(-1,ios::cur);

		   return;
	   }
	   if(temp=='>'||temp=='='||temp=='<'||temp=='!')        //判定是不是>=,<=,!=,==
	   {
		   myfile.get(temp);
		   if(temp=='=')    
		   {
			   w.ch[j++]=temp;
			   return;
		   }
		   myfile.seekg(-1,ios::cur);

		   return;
	   }
	   return;   
}

⌨️ 快捷键说明

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