编译原理实验一 词法分析程序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<ctype.h>
#include<fstream>
#include<algorithm>
using namespace std;
#define keywordSum 8

const int maxn=30;
const int maxnum=100;
//The reserved word list
char *keyword[keywordSum]={"if","else","for","while","do","int","read","write"};
//Single delimiter
char singleword[50]="+-*(){};,:";
//First Double delimiter
char doubleword[10]="<>=!";
fstream fout;
fstream fin;

char line[105];//Temporary space
char words[10005];//File character vector

//function of programs
int Deal()
{
	//Definition of variables
	int i,j,n,ans=0,cnt=0;//ans=0 mean no error,cnt mean start index
	char ch,token[105];//ch:The current character  
	fin.open("C:\\Users\\Administrator.PC-201209211725\\Desktop\\in.txt",ios::in);
	for( i=0;i<maxn;i++)
	{
		fin.getline(line,maxnum);
		for( j=0;j<strlen(line);j++)
		{
			words[cnt++]=line[j];
		}
		memset(line,'\0',sizeof(line));
	}
	for( i=0;i<cnt;i++)
	{
		putchar(words[i]);
	}
	if(!fin.is_open())
	{
		printf("\n我去打不开。。好坑。。→_→!\n");
		return 1;
	}
	printf("\n");
	
	int tmp=0;
	ch=words[tmp++];
	while(ch!='\0')
	{
		while(ch==' '||ch=='\n'||ch=='\t')ch=words[tmp++];
		if(isalpha(ch))//alpha
		{
			memset(token,'\0',sizeof(token));
			token[0]=ch;j=1;
			ch=words[tmp++];
			while(isalnum(ch))//num 字母加数字是合法状态
			{
				token[j++]=ch;
				ch=words[tmp++];
			}
			token[j]='\0';
			n=0;
			while((n<keywordSum)&&strcmp(token,keyword[n]))n++;
			if(n>=keywordSum)//比较是否是保留字
				printf("%s\t%s\n","ID",token);
			else 
				printf("%s\t%s\n",token,token);
		}
		else if(isdigit(ch))//digit
		{
			memset(token,'\0',sizeof(token));
			token[0]=ch;
			j=1;
			ch=words[tmp++];
			while(isdigit(ch))//digit 数字加数字还是数字
			{
				token[j++]=ch;
				ch=words[tmp++];
			}
			token[j]='\0';
			//输出这个数字  NUM标记
			printf("%s\t%s\n","NUM",token);
		}
		else if(strchr(singleword,ch)>0)//find ch in singleword
		{
			memset(token,'\0',sizeof(token));
			token[0]=ch;
			token[1]='\0';
			ch=words[tmp++];
			printf("%s\t%s\n",token,token);
		}
		else if(strchr(doubleword,ch)>0)//find ch in doubleword
		{
			memset(token,'\0',sizeof(token));
			token[0]=ch;
			ch=words[tmp++];
			if(ch=='=')//如果是双分界符
			{
				token[1]=ch;
				token[2]='\0';
				ch=words[tmp++];
			}
			else token[1]='\0';
			printf("%s\t%s\n",token,token);
		}
		else if(ch=='/')//注释部分
		{
			memset(token,'\0',sizeof(token));
			ch=words[tmp++];
			if(ch=='*')//进入注释阶段
			{
				char ch1;
				ch1=words[tmp++];
				do
				{
					ch=ch1;
					ch1=words[tmp++];
				}while((ch!='*'||ch1!='/')&&ch1!='\0');
				ch=words[tmp++];
			}
			else
			{
				token[0]='/';
				token[1]='\0';
				//printf("%s\t%s\n",token,token);
			}
		}
		else//report error
		{
			//printf("caocaocaoaoalalalalalla\n");
			memset(token,'\0',sizeof(token));
			token[0]=ch;
			token[1]='\0';
			ch=words[tmp++];
			ans=3;
			printf("%s\t%s\n","ERROR",token);
		}
	}
	fin.close();
	return ans;
}
int main()
{
	freopen("C:\\Users\\Administrator.PC-201209211725\\Desktop\\in.txt","r",stdin);	
	freopen("C:\\Users\\Administrator.PC-201209211725\\Desktop\\out.txt","w",stdout);

	int ans=0;
	ans=Deal();
	if(ans>0)printf("\n词法有错误啊! 。。。→_→\n");
	else printf("\n居然对了。。不可思议。。→_→\n");
	return 0;
}

你可能感兴趣的:(词法分析程序)