YLLEN

要去看埃菲尔铁塔的顶

欢迎关注本人微博:t.cn/RGSLVUk

<编译原理 ,词法分析程序>

/*

代码如下 , 排版不好,建议复制到

https://web.chacuo.net/formatcpp

格式化一下查看

参考

https://blog.sina.com.cn/s/blog_68b606350100v7hf.html

实现,在此感谢原作者。

*/

#include <iostream>

#include <iostream>

#include<math.h>

#include<string>

#include<stdlib.h>

#include<stdio.h>

#include<fstream>


using namespace std;


/*

0 ~ 33 是 key区

33  是变量 

34  int型 

35  实型 

36  char


*/


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]={

                  "=","+","-","*","/","++","--","+=","-=",

                  "*=","/=","==","!=",">","<",">=","<=","(",

                  ")","[","]","{","}",",",":",";" };


string text;//代码 

string token; // 工作区字符串 

int pText;//游标 

char ch;//字符 

int line;


void getChar()//读取一个字符 

{

ch =  text[pText++];

}

void passSpace()//忽略读取到的空格 

{

while(text[pText] == ' ') 

getChar();

}

int findkey( )//比对key

{

for(int i =0; i< 33 ; i ++)

{

if ( key[i] == token )

{

return i;// 

}

}

return -1;

}

int findsymbol()//比对 符号

{

for(int i =0; i< 25 ; i ++)

{

if ( symbol[i] == token )

{

return i  + 37 ; 

}

}

return -1;

}

void Concat()// 将ch 连接到 工作区中 

{

token += ch;

}

void Retract()//回退 

{

ch = ' ';

pText -- ;

}

bool IsDigital()//判断是否是数字 

{

return   '0'<=ch && ch <='9' ? true : false; 

}

bool IsLetter()// 是字母? 

{

return ('a'<=ch && ch <= 'z' ) || ('A' <= ch && ch <= 'Z') ? true : false;

}


// 分析程序 

void Analysis()

{

//标示符

// + - * / =

//其他符号

int code ;

token = "";//工作字符串

passSpace();

getChar();

if( IsLetter() )

{

while( IsLetter() || IsDigital() || ch == '_')

{

Concat();

getChar();

}

Retract();  //清空当前状态

code = findkey();

if(code != -1)

cout<< "(" <<code<<", "<<token<<", "<<line<< ")"<<endl;

else

cout<< "(" <<33<<", "<<token<<", "<<line<< ")"<<endl;

}

else if( IsDigital()) 

{

while(  IsDigital() )//整数识别 

{

Concat();

getChar();

}

if(ch != '.')

{

Retract();  //清空当前状态

cout<< "(" <<34<<", "<<token<<", "<<line<< ")"<<endl;

}

else if( ch == '.')

{

do

{

Concat();

getChar();

}while(IsDigital() );

Retract();

cout<< "(" <<35<<", "<<token<<", "<<line<< ")"<<endl;

}

}

else if(ch==39) //识别字符常量  

    {

        Concat();

       getChar();

        while(ch!=39)

        {

            Concat();

           getChar();

        }

         //  Retract(); 这里游标不能后退,如果后退的话 pText指向的依然还是 '  会出现错误 

       cout<< "(" <<36<<", "<<token<<", "<<line<< ")"<<endl;

    }

else if(ch == '=')   // =  , == 

{

int code ;

Concat();

getChar();

if( ch == '=')

{

Concat();

}

Retract();

code = findsymbol();

cout<< "(" <<code<<", "<<token<<", "<<line<< ")"<<endl;

}

else if(ch == '+')      // + +=  ++

{

int code ;

Concat();

getChar();

if( ch == '+' ||  ch == '=')

{

Concat();

}

Retract();

code = findsymbol();

cout<< "(" <<code<<", "<<token<<", "<<line<< ")"<<endl;

}

else if(ch == '-')    // - -= --

{

int code ;

Concat();

getChar();

if( ch == '-' ||  ch == '=')

{

Concat();

}

Retract();

code = findsymbol();

cout<< "(" <<code<<", "<<token<<", "<<line<< ")"<<endl;

}

else if(ch == '*')  // *  *= */

{

int code ;

Concat();

getChar();

if( ch == '=' ||  ch == '/')

{

Concat();

}

Retract();

code = findsymbol();

cout<< "(" <<code<<", "<<token<<", "<<line<< ")"<<endl;

}

else if(ch == '/')  // / ,/=    // /*

{

int code ;

Concat();

getChar();

if( ch == '=')

{

Concat();

}

Retract();

code = findsymbol();

cout<< "(" <<code<<", "<<token<<", "<<line<< ")"<<endl;

}

else if (ch == '\n') line ++ ;

else

{

Concat();

int code = findsymbol();

if( code!= -1)

cout<< "(" <<code<<", "<<token<<", "<<line<< ")"<<endl;

}

}


void init(string file)

{

fstream data;

text.clear();

char ch;

data.open(file.c_str());

while(!data.eof())

{

data.get(ch);

text += ch;

}

data.close();

}


int main(int argc, char** argv) 

{


init("d:/data.txt");

line = 0;

cout<<text<<endl;

while(pText < text.length())

{

Analysis();

}

return 0;

}

评论 ( 3 )
热度 ( 1 )

© YLLEN | Powered by LOFTER