一、设计内容:
处理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]