📄 word_analyse.cpp
字号:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
struct save /* 用于存储关键字和标识符 */
{
char string[64];
struct save *next;
};
struct save *key,*pkey,*emkey=NULL,*sign=NULL,*const_list=NULL;
int main()
{
/* 以下是变量声明区 */
FILE *fp,*fpout;
char p[8000],buffer[4096],in_ch,*buffer_point=buffer;
int i=0,n=0;
/* 以下是函数声明区 */
void pre(char *p,char *buffer,char ch);
void token(char *p,char ch);
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
/* 链表的初始化 */
key=(struct save *)malloc(sizeof(struct save));
pkey=key;
strcpy(pkey->string,"program");
pkey->next=(struct save *)malloc(sizeof(struct save));pkey=key->next;
strcpy(pkey->string,"begin");
pkey->next=(struct save *)malloc(sizeof(struct save));pkey=pkey->next;
strcpy(pkey->string,"end");
pkey->next=(struct save *)malloc(sizeof(struct save));pkey=pkey->next;
strcpy(pkey->string,"if");
pkey->next=(struct save *)malloc(sizeof(struct save));pkey=pkey->next;
strcpy(pkey->string,"then");
pkey->next=NULL;
/*-------------------------------------------------------------*/
fp=fopen("test.txt","r");
in_ch=fgetc(fp);
while(in_ch!=EOF){
p[n++]=in_ch;
in_ch=fgetc(fp);
}
fclose(fp);
pre(p,buffer_point,'#');
puts(buffer_point);
printf("\n");
token(buffer,'#');
/* 将关键字、标识符和常数输出到文件中 */
fpout=fopen("out.txt","w");
fputs("keyword list(sort:0)----------------",fpout);
fputc('\n',fpout);
while(emkey){
fputs(emkey->string,fpout);
fputc(' ',fpout);
emkey=emkey->next;
}
fputs("\n\n",fpout);
fputs("sign list(sort:1)-------------------",fpout);
fputc('\n',fpout);
while(sign){
fputs(sign->string,fpout);
fputc(' ',fpout);
sign=sign->next;
}
fputs("\n\n",fpout);
fputs("constant list(sort:2)---------------",fpout);
fputc('\n',fpout);
while(const_list){
fputs(const_list->string,fpout);
fputc(' ',fpout);
const_list=const_list->next;
}
fclose(fpout);
return 0;
}/*main 函数结束 */
/* 以下是预处理的代码。
预处理包括:注释、多余的空格,均丢掉。
输入:字符数组形式。 */
void pre(char *p,char buffer[],char ch)
{
int flag=1,end=0;/*flag=1 表示光标还未遇到首个文字。 */
char *q;
/* 处理掉换行符( '\n' )。 */
q=p;
while(*q!=ch){
if(*q=='\n')*q=' ';
q++;
}
while(*p!=ch){
if(flag){
if(*p==' ')p++;
else if(*p=='/'&&*(p+1)=='*'){
p=p+2;
while(*p!='*'&&*(p+1)!='/'){
if(*p==ch){printf("error");exit(0);}
else p++;}
p=p+2;
}
else {flag=0;*buffer++=*p++;}
}
else if(*p==' '){
if(*(p+1)==' ')p++;
else if(*(p+1)==ch){*buffer=ch;end=1;*(++buffer)='\0';break;}
else *buffer++=*p++;}
else if(*p=='/'&&*(p+1)=='*'){
p=p+2;
while(*p!='*'&&*(p+1)!='/'){
if(*p==ch){printf("error");exit(0);}
else p++;}
p=p+2;
}
else *buffer++=*p++;
}
if(end);
else {*buffer=ch;*(++buffer)='\0';}
}
/* 识别单词的函数 */
void token(char p[],char ch){
char temp[64],*ptemp=temp;
int i=0,first=1;
/*first 是用来判别在一个常数中有没有点的标志,如果出现过一次后再就不能出现。 */
struct save * q;
struct save * search(char *p,struct save *key);
void insert_key(char *s,struct save *p);
void insert_sign(char *s,struct save *p);
void insert_const(char *s,struct save *p);
while(*p!=ch)
{
/* 处理关键字和标识符 ----------------------------*/
if(isalpha((int)*p)){
*(ptemp+i++)=*p++;
while(*p!=ch&&isalnum((int)*p))
*(ptemp+i++)=*p++;
*(ptemp+i)='\0';
i=0;/* 临时字符数组使用结束,将其起始位置清零 , 下同。 */
/* 处理 PTEMP( 查找并判断其是否为关键字 )*/
if(q=search(ptemp,key)){
printf("%s is keyword.\n",q->string);
if(search(ptemp,emkey));
else insert_key(ptemp,emkey);
}
else if(q=search(ptemp,sign))
printf("%s was already exist in sign-list.\n",q->string);
/* 否则插入到标识符链表中 */
else{insert_sign(ptemp,sign);
printf("%s is a sign,it was inserted to sign_list finish.\n",ptemp);
}
continue;
}
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
/* 处理常数 -------------------------------------*/
else if(isdigit((int)*p)){
*(ptemp+i++)=*p++;
while(*p!=ch&&(isdigit((int)*p)||(first&&*p=='.'&&isdigit((int)*(p+1))))){
if(*p=='.')first=0;
*(ptemp+i++)=*p++;
}
*(ptemp+i)='\0';
i=0;first=1;
if(q=search(ptemp,const_list))
printf("%s was already exist in constant-list.\n",q->string);
/* 否则插入到常数链表中 */
else{insert_const(ptemp,const_list);
printf("%s is a constant,it was inserted to constant_list finish.\n",ptemp);
}
continue;
}
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
/* 处理界限符 -------------------------------------*/
else if(*p==';'){
printf("; is a bounds symbol,its sort code is 3.\n");
if(*(p+1)!=ch) {p++;continue;}
else return;
}
else if(*p=='.'){
printf(".(dot) is a bounds symbol,its sort code is 4.\n");
if(*(p+1)!=ch) {p++;continue;}
else return;
}
else if(*p=='('){
printf("( is a bounds symbol,its sort code is 5.\n");
if(*(p+1)!=ch) {p++;continue;}
else return;
}
else if(*p==')'){
printf(") is a bounds symbol,its sort code is 6.\n");
if(*(p+1)!=ch) {p++;continue;}
else return;
}
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
/* 处理界限符 -------------------------------------*/
else if(*p=='+'){
printf("+ is a mathematic symbol,its sort code is 3.\n");
if(*(p+1)!=ch) {p++;continue;}
else return;
}
else if(*p=='-'){
printf("- is a mathematic symbol,its sort code is 4.\n");
if(*(p+1)!=ch) {p++;continue;}
else return;
}
else if(*p=='*'){
printf("* is a mathematic symbol,its sort code is 5.\n");
if(*(p+1)!=ch) {p++;continue;}
else return;
}
else if(*p=='/'){
printf("/ is a mathematic symbol,its sort code is 6.\n");
if(*(p+1)!=ch) {p++;continue;}
else return;
}
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
/* 处理赋值运算符 -------------------------------------*/
else if(*p==':'){
if(*(p+1)=='='){
printf(":= is a evaluate symbol,its sort code is 7.\n");
p++;
}
if(*(p+1)!=ch) {p++;continue;}
else return;
}
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
/* 处理比较运算符 -------------------------------------*/
else if(*p=='='){
printf(") is a compare symbol,its sort code is 12.\n");
if(*(p+1)!=ch) {p++;continue;}
else return;
}
else if(*p=='>'){
if(*(p+1)=='='){
printf(">= is a compare symbol,its sort code is 14.\n");
p++;
}
else printf("> is a compare symbol,its sort code is 13.\n");
if(*(p+1)!=ch) {p++;continue;}
else return;
}
else if(*p=='<'){
if(*(p+1)=='='){
printf("<= is a compare symbol,its sort code is 16.\n");
p++;
}
else if(*(p+1)=='>'){
printf("<= is a compare symbol,its sort code is 18.\n");
p++;
}
else printf("< is a compare symbol,its sort code is 15.\n");
if(*(p+1)!=ch) {p++;continue;}
else return;
}
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
else if(*p==' ') {p++;continue;}
else if(*p==ch)return;
else {printf("%c error",*p);
return;}
}
}
struct save * search(char *p,struct save *q)/* 搜索某一字符串是否为关键字 */
{
struct save *key=q;
while(key){
if(strcmp(p,key->string)==0)return key;
key=key->next;
}
return key;
}
void insert_key(char *s,struct save *p)/* 插入某一字符串到标识符链表中 */
{
struct save *q;
if(p==NULL){
p=(struct save *)malloc(sizeof(struct save));
emkey=p;
}
else{
while(p){
q=p;
p=p->next;
}
q->next=(struct save *)malloc(sizeof(struct save));
p=q->next;
}
strcpy(p->string,s);
p->next=NULL;
}
void insert_sign(char *s,struct save *p)/* 插入某一字符串到标识符链表中 */
{
struct save *q;
if(p==NULL){
p=(struct save *)malloc(sizeof(struct save));
sign=p;
}
else{
while(p){
q=p;
p=p->next;
}
q->next=(struct save *)malloc(sizeof(struct save));
p=q->next;
}
strcpy(p->string,s);
p->next=NULL;
}
void insert_const(char *s,struct save *p)/* 插入某一字符串到常数链表中 */
{
struct save *q;
if(p==NULL){
p=(struct save *)malloc(sizeof(struct save));
const_list=p;
}
else{
while(p){
q=p;
p=p->next;
}
q->next=(struct save *)malloc(sizeof(struct save));
p=q->next;
}
strcpy(p->string,s);
p->next=NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -