词法分析器问题

Max7Chou 2012-05-31 08:28:28
写了个词法分析器,但是执行时有问题,以下是代码,希望大家看看,找出问题所在并改正,不胜感激。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>


enum Token_Type // 记号的类别
{ ORIGIN, SCALE, ROT, IS, // 保留字(一字一码)
TO, STEP, DRAW,FOR, FROM, // 保留字
T, // 参数
COMMENT,//注释
SEMICO, L_BRACKET, R_BRACKET, COMMA,// 分隔符
PLUS, MINUS, MUL, DIV, POWER, // 运算符
FUNC, // 函数
CONST_ID, // 常数
NONTOKEN, // 空记号(源程序结束)
ERRTOKEN // 出错记号(非法输入)
};


struct Token // 记号的数据结构
{ Token_Type type; // 类别
char * lexeme; // 属性,原始输入的字符串
double value; // 属性,若记号是常数则是常数的值
double (* FuncPtr)(double); // 属性,若记号是函数则是函数指针
};


static Token TokenTab[] =
{ {CONST_ID, "PI", 3.1415926, NULL},
{CONST_ID, "E", 2.71828, NULL},//常量
{T, "T", 0.0, NULL},//定值变量
{FUNC, "SIN", 0.0, sin},//函数
{FUNC, "COS", 0.0, cos},
{FUNC, "TAN", 0.0, tan},
{FUNC, "LN", 0.0, log},
{FUNC, "EXP", 0.0, exp},
{FUNC, "SQRT", 0.0, sqrt},
{ORIGIN, "ORIGIN", 0.0, NULL},//保留字
{SCALE, "SCALE", 0.0, NULL},
{ROT, "ROT", 0.0, NULL},
{IS, "IS", 0.0, NULL},
{FOR, "FOR", 0.0, NULL},
{FROM, "FROM", 0.0, NULL},
{TO, "TO", 0.0, NULL},
{STEP, "STEP", 0.0, NULL},
{DRAW, "DRAW", 0.0, NULL}
};



