本程序将对用户输入的字符串进行判断,分别输出每个字符串所属的类型。其中的判断标准如下:
无符号整数:<整数>{<整数>}
标识符:<字母>{<字母><数字>}
数字:0|1|2|......|8|9
字母:a|b|......x|y|z
保留字:const|var|procedur|begin|end|odd|if|then|call|while|do|read|write
运算符:+|-|*|/|=|#|<|<=|>|>=|:=
界符:(|)|,|;|.
用文法描述词法规则如下:
无符号整数=A,A->1|2|......8|9|A0|A1|......A8|A9
标识符=@#,@->a|b|......y|z|A|B|......X|Y|Z|@a|@b......|@y|@z|@A|@B|......|@X|@Y|@Z,#->null|0|1|2|......|8|9|#0|#1|......#9
数字=0|1|2|......|8|9
字母=a|b|......x|y|z
保留字=const|var|procedur|begin|end|odd|if|then|call|while|do|read|write
运算符=+|-|*|/|=|#|<|<=|>|>=|:=
界符=(|)|,|;|.
词法分析的代码:
1 #include<stdio.h> 2 char const_temp[6]={'c','o','n','s','t',' '}; 3 char var_temp[4]={'v','a','r',' '}; 4 char procedur_temp[9]={'p','r','o','c','e','d','u','r',' '}; 5 char begin_temp[6]={'b','e','g','i','n',' '}; 6 char end_temp[4]={'e','n','d',' '}; 7 char odd_temp[4]={'o','d','d',' '}; 8 char if_temp[3]={'i','f',' '}; 9 char then_temp[5]={'t','h','e','n',' '}; 10 char call_temp[5]={'c','a','l','l',' '}; 11 char while_temp[6]={'w','h','i','l','e',' '}; 12 char do_temp[3]={'d','o',' '}; 13 char read_temp[5]={'r','e','a','d',' '}; 14 char write_temp[6]={'w','r','i','t','e',' '}; 15 char Keyworld_first(char temp,char temppp); 16 char Keyworld_double(char temp,char temppp); 17 main() 18 { 19 char ch,chtemp; 20 ch=getchar(); 21 while(ch!=' ') 22 { 23 if(ch>='0'&&ch<='9') 24 { 25 chtemp=getchar(); 26 if(chtemp<'0'||chtemp>'9') 27 { 28 printf("数字:%c ",ch); 29 ch=chtemp; 30 } 31 else 32 { 33 printf("无符号整数:%c",ch); 34 ch=chtemp; 35 while(ch>='0'&&ch<='9') 36 { 37 printf("%c",ch); 38 ch=getchar(); 39 } 40 printf(" "); 41 } 42 } 43 else if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z') 44 { 45 chtemp=getchar(); 46 if((chtemp<'a'||chtemp>'z')&&(chtemp<'A'||chtemp>'Z')) 47 { 48 printf("字母:%c ",ch); 49 ch=chtemp; 50 } 51 else 52 { 53 if(chtemp!=' '&&(ch=='v'||ch=='p'||ch=='b'||ch=='e'||ch=='o'||ch=='i'||ch=='t'||ch=='d'||ch=='r')) 54 ch=Keyworld_first(ch,chtemp); 55 else if(chtemp!=' '&&(ch=='w'||ch=='c')) 56 ch=Keyworld_double(ch,chtemp); 57 else 58 { 59 printf("标识符:%c%c",ch,chtemp); 60 if(chtemp==' ')break; 61 ch=getchar(); 62 while(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'||ch>='0'&&ch<='9') 63 { 64 printf("%c",ch); 65 ch=getchar(); 66 } 67 printf(" "); 68 } 69 } 70 } 71 else if(ch=='+'||ch=='-'||ch=='*'||ch=='='||ch=='<'||ch=='>'||ch==':'||ch=='/'||ch=='#') 72 { 73 chtemp=getchar(); 74 if(chtemp=='='&&(ch=='>'||ch=='<'||ch==':')) 75 { 76 printf("运算符:%c%c ",ch,chtemp); 77 ch=getchar(); 78 } 79 else 80 { 81 printf("运算符:%c ",ch); 82 ch=chtemp; 83 } 84 } 85 else if(ch=='('||ch==')'||ch==','||ch==';'||ch=='.') 86 { 87 printf("界符:%c ",ch); 88 ch=getchar(); 89 } 90 else 91 ch=getchar(); 92 } 93 } 94 char Keyworld_first(char temp,char temppp) 95 { 96 char *p; 97 int i,number,length=-1; 98 switch (temp) 99 { 100 case 'v': p=var_temp; 101 break; 102 case 'p': p=procedur_temp; 103 break; 104 case 'b': p=begin_temp; 105 break; 106 case 'e': p=end_temp; 107 break; 108 case 'o': p=odd_temp; 109 break; 110 case 'i': p=if_temp; 111 break; 112 case 't': p=then_temp; 113 break; 114 case 'd': p=do_temp; 115 break; 116 case 'r': p=read_temp; 117 break; 118 } 119 for(i=0;i<9;i++) 120 { 121 if(p[i]==' ')break; 122 length++; 123 } 124 number=0; 125 while(temp==p[number]) 126 { 127 if(number==length+1)break; 128 if(number==0) 129 temp=temppp; 130 else 131 temp=getchar(); 132 number++; 133 } 134 if(number==length+1&&(!(temp>='a'&&temp<='z'||temp>='A'&&temp<='Z'||temp>='0'&&temp<='9'))) 135 { 136 printf("保留字:"); 137 for(i=0;i<=length;i++) 138 printf("%c",p[i]); 139 printf(" "); 140 } 141 else 142 { 143 printf("标识符:"); 144 for(i=0;i<number;i++) 145 printf("%c",p[i]); 146 while(temp>='a'&&temp<='z'||temp>='A'&&temp<='Z'||temp>='0'&&temp<='9') 147 { 148 printf("%c",temp); 149 temp=getchar(); 150 } 151 printf(" "); 152 } 153 return temp; 154 } 155 char Keyworld_double(char temp,char temppp) 156 { 157 char *p; 158 int i,number,length=-1; 159 switch (temp) 160 { 161 case 'c': if(temppp=='o') 162 p=const_temp; 163 else if(temppp=='a') 164 p=call_temp; 165 else 166 { 167 printf("标识符:"); 168 printf("%c",temp); 169 temp=temppp; 170 while(temp>='a'&&temp<='z'||temp>='A'&&temp<='Z'||temp>='0'&&temp<='9') 171 { 172 printf("%c",temp); 173 temp=getchar(); 174 } 175 printf(" "); 176 return temp; 177 } 178 break; 179 case 'w': if(temppp=='h') 180 p=while_temp; 181 else if(temppp=='r') 182 p=write_temp; 183 else 184 { 185 printf("标识符:"); 186 printf("%c",temp); 187 temp=temppp; 188 printf("%c",temp); 189 while(temp>='a'&&temp<='z'||temp>='A'&&temp<='Z'||temp>='0'&&temp<='9') 190 { 191 printf("%c",temp); 192 temp=getchar(); 193 } 194 printf(" "); 195 return temp; 196 } 197 break; 198 } 199 for(i=0;i<9;i++) 200 { 201 if(p[i]==' ')break; 202 length++; 203 } 204 number=1; 205 temp=temppp; 206 while(temp==p[number]) 207 { 208 if(number==length+1)break; 209 temp=getchar(); 210 number++; 211 } 212 if(number==length+1&&(!(temp>='a'&&temp<='z'||temp>='A'&&temp<='Z'||temp>='0'&&temp<='9'))) 213 { 214 printf("保留字:"); 215 for(i=0;i<=length;i++) 216 printf("%c",p[i]); 217 printf(" "); 218 } 219 else 220 { 221 printf("标识符:"); 222 for(i=0;i<number;i++) 223 printf("%c",p[i]); 224 while(temp>='a'&&temp<='z'||temp>='A'&&temp<='Z'||temp>='0'&&temp<='9') 225 { 226 printf("%c",temp); 227 temp=getchar(); 228 } 229 printf(" "); 230 } 231 return temp; 232 }
程序运行截图: