C语言词法分析器

一、设计内容:

处理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]

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值