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

📄 exam1.cpp

📁 snl的词法分析器 C语言编写 VC下运行 snl的词法分析器 C语言编写 VC下运行
💻 CPP
字号:
#include <stdlib.h>
#include "stdio.h"
#include "string.h"
#define   MAX 22             /*关键字的最大容量*/
#define   MAXBUF 255         /*缓冲区的大小*/
char   ch =' ';              /*存放读入当前的输入字符*/
int Line_NO;                /*纪录行号*/
int tmp;

char str[MAX][10]={"program","begin","end","var","integer","real", "for","if","then","else","do","while",
"array","procedure", "function","of","boolean","const","div","mod","and","or"};


/***************对关键字进行搜索**************/
int se_res(char * is_res){
	int i;
	for(i=0;i<MAX;i++)
	{
		if((strcmp(str[i],is_res))==0) break;
	}
	if(i<MAX) 
		return 1;
	else 
		return 0;
}
/*****************判断是否为字母*****************/
int IsLetter(char c)
{
	if(((c<='z')&&(c>='a'))||((c<='Z')&&(c>='A')))
		return 1;
    else
		return 0;
}
/*************判断是否为数字**************/
int IsDigit(char c)
{
	if(c>='0'&&c<='9')
		return 1;
	else
		return 0;
}

/***************分析程序**************/
void analyse(FILE *fpin,FILE *fpout)
{
	char arr[MAXBUF];       /* 输入缓冲区,存放一个单词符号 */
    int j=0;
    while((ch=fgetc(fpin))!=EOF)
	{
		if(ch==' '||ch=='\t'){}                    /*碰到空格、tab则跳过*/
		else
			if(ch=='\n')
			{
				Line_NO++;
			}
			/*************************字符串的处理****************************/
			else
				if(IsLetter(ch))
				{
					while(IsLetter(ch)|IsDigit(ch))
					{
						if((ch<='Z')&&(ch>='A'))
							ch=ch+32;   /*忽略大小写*/
						arr[j]=ch;
						j++;
						ch=fgetc(fpin);
					}
					fseek(fpin,-1L,SEEK_CUR);   /*输入指针回退一个字符*/
					arr[j]='\0';
					j=0;
					if (se_res(arr))
					{   /*如果是关键字*/
						fprintf(fpout,"%d\t%s\t\t%s\n",Line_NO,arr,"reserved word");
					}
					else   
						fprintf(fpout,"%d\t%s\t\t%s\n",Line_NO,arr,"ID");    /*普通标识符*/
					/*************************数字的处理****************************/      
				}
				else
					if(IsDigit(ch))
					{
						while(IsDigit(ch))
						{
							arr[j]=ch;
							j++;
							ch=fgetc(fpin);
						}
						fseek(fpin,-1L,SEEK_CUR);
						arr[j]='\0';
						j=0;
						fprintf(fpout,"%d\t%s\t\t%s\n",Line_NO,arr,"digit") ;     /*无符号整数*/
					}
					else 
						switch(ch)
					{           
							case'+' :fprintf(fpout,"%d\t%s\n",Line_NO,"+");break;
							case'-' :fprintf(fpout,"%d\t%s\n",Line_NO,"-");break;
							case'*' :fprintf(fpout,"%d\t%s\n",Line_NO,"*");break;
							case'/' :fprintf(fpout,"%d\t%s\n",Line_NO,"/");break;
							case'(' :fprintf(fpout,"%d\t%s\n",Line_NO,"(");break;
							case')' :fprintf(fpout,"%d\t%s\n",Line_NO,")");break;
							case'[' :fprintf(fpout,"%d\t%s\n",Line_NO,"[");break;
							case']' :fprintf(fpout,"%d\t%s\n",Line_NO,"]");break;                      
							case';' :fprintf(fpout,"%d\t%s\n",Line_NO,";");break;
							case'=' :fprintf(fpout,"%d\t%s\n",Line_NO,"=");break;
							case'<' :fprintf(fpout,"%d\t%s\n",Line_NO,"<");break;
							case',' :fprintf(fpout,"%d\t%s\n",Line_NO,",");break;               
							case':' :
								{
									ch=fgetc(fpin);
									if(ch=='=')
										fprintf(fpout,"%d\t%s\n",Line_NO,":=");
									else
									{
										fprintf(fpout,"%d\t%s\n",Line_NO,":");fseek(fpin,-1L,SEEK_CUR);
									}
								}break;
								
							case'.' :
								{
									ch=fgetc(fpin);
									if(ch=='.') 
										fprintf(fpout,"%d\t%s\n",Line_NO,"..");
									else 
									{
										fprintf(fpout,"%d\t%s\n",Line_NO,".");fseek(fpin,-1L,SEEK_CUR);
									}
								}break;
								
								/*****************************字符处理******************************************/
							case'\'' :
								{
									ch=fgetc(fpin);
									while(ch!='\'' && ch!=EOF)
										ch=fgetc(fpin);
									if(ch==EOF)
										fprintf(fpout,"缺少一个'''");
								}break;
								/***************出现在{ }之间的全部作为注释部分处理*******************/  
							case'{' :
								{
									ch=fgetc(fpin);
									tmp=Line_NO;
									while(ch!='}'&&ch!=EOF)
									{
										ch=fgetc(fpin);
									}
								}break; 
								/***************非法字符*******************/                         
							default :fprintf(fpout,"在第%d行无法识别的字符\t%c\n",Line_NO,ch);
					}
    }
	fprintf(fpout,"%d\t%s\n",Line_NO,"EOF");
}

/**********主程序中完成对输入输出文件的读写***********/
void main(){
	int flag=1;
	char in_fn[25],out_fn[25];
	FILE * fpin,* fpout;
	printf("//////////////词法分析器////////////////\n");
	while(flag)
	{
		printf("Please input the sourse file :\n");
		scanf("%s",in_fn);
		if((fpin=fopen(in_fn,"r"))==NULL)
		{
			printf("can not open source file\n");
			exit(0);
		}
		printf("Please input the defination file :\n");
		scanf("%s",out_fn);
		if((fpout=fopen(out_fn,"w"))==NULL)
		{
			printf("can not create destination file\n");
			exit(0);
		}
		analyse(fpin,fpout);
		fclose(fpin);
		fclose(fpout);
		printf("Continue?(0:NO;1:YES)\n");
		printf("please choose:");
		scanf("%u",&flag);
	}
	printf("the program of scanner has been finished!\n");
}





⌨️ 快捷键说明

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