码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-10-14 14:18:50      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

 

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


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

char word[255];
char *word_default[6]={"begin","if","then","while","do","end"};


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

//逻辑判断
int acceptation_logic(char *ch){

puts(ch);
int key=0;
if(strcmp(ch,"begin")==0){
printf("<%s \t 1 >\n",ch);
}

else if(strcmp(ch,"if")==0){
printf("<%s \t 2 >\n",ch);
}
else if(strcmp(ch,"then")==0){
printf("<%s \t 3 >\n",ch);
}
else if(strcmp(ch,"while")==0){
printf("<%s \t 4 >\n",ch);
}
else if(strcmp(ch,"do")==0){
printf("<%s \t 5 >\n",ch);
}
else if(strcmp(ch,"end")==0){
printf("<%s \t 6 >\n",ch);
}
else if(strcmp(ch,"+")==0){
printf("<%s \t 13>\n",ch);
}
else if(strcmp(ch,"-")==0){
printf("<%s \t 14>\n",ch);
}
else if(strcmp(ch,"*")==0){
printf("<%s \t 15>\n",ch);
}
else if(strcmp(ch,"/")==0){
printf("<%s \t 16>\n",ch);
}
else if(strcmp(ch,":")==0){
printf("<%s \t 17>\n",ch);
}
else if(strcmp(ch,":=")==0){
printf("<%s \t 18>\n",ch);
}
else if(strcmp(ch,"<")==0){
printf("<%s \t 20>\n",ch);
}
else if(strcmp(ch,"<=")==0){
printf("<%s \t 21>\n",ch);
}
else if(strcmp(ch,"<>")==0){
printf("<%s \t 22>\n",ch);
}
else if(strcmp(ch,">")==0){
printf("<%s \t 23>\n",ch);
}
else if(strcmp(ch,">=")==0){
printf("<%s \t 24>\n",ch);
}
else if(strcmp(ch,"=")==0){
printf("<%s \t 25>\n",ch);
}
else if(strcmp(ch,";")==0){
printf("<%s \t 26>\n",ch);
}
else if(strcmp(ch,"(")==0){
printf("<%s \t 27>\n",ch);
}
else if(strcmp(ch,")")==0){
printf("<%s \t 28>\n",ch);
}
else if(strcmp(ch,"#")==0){
printf("<%s \t 0>\n",ch);
}
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 \t 11>\n",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!=‘\0‘){

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

}
if(key==0)
printf("<%s \t 10>\n",str);

return key;

}

int acceptation_string_key(char ch){
int key=0;
if(ch<=‘z‘&&ch>=‘a‘){
key=0;
}
else
key=1;
return key;
}

void node_in(char *str,NODE *node){

}

//重置
int acceptation_str(char ch,char *str,int i,char *sch,NODE *tnode){
*sch++;
//printf("%d\n",acceptation_string_key(ch));
if(acceptation_logic(str)==0){

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

}
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);i=0;
memset(str, 0,i);

}

}

if(acceptation_number_key(ch)==0){
if(acceptation_number_key(*sch)!=0){
acceptation_number(str);
splitter_node_add(str,tnode);i=0;
memset(str, 0,i);

}
}


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]=‘\0‘;

//存入str
while(*ch!=‘\0‘){
*ch++;
k++;
str[i]=*ch;
i++;
str[i]=‘\0‘;
**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==‘\0‘)
break;
if(*str==‘\n‘)
break;
}
return i;
}


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

char *aword=NULL;
char **sword=NULL;

sword=&str;

//printf("splitter_start=%d\n",splitter_start(*sword));

//截取第一段
aword=(char *)malloc(splitter_start(*sword));
strncpy(aword,*sword,splitter_start(*sword));
aword[splitter_start(*sword)]=‘\0‘;
//puts(aword);
splitter_node_add(aword,node);
memset(aword, 0, splitter_start(*sword));

while(*str!=‘\0‘){

//printf("%c\n",*str);
*str++;
*aword++;

if(*str==‘ ‘||*str==‘\n‘){
aword=(char *)malloc(splitter_start(*sword));

*str++;//截取空格或回车
**sword++;
sword=&str;

//printf("splitter_start=%d\n",splitter_start(*sword));
//puts(*sword);

strncpy(aword,*sword,splitter_start(*sword));
aword[splitter_start(*sword)]=‘\0‘;

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存入node节点
//splitter_node_add("if",node);
word_splitter(word,node);

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

printf("\n\n");

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("<错误>\n");
}

//puts(p->words);
}

}


int main(){
NODE *node;

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

printf("请输入:\n");
scanf("%[^#]s",word);

word_node(word,node);

//puts(word);

return 0;

}

 

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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!