編譯原理實驗一 詞法分析程序

#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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章