词法分析程序实验

一、词法分析程序的功能

输入一段字符串,从字符串表示的源程序中识别出具有独立意义的单词符号,根据扫描到单词符号的第一个字符的种类,拼出相应的单词符号。

二、符号与种别码对照表

单词符号

种别码

单词符号

种别码

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

三、用文法描述词法规则

<字母>:A→a|b|c|…|X|Y|Z
<数字>:B→0|1|2|…|9
<整数常数>:Z→C|ZC

                   C→0|1|2|...|9
<标识符>: I→X|IX|ID
X→a|b|c|…|X|Y|Z|_

D→0|1|2|...|9
<关键字>: K→const|var|procedure|begin|end|odd|if|then|call|while|do|read|write
<运算符>: Y→+|-|*|/|=|#|<|<=|>|>=|!=
<界符>: J→(|)|,|;|.

四、c语言源代码和运行结果截图

#include<stdio.h>
#include<string.h>
#include<math.h>
char prog[80]; 
char token[8]; 
char ch; 
int syn,p,m,n; 
double sum;     
int count;

int isSignal; 

int isDecimal;
double decimal; 
int isExp;  
int index;  
int isNegative; 
double temp;
int temp2;

void scanner();

char *rwtab[6]={"begin","if","then","while","do","end"};

void main()
{
    p=0;
    count=0;
    isDecimal=0;
    index=0;
    printf("请输入字符串:
");
    do{
        ch=getchar();
        prog[p++]=ch;
    }while(ch!='#'); 

    p=0;
    do{
        scanner(); 
        switch(syn)
        {
        case 11:
            if(isDecimal==0)
            {
                printf("(%2d,%8d)
",syn,(int)sum);
                break;
            }
            else if(isExp==1)
            {
                printf("(%2d,%10.5e)
",syn,sum);
                isExp=0;
                isDecimal=0;
                break;
            }
            else if(isDecimal==1)
            {
                printf("(%2d,%8.4f)
",syn,sum);
                isDecimal=0;
                break;
            }
        case -1:
            printf("input error
");
            break;
        default:
            printf("(%2d,%8s)
",syn,token);
        }
    }while(syn!=0);
}

void scanner()
{
    sum=0;
    decimal=0;
    m=0;

    for(n=0;n<8;n++)
        token[n]=NULL;
    ch=prog[p++]; 
    while(ch==' ')  
        ch=prog[p++];

    if(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z')))
    {
        while(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z'))||((ch>='0')&&(ch<='9')))
        {
            token[m++]=ch;
            ch=prog[p++]; 
        }
        token[m++]='