txt文件最后一位是什么?
(词法分析)源程序:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <alloc.h>
#include <conio.h>
int IsLetter(char ch)
{/*判断是否是字母,是则返回 1,否则返回 0 */
if(isalpha(ch)) return 1;
return 0;
}
int IsDigit(char ch)
{/*判断是否为数字或字母,是则返回 1,否则返回 0 */
if(isalnum(ch)) return 1;
return 0;
}
int IsSpace(char ch)
{/*判断是否为空白符(空格、换行、制表符等),是则返回 1,否则返回 0*/
if(isspace(ch)) return 1;
return 0;
}
void GetChar(FILE *fp,char *ch)
{/*读取字符送ch*/
*ch = fgetc(fp);
}
void GetBC(FILE *fp,char *ch)
{/*如果是空白则继续读下一个,直到不是空白*/
do {
GetChar(fp,ch);
}while(IsSpace(*ch)&&(*ch != EOF));
}
void Retract(FILE *fp,char *ch)
{/*光标回退一位,并使 ch 为空*/
fseek(fp,-1,1);
*ch = '';
}
char Reserve(char *strToken)
{/*返回关键字的编码*/
if(strcmp(strToken,"main") == 0) return '2';
if(strcmp(strToken,"int") == 0) return '1';
if(strcmp(strToken,"char") == 0) return '3';
if(strcmp(strToken,"if") == 0) return '4';
if(strcmp(strToken,"else") == 0) return '5';
if(strcmp(strToken,"for") == 0) return '6';
if(strcmp(strToken,"while") == 0) return '7';
return '0';
}
void Concat(char *strToken, char *ch)
{/*将ch中的字符连接到strToken后面*/
int i;
for(i=0;i<80;i++) {
if(*strToken == NULL) {
*strToken = *ch;
break;
}
strToken++;
}
}
int lexSubFunc(FILE *fp1,FILE *fp2)
{/*词法分析子程序*/
char ch,code;
int i;
char strToken[80];
while(1) {
GetBC(fp1,&ch);
for(i=0;i<80;i++) strToken[i]=NULL;
if(ch == EOF) return 0;
if (IsLetter(ch)) {
while (IsLetter(ch) || IsDigit(ch)) {
Concat(strToken,&ch);
GetChar(fp1,&ch);
}
Retract(fp1,&ch);
code = Reserve(strToken);
if (code == '0') {
printf("(10,%-6s)\n",strToken);
fputs("(10,\"",fp2); fputs(strToken,fp2); fputs("\")\n",fp2);
}
else {
printf("(%2c,%-6s)\n",code,strToken);
fputs("(",fp2); fputc(code,fp2);fputs(",\"",fp2);fputs(strToken,fp2); fputs("\")\n",fp2);
}
}
else if (IsDigit(ch)) {
while (IsDigit(ch)) {
Concat(strToken,&ch);
GetChar(fp1,&ch);
}
Retract(fp1,&ch);
printf("(20,%-6s>\n",strToken);
fputs("(20,\"",fp2); fputs(strToken,fp2); fputs("\")\n",fp2);
}
else if (ch == '=') {
GetChar(fp1,&ch);
if(ch == '='){
printf("(39,==%3c)\n",' ');
fputs("(39,\"==\")\n",fp2);
}
else{
Retract(fp1,&ch);
printf("(21,=%5c)\n",' ');
fputs("(21,\"=\")\n",fp2);
}
}
else if (ch == '+') {
printf("(22,+%5c)\n",' ');
fputs("(22,\"+\")\n",fp2);
}
else if (ch == '-') {
printf("(23,-%5c)\n",' ');
fputs("(23,\"-\")\n",fp2);
}
else if (ch == '*') {
printf("(24,*%5c)\n",' ');
fputs("(24,\"*\")\n",fp2);
}
else if (ch == '/') {
printf("(25,/%5c)\n",' ');
fputs("(25,\"/\")\n",fp2);
}
else if (ch == '(') {
printf("(26,(%5c)\n",' ');
fputs("(26,\"(\")\n",fp2);
}
else if (ch == ')') {
printf("(27,)%5c)\n",' ');
fputs("(27,\")\")\n",fp2);
}
else if (ch == '[') {
printf("(28,[%5c)\n",' ');
fputs("(28,\"[\")\n",fp2);
}
else if (ch == ']}') {
printf("(29,]%5c)\n",' ');
fputs("(29,\"]\")\n",fp2);
}
else if (ch == '{') {
printf("(30,{%5c)\n",' ');
fputs("(30,\"{\")\n",fp2);
}
else if (ch == '}') {
printf("(31,}%5c)\n",' ');
fputs("(31,\"}\")\n",fp2);
}
else if (ch == ',') {
printf("(32,,%5c)\n",' ');
fputs("(32,\",\")\n",fp2);
}
else if (ch == ':') {
printf("(33,:%5c)\n",' ');
fputs("(33,\":\")\n",fp2);
}
else if (ch == ';') {
printf("(34,;%5c)\n",' ');
fputs("(34,\";\")\n",fp2);
}
else if (ch == '>') {
GetChar(fp1,&ch);
if(ch == '='){
printf("(37,>=%4c)\n",' ');
fputs("(37,\">=\")\n",fp2);
}
else{
Retract(fp1,&ch);
printf("(35,>%5c)\n",' ');
fputs("(35,\">\")\n",fp2);
}
}
else if (ch == '<') {
GetChar(fp1,&ch);
if(ch == '='){
printf("(38,<=%4c)\n",' ');
fputs("(38,\"<=\")\n",fp2);
}
else{
Retract(fp1,&ch);
printf("(36,<%5c)\n",' ');
fputs("(36,\"<\")\n",fp2);
}
}
else if (ch == '!') {
GetChar(fp1,&ch);
if(ch == '='){
printf("(40,!=%3c)\n",' ');
fputs("(40,\"!=\")\n",fp2);
}
else
Retract(fp1,&ch);
}
else if (ch == '\0') {
printf("(1000,\0%3c)\n",' ');
fputs("(1000,\"\0\")\n",fp2);
}
}
}
void main()
{/*主程序*/
FILE *fp1,*fp2;
if((fp1=fopen("input.txt","rt"))==NULL)
{
printf("Cannot open input.txt\n");
getch(); exit(1);
}
if((fp2=fopen("out.txt","wt+"))==NULL)
{
printf("Cannot create out.txt FILE.strike any key exit");
getch(); exit(1);
}
lexSubFunc(fp1,fp2);
fclose(fp1);
fclose(fp2);
getch();
}
如果input.txt文件内容为:{9*a
运行后进入死循环(注意a后面没有空格)
如果input.txt文件内容为:{9*a}
运行后程序可以正常结束。
同样是当ch==EOF是退出的,为什么第一种就退不了呢?
各位GGJJ帮我看看啊
谢谢了