FILE *fp;
char str;
char buffer[50]={0};
//int k=0;
struct Token token={ERRTOKEN, "", 0.0, NULL}; // 用于返回记号
void main()
{
if ((fp=fopen("yuan.txt","r"))==NULL)
printf("程序无法打开!\n");
else
{
str =fgetc(fp);
while (str!=EOF)
{
if(isalpha(str)!=0)
{
int i=0;
while(isalpha(str)!=0)
{
buffer[i]=str;
i++;
str=fgetc(fp);
}
buffer[i]='\0';
for(int j=0;j<=17;j++)
{
if(strcmp(buffer,TokenTab[j].lexeme)==0)
printf("%s\t%s\t%s\t%s\t\n",TokenTab[j].type,TokenTab[j].lexeme,TokenTab[j].value,TokenTab[j].FuncPtr);
}
}
else if(isdigit(str)!=0)
{
int i=0;
while(isdigit(str)!=0)
{
buffer[i]=str;
i++;
str=fgetc(fp);
}
buffer[i]='\0';
token.type=CONST_ID;
token.lexeme=buffer;
token.value=(char)buffer;
printf("%s\t%s\t%s\t%s\t\n",token.type,token.lexeme,token.value,token.FuncPtr);
//printf("CONST_ID\t%s\t%s\tNULL\t\n",buffer,buffer);
}
else if(str=='*')
{
str=fgetc(fp);
if(str=='*')
{
token.type=POWER;
token.lexeme="**";
printf("%s\t%s\t%s\t%s\t\n",token.type,token.lexeme,token.value,token.FuncPtr);
}
else
{
token.type=MUL;
token.lexeme="*";
printf("%s\t%s\t%s\t%s\t\n",token.type,token.lexeme,token.value,token.FuncPtr);
}
}
else if(str=='/')
{
str=fgetc(fp);
if(str=='/')
{
int i=0;
token.type=COMMENT;
token.lexeme="//";
printf("%s\t%s\t%s\t%s\t\n",token.type,token.lexeme,token.value,token.FuncPtr);
while(str!='\n')
{

str=fgetc(fp);
buffer[i]=str;
i++;
str=fgetc(fp);
}
buffer[i]='\0';
printf("%s",buffer);
}
else
{
token.type=DIV;
token.lexeme="/";
printf("%s\t%s\t%s\t%s\t\n",token.type,token.lexeme,token.value,token.FuncPtr);
}
}
else if(str=='-')
{
str=fgetc(fp);
if(str=='-')
{
int i=0;
token.type=COMMENT;
token.lexeme="--";
printf("%s\t%s\t%s\t%s\t\n",token.type,token.lexeme,token.value,token.FuncPtr);
while(str!='\n')
{

str=fgetc(fp);
buffer[i]=str;
i++;
str=fgetc(fp);
}
buffer[i]='\0';
printf("%s",buffer);
}
else
{
token.type=MINUS;
token.lexeme="-";
printf("%s\t%s\t%s\t%s\t\n",token.type,token.lexeme,token.value,token.FuncPtr);
}
}
else if(str=='+')
{
token.type=PLUS;
token.lexeme="+";
printf("%s\t%s\t%s\t%s\t\n",token.type,token.lexeme,token.value,token.FuncPtr);
}
else if(str=='(')
{
token.type=L_BRACKET;
token.lexeme="(";
printf("%s\t%s\t%s\t%s\t\n",token.type,token.lexeme,token.value,token.FuncPtr);
}
else if(str==')')
{
token.type=R_BRACKET;
token.lexeme=")";
printf("%s\t%s\t%s\t%s\t\n",token.type,token.lexeme,token.value,token.FuncPtr);
}
else if(str==',')
{
token.type=COMMA;
token.lexeme=",";
printf("%s\t%s\t%s\t%s\t\n",token.type,token.lexeme,token.value,token.FuncPtr);
}
else if(str==';')
{
token.type=SEMICO;
token.lexeme=";";
printf("%s\t%s\t%s\t%s\t\n",token.type,token.lexeme,token.value,token.FuncPtr);
}
else if(str=' '||str=='\t')
{str=fgetc(fp);
}
}
}
}
...全文
48 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
设计思想 (1)程序主体结构部分: 说明部分 %% 规则部分 %% 辅助程序部分 (2)主体结构的说明 在这里说明部分告诉我们使用的LETTER,DIGIT, IDENT(标识符,通常定义为字母开头的字母数字串)和STR(字符串常量,通常定义为双引号括起来的一串字符)是什么意思.这部分也可以包含一些初始化代码.例如用#include来使用标准的头文件和前向说明(forward ,references).这些代码应该再标记"%{"和"%}"之间;规则部分>可以包括任何你想用来分析的代码;我们这里包括了忽略所有注释中字符的功能,传送ID名称和字符串常量内容到主调函数和main函数的功能. (3)实现原理 程序中先判断这个句语句中每个单元为关键字、常数、运算符、界符,对与不同的单词符号给出不同编码形式的编码,用以区分之。 PL/0语言的EBNF表示 <常量定义>::=<标识符>=<无符号整数>; <标识符>::=<字母>={<字母>|<数字>}; <加法运算符>::=+|- <乘法运算符>::=*|/ <关系运算符>::==|#|<|<=|>|>= <字母>::=a|b|…|X|Y|Z <数字>::=0|1|2|…|8|9 三:设计过程 1. 关键字:void,main,if,then,break,int,Char,float,include,for,while,printfscanf 并为小写。 2."+”;”-”;”*”;”/”;”:=“;”:”;”<“;”<=“;”>“;”>=“;”<>“;”=“;”(“;”)”;”;”;”#”为运算符。 3. 其他标记 如字符串,表示以字母开头的标识符。 4. 空格符跳过。 5. 各符号对应种别码 关键字分别对应1-13 运算符分别对应401-418,501-513。 字符串对应100 常量对应200 结束符# 四:举例说明 目标:实现对常量的判别 代码: digit [0-9] letter [A-Za-z] other_char [!-@\[-~] id ({letter}|[_])({letter}|{digit}|[_])* string {({letter}|{digit}|{other_char})+} int_num {digit}+ %% [ |\t|\n]+ "auto"|"double"|"int"|"struct"|"break"|"else"|"long"|"switch"|"case"|"enum"|"register"|"typedef"|"char"|"extern"|"return"|"union"|"const"|"float"|"short"|"unsigned"|"continue"|"for"|"signed"|"void"|"default"|"goto"|"sizeof"|"do"|"if"|"static"|"while"|"main" {Upper(yytext,yyleng);printf("%s,NULL\n",yytext);} \"([!-~])*\" {printf("CONST_string,%s\n",yytext);} -?{int_num}[.]{int_num}?([E][+|-]?{int_num})? {printf("CONST_real,%s\n",yytext);} "0x"?{int_num} {printf("CONST_int,%s\n",yytext);} ","|";"|"("|")"|"{"|"}"|"["|"]"|"->"|"."|"!"|"~"|"++"|"--"|"*"|"&"|"sizeof"|"/"|"%"|"+"|"-"|">"|"<"|">="|"<="|"=="|"!="|"&"|"^"|"|"|"&"|"||"|"+="|"-="|"*="|"/="|"%="|">>="|"<<="|"&="|"^="|"|="|"=" {printf("%s,NULL\n",yytext);} {id} {printf("ID,%s\n",yytext);} {digit}({letter})+ {printf("error1:%s\n",yytext);} %% #include Upper(char *s,int l) { int i; for(i=0;i #include #include #include int i,j,k,flag,number,status; /*status which is use to judge the string is keywords or not!*/ char ch; char words[10] = {" "}; char program[500]; int Scan(char program[]) { char *keywords[13] = {"void","main","if","then","break","int", "char","float","include","for","while","printf", "scanf"}; number = 0; status = 0; j = 0; ch = program[i++]; /* To handle the lettle space ands tab*/ /*handle letters*/ if ((ch >= 'a') && (ch <= 'z' )) { while ((ch >= 'a') && (ch <= 'z' )) { words[j++]=ch; ch=program[i++]; } i--; words[j++] = '\0'; for (k = 0; k < 13; k++) if (strcmp (words,keywords[k]) == 0) switch(k) { case 0:{ flag = 1; status = 1; break; } case 1:{ flag = 2; status = 1; break; } case 2:{ flag = 3; status = 1; break; } case 3:{ flag = 4; status = 1; break; } case 4:{ flag = 5; status = 1; break; } case 5:{ flag = 6; status = 1; break; } case 6:{ flag = 7; status = 1; break; } case 7:{ flag = 8; status = 1; break; } case 8:{ flag = 9; status = 1; break; } case 9:{ flag = 10; status = 1; break; } case 10:{ flag = 11; status = 1; break; } case 11:{ flag = 12; status = 1; break; } case 12:{ flag = 13; status = 1; break; } } if (status == 0) { flag = 100; } } /*handle digits*/ else if ((ch >= '0') && (ch <= '9')) { number = 0; while ((ch >= '0' ) && (ch <= '9' )) { number = number*10+(ch-'0'); ch = program[i++]; } flag = 200; i--; } /*opereation and edge handle*/ else switch (ch) { case '=':{ if (ch == '=') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 401; } else { i--; flag = 402; } break; } case'>':{ if (ch == '>') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 403; } else { i--; flag = 404; } break; } case'<':{ if (ch == '<') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 405; } else { i--; flag = 406; } break; } case'!':{ if (ch == '!') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 407; } else { i--; flag = 408; } break; } case'+':{ if (ch == '+') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 409; } else if (ch == '+') { words[j++] = ch; words[j] = '\0'; flag = 410; } else { i--; flag = 411; } break; } case'-':{ if (ch == '-') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 412; } else if( ch == '-') { words[j++] = ch; words[j] = '\0'; flag = 413; } else { i--; flag = 414; } break; } case'*':{ if (ch == '*') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 415; } else { i--; flag = 416; } break; } case'/':{ if (ch == '/') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 417; } else { i--; flag = 418; } break; } case';':{ words[j] = ch; words[j+1] = '\0'; flag = 501; break; } case'(':{ words[j] = ch; words[j+1] = '\0'; flag = 502; break; } case')':{ words[j] = ch; words[j+1] = '\0'; flag = 503; break; } case'[':{ words[j] = ch; words[j+1] = '\0'; flag = 504; break; } case']':{ words[j] = ch; words[j+1] = '\0'; flag = 505; break; } case'{':{ words[j] = ch; words[j+1] = '\0'; flag = 506; break; } case'}':{ words[j] = ch; words[j+1] = '\0'; flag = 507; break; } case':':{ words[j] = ch; words[j+1] = '\0'; flag = 508; break; } case'"':{ words[j] = ch; words[j+1] = '\0'; flag = 509; break; } case'%':{ if (ch == '%') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 510; } else { i--; flag = 511; } break; } case',':{ words[j] = ch; words[j+1] = '\0'; flag = 512; break; } case'#':{ words[j] = ch; words[j+1] = '\0'; flag = 513; break; } case'@':{ words[j] = '#'; flag = 0; break; } default:{ flag = -1; break; } } return flag; } main() { i=0; printf("please input a program end with @"); do { ch = getchar(); program[i++] = ch; }while(ch != '@'); i = 0; do{ flag = Scan(program); if (flag == 200) { printf("(%2d,%4d)",flag,number); } else if (flag == -1) { printf("(%d,error)",flag); } else { printf("(%2d,%4s)",flag,words); } }while (flag != 0); system("pause"); }

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