读书人

!C程序运行时fprintf的异常。

发布时间: 2012-12-26 14:39:29 作者: rapoo

求助!!!C程序运行时fprintf的错误。。。
程序运行时出现下面的错误:
File:fprintf.c
Expression: str!=NULL
向各位大牛请教一下,不胜感激!!!

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include"scan.h"

/* lexeme of identifier or reserved word */
char tokenString[MAXTOKENLEN+1];

/* BUFLEN = length of the input buffer for
source code lines */
#define BUFLEN 256

static char lineBuf[BUFLEN]; /* holds the current line */
static int linepos = 0; /* current position in LineBuf */
static int bufsize = 0; /* current size of buffer string */
static bool EOF_flag = false; /* corrects ungetNextChar behavior on EOF */

FILE* source; /* source code text file */
FILE* listing; /* listing output text file */
FILE* code; /* code text file for TM simulator */

int lineno = 1; /* source line number for listing */

/* getNextChar fetches the next non-blank character
from lineBuf, reading in a new line if lineBuf is
exhausted */
static int getNextChar(void)
{
if (!(linepos < bufsize))
{ lineno++;
if (fgets(lineBuf,BUFLEN-1,source))
{ if (true) fprintf(listing,"%4d: %s",lineno,lineBuf);
bufsize = strlen(lineBuf);
linepos = 0;
return lineBuf[linepos++];
}
else
{ EOF_flag = true;
return EOF;
}
}
else
return lineBuf[linepos++];
}

/* ungetNextChar backtracks one character
in lineBuf */
static void ungetNextChar(void)
{
if (!EOF_flag)
linepos-- ;
}

/* lookup an identifier to see if it is a reserved word */
/* uses linear search */
static TokenType reservedLookup (char * s)
{
if (!strcmp(s,reservedWords.str))
return reservedWords.tok;
return ID;
}

void printToken(TokenType currentToken,char tokenString[])
{
fprintf(code,"<%d,%s>\n",currentToken,tokenString);
}

/* function getToken returns the
* next token in source file
*/
TokenType getToken(void)
{ /* index for storing into tokenString */
int tokenStringIndex = 0;
/* holds current token to be returned */
TokenType currentToken;
/* current state - always begins at START */
StateType state = START;
/* flag to indicate save to tokenString */
bool save;
while (state != DONE)
{
int c = getNextChar();
save = true;
switch (state)
{
case START:
if (isalpha(c))
state = INID;
else if (c == '+')
state = TRANS1;
else if (c == '-')
state = TRANS2;
else if (isdigit(c))
state = INNUM1;
else if (c == ':')
state = INEQ;
else if ((c == ' ') || (c == '\t') || (c == '\n'))
save = false;
else
{
state = DONE;
switch (c)
{
case EOF:
save = false;
currentToken = ENDFILE;
break;
case ',':
currentToken = COMMA;
break;
case '(':
currentToken = LPAREN;
break;
case ')':
currentToken = RPAREN;
break;
case ';':
currentToken = SEMI;
break;
default:
currentToken = ERROR;
break;
}
}
break;
case INEQ:
state = DONE;
if (c == '=')
currentToken = EQ;
else
{ /* backup in the input */
ungetNextChar();
save = false;
currentToken = ERROR;
}
break;
case INID:
if (!isalpha(c) || !isdigit(c))


{ /* backup in the input */
ungetNextChar();
save = false;
state = DONE;
currentToken = ID;
}
break;
case TRANS1:
if (isdigit(c))
state = INNUM1;
else
{ /* backup in the input */
ungetNextChar();
save = false;
state = DONE;
currentToken = PLUS;
}
break;
case TRANS2:
if (isdigit(c))
state = INNUM1;
else
{
ungetNextChar();
save = false;
currentToken = ERROR;
}
break;
case INNUM1:
if (isdigit(c))
state = INNUM1;
else if (c == '.')
state = TRANS3;
else if (c == 'E')
state = TRANS4;
else
{ /* backup in the input */
ungetNextChar();
save = false;
state = DONE;
currentToken = NUM;
}
break;
case TRANS3:
if (isdigit(c))
state = INNUM2;
else
{ /* backup in the input */
ungetNextChar();
save = false;
currentToken = ERROR;
}
break;
case INNUM2:
if (isdigit(c))
state = INNUM2;
else if (c == 'E')
state = TRANS4;
else
{ /* backup in the input */
ungetNextChar();
save = false;
state = DONE;
currentToken = NUM;
}
break;
case TRANS4:
if (c == '+' || c == '-')
state = TRANS5;
else if (isdigit(c))
state = INNUM3;
else
{ /* backup in the input */
ungetNextChar();
save = false;
currentToken = ERROR;
}
break;
case TRANS5:
if (isdigit(c))
state = INNUM3;
else
{ /* backup in the input */
ungetNextChar();
save = false;
currentToken = ERROR;
}
break;
case INNUM3:
if (isdigit(c))
state = INNUM3;
else
{ /* backup in the input */
ungetNextChar();
save = false;
state = DONE;
currentToken = NUM;
}
break;
case DONE:
default: /* should never happen */
fprintf(listing,"Scanner Bug: state= %d\n",state);
state = DONE;
currentToken = ERROR;
break;
}
if ((save) && (tokenStringIndex <= MAXTOKENLEN))
tokenString[tokenStringIndex++] = (char) c;
if (state == DONE)
{
tokenString[tokenStringIndex] = '\0';
if (currentToken == ID)
currentToken = reservedLookup(tokenString);
}
}
if (true)
{
fprintf(listing,"\t%d: ",lineno);
printToken(currentToken,tokenString);
}
return currentToken;
} /* end getToken */

void main()
{
char ch;
source = fopen("test1.txt","r");
if (source == NULL)
{
printf("cannot open source file!\n");
exit(0);
}
if ((code = fopen("tokenOut.txt","w")) == NULL)
{
printf("cannot open destination file!\n");
exit(0);
}
ch=fgetc(source);
while(!feof(source))
{
getToken();
}
}


读书人网 >C语言

热点推荐