首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

词法分析器(最后一行有有关问题)

2012-02-28 
词法分析器(最后一行有问题)#includeiostream#includecstdio#includefstream#includestring#inclu

词法分析器(最后一行有问题)
#include   <iostream>
#include   <cstdio>
#include   <fstream>
#include   <string>
#include   <vector>
#include   <cstdlib>
#include   <iomanip>
#include   <bitset>
using   namespace   std;
string   strToken;
vector <string>   *words=new   vector <string> ;
static   const   string   table[5]={ "DIM ", "IF ", "DO ", "STOP ", "END "};
vector <string>   tableID;
vector <string>   tableConst;
char   ch;
int   code,value;
inline   bool   IsLetter()
{
        if(ch> = 'A '&&ch <= 'Z '||ch> = 'a '&&ch <= 'z ')
                return   true;
        else
                return   false;
}
inline   bool   IsDigit()
{
        if(ch> = '0 '&&ch <= '9 ')
                return   true;
        else
                return   false;
}

inline   void   Concat()
{
        strToken.insert(strToken.end(),ch);
}

inline   int   Reserve()
{
        int   count=1,i;
        for(i=0;i <5;i++,count++)
                if(strToken==table[i])  
                        return   count;
        return   0;
}
inline   int   InsertId()
{static   int   count=0;
        tableID.insert(tableID.end(),strToken);
count++;
return   count;  
}
inline   int   InsertConst()
{      
        static   int   count=0;
tableConst.insert(tableConst.end(),strToken);
count++;
return   count;
}

int   main(void)
{
       
        FILE   *fp;
fp=fopen( "text1.txt ", "r ");
        if(fp==NULL){
                cerr < < "oops!unable   to   open   file--bailing   out!\n ";
                        exit(-1);
        }
strToken= " ";
while((ch=fgetc(fp))!=EOF){
strToken= " ";

                while(ch== '\t '||ch== '   '||ch== '\n ')
                        ch=fgetc(fp);
if(IsLetter()){
                do{
Concat();
ch=fgetc(fp);
}while(IsLetter()||IsDigit());
fseek(fp,-1,SEEK_CUR);

code=Reserve();
if(code==0){
value=InsertId();
cout < <strToken < < ": <$ID, " < <strToken < < "> " < <endl;


continue   ;
}else{
cout < <strToken < < ": <$ " < <strToken < < ",-> " < <endl;
continue   ;
}

}
       
if(IsDigit()){
do{
Concat();
        ch=fgetc(fp);
}while(IsDigit());
fseek(fp,-1,SEEK_CUR);
value=InsertConst();
int   temp1=atoi(strToken.c_str());
bitset <32>       myset(temp1);
cout < <strToken < < ": <$INT, " < <myset < < "> " < <endl;
continue   ;
}
       
if(ch== '= ')   {
cout < <ch < < ": <$ASSIGN,-> " < <endl;
continue   ;
                }
       
if(ch== '+ ')   {
cout < <ch < < ": <$PLUS,-> " < <endl;
continue   ;
                }
       
if(ch== '* '){
ch=fgetc(fp);
if(ch== '* '){
cout < <ch < <ch < < ": <$POWER,-> " < <endl;
continue   ;
}else{
cout < < '* ' < < ": <$STAR,-> " < <endl;
fseek(fp,-1,SEEK_CUR);
continue   ;
}
}
       
if(ch== '; ')   {
cout < <ch < < ": <$SEMICOLON,-> " < <endl;
continue   ;
                }
       
if(ch== '( ')   {
cout < <ch < < ": <$LPAR,-> " < <endl;
                        continue   ;
                }
       
        if(ch== ') ')   {
cout < <ch < < ": <$RPAR,-> " < <endl;
                        continue   ;
                }
       
if(ch== '{ ')   {
                        cout < <ch < < ": <$LBRACE,-> " < <endl;
                        continue   ;
                }
       
if(ch== '} '){
cout < <ch < < ": <$RBRACE,-> " < <endl;
continue   ;
                }
else  
                        cout < <ch < < ":can 't   be   complied " < <endl;

        }
fclose(fp);
system( "pause ");
return   0;
}
text1.txt:
DIM     IF     DO     STOP     END   ,   >   ~   !
biaozhifu
8
=
+
*
**
;
(
)


                                                 


                             
                 


[解决办法]
你去看看http://www-128.ibm.com/developerworks/cn/linux/sdk/lex/index.html
里面讲解了Lex & Yacc,是词法分析和语法分析的bision的入门知识。

热点排行