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

📄 1111.txt

📁 编译原理课程实验
💻 TXT
字号:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
//定义缓冲区大小
#define  BUFFERLEN  2048
//定义缓冲区索引
int  front=0,rear=0;
//定义缓冲区并初始化
char buffer[BUFFERLEN]={0};
//用于指示装入哪半个缓冲区
int  position=0;
//全局变量指示行号
int  line;
//filename: 要解析的源文件名
FILE * fin;
//临时变量数组
char id[100];
int  dex=0;
//符号结构定义
struct token{
	int   lineno;//行号
	int   position;//在该行中的位置
	int   wordkey;//符号类型,用十六进制表示
    char* value;
	struct token* next_token;
}token;
//变量表
struct token* token_list_head=NULL;
//关键字表
char* keyword[]={
	"abstract",
    "boolean",
	"break",
	"byte",
	"case",
	"catch",
	"char",
	"class",
	"const",
	"continue",
	"default",
	"do",
	"double",
	"else",
	"extends",
	"false",
	"final",
	"finally",
	"float",
	"for",
	"goto",
	"if",
	"implements",
	"import"
	"instanceof",
	"int",
	"interface",
	"long",
	"native",
	"new",
	"null",
	"package",
	"private",
	"protect",
	"public",
	"return",
	"short",
	"static",
	"super",
	"switch",
	"synchronized",
	"this",
	"throw",
	"throws",
	"transient",
	"true",
	"try",
	"void",
	"volatile",
	"while"
};
//将源文件装入缓冲区
void  load(FILE * fin,int flag){
	if(flag==0){
	    fread(buffer,sizeof(char),1023,fin);
		rear=0;
	}
	else{
		fread(buffer+1024,sizeof(char),1023,fin);
		rear=1025;
	}
}
//在关键字表中进行查找,若照到则返回1,否则返回0
int lookup_keyword(char* word){
	int i;
	for(i=0;i<49;i++){
		if(strcmp(word,keyword[i])==0)
			return 0;
	}
	if(i==49)
		return 1;
}
//获取一个符号
struct token* get_token(){
	int i;
    struct token* token_pointer;
	char ch=buffer[rear];
	if(ch==EOF){
		position++;
		fseek(fin,1023,1);
		if(position/2==0){
		    load(fin,0);
		}
		else{
			 load(fin,1);
		}
		ch=buffer[rear];
	}
	if(isspace(ch)){
	    do{
			ch=buffer[++rear];
			if(ch==EOF){
	            position++;
		        fseek(fin,1023,1);
	        	if(position/2==0){
		            load(fin,0);
				}
	        	else{
		        	load(fin,1);
				}
		        ch=buffer[rear];
			}
		}while(isspace(ch));
		front=rear;
     
        token_pointer=(struct token*)malloc(sizeof(token));
	    token_pointer->lineno=line;
	    token_pointer->wordkey=0x102;
		token_pointer->value=(char*)malloc(2*sizeof(char));
		strcpy(token_pointer->value," ");
		token_pointer->next_token=token_list_head;
		token_list_head=token_pointer;
	}
	if(isalnum(ch)||ch=='_'||ch=='$'){
	    do{
			ch=buffer[++rear];
			if(ch==EOF){
	            position++;
		        fseek(fin,1023,1);
	        	if(position/2==0){
		            load(fin,0);
				}
	        	else{
		        	load(fin,1);
				}
		        ch=buffer[rear];
			}
		}while(isalnum(ch)||isdigit(ch)||ch=='_'||ch=='$');
		i=front;
		while(i!=rear){
			if(buffer[i]==EOF){
				i=(rear/1024)*1024;
			}
			id[dex]=buffer[i];
			dex++;
			i++;
		}
		if(lookup_keyword(id)){
			token_pointer=(struct token*)malloc(sizeof(token));
	        token_pointer->lineno=line;
			
			token_pointer->value=(char *)malloc(dex*sizeof(char));
			strcpy(token_pointer->value,id);
	        
			token_pointer->wordkey=0x104;
		    token_pointer->next_token=token_list_head;
		    token_list_head=token_pointer;
		}
		else{
			if(strcmp(id,"true")==0 || strcmp(id,"false")==0){
				token_pointer=(struct token*)malloc(sizeof(token));
	            token_pointer->lineno=line;
				token_pointer->wordkey=0x103;
		        
				token_pointer->value=(char *)malloc(dex*sizeof(char));
				strcpy(token_pointer->value,id);

			    token_pointer->next_token=token_list_head;
		        token_list_head=token_pointer;
			}
			else{
				token_pointer=(struct token*)malloc(sizeof(token));
	            token_pointer->lineno=line;
			  
				token_pointer->value=(char *)malloc(dex*sizeof(char));
				strcpy(token_pointer->value,id);
	            
				token_pointer->wordkey=0x103;
		        token_pointer->next_token=token_list_head;
		        token_list_head=token_pointer;
			}
		}
		front=rear;
		dex=0;
		memset(id,0,100);

	}
		

}	
int main(int argc,char * argv[]){
    
	char filename[100];
	int  i=0;

    //设立缓冲区标志位
	buffer[1023]=EOF;
    buffer[2047]=EOF;

	printf("Please enter file name:\n");
    scanf("%s",filename);

    //打开文件,并将1023个字符读入前一半缓冲区
	fin=fopen(filename,"r");
    if(fin==NULL){
		printf("FILE_CANNOT_OPEN\n");
	}
	else{
		load(fin,0);

	}
	for(i=0;i<10;i++){
		get_token();
	}
   

}
	

⌨️ 快捷键说明

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