(改)编程实验一 词法分析程序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node{
char words[255];
struct node *next;
}NODE;
char word[255];

//判断默认字符
int acceptation_logic_key(char *ch);
int acceptation_logic(char *ch);
//判断数字
int acceptation_number(char *str);
int acceptation_number_key(char ch);
//判断字符
int acceptation_string(char *str);
int acceptation_string_key(char ch);
//截取
int acceptation_str(char ch,char *str,int i,char *sch,NODE *tnode);

void splitter_node_add(char *str,NODE *node);
int splitter_start(char *str);
void splitter_node_add(char *str,NODE *node);

void word_acceptation(char *ch,NODE *tnode);
void word_node(char *ch,NODE *node);
void word_splitter(char* str,NODE *node);
//逻辑判断
int acceptation_logic(char *ch){

int key=0;
if(strcmp(ch,"begin")==0){
printf("<%s 	 1 >
",ch);
}
else if(strcmp(ch,"if")==0){
printf("<%s 	 2 >
",ch);
}
else if(strcmp(ch,"then")==0){
printf("<%s 	 3 >
",ch);
}
else if(strcmp(ch,"while")==0){
printf("<%s 	 4 >
",ch);
}
else if(strcmp(ch,"do")==0){
printf("<%s 	 5 >
",ch);
}
else if(strcmp(ch,"end")==0){
printf("<%s 	 6 >
",ch);
}
else if(strcmp(ch,"+")==0){
printf("<%s 	 13>
",ch);
}
else if(strcmp(ch,"-")==0){
printf("<%s 	 14>
",ch);
}
else if(strcmp(ch,"*")==0){
printf("<%s 	 15>
",ch);
}
else if(strcmp(ch,"/")==0){
printf("<%s 	 16>
",ch);
}
else if(strcmp(ch,":")==0){
printf("<%s 	 17>
",ch);
}
else if(strcmp(ch,":=")==0){
printf("<%s 	 18>
",ch);
}
else if(strcmp(ch,"<")==0){
printf("<%s 	 20>
",ch);
}
else if(strcmp(ch,"<=")==0){
printf("<%s 	 21>
",ch);
}
else if(strcmp(ch,"<>")==0){
printf("<%s 	 22>
",ch);
}
else if(strcmp(ch,">")==0){
printf("<%s 	 23>
",ch);
}
else if(strcmp(ch,">=")==0){
printf("<%s 	 24>
",ch);
}
else if(strcmp(ch,"=")==0){
printf("<%s 	 25>
",ch);
}
else if(strcmp(ch,";")==0){
printf("<%s 	 26>
",ch);
}
else if(strcmp(ch,"(")==0){
printf("<%s 	 27>
",ch);
}
else if(strcmp(ch,")")==0){
printf("<%s 	 28>
",ch);
}
else if(strcmp(ch,"#")==0){
printf("<%s 	 0>
",ch);
}
else{

key=1;
}
return key;

}
int acceptation_logic_key(char *ch){

int key=0;
if(strcmp(ch,"begin")==0);
else if(strcmp(ch,"if")==0);
else if(strcmp(ch,"then")==0);
else if(strcmp(ch,"while")==0);
else if(strcmp(ch,"do")==0);
else if(strcmp(ch,"end")==0);
else if(strcmp(ch,"+")==0);
else if(strcmp(ch,"-")==0);
else if(strcmp(ch,"*")==0);
else if(strcmp(ch,"/")==0);
else if(strcmp(ch,":")==0);
else if(strcmp(ch,":=")==0);
else if(strcmp(ch,"<")==0);
else if(strcmp(ch,"<=")==0);
else if(strcmp(ch,"<>")==0);
else if(strcmp(ch,">")==0);
else if(strcmp(ch,">=")==0);
else if(strcmp(ch,"=")==0);
else if(strcmp(ch,";")==0);
else if(strcmp(ch,"(")==0);
else if(strcmp(ch,")")==0);
else if(strcmp(ch,"#")==0);
else{

key=1;
}
return key;

}
int acceptation_string_key(char ch);
int acceptation_string(char *str){
int key=1;
char *ch= NULL;
ch=str;
if(acceptation_string_key(str[0])==0){
printf("<%s 	 11>
",str);
key=0;
}
return key;

}
int acceptation_number_key(char ch){
int key=0;
if(ch<='9'&&ch>='0'){
key=0;
}
else
key=1;
return key;
}
int acceptation_number(char *str){
int key=0;
char *ch=NULL;
ch=str;
while(*ch!=''){

key+=acceptation_number_key(*ch);
//printf("keynumber=%d,ch=%c
",key,*ch);
*ch++;

}
if(key==0)
printf("<%s 	 10>
",str);
return key;
}
int acceptation_string_key(char ch){
int key=0;
if(ch<='z'&&ch>='a'){
key=0;
}
else
key=1;
return key;
}

//重置
int acceptation_str(char ch,char *str,int i,char *sch,NODE *tnode){ 
*sch++;
//printf("%d
",acceptation_string_key(ch));
if(acceptation_string_key(ch)==0){
if(acceptation_string_key(*sch)!=0&&acceptation_number_key(*sch)!=0){
//printf("str=%s",str);
//acceptation_string(str);

splitter_node_add(str,tnode);
memset(str, 0,i);
i=0;
} 
}
if(acceptation_logic_key(str)==0){

splitter_node_add(str,tnode);

memset(str, 0,i);
i=0;

}
if(acceptation_number_key(ch)==0){
if(acceptation_number_key(*sch)!=0){
//acceptation_number(str);

splitter_node_add(str,tnode);
memset(str, 0,i);
i=0;
}
}

return i;
}

//判断词意
void word_acceptation(char *ch,NODE *tnode){
char *str=NULL;
char **sstr=NULL;
int i=1,k=0,key=1;
sstr=&ch;
str=(char *)malloc(1);
str[0]=*ch;
str[1]='';

//存入str
while(*ch!=''){
*ch++;
k++;
str[i]=*ch;
i++;
str[i]='';
**sstr++;
sstr=&ch;
i=acceptation_str(*ch,str,i,*sstr,tnode);

//puts(str); 
}

}

//存入节点
void splitter_node_add(char *str,NODE *node){
NODE *p,*q;
p=node;
while(p->next!=NULL){
p=p->next;
}
q=(NODE *)malloc(sizeof(NODE));
q->next=NULL;
strcpy(q->words,str);
p->next=q;
p=q;

}
//判断空格回车位置
int splitter_start(char *str){
int i=0;
while(*str!=' '){
*str++;
i++;
if(*str=='')
break;
if(*str=='
')
break;
}
return i;
}

//将每个字母以空格或回车分成一段词
void word_splitter(char* str,NODE *node){
char *aword=NULL;
char **sword=NULL;
sword=&str;

//截取第一段
aword=(char *)malloc(splitter_start(*sword));
strncpy(aword,*sword,splitter_start(*sword));
aword[splitter_start(*sword)]='';

splitter_node_add(aword,node);
memset(aword, 0, splitter_start(*sword));
while(*str!=''){

//printf("%c
",*str);
*str++;
*aword++;
if(*str==' '||*str=='
'){
aword=(char *)malloc(splitter_start(*sword));

*str++;//截取空格或回车
**sword++;
sword=&str;
//printf("splitter_start=%d
",splitter_start(*sword));
//puts(*sword);
strncpy(aword,*sword,splitter_start(*sword));
aword[splitter_start(*sword)]='';
splitter_node_add(aword,node);
//puts(aword);
memset(aword, 0, splitter_start(*sword));
}

}
}




void word_node(char *ch,NODE *node){

NODE *p,*tnode;
puts(word);
tnode=(NODE *)malloc(sizeof(NODE));
tnode->next=NULL;

word_splitter(word,node);


p=node;
while(p->next!=NULL){
p=p->next;
word_acceptation(p->words,tnode);

}
printf("执行:

");
p=tnode;
while(p->next!=NULL){

p=p->next;
if(acceptation_logic(p->words)==0);
else if(acceptation_string(p->words)==0);
else if(acceptation_number(p->words)==0);
else{
printf("<错误>
");
}
}
}

int main(){
NODE *node;
node=(NODE *)malloc(sizeof(NODE));
node->next=NULL;

printf("请输入:
");
scanf("%[^#]s",word);
word_node(word,node);
return 0;

}
原文地址:https://www.cnblogs.com/caishun/p/4877110.html