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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章