C语言词法分析器

保留字表:

关键字

种别码

 

关键字

种别码

main

0

 

return

32

auto

1

 

变量

33

short

2

 

int常量

34

int

3

 

实型常量

35

long

4

 

char常量

36

float

5

 

=

37

double

6

 

+

38

char

7

 

-

39

struct

8

 

*

40

union

9

 

/

41

enum

10

 

++

42

typedef

11

 

--

43

const

12

 

+=

44

usigned

13

 

-=

45

signed

14

 

*=

46

extern

15

 

/=

47

register

16

 

==

48

static

17

 

!=

49

volatile

18

 

50

void

19

 

51

if

20

 

>=

52

else

21

 

<=

53

switch

22

 

(

54

case

23

 

)

55

for

24

 

[

56

do

25

 

]

57

while

26

 

{

58

goto

27

 

}

59

continue

28

 

,

60

break

29

 

:

61

default

30

 

;

62

sizeof

31

 

 

 


源代码:
#include <iostream>
#include<math.h>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<fstream>
using namespace std;

string  key[34]={"main",
                "auto","short","int","long","float","double","char","struct"
                ,"union","enum","typedef","const","unsigned","signed","extern","register"
                ,"static","volatile","void","if","else","switch","case","for"
                ,"do","while","goto","continue","break","default","sizeof","return"};

string  symbol[26]={
                  "=","+","-","*","/","++","--","+=","-=",
                  "*=","/=","==","!=",">","<",">=","<=","(",
                  ")","[","]","{","}",",",":",";" };

char ch;           ///存放最新读进的源程序字符
string strToken;       ///存放构成单词的字符串
string text="int main()\n{\n  int a,b;\n  a=10.3211;\n  a+=20; if (b==15) break; \n   float fa=3.141592653; \n  char s='z';\n}"; ///要读进的文本
//string text="main(){  int a,b; a=10;  b=a+20;}"; ///要读进的文本
int pText=0;        ///搜索指示器
int line=1;
int FindInSymbol(string str)
{
    for(int i=0;i<26;i++)
    {
        if(str==symbol[i])  return i+37;
    }
    return -1;
}
void GetChar()    ///---------1
{
    ch=text[pText];
    pText++;
}
void GetBC()     ///-------2
{
    while(1)
    {
        if(ch==' ')  GetChar();
        else break;
    }
}
void Concat()   ///----------3
{
    strToken+=ch;
}
bool IsLetter()   ///---------4
{
    if( ( ch>='a' &&ch<='z') ||( ch>='A' &&ch<='Z') )
        return true;
    return false;
}
bool IsDigit()   ///---------5
{
    if( ch>='0' &&ch<='9')
        return true;
    return false;
}
int Reserve()    ///---------6
{
    for(int i=0;i<=33;i++)
    {
        if(strToken==key[i])  return i;
    }
    return -1;
}
void Retract()   ///----------7
{
    ch=' ';
    pText--;
}

