单词符号 | 种别码 | 单词符号 | 种别码 |
begin | 1 | : | 17 |
if | 2 | := | 18 |
then | 3 | < | 20 |
while | 4 | <= | 21 |
do | 5 | <> | 22 |
end | 6 | > | 23 |
l(l|d)* | 10 | >= | 24 |
dd* | 11 | = | 25 |
+ | 13 | ; | 26 |
- | 14 | ( | 27 |
* | 15 | ) | 28 |
/ | 16 | # | 0 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
用文法描述词法规则:<BR><字母>->a/b/c.../z<BR><数字>->1/2/3/4/5/6/7/8/9<BR><标识符>->sum.....<BR><关键字>->begin/
if
/then/
while
/
do
/end<BR><运算符>->+,-,*,/,:,:=,<,<=,<>,>,>=,=<BR><界符>->;|(|)|[|]|{|}<BR>已完成的代码如下:<BR>#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char
word[6][5]={
"begin"
,
"if"
,
"then"
,
"while"
,
"do"
,
"end"
};
char
word2[18][3]={
"|"
,
"dd*"
,
"+"
,
"-"
,
"*"
,
"/"
,
":"
,
":="
,
"<"
,
"<="
,
"<>"
,
">"
,
">="
,
"="
,
";"
,
"("
,
")"
,
"#"
};
int
words(
char
ch[][5]);
int
signs(
char
ch[][3]);
main()
{
char
ch[10];
printf(
"请输入保留字:"
);
scanf(
"%s"
,ch);
words(ch);
signs(ch);
}
int
words(
char
ch[][5])
{
while
(ch==
''
||ch==10||ch==13||ch==9)
{
getchar;
}
if
(ch>=
'a'
&&ch<=
'z'
)
{
switch
(ch)<BR> {}<BR>
}
}
int
signs(
char
ch[][3])
{
while
(ch==
''
||ch==10||ch==13||ch==9)
{
getchar;
}
if
(ch>=
'0'
&&ch<=
'9'
)
{
switch
(ch)<BR> {}
}
}
|