一、设计内容:
处理c语言源程序,过滤掉无用符号,判断源程
序中单词的合法性,并分解出正确的单词,以二元组形式
存放在文件中。
二、设计目的:
了解高级语言单词的分类,了解状态图以及如
何表示并识别单词规则,掌握状态图到识别程序的编程。
三、算法流程图:
四、种别码与对应的单词符号:
单词符号 |
种别码 |
单词符号 |
种别码 |
auto |
1 |
double |
2 |
int |
3 |
struct |
4 |
break |
5 |
else |
6 |
long |
7 |
switch |
8 |
case |
9 |
enum |
10 |
register |
11 |
typedef |
12 |
char |
13 |
extern |
14 |
return |
15 |
union |
16 |
const |
17 |
float |
18 |
short |
19 |
unsigned |
20 |
continue |
21 |
for |
22 |
signed |
23 |
void |
24 |
default |
25 |
goto |
26 |
sizeof |
27 |
volatile |
28 |
do |
29 |
while |
30 |
static |
31 |
if |
32 |
\a |
33 |
\b |
34 |
\f |
35 |
\n |
36 |
\t |
37 |
\v |
38 |
\\ |
39 |
\? |
40 |
\’ |
41 |
\” |
42 |
\0 |
43 |
\ddd |
44 |
\ xhh |
45 |
数字 |
46 |
标识符 |
47 |
# |
48 |
( |
49 |
) |
50 |
[ |
51 |
] |
52 |
{ |
53 |
} |
54 |
‘ |
55 |
* |
56 |
: |
57 |
~ |
58 |
% |
59 |
^ |
60 |
+ |
61 |
? |
62 |
= |
63 |
| |
64 |
& |
65 |
! |
66 |
< |
67 |
> |
68 |
>= |
69 |
== |
70 |
>> |
71 |
!= |
72 |
<< |
73 |
&& |
74 |
<= |
75 |
|| |
76 |
++ |
77 |
?= |
78 |
- |
79 |
-- |
80 |
-> |
81 |
“ |
82 |
%A(A可为d’s’c) |
83 |
; |
84 |
_ |
85 |
/ |
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
其他类别 |
99 |
五、程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iostream>
usingnamespacestd;
stringkeywords[20]={"include","void","main","int","char","float","double","if","else","then","break","continue","for","do","while","printf","scanf","begin","end","return"};
charrz[99999]=" ";
stringid[10000];
intpp=0;
stringnu[10000];
intqq=0;
intchoice1(chara) //判断是否是字母
{
if((a>='a'&&a<='z')||(a>='A'&&a<='Z'))
return1;
elsereturn0;
}
intchoice2(chara) //判断是否是数字
{
if(a>='0'&&a<='9')
return1;
elsereturn0;
}
intalpha(intst) //识别保留字和标识符
{
charwordbuf[20]=" ";
intn=0;
for(;;)
{
wordbuf[n]=rz[st];
st++;
n++;
if((choice2(rz[st])==1)||(choice1(rz[st])==1)||(rz[st]=='_'))
wordbuf[n]=rz[st];
elsebreak;
}
intflag=0;
for(intk=0;k<20;k++)
{
if(strcmp(keywords[k].c_str(),wordbuf)==0)flag=1;
}
if(flag==0)
{
intflagg=-1;
for(intt=0;t<pp;t++)
{
if(strcmp(id[t].c_str(),wordbuf)==0)
{
flagg=t;
}
}
if(flagg!=-1)printf(" (id,%d) ",flagg);
else
{
id[pp]=wordbuf;
printf(" (id,%d) ",pp);
pp++;
}
}
else
{
printf(" (");
for(inti=0;i<n;i++)
{
printf("%c",wordbuf[i]);
}
printf(",-) ");
}
returnst;
}
intnumber(intst) //识别整数
{
charnumbuf[20]=" ";
intn=0;
intk=0;
intflag=0;
for(;;)
{
numbuf[n]=rz[st];
st++;
n++;
if(choice2(rz[st])==1)
{
numbuf[n]=rz[st];
}
elseif((k==0)&&(rz[st]=='.'))
{
numbuf[n]=rz[st];
k++;
}
elseif(choice1(rz[st])==1)
{
numbuf[n]=rz[st];
flag=1;
continue;
}
elsebreak;
}
if(flag==0)
{
intflagg=-1;
for(intt=0;t<qq;t++)
if(strcmp(nu[t].c_str(),numbuf)==0)
flagg=t;
if(flagg!=-1)printf(" (nu,%d) ",flagg);
else
{
nu[qq]=numbuf;
printf(" (nu,%d) ",qq);
qq++;
}
}
else
{
printf(" (");
for(inti=0;i<n;i++)printf("%c",numbuf[i]);
printf(",error digital!) ");
}
returnst;
}
intanotation(intst) //处理除号/和注释
{
chartabuf[9999]=" ";
intn=0;
st++;
if(rz[st]=='/')
{
printf(" (//,-)");
st++;
while(rz[st]!=10)
{
tabuf[n]=rz[st];
st++;
n++;
}
printf(" \n 注释");
for(inti=0;i<n;i++)
printf("%c",tabuf[i]);
}
elseif(rz[st]=='*')
{
printf(" (/*,-) ");
st++;
intstt=st+1;
while(1)
{
if(rz[st]=='*'&&rz[st+1]=='/')break;
tabuf[n]=rz[st];
st++;
n++;
if(rz[st+1]=='\0')
{
printf("(/* error!!\n)");
returnst+1;
}
} printf(" \n 注释");
for(inti=0;i<n;i++)
printf("%c",tabuf[i]);
printf(" (*/,-) ");
st=st+2;
}
elseif(rz[st]=='=')
{
st++;
printf(" (/*,-) ");
}
elseprintf(" (/,-) ");
returnst;
}
intother(intst) //函数识别其他特殊字符
{
switch(rz[st])
{
case'=':st++;
if(rz[st]=='=')
{
st++;
printf(" (rlop,==) ");
}
elseprintf(" (rlop,=) ");
break;
case'+':st++;
if(rz[st]=='=')
{
st++;
printf(" (+=,-) ");
}
elseif(rz[st]=='+')
{
st++;
printf(" (++,-) ");
}
elseprintf(" (+,-) ");
break;
case'-':st++;
if(rz[st]=='=')
{
st++;
printf(" (-=,-) ");
}
elseif(rz[st]=='-')
{
st++;
printf(" (--,-) ");
}
elseprintf(" (-,-) ");
break;
case'*':st++;
if(rz[st]=='=')
{
st++;
printf(" (*=,-) ");
}
else printf(" (*,-) ");
break;
case'>':st++;
if(rz[st]=='=')
{
st++;
printf(" (rlop,>=) ");
}
elseprintf(" (rlop,>) ");
break;
case'<':st++;
if(rz[st]=='=')
{
st++;
printf(" (rlop,<=) ");
}
elseprintf(" (rlop,<) ");
break;
case'%':st++;
if(rz[st]=='=')
{
st++;
printf(" (\%=,-) ");
}
elseprintf(" (\%,-) ");
break;
case'!':st++;
if(rz[st]=='=')
{
st++;
printf(" (!=,-) ");
}
else printf(" (!,wrong thing!) ");
break;
case'&':st++;
if(rz[st]=='&')
{
st++;
printf(" (&&,-) ");
}
elseprintf(" (&,worng word!) ");
break;
case'|':st++;
if(rz[st]=='|')
{
st++;
printf(" (||,-) ");
}
elseprintf(" (|,worngword!) ");
break;
case'{':st++;
printf(" ({,-) ");
break;
case'}':st++;
printf(" (},-) ");
break;
case'(':st++;
printf(" ((,-) ");
break;
case')':st++;
printf(" (),-) ");
break;
case'[':st++;
printf(" ([,-) ");
break;
case']':st++;
printf(" (],-) ");
break;
case':':st++;
printf(" (:,-) ");
break;
case'#':st++;
printf(" (#,-) ");
break;
case';':st++;
printf(" (;,-) ");
break;
case'.':st++;
printf(" (.,-) ");
break;
case',':st++;
printf(" (,,-) ");
break;
case' ':st++;
break;
case' ':st++;
break;
case10: st++;
printf("\n");
break;
case34: st++;
printf(" (\",-) ");
break;
case39: st++;
printf(" (',-) ");
break;
default:printf(" (%c,worngthing) ",rz[st]);
st++;
}
returnst;
}
intchoice(intst) //根据读入的单词的第一个字符确定调用不同的单词识别函数
{
if(choice1(rz[st])==1)
st=alpha(st);
elseif(choice2(rz[st])==1)
st=number(st);
elseif(rz[st]=='/')
st=anotation(st);
elsest=other(st);
returnst;
}
intmain()
{
inti=0;
FILE*fp;
charname[10];
printf("请输入文件名:\n");
scanf("%s",&name);
if((fp=fopen(name,"r"))==NULL)
{
printf("Open error!");
exit(0);
}
charch=fgetc(fp);
while(ch!=EOF)
{
rz[i]=ch;
i++;
ch=fgetc(fp);
}
fclose(fp);
intj=0;
while(rz[j]!='\0')
j=choice(j);
cout<<endl<<" 程序中标示符如下 "<<endl;
for(inti=0;i<pp;i++)
cout<<i<<" "<<id[i]<<endl;
cout<<" 程序中数字如下"<<endl;
for(intj=0;j<qq;j++)
cout<<j<<" "<<nu[j]<<endl;
system("pause");
}
六、实验结果:
请输入文件名:
C:\Users\qyjbo\Desktop\a.txt
(#,-) (include,-) (rlop,<) (id,0) (.,-) (id,1) (rlop,>)
(#,-) (include,-) (rlop,<) (id,2) (.,-) (id,1) (rlop,>)
(#,-) (include,-) (rlop,<) (id,3) (rlop,>)
(#,-) (include,-) (rlop,<) (id,4) (rlop,>)
(id,5) (id,6) (id,7) (;,-)
(id,3) (id,8) ([,-) (nu,0) (],-) (rlop,=) ({,-) (",-) (include,-) (",-) (,,-) (",-) (void,-) (",-) (,,-) (",-) (main,-) (",-) (,,-) (",-) (int,-) (",-) (,,-) (",-) (char,-) (",-) (,,-) (",-) (float,-) (",-) (,,-) (",-) (double,-) (",-) (,,-) (",-) (if,-) (",-) (,,-) (",-) (else,-) (",-) (,,-) (",-) (then,-) (",-) (,,-) (",-) (break,-) (",-) (,,-) (",-) (continue,-) (",-) (,,-) (",-) (for,-) (",-) (,,-) (",-) (do,-) (",-) (,,-) (",-) (while,-) (",-) (,,-) (",-) (printf,-) (",-) (,,-) (",-) (scanf,-) (",-) (,,-) (",-) (begin,-) (",-) (,,-) (",-) (end,-) (",-) (,,-) (",-) (return,-) (",-) (},-) (;,-)
(char,-) (id,9) ([,-) (nu,1) (],-) (rlop,=) (",-) (",-) (;,-)
(id,3) (id,10) ([,-) (nu,2) (],-) (;,-)
(int,-) (id,11) (rlop,=) (nu,3) (;,-)
(id,3) (id,12) ([,-) (nu,2) (],-) (;,-)
(int,-) (id,13) (rlop,=) (nu,3) (;,-)
(int,-) (id,14) ((,-) (char,-) (id,15) (),-) (//,-)
注释判断是否是字母
({,-)
(if,-) ((,-) ((,-) (id,15) (rlop,>=) (',-) (id,15) (',-) (&&,-) (id,15) (rlop,<=) (',-) (id,16) (',-) (),-) (||,-) ((,-) (id,15) (rlop,>=) (',-) (id,17) (',-) (&&,-) (id,15) (rlop,<=) (',-) (id,18) (',-) (),-) (),-)
(return,-) (nu,4) (;,-)
(else,-) (return,-) (nu,3) (;,-)
(},-)
(int,-) (id,19) ((,-) (char,-) (id,15) (),-) (//,-)
注释判断是否是数字
({,-)
(if,-) ((,-) (id,15) (rlop,>=) (',-) (nu,3) (',-) (&&,-) (id,15) (rlop,<=) (',-) (nu,5) (',-) (),-)
(return,-) (nu,4) (;,-)
(else,-) (return,-) (nu,3) (;,-)
(},-)
(int,-) (id,20) ((,-) (int,-) (id,21) (),-) (//,-)
注释识别保留字和标识符
({,-)
(char,-) (id,22) ([,-) (nu,0) (],-) (rlop,=) (",-) (",-) (;,-)
(int,-) (id,23) (rlop,=) (nu,3) (;,-)
(for,-) ((,-) (;,-) (;,-) (),-)
({,-)
(id,22) ([,-) (id,23) (],-) (rlop,=) (id,9) ([,-) (id,21) (],-) (;,-)
(id,21) (++,-) (;,-)
(id,23) (++,-) (;,-)
(if,-) ((,-) ((,-) (id,19) ((,-) (id,9) ([,-) (id,21) (],-) (),-) (rlop,==) (nu,4) (),-) (||,-) ((,-) (id,14) ((,-) (id,9) ([,-) (id,21) (],-) (),-) (rlop,==) (nu,4) (),-) (||,-) ((,-) (id,9) ([,-) (id,21) (],-) (rlop,==) (',-) (_,worng thing) (',-) (),-) (),-)
(id,22) ([,-) (id,23) (],-) (rlop,=) (id,9) ([,-) (id,21) (],-) (;,-)
(else,-) (break,-) (;,-)
(},-)
(int,-) (id,24) (rlop,=) (nu,3) (;,-)
(for,-) ((,-) (int,-) (id,25) (rlop,=) (nu,3) (;,-) (id,25) (rlop,<) (nu,0) (;,-) (id,25) (++,-) (),-)
({,-)
(if,-) ((,-) (id,26) ((,-) (id,8) ([,-) (id,25) (],-) (.,-) (id,27) ((,-) (),-) (,,-) (id,22) (),-) (rlop,==) (nu,3) (),-) (id,24) (rlop,=) (nu,4) (;,-)
(},-)
(if,-) ((,-) (id,24) (rlop,==) (nu,3) (),-)
({,-)
(int,-) (id,28) (rlop,=) (-,-) (nu,4) (;,-)
(for,-) ((,-) (int,-) (id,29) (rlop,=) (nu,3) (;,-) (id,29) (rlop,<) (id,11) (;,-) (id,29) (++,-) (),-)
({,-)
(if,-) ((,-) (id,26) ((,-) (id,10) ([,-) (id,29) (],-) (.,-) (id,27) ((,-) (),-) (,,-) (id,22) (),-) (rlop,==) (nu,3) (),-)
({,-)
(id,28) (rlop,=) (id,29) (;,-)
(},-)
(},-)
(if,-) ((,-) (id,28) (!=,-) (-,-) (nu,4) (),-) (printf,-) ((,-) (",-) ((,-) (id,10) (,,-) (,-) (id,30) (),-) (",-) (,,-) (id,28) (),-) (;,-)
(else,-)
({,-)
(id,10) ([,-) (id,11) (],-) (rlop,=) (id,22) (;,-)
(printf,-) ((,-) (",-) ((,-) (id,10) (,,-) (,-) (id,30) (),-) (",-) (,,-) (id,11) (),-) (;,-)
(id,11) (++,-) (;,-)
(},-)
(},-)
(else,-)
({,-)
(printf,-) ((,-) (",-) ((,-) (",-) (),-) (;,-)
(for,-) ((,-) (int,-) (id,31) (rlop,=) (nu,3) (;,-) (id,31) (rlop,<) (id,23) (;,-) (id,31) (++,-) (),-)
({,-)
(printf,-) ((,-) (",-) (,-) (id,32) (",-) (,,-) (id,22) ([,-) (id,31) (],-) (),-) (;,-)
(},-)
(printf,-) ((,-) (",-) (,,-) (-,-) (),-) (",-) (),-) (;,-)
(},-)
(return,-) (id,21) (;,-)
(},-)
(int,-) (id,33) ((,-) (int,-) (id,21) (),-) (//,-)
注释识别整数
({,-)
(char,-) (id,34) ([,-) (nu,0) (],-) (rlop,=) (",-) (",-) (;,-)
(int,-) (id,23) (rlop,=) (nu,3) (;,-)
(int,-) (id,25) (rlop,=) (nu,3) (;,-)
(int,-) (id,24) (rlop,=) (nu,3) (;,-)
(for,-) ((,-) (;,-) (;,-) (),-)
({,-)
(id,34) ([,-) (id,23) (],-) (rlop,=) (id,9) ([,-) (id,21) (],-) (;,-)
(id,21) (++,-) (;,-)
(id,23) (++,-) (;,-)
(if,-) ((,-) (id,19) ((,-) (id,9) ([,-) (id,21) (],-) (),-) (rlop,==) (nu,4) (),-)
({,-)
(id,34) ([,-) (id,23) (],-) (rlop,=) (id,9) ([,-) (id,21) (],-) (;,-)
(},-)
(else,-) (if,-) ((,-) ((,-) (id,25) (rlop,==) (nu,3) (),-) (&&,-) ((,-) (id,9) ([,-) (id,21) (],-) (rlop,==) (',-) (.,-) (',-) (),-) (),-)
({,-)
(id,34) ([,-) (id,23) (],-) (rlop,=) (id,9) ([,-) (id,21) (],-) (;,-)
(id,25) (++,-) (;,-)
(},-)
(else,-) (if,-) ((,-) (id,14) ((,-) (id,9) ([,-) (id,21) (],-) (),-) (rlop,==) (nu,4) (),-)
({,-)
(id,34) ([,-) (id,23) (],-) (rlop,=) (id,9) ([,-) (id,21) (],-) (;,-)
(id,24) (rlop,=) (nu,4) (;,-)
(continue,-) (;,-)
(},-)
(else,-) (break,-) (;,-)
(},-)
(if,-) ((,-) (id,24) (rlop,==) (nu,3) (),-)
({,-)
(int,-) (id,28) (rlop,=) (-,-) (nu,4) (;,-)
(for,-) ((,-) (int,-) (id,29) (rlop,=) (nu,3) (;,-) (id,29) (rlop,<) (id,13) (;,-) (id,29) (++,-) (),-)
(if,-) ((,-) (id,26) ((,-) (id,12) ([,-) (id,29) (],-) (.,-) (id,27) ((,-) (),-) (,,-) (id,34) (),-) (rlop,==) (nu,3) (),-)
(id,28) (rlop,=) (id,29) (;,-)
(if,-) ((,-) (id,28) (!=,-) (-,-) (nu,4) (),-) (printf,-) ((,-) (",-) ((,-) (id,12) (,,-) (,-) (id,30) (),-) (",-) (,,-) (id,28) (),-) (;,-)
(else,-)
({,-)
(id,12) ([,-) (id,13) (],-) (rlop,=) (id,34) (;,-)
(printf,-) ((,-) (",-) ((,-) (id,12) (,,-) (,-) (id,30) (),-) (",-) (,,-) (id,13) (),-) (;,-)
(id,13) (++,-) (;,-)
(},-)
(},-)
(else,-)
({,-)
(printf,-) ((,-) (",-) ((,-) (",-) (),-) (;,-)
(for,-) ((,-) (int,-) (id,31) (rlop,=) (nu,3) (;,-) (id,31) (rlop,<) (id,23) (;,-) (id,31) (++,-) (),-) (printf,-) ((,-) (",-) (,-) (id,32) (",-) (,,-) (id,34) ([,-) (id,31) (],-) (),-) (;,-)
(printf,-) ((,-) (",-) (,,-) (id,35) (id,36) (!,wrong thing!) (),-) (",-) (),-) (;,-)
(},-)
(return,-) (id,21) (;,-)
(},-)
(int,-) (id,37) ((,-) (int,-) (id,21) (),-) (//,-)
注释处理除号/和注释
({,-)
(char,-) (id,38) ([,-) (nu,6) (],-) (rlop,=) (",-) (",-) (;,-)
(int,-) (id,23) (rlop,=) (nu,3) (;,-)
(id,21) (++,-) (;,-)
(if,-) ((,-) (id,9) ([,-) (id,21) (],-) (rlop,==) (',-) (/,-) (',-) (),-)
({,-)
(printf,-) ((,-) (",-) ((,-) (//,-)
注释,-) ");
(id,21) (++,-) (;,-)
(while,-) ((,-) (id,9) ([,-) (id,21) (],-) (!=,-) (nu,7) (),-)
({,-)
(id,38) ([,-) (id,23) (],-) (rlop,=) (id,9) ([,-) (id,21) (],-) (;,-)
(id,21) (++,-) (;,-)
(id,23) (++,-) (;,-)
(},-)
(printf,-) ((,-) (",-) (\,worng thing) (id,23) (?worng thing) (?worngthing) (?worng thing) (?worng thing) (",-) (),-) (;,-)
(for,-) ((,-) (int,-) (id,31) (rlop,=) (nu,3) (;,-) (id,31) (rlop,<) (id,23) (;,-) (id,31) (++,-) (),-)
(printf,-) ((,-) (",-) (,-) (id,32) (",-) (,,-) (id,38) ([,-) (id,31) (],-) (),-) (;,-)
(},-)
(else,-) (if,-) ((,-) (id,9) ([,-) (id,21) (],-) (rlop,==) (',-) (*,-) (',-) (),-)
({,-)
(printf,-) ((,-) (",-) ((,-) (/*,-)
注释,-) ");
st++;
int stt = st + 1;
while (1)
{
if (rz[st] =='*'&&rz[st + 1] == '/') break;
tabuf[n] = rz[st];
st++;
n++;
if (rz[st + 1] == '\0')
{
printf("(/* error!!\n)");
return st + 1;
}
} printf(" \n 注释");
for (int i = 0; i<n; i++)
printf("%c",tabuf[i]);
printf(" ( (*/,-) (,,-) (-,-) (),-) (",-) (),-) (;,-)
(id,21) (rlop,=) (id,21) (+,-) (nu,8) (;,-)
(},-)
(else,-) (if,-) ((,-) (id,9) ([,-) (id,21) (],-) (rlop,==) (',-) (rlop,=) (',-) (),-)
({,-)
(id,21) (++,-) (;,-)
(printf,-) ((,-) (",-) ((,-) (/*,-) (/* error!!
)
程序中标示符如下
0 stdio
1 h
2 stdlib
3 string
4 iostream
5 using
6 namespace
7 std
8 keywords
9 rz
10 id
11 pp
12 nu
13 qq
14 choice1
15 a
16 z
17 A
18 Z
19 choice2
20 alpha
21 st
22 wordbuf
23 n
24 flag
25 k
26 strcmp
27 c_str
28 flagg
29 t
30 d
31 i
32 c
33 number
34 numbuf
35 error
36 digital
37 anotation
38 tabuf
程序中数字如下
0 20
1 99999
2 10000
3 0
4 1
5 9
6 9999
7 10
8 2