/******************************************/
/* file:unit2.h (scan.h) */
/* a scanner for lexical analysis for c++ */
/* author:zhanjiantao(compower) */
/******************************************/
#include <vcl.h>
#define maxno 48
typedef struct typetoken
{
int line;
ansistring words;
ansistring type;
}atoken;
typedef atoken *listnd;
class scan
{
public:
bool isreserveword(ansistring token);
void initrw();
void doscan(char *infile);
void print(int lineno, ansistring stoken, int strgrdl, ansistring type);
void maketl(int line,ansistring words,ansistring type);
void compress(char *zipfname);
public:
char *file;
char ch;
ansistring strtoken;
ansistring reservews[maxno];
tlist *tokenlist;
listnd anode;
};
#endif
/******************************************/
/* file:unit2.cpp (scan.cpp) */
/* a scanner for lexical analysis for c++ */
/* author:zhanjiantao(compower) */
/******************************************/
#include "unit2.h"
#include <string.h>
#include <fstream.h>
//initiate reserved words list
void scan::initrw()
{
const int buflen = 10;
char buf[buflen];
ansistring gotword = "";
ifstream inirw("initrw.ini");
int i = 0;
while(inirw.getline(buf,buflen))
{
gotword = buf;
reservews[i++] = gotword;
}
inirw.close();
tokenlist = new tlist;
}
//judge the ch in the rw list or not
bool scan::isreserveword(ansistring token)
{
bool result = false;
int low = 0;
int high = maxno-1;
while(low<=high)
{
int mid = (low + high)/2;
int rscomp = token.ansicompare(reservews[mid]);
if(rscomp==0)
{
result = true;
break;
}
if(rscomp<0)
{
high = mid-1;
}
else
{
low = mid+1;
}
}
return result;
}
//print on stringgrid
void scan::print(int lineno, ansistring stoken, int strgrdl, ansistring type)
{
form1->stringgrid1->rowcount++;
form1->stringgrid1->cells[0][strgrdl] = lineno;
form1->stringgrid1->cells[1][strgrdl] = stoken;
form1->stringgrid1->cells[2][strgrdl] = type;
}
//make a token list
void scan::maketl(int line,ansistring words,ansistring type)
{
anode = new atoken;
anode->line = line;
anode->words = words;
anode->type = type;
tokenlist->add(anode);
}
//scan--the hardcore of the scanner
void scan::doscan(char *infile)
{
file = infile ;
ifstream scanfile(file);
int linecount = 1; //the word in which line
strtoken = ""; //member of class scan
int strgrdline = 1; //temp var for show result on stringgrid
const int bflength = 254; //length of getline buffer
char buffer[bflength]; //getline buffer