?? scanner.cpp
字號:
//Scanner.cpp
#include "stdafx.h"
#include "Scanner.h"
char Scanner::alphaprocess(char buffer) //關鍵字和標識符處理子函數
{
symbol atype;
int i=-1;
char alphatp[20];
while ( (isalpha(buffer)) || (isdigit(buffer)) ) //如果是連續的字母或數字就連續吃進
{
alphatp[++i]=buffer;
buffer=fgetc(fp);
};
alphatp[i+1]='\0';
atype=g->Search(alphatp,Key); //在關鍵字表中查找
if (atype.first != Default)
tokenlist.push_back(TOKEN(atype,linenum));
else //找不到就是標識符,查找并給出偏移
{
atype=g->Search(alphatp,Id);
tokenlist.push_back(TOKEN(atype,linenum));
};
return buffer;
}
char Scanner::digitprocess(char buffer) //數字處理函數
{
int i=0;
char digittp[20];
symbol dtype;
while ((isdigit(buffer))) //連續的數字就連續吃進
{
digittp[i]=buffer;
i++;
buffer=fgetc(fp);
}
digittp[i]='\0';
dtype=g->Search(digittp,Intc); //在表中找是否已經存在,并給出偏移.
tokenlist.push_back(TOKEN(dtype,linenum));
return buffer;
}
char Scanner::otherprocess(char buffer) //其他字符處理函數
{
int i=0;
char othertp[20];
symbol otype,ttype=make_pair(Default,0);
char cbuffer=buffer;
if ( buffer == '\n' || buffer == ' ' || buffer == '\t' ) //如果是回車,空格或制表位就跳過
{
buffer=fgetc(fp);
return buffer;
}
//如果不是字母或數字,也不是空格或回車,一定是特殊符號
while ( (!isdigit(buffer)) && (!isalpha(buffer)) && (buffer != ' ') && (buffer != '\n') )
{
othertp[i]=buffer;
i++;
othertp[i]='\0';
//先吃進一個看是不是運算符或特殊符號或關系符號,找到就再吃進一個查找(雙目)直至沒找到為止.
otype=g->Search(othertp,Arithmetic);
if (otype.first == Default) break;
else { ttype=otype; buffer=fgetc(fp); continue; }
otype=g->Search(othertp,Relation);
if (otype.first == Default) break;
else { ttype=otype; continue; }
otype=g->Search(othertp,Border);
if (otype.first == Default) break;
else { ttype=otype; buffer=fgetc(fp); continue; }
}
if (ttype.first != Default)
{
tokenlist.push_back(TOKEN(ttype,linenum));
return buffer;
}
else //如果以上條件都不符合,顯示出錯.
{
cout<<othertp<<'('<<linenum<<") error,not a word\n";
exit(1);
}
}
Scanner::Scanner(Grammar *grammar)
{
g=grammar;
linenum=1;
if ((fp=fopen(".\\demo.txt","r"))==NULL) //打開E:\DEMO.TXT(源程序文件)
{
cerr<<"error,can't open the file!\n"<<endl;
exit(1);
}
scan(); //掃描該文件.
}
void Scanner::scan()
{
cout<<"Scanning..."<<endl<<endl;
cbuffer = fgetc(fp);
while (cbuffer!=EOF)
{
if (cbuffer == '\n') linenum++;
if (isalpha(cbuffer)) //是字母就先看是不是關鍵字或標識符
cbuffer=alphaprocess(cbuffer);
else if (isdigit(cbuffer)) //是數字就先看是不是數字
cbuffer=digitprocess(cbuffer);
else cbuffer=otherprocess(cbuffer); //其他情況考慮特殊符號
};
printoken();
cout<<endl;
};
void Scanner::printoken() //打印整個TOKEN序列
{
cout<<"Token list:"<<endl;
list<TOKEN>::iterator itr=tokenlist.begin();
while(itr != tokenlist.end())
{
cout.width(8);
cout<<g->GetStr(itr->value);
cout.width(5);
cout<<itr->value.first<<setw(3)<<itr->value.second<<setw(3)<<itr->line<<endl;
itr++;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -