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

 

#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;

}

时间: 2024-08-25 04:55:50

(改)编程实验一 词法分析程序的相关文章

0916 编程实验一 词法分析程序

#include<stdio.h> #include<stdlib.h> #include<string.h> #define _KEY_WOED_END "waiting for your expanding" typedef struct { int typenum; char * word; }WORD; char input[255]; char token[255] = ""; int p_input; int p_to

编程实验一 词法分析程序

#include<stdio.h> #include<stdlib.h> #include<string.h> #define _KEY_WOED_END "waiting for your expanding" //关键字结束标志 typedef struct { int typenum; char * word; }WORD; char input[255]; //输入换缓冲区 char token[255]=""; //单词

实验一&#160;&#160;词法分析程序045

实验一  词法分析程序实验 专业 计算机科学与技术   姓名 邹汉辉  学号 201508030045 一.实验目的 1. 编制一个词法分析程序 二. 实验内容和要求 1. 输入:源程序字符串 2. 输出:二元组(种别,单词本身) 3. 待分析语言的词法规则 三. 实验方法.步骤及结果测试 1.  源程序名:压缩包文件(rar或zip)中源程序名 cifafenxi.c 可执行程序名:cifafenxi.exe 2.  原理分析及流程图 3.  主要程序段及其解释: #include<stdio

实验一词法分析程序

一.        实验目的 编制一个词法分析程序 二.        实验内容和要求 输入:源程序字符串 输出:二元组(种别,单词本身) 待分析语言的词法规则 三.        实验方法.步骤及结果测试 1.      源程序名:压缩包文件(rar或zip)中源程序名 cifafenxi.c  cifafenxi.exe 2.      原理分析及流程图 3.      主要程序段及其解释: do { scanf("%s\n",input); i=0; while(input[i]

实验一 词法分析程序实验

实验一.词法分析程序实验 商业软件工程   蓝海鹏  201506110171 一.        实验目的        编制一个词法分析程序. 二.        实验内容和要求 实验内容: 对字符串表示的源程序 从左到右进行扫描和分解 根据词法规则: 单词符号 种别码 单词符号 种别码 begin 1 : 17 if 2 := 18 then 3 < 20 while 4 <= 21 do 5 <> 22 end 6 > 23 l(l|d)*(标识符) 10 >=

0917 实验一 词法分析程序

#include <stdio.h> #include <string.h> #include <iostream.h> #define BEGIN 1 #define IF 2 #define THEN 3 #define WHILE 4 #define DO 5 #define END 6 #define INT 11 #define LT 20 #define LE 21 #define EQ 24 #define NE 22 #define GT 12 #def

0917 实验一词法分析程序

#include <stdio.h>#include <string.h>int Input1(char a){ int i=0; switch(a) { case('+'):  printf("%c\t   13\n",a);i++;break; case('-'):  printf("%c\t   14\n",a);i++;break; case('*'):  printf("%c\t   15\n",a);i++;b

词法分析程序实验报告

实验一词法分析实验 商业软件工程2班  黄铭坤  201506110147 一.        实验目的        编制一个词法分析程序. 二.        实验内容和要求 输入:源程序字符串 输出:二元组(种别,单词本身) 待分析语言的词法规则 三.        实验方法.步骤及结果测试 1.      源程序名:词法分析.c 可执行程序名:词法分析.exe 2.      原理分析及流程图 (1)   关键字: begin  if  then  while  do  end (2) 

实验报告一 词法分析程序

实验一.词法分析程序实验 专业:商业软件工程   姓名:卓润峰  学号:201506110202 一.        实验目的 编制一个词法分析程序. 二.        实验内容和要求 1.输入:源程序字符串. 2.输出:二元组(种别,单词本身) 3.待分析语言的词法规则 主要是从左至右逐个字符地对源程序进行扫描,产生一个个单词序列,用于语法分析 三.        实验方法.步骤及结果测试 1.     源程序名:压缩包文件(rar或zip)中源程序名 cifafenxi.c 可执行程序名: