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

📄 cifa.cpp

📁 C++做的简单的词法分析器
💻 CPP
字号:
#include <iostream>
#include<stdio.h>
#include"stdio.h"
#include <string.h>
#include<string>
//#include"IsIndex.h"
//#include"IsOperator.h"
//#include"IsNumber.h"
//using namespace std;

#include<string>
#include<iostream>
#define IndexNum 13
const int MAX=100;
using namespace std;
int IsIndex(const char temp[])//定义关键字 
{	
	char* table[]={"char","int","float","double","if","else","do",
					"while","main","printf","scanf","include","stdio.h"
					};
	for(int i=0;i<IndexNum;i++)
		if(strcmp(temp,table[i])==0)
			return 0;
	return 1;
}
	
int IsParenthesis (const char temp[])
{	int sign=1;
	if(temp[0]==')'){
		sign=0;
		
	}
	else if(temp[0]=='('){
		sign=0;
		
	}
	else if(temp[0]=='{'){
		sign=0;
	}
	if(temp[0]=='}'){
		sign=0;
	}
	return sign;
}

int IsNumber(const char temp[])
{	const char *p=temp;
	int len_digits=strlen(temp);
	int len=0;
	if(*p>='1'&&*p<='9'){
		p++;
		len++;
		while(*p>='0'&&*p<='9'){
			p++;
			len++;
		}
		if(len==len_digits)
			return 0;
		else
			return 1;
	}
}
int IsOperator(const char temp[])
{	int sign=0;
	if(temp[0]=='+'){
		sign=1;
	}
	if(temp[0]=='-'){
	sign=1;
	}
	if(temp[0]=='*'){
		sign=1;
	}
	if(temp[0]=='/'){
		sign=1;
	}
	if(temp[0]=='='){
		sign=1;
	}
	if(sign==1)
		return 0;
	else
		return 1;
}

int IsIndenti(const char temp[])
{   int k=0;
	if(temp[0]>='a'&&temp[0]<='z'||temp[0]>='A'&&temp[0]<='Z'||temp[0]=='_'){//判读首字符是否符合条件
				k++;
				//num_char++;
				while(temp[k]>='a'&&temp[k]<='z'||temp[k]>='A'&&temp[k]<='Z'||temp[k]>='0'&&temp[k]<='9'||temp[0]=='_'){  //判断是否有非法字符出现
					 k++;
				}
	}
	return 0;
}
				  
				

int main()
{	int LineNum=0;
	int k=0;
	char temp[MAX];
	char arr[MAX];
	int len=0;

	
	int num=1;      
	while(num--){      //只使用一次文件
		FILE *fp;
		FILE *fout;
		
		//fp=fopen("E:\in.txt","r");

		if((fp=fopen("E:\\in.txt","r"))==NULL){  //读的方式
			fprintf(stderr," Error in opening file! ");
			return 0;
		}
		else 
			cout<<"成功打开输入文件  ^_^"<<endl;
		if((fout=fopen("E:\\out.txt","w"))==NULL){      //是写的方式
			fprintf(stderr," Error in opening file! ");
			return 0;
		}
		else
			cout<<"成功打开写入输出文件  ^_^"<<endl;
     
		while(fgets(arr,MAX,fp)!=NULL){    //获得一行字符串
			
			char *p=arr;
			LineNum++;
			while(*p!=0){
				memset(temp,0,sizeof(temp));
				int k=0;
				while(*p==' '||*p=='\n'||*p=='	')//过滤掉空格
					p++;
				while(*p>='0'&&*p<='9'||*p>='a'&&*p<='z'||*p>='A'&&*p<='Z'||*p==';'||*p==')'||*p=='('||*p=='{'||*p=='}'||*p=='+'||*p=='-'||*p=='*'||*p=='/'||*p=='='||*p==';'||*p=='"'||*p=='\''||*p=='#'||*p=='.'||*p=='>'||*p=='<'){
					temp[k++]=*p++;
				}
				
				
				temp[k]=0;
				if(IsIndex(temp)==0){
					fprintf(fout,"保留字:%10.10s--------------------------Line:%d\n ",temp,LineNum);
				}
				else if(IsNumber(temp)==0){
					fprintf(fout,"数字:%10.10s -------------------------- Line:%d \n",temp,LineNum);

				}
				else if(IsOperator(temp)==0){
					fprintf(fout,"运算符:%10.10s-------------------------- Line :%d \n",temp,LineNum);
				}
				else if(temp[0]==';'){
					fprintf(fout,"分号 :%10.10s---------------------------------Line :%d \n",temp,LineNum);
				}
				else if(IsParenthesis(temp)==0){
					fprintf(fout,"括号:%10.10s -------------------------- Line:%d \n",temp,LineNum);
				}
				else if (IsIndenti(temp)==0){
					fprintf(fout,"标识符:%10.10s -------------------------- Line:%d \n",temp,LineNum);
				}
				else if (temp[0]=='#'){
					fprintf(fout,"#:%10.10s -------------------------- Line:%d \n",temp,LineNum);
				}
				else
					cout<<"非法标识符"<<endl;
			}
			
		}
		
		if(fclose(fp)!=0){     //若成功关闭fclose放回0
          perror("fclose(fp)"); // printf: "fclose(filep):NO such file or directory "
		}
        if(fclose(fout)!=0){     //若成功关闭fclose放回0
          perror("fclose(fout)"); // printf: "fclose(filep):NO such file or directory "
		}
	}
}


		

⌨️ 快捷键说明

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