首页 > 代码库 > 简单的词法分析小程序
简单的词法分析小程序
实验:词法分析
一.实验目的:编写一个词法分析
实验要求:输入:源程序字符串
输出:二元组(种别,单词本身)
二.词法分析程序设计
词法规则:字母<a|b|c|...|z>
数字<0|1|2|...|9>
整数常数:<数字>
标识符:<变量〉
关键字:<main|scanf|printf|...|const>
运算符:<+|-|*|...|:=>
界符 :<(|)|,|;|.>
各单词对应的种别码
三:源代码展示
1 #include<stdio.h> 2 #include<malloc.h> 3 #include<stdlib.h> 4 #include<ctype.h> 5 typedef struct node 6 { 7 char ch; 8 struct node *next; 9 }QNode,*QueuePtr; 10 11 typedef struct 12 { 13 QueuePtr front; 14 QueuePtr rear; 15 }LinkQueue; 16 17 LinkQueue Q; 18 QueuePtr p; //定义一个指针变量 19 char *keyword[30]={"main","scanf","printf","void","int","float","double","char","long","short","signed", 20 "struct","do","while","for","switch","case","break","continue","if","define","typedef","union", 21 "static","sizeof","return","extern","goto","auto","const"}; 22 char *operatornum[6]={"+","-","*","/","++","--"}; 23 char *comparison[8]={"<","<=","=",">",">=","<>","==","!="}; 24 char *interpunction[8]={",",";",":=",".","(",")","{","}"}; 25 char *biaoshifu[6]={"%","$","^","&","_","#"}; //特殊标识符 26 char *zhushifu[3]={"//","/*","*/"}; //注释符 27 char *luoji[3]={"&&","||","!"}; //逻辑运算符 28 char *k=""; 29 30 void InitQueue() //初始化一个带节点的空队列 31 { 32 Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode)); 33 if(!Q.front) 34 exit(0); 35 Q.front->next=NULL; 36 } 37 void Printf() 38 { 39 QueuePtr p; //定义一个指针变量 40 p=Q.front; 41 while(!(p==Q.rear)) 42 { 43 p=p->next; 44 printf("%c",p->ch); 45 } 46 printf("\n"); 47 } 48 void Creat() //创建队列 49 { 50 char ch; 51 QueuePtr p; 52 printf("请输入您要分析的字符串"); 53 ch=getchar(); 54 while(ch!=‘@‘) 55 { 56 p=(QueuePtr)malloc(sizeof(QNode)); 57 p->ch=ch; 58 p->next=NULL; 59 Q.rear->next=p; 60 Q.rear=p; 61 ch=getchar(); 62 } 63 } 64 65 66 int search(char searchstr[],int wordtype)//符号匹配 67 { 68 int i; 69 switch (wordtype) 70 { 71 case 1: 72 printf("你好"); 73 for(i=0;i<30;i++) 74 { 75 if(strcmp(keyword[i],searchstr)==0) 76 return i; 77 } 78 break; 79 case 2: 80 for(i=0;i<=5;i++) 81 { 82 if(strcmp(operatornum[i],searchstr)==0) 83 return 30+i; 84 } 85 break; 86 87 case 3: 88 for(i=0;i<=7;i++) 89 { 90 if(strcmp(comparison[i],searchstr)==0) 91 return 36+i; 92 } 93 break; 94 95 case 4: 96 for(i=0;i<=7;i++) 97 { 98 if(strcmp(interpunction[i],searchstr)==0) 99 return 44+i; 100 } 101 102 break; 103 case 5: 104 for(i=0;i<=5;i++) 105 { 106 if(strcmp(biaoshifu[i],searchstr)==0) 107 return 52+i; 108 } 109 break; 110 case 6: 111 for(i=0;i<=2;i++) 112 { 113 if(strcmp(zhushifu[i],searchstr)==0) 114 return 58+i; 115 } 116 break; 117 case 7: 118 for(i=0;i<=2;i++) 119 { 120 if(strcmp(luoji[i],searchstr)==0) 121 return 61+i; 122 } 123 break; 124 } 125 126 return 0; 127 } 128 void Analyze()129 {130 char str;131 char letter[20]; 132 char num[20]; 133 char other[20]; 134 int i;135 p=Q.front;136 while(p!=Q.rear) 137 {138 139 p=p->next; //获取单个字符 140 str=p->ch;141 if (isalpha(str)!=0) //如果是字符 142 {143 i=-1;144 while (isalnum(str)!=0) 145 { 146 letter[++i]=str; 147 str=p->next->ch; 148 } 149 letter[i+1]=‘\0‘; 150 if (search(letter,1)) 151 { 152 printf("( %d,%s )",search(letter,1),letter); 153 154 } 155 else 156 { 157 printf("( %d,%s )\n",65,letter); 158 } 159 } 160 else 161 { 162 if (isdigit(str)!=0) 163 {164 i=-1;165 while (isdigit(str)!=0) 166 { 167 num[++i]=str; 168 str=p->next->ch; 169 } 170 if(isalpha(str)!=0) //数字后面是字符 171 { 172 while(isspace(str)==0) 173 { 174 num[++i]=str; 175 str=p->next->ch; 176 } 177 num[i+1]=‘\0‘; 178 printf("错误!非法标识符:%s\n",num); 179 180 } 181 num[i+1]=‘\0‘; 182 printf("( %d,%s )",64,num); 183 }184 else 185 {186 i=-1; 187 if (isspace(str)!=0) 188 { 189 str=p->next->ch;190 191 } 192 while ((isspace(str)==0)&&(isalnum(str)==0)) 193 { 194 other[++i]=str; 195 str=p->next->ch; 196 } 197 other[i+1]=‘\0‘; 198 if (search(other,2)) 199 printf("( %d,%s )\n",search(other,2),other); 200 else if (search(other,3)) 201 printf("( %d,%s )\n",search(other,3),other); 202 else if (search(other,4)) 203 printf("( %d,%s )\n",search(other,4),other); 204 else if (search(other,5)) 205 printf("( %d,%s )\n",search(other,5),other); 206 else if (search(other,6)) 207 printf("( %s,注释符号 )\n",other); 208 else if (search(other,7)) 209 printf("( %d,%s )\n",search(other,7),other); 210 else 211 printf("错误!非法字符:%s\n",other); 212 }213 } 214 }; 215 printf("词法分析结束,谢谢使用!\n"); 216 }217 218 int main()219 {220 221 InitQueue(); //创建空链队列 222 Creat(); //创建队列 223 Printf(); //输出队列 224 Analyze(); //词法分析 225 return 0;226 }
简单的词法分析小程序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。