void Analyse()
{
    int code;
    strToken="";
    GetChar();
    GetBC();
    if(IsLetter())  ///识别标示符和关键字
    {
        while(IsLetter() || IsDigit() ||ch=='_')
        {
            Concat();
            GetChar();
        }
        Retract();
        code=Reserve();
        if(code!=-1)   ///识别出来的是常量
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
        else            ///识别出来的是标示符
            cout<<"("<<33<<","<<strToken<<",line="<<line<<")"<<endl;
    }
    else if(IsDigit())   ///识别整形常量、浮点型常量
    {
        while(IsDigit())
        {
            Concat();
            GetChar();
        }
        if(ch!='.')    ///识别整形常量
        {
            Retract();
            int temp=atoi(strToken.c_str());
            ///---------------------二进制显示----------------
            int a[128],i;
            for(i=0; ;i++)
            {
                if(temp<=0)  break;
                a[i]=temp%2;
                temp/=2;
            }
            cout<<"("<<34<<",";
            for(int j=i-1;j>=0;j--)
            {
                cout<<a[j];
            }
            cout<<",line"<<line<<")"<<endl;
        }
        else if(ch=='.')  ///识别实型常量
        {
            Concat();
            GetChar();
            while(IsDigit())
            {
                Concat();
                GetChar();
            }
            Retract();
            cout<<"("<<35<<","<<strToken<<",line="<<line<<")"<<endl;
        }
    }
    else if(ch==39) ///识别字符常量
    {
        Concat();
        GetChar();
        while(ch!=39)
        {
            Concat();
            GetChar();
        }
        cout<<"("<<36<<","<<strToken<<"',line="<<line<<")"<<endl;
    }
    else if(ch=='=')  ///识别=、==
    {
        Concat();
        GetChar();
        int code;
        if(ch=='=')
        {
            Concat();
            code=FindInSymbol(strToken);
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
        }
        else
        {
            code=FindInSymbol(strToken);
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
            Retract();
        }
    }
    else if(ch=='+') ///识别+、+=、++
    {
        Concat();
        GetChar();
        int code;
        if(ch=='=')
        {
            Concat();
            code=FindInSymbol(strToken);
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
        }
        else if(ch=='+')
        {
            Concat();
            code=FindInSymbol(strToken);
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
        }
        else
        {
            code=FindInSymbol(strToken);
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
            Retract();
        }
    }
    else if(ch=='-')  ///识别-、-=、--
    {
        Concat();
        GetChar();
        int code;
        if(ch=='=')
        {
            Concat();
            code=FindInSymbol(strToken);
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
        }
        else if(ch=='-')
        {
            Concat();
            code=FindInSymbol(strToken);
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
        }
        else
        {
            code=FindInSymbol(strToken);
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
            Retract();
        }
    }
    else if(ch=='*')  ///识别*、*=
    {
        Concat();
        GetChar();
        int code;
        if(ch=='=')
        {
            Concat();
            code=FindInSymbol(strToken);
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
        }
        else
        {
            code=FindInSymbol(strToken);
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
            Retract();
        }
    }
    else if(ch=='/')   ///识别/、/=
    {
        Concat();
        GetChar();
        int code;
        if(ch=='=')
        {
            Concat();
            code=FindInSymbol(strToken);
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
        }
        else
        {
            code=FindInSymbol(strToken);
            cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
            Retract();
        }
    }
    else if(ch==';')  cout<<"("<<34<<",;"<<",line="<<line<<")"<<endl;
    else if(ch=='(')  cout<<"("<<26<<",("<<",line="<<line<<")"<<endl;
    else if(ch==')')  cout<<"("<<27<<",)"<<",line="<<line<<")"<<endl;
    else if(ch=='{')  cout<<"("<<30<<",{"<<",line="<<line<<")"<<endl;
    else if(ch=='}')  cout<<"("<<31<<",}"<<",line="<<line<<")"<<endl;
    else if(ch==',')  cout<<"("<<32<<",,"<<",line="<<line<<")"<<endl;
    else if(ch=='\n') line++;
}

void choice()
{
    cout<<"\t\t---------从文件中读------>1----"<<endl;
    cout<<"\t\t---------默 认 输入------>2----"<<endl;
    cout<<"\t\t---------退      出------>0----"<<endl;
    int choice;
    while(1)
    {
    cin>>choice;
    if(choice==1)
    {
        fstream data;
        data.open("D:\data.txt");
        text.clear();
        char temp;
        while(1)
        {
          if(data.eof()) break;
          data.get(temp);
          text+=temp;
        }
        cout<<"要分析的C程序内容:"<<endl;
        cout<<text<<endl;
        break;
    }
    else if(choice==2)
    {
        cout<<"要分析的C程序内容:"<<endl;
        cout<<text<<endl;
        break;
    }
    else if(choice==0)
        exit(0);
    }
}
int main()
{
    choice();
    while(pText<text.length())
    {
       Analyse();
    }
    cout<<strToken;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值