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

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

//重置
int acceptation_str(char ch,char *str,int i,char *sch,NODE *tnode){ 
*sch++;
//printf("%d\n",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]='\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;

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

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_splitter(word,node);


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

}
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");
}
}
}

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

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

}

 

转载于:https://www.cnblogs.com/caishun/p/4877110.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java编程 含有界面 以及完整代码 〈程序〉→ main()〈语句块〉 〈语句块〉→{〈语句串〉} 〈语句串〉→〈语句〉;〈语句串〉|〈语句〉; 〈语句〉→〈赋值语句〉|〈条件语句〉|〈循环语句〉 〈赋值语句〉→ ID =〈表达式〉; 〈条件语句〉→ if〈条件〉〈语句块〉 〈循环语句〉→ while〈条件〉〈语句块〉 〈条件〉→(〈表达式〉〈关系符〉〈表达式〉) 〈表达式〉→〈表达式〉〈运算符〉〈表达式〉|(〈表达式〉)|ID|NUM 〈运算符〉→+|-|*|/ 〈关系符〉→<|<=|>|>=|=|!> word.wordList包(存储了关键字): word:此类是定义了存储关键字的结构:包括String型的关键字,和int型的识别符。 wordList:此类存储了29个关键字,在构造函数中初始化。 2、word包(进行词法分析)中: basicFunction:此类定义了做词法分析的基本函数: GetChar()将下一输入字符读到ch中,搜索知识器前移一个字符位置 GetBC();检查ch中的字符是否为空白。若是,则调用GetChar直至不 是字符为止 Concat();将ch中的字符连接到strToken之后 IsLetter();判断ch中的字符是否为字母 IsDigit();判断ch中的字符是否为数字 Reserve();对strToken中的字符创查找保留字表,若是则返回它的编码,否则返回0 Retract();将搜索指示器回调一个字符位置 RetractStr();将strToken置空 lexAnalysis:此类是用来进行词法分析,将分析后的单词存入word数组中,(注:在词法分析中,若是一串字母,则认为是ID,若是数字,则认为是NUM。存储的时候识别符分别存ID与NUM的识别符,但是内容仍然是自己的内容) 其中的wordAnalysis函数就是词法分析函数(具体实现请看后面的重要函数分析) 3、stack包(定义栈)中: 栈是通过链表来定义的,因此 StringListElement:次类定义了链表的每一个节点 StringStrack:此类定义了栈,其中有长度属性,有函数: Top();用来取得栈顶 Push();压栈 Pop();出栈 4、sentence包(语法分析)中: juzi :定义了文法的句子的结构:key(左边部分) content[](右边推出的部分) lo(长度) grammar :存储了文法的27个关系式 AnalysisFB :定义了分析表的存储结构 AnalysisF :存储分析表 SentenceAnalysis :语法分析 JuProduction(word w):此函数是用来判断在当前栈与输入串的情况下,用哪一个产生式,返回产生式在数组中的下标 若输入串的第一个字符与栈顶字符相同则表示可以规约,则返回-1; 若不能过用产生式,则返回-2; AnalysisBasic(word w):此函数是分布进行语法分析,对栈操作 * 根据所需要的产生式对符号栈进行操作 * 返回0表示规约;返回1表示移进;否则表示输入串不是文法的句子 5.Main包(主界面)中 Main:此类定义了图形界面
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值