?? lexical1.cpp
字號:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
char *reserve[21]={"and","begin","const","div","do","else","end","function","if","integer",
"not","or","procedure","program","read","real","then","type","var","while","write"}; //保留字表
char* symbol[20]; //簡易符號表
int num[20]; //常量表
char line[100]; //放入文件的一行
static int ii=0,jj=0;//當前文件讀頭位置,行未標志,行號
int scanner(ifstream&);
void getnbc(ifstream&, char&);
void getchar(ifstream&, char&);
int search_reserve(char*);
int search_symbol(char*);
int search_num(char*);
void retract(char&);
void main( )
{
int end;
ifstream sf("c:\\file.txt");
if(sf.fail())
{
cerr<<"error opening file file.txt\n";
return;
}
end=scanner(sf);
cout<<end<<endl;
while(end!=26)
{
end=scanner(sf);
cout<<end<<endl;
}
sf.close();
}
int scanner(ifstream& sf)
{
char token[11];
int token_num=0;
int c;
char current;
getnbc(sf,current);
switch(current)
{
case'a':
case'b':
case'c':
case'd':
case'e':
case'f':
case'g':
case'h':
case'i':
case'j':
case'k':
case'l':
case'm':
case'n':
case'o':
case'p':
case'q':
case'r':
case's':
case't':
case'u':
case'v':
case'w':
case'x':
case'y':
case'z': token[token_num]=current;
token_num++;
getchar(sf,current);
while((current<='z'&¤t>='a')||(current<='9'&¤t>='0'))
{
if(token_num<10)
{
token[token_num]=current;
token_num++;
}
getchar(sf,current);
}
token[token_num]='\0';
retract(current);
c=search_reserve(token);
if(c==-1)
{
cout<<token<<" |";
return 21;
}
else
{
cout<<token<<" |";
return c;
}
break;
case'0':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9': token[token_num]=current;
token_num++;
getchar(sf,current);
while(current<='9'&¤t>='0')
{
if(token_num<10)
{
token[token_num]=current;
token_num++;
}
getchar(sf,current);
}
token[token_num]='\0';
retract(current);
cout<<token<<" |";
return 22;
break;
case',': cout<<','<<" |";
return 23;
case';': cout<<';'<<" |";
return 24;
case':': getchar(sf,current);
if(current=='=')
{
cout<<':'<<'='<<" |";
return 44;
}else
{
retract(current);
cout<<':'<<" |";
return 25;
}
case'.': getchar(sf,current);
if(current=='.')
{
cout<<'.'<<'.'<<" |";
return 31;
}else
{
retract(current);
cout<<'.'<<" |";
return 26;
}
case'(': cout<<'('<<" |";
return 27;
case')': cout<<')'<<" |";
return 28;
case'[': cout<<'['<<" |";
return 29;
case']': cout<<']'<<" |";
return 30;
//缺單目加
//缺單目減
case'+': cout<<'+'<<" |";
return 34;
case'-': cout<<'-'<<" |";
return 35;
case'*': cout<<'*'<<" |";
return 36;
case'/': cout<<'/'<<" |";
return 37;
case'=': cout<<'='<<" |";
return 38;
case'<': getchar(sf,current);
if(current=='>')
{
cout<<'<'<<'>'<<" |";
return 41;
}
if(current=='=')
{
cout<<'<'<<'='<<" |";
return 42;
}
else
{
retract(current);
cout<<'<'<<" |";
return 39;
}
case'>': getchar(sf,current);
if(current=='=')
{
cout<<'>'<<'='<<" |";
return 43;
}else
{
retract(current);
cout<<'>'<<" |";
return 40;
}
case'{': cout<<'{'<<" |";
return 45;
case'}': cout<<'}'<<" |";
return 46;
case'#': cout<<'#'<<" |";
return 47;
default: cout<<"error"<<" |";
return -1;
}
}
void getnbc(ifstream& sf, char& current)
{
GET: getchar(sf,current);
if(current==' '||current=='\0'||current=='\t') //濾掉空格,回車,tab鍵
{
do{
getchar(sf,current);
}while(current==' '||current=='\0'||current=='\t');
}
if(current=='{')
{
do{
getchar(sf,current);
}while(current!='}');
goto GET;
}
}
void getchar(ifstream& sf, char& current)
{
static int lineno=1;
if(ii==jj)
{
ii=jj=0;
sf.getline(line,100);
cout<<endl<<"line"<<lineno<<": "<<line<<endl;
for( ;line[jj]!='\0';jj++);
lineno=lineno+1;
current=line[ii];
return;
}
current=line[++ii];
if(current>='A'&¤t<='Z')
current+=32;
}
int search_reserve(char* token)
{
for(int i=0;i<=20;i++)
{
if(strcmp(token,reserve[i])==0)
return i;
}
return search_symbol(token);
}
int search_symbol(char* token)
{
for(int j=0;j<=19;j++)
{
if(symbol[j]==NULL)
{
symbol[j]=new char[11];
strcpy(symbol[j],token);
return -1;
}
if(strcmp(symbol[j],token)==0)
return -1;
}
cout<<"symbol table is full!"<<endl;
return -1;
}
void retract(char& current)
{
ii--;
current=line[ii];
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -