我已经用 C++ 为扫描仪编写了一些代码,但我一直收到分段错误。奇怪的是,分段错误发生在代码完成时。我认为这与我的扫描功能和其中的使用有关file.get()
,但这不会导致该代码行出现分段错误吗?我有一个简单的 main.cpp,它在执行 cout 语句时调用函数,并且在 return 语句之后发生分段错误。当我运行 GDB 和回溯时,我收到:
程序收到信号 SIGSEGV,分段错误。
0x00011260 in __do_global_dtors_aux ()
(gdb)
(gdb) backtrace
#0 0x00011260 in __do_global_dtors_aux ()
#1 0x00012504 in _fini ()
#2 0xfefc3120 in _exithandle () from /lib/libc.so.1 (
3 0xfefb10e0 in) /lib/libc.so.1
#4 0x00011218 在_start()
这是简单的 main.cpp:
int main(int argc, char *argv[]) {
tokenType * token, * testtk;
string filename;
if(argc == 2){
filename = argv[1];
filename.append(".lan");
}
scan(filename);
cout << endl;
cout << "WHAT?!" << endl;
return 0;
}
这里只是扫描仪的功能部分:
void scan(string filename){
char current;
char look;
int currentline = 1;
Type test;
tokenType * testtk;
std::ifstream file(filename.c_str()); // open file
/***scanner creation***/
while (file.good()) // loop while extraction from file file possible
{
int state = 0;
int lookstate = 0;
int type = 0;
int i = 0;
int nextstate;
look = file.peek();
/*** check for comments ***/
if(assignType(look)==5){
current = file.get(); //"current = look" at this point
current = file.get(); //current moves to next character
while(current != 24) {
current = file.get();
}
}
initializeToken(testtk);
while(state >= 0) {
current = file.get(); // get character from file
look = file.peek();
lookstate = state;
if(assignType(current)!=24){ //keeps newlines from printing
testtk->str[i] = current; //put current scanned char into string
}
testtk->tokenId = (tokenIdType) state;
nextstate = table[state][assignType(current)];
state = nextstate;
if(assignType(current)==24) { //keeps track of '\n'
currentline++;
}
if(i<STRSIZ) i++;
}
testtk->line=currentline;
printToken(testtk);
}
file.close();
}
任何有关导致此分段错误的原因的帮助将不胜感激,
已编辑
这是我的大部分 token.h:
typedef enum
{ EOFtk, IDtk, NOTtk, DOTtk, NUMtk, COMMENTtk, ASSIGNtk, EQUALtk, LESSEQtk,
GREATEQtk, PLUStk, MINUStk, MULTtk, DIVtk, MODtk, PARENLtk,
PARENRtk, COMMAtk, CURLYLtk, CURLYRtk, SEMItk, BRACKLtk, BRACKRtk, COLONtk,
LESStk, GREATtk, STARTtk, STOPtk, THENtk, IFtk, IFFtk, WHILEtk, VARtk, INTtk,
FLOATtk, DOtk, READtk, WRITEtk, VOIDtk, RETURNtk, DUMMYtk, PROGRAMtk
} tokenIdType;
typedef enum
{ WS, LETTER, NUMBER, AMBER, NOT, PLUS, MINUS, MULT, DIV,
MOD, EQUAL, LESS, GREATER, UNDERSC, DOT, PARENL,
PARENR, COMMA, CURLYL, CURLYR, SEMI, BRACKL, BRACKR, COLON,
NEWLINE, NONALPHA, EOF
} Type;
typedef struct
{ char str[STRSIZ];
tokenIdType tokenId;
int line;
} tokenType;
这是我的scanner.cpp中的一些其他功能:
Type assignType(unsigned char current) {
Type temp;
if((current <= 122 && current >=97) || (current <= 90 && current >=65)){
temp = LETTER;
return temp;
}
if(current <= 57 && current >=48) {
temp = NUMBER;
return temp;
}
if(current == 10) {
temp = NEWLINE;
return temp;
}
switch (current) {
case ' ':
temp = WS;
break;
case '&':
temp = AMBER;
break;
case '!':
temp = NOT;
break;
case '+':
temp = PLUS;
break;
case '-':
temp = MINUS;
break;
case '*':
temp = MULT;
break;
case '/':
temp = DIV;
break;
case '%':
temp = MOD;
break;
case '=':
temp = EQUAL;
break;
case '<':
temp = LESS;
break;
case '>':
temp = GREATER;
break;
case '_':
temp = UNDERSC;
break;
case '.':
temp = DOT;
break;
case '(':
temp = PARENL;
break;
case ')':
temp = PARENR;
break;
case ',':
temp = COMMA;
break;
case '{':
temp = CURLYL;
break;
case '}':
temp = CURLYR;
break;
case ';':
temp = SEMI;
break;
case '[':
temp = BRACKL;
break;
case ']':
temp = BRACKR;
break;
case ':':
temp = COLON;
break;
default :
temp = NONALPHA;
break;
}
return temp;
}
void initializeToken(tokenType *token){
for(int i=0;i<STRSIZ;i++){ //initialize tokenType str
token->str[i]=0;
}
}
void printToken(tokenType *token){
if(token->tokenId != COMMENTtk && token->tokenId != EOFtk) { //ignore comments
cout<<"Line:";
cout.width(3);
cout<<token->line;
cout.flush();
cout<<" TokenID:";
cout.width(3);
cout<<token->tokenId;
cout.flush();
cout<<" String: ";
printf("%s\n", token->str);
}
}
bool isToken(int state, int look){
if(table[state][assignType(look)] >=0)
return false;
else return true;
}
bool compareStr(char x[], char y[]){
int score;
for(int i=0;i<STRSIZ;i++) {
if(x[i] != y[i])
score++;
}
if(score == 0)
return true;
else
return false;
}