PL/0 词法分析器
#include<stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h> typedef enum SymEnum
{
Identifier=, //标识符
Const=, //常数
Key=, //关键字
Operator=, //运算符
Delimiter= //界符
} SymEnum; char KeyWord[][]; //关键字 void Init()
{
strcpy(&KeyWord[][],"begin");
strcpy(&KeyWord[][],"call");
strcpy(&KeyWord[][],"const");
strcpy(&KeyWord[][],"do");
strcpy(&KeyWord[][],"end");
strcpy(&KeyWord[][],"if");
strcpy(&KeyWord[][],"odd");
strcpy(&KeyWord[][],"procedure");
strcpy(&KeyWord[][],"read");
strcpy(&KeyWord[][],"then");
strcpy(&KeyWord[][],"var");
strcpy(&KeyWord[][],"while");
strcpy(&KeyWord[][],"write");
} //判断一个单词是否为关键字
int IsKeyWord(char *word)
{
int i=,j=;
while(i<=j)
{
int k=(i+j)/;
if(strcmp(word,KeyWord[k])<=)
j=k-;
if(strcmp(word,KeyWord[k])>=)
i=k+;
}
return i->j ? : ;
} //判断一个单词是否为界符
int IsDelimiter(char word)
{
if(word==','||word==';'||word=='.'||word=='('||word==')')
return ;
return ;
} //判断一个单词是否为操作符
int IsOperator(char *word)
{
if(word[]=='+'||word[]=='-'||word[]=='*'||word[]=='/'||word[]=='<'||word[]=='>'||strcmp(word,":=")==||strcmp(word,">=")==||strcmp(word,"<=")==||word[]=='#'||word[]=='=')
return ;
return ;
} //判断一个单词是否是常数
int IsConst(char *word)
{
if(word[]>=''&&word[]<='')
return ;
return ;
} //判断连个字符是否属于相同类型
int IsSame(char f,char s)
{
int bf = (f>=''&&f<=''||f>='a'&&f<='z'||f>='A'&&f<='Z')? : ;
int bs = (s>=''&&s<=''||s>='a'&&s<='z'||s>='A'&&s<='Z')? : ;
return bf == bs;
} //判断 word 的类型
SymEnum TypeOfWord(char *word)
{
if(IsKeyWord(word))
return Key;
if(IsConst(word))
return Const;
if(IsOperator(word))
return Operator;
return Identifier;
} int GetSym()
{
FILE *fp,*fout;
if((fp=fopen("PL0.txt","r"))==NULL || (fout=fopen("source.txt","w+"))==NULL)
{
printf("Open Error!\n");
exit();
}
SymEnum SYM; //存放每个单词的类别,用内部编码形式表示;
char word[]; //存储单词
word[]='\0';
int len=; //单词长度
char ch;
while(fscanf(fp,"%c",&ch)!=EOF)
{
// ch 为空格,回车符,制表符
if(ch==' '||ch=='\n'||ch=='\t')
{
// word 不为空
if(len)
{
//判断单词类型
SYM=TypeOfWord(word);
fprintf(fout,"%d %s\n",SYM,word);
//清空缓存区
len=;
word[len]='\0';
}
// word 为空,忽略 ch
}
else if(IsDelimiter(ch))
{
//word 不为空
if(len)
{
//判断单词的类型
SYM=TypeOfWord(word);
fprintf(fout,"%d %s\n",SYM,word);
// ch == delimiter
SYM=Delimiter;
fprintf(fout,"%d %c\n",SYM,ch);
//清空缓存区
len=;
word[len]='\0';
}
else
{
//word 为空, ch 为界符
SYM=Delimiter;
fprintf(fout,"%d %c\n",SYM,ch);
//清空缓存区
len=;
word[len]='\0';
}
}
else
{
if(len>)
{
if(IsSame(word[len-],ch))
{
//判断 word 与 ch 是否同类型
word[len++]=ch;
word[len]='\0'; //字符串终结符
}
else
{
//判断单词类型
SYM=TypeOfWord(word);
fprintf(fout,"%d %s\n",SYM,word);
//清空缓存区,并把 ch 放入缓存区
len=;
word[len++]=ch;
word[len]='\0'; //字符串终结符
}
}
else
{
word[len++]=ch;
word[len]='\0';
}
}
}
fclose(fp);
fclose(fout);
return ;
} void PrintToScream()
{
FILE *fp;
if((fp=fopen("source.txt","r+"))==NULL)
{
printf("Open File Error!\n");
exit();
}
int id;
char word[];
printf("0-标识符 1-常数 2-关键字 3-操作符 4-界符\n");
while(fscanf(fp,"%d %s",&id,word)!=EOF)
{
printf("(%d,%s)\n",id,word);
}
fclose(fp);
} int main()
{
Init();
GetSym();
PrintToScream();
return ;
}