自己写一个简单的C++单词扫描程序。[2]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 zjt621 的 blog

   bool comment = false;      //for this kind of comment--"/**/"
   char prech = '@';          //pre char for /**/ comment

   ansistring pretoken = "";   //pre token for judging pointee and multi '*'

   while(scanfile.getline(buffer,bflength))   //get each line of the .cpp file
   {
     int lnscptr = 0;
     while(buffer[lnscptr]==' ')  //trim left space
       lnscptr++;
     ch = buffer[lnscptr];
 /*scan:important arithmetic*/
    if(comment)
    {
     prech = ch;
     goto flag1;
    }
    else
    {
     while(ch!='\0')   //while not the line finish symbol do analyse
     {
        if(isalpha(ch) || ch=='_')  // id or keyword
        {
          while(isalpha(ch) || isdigit(ch) || ch=='_')
          {
           strtoken = strtoken + ch;
           ch = buffer[++lnscptr];
          }
          if(isreserveword(strtoken))     //is reserveword
          {
           print(linecount,strtoken,strgrdline,"保留字");
           maketl(linecount,strtoken,"保留字");
          }
          else                    //is id
          {
           print(linecount,strtoken,strgrdline,"标识符");
           maketl(linecount,strtoken,"标识符");
          }
          pretoken = strtoken;
          strgrdline++;
          strtoken.delete(1,strtoken.length());
        }
        else if(isdigit(ch))     // numerci
        {
          while(isdigit(ch) || ch=='.')
          {
           strtoken = strtoken + ch;
           ch = buffer[++lnscptr];
          }

本文关键:自己写一个简单的C++单词扫描程序。
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